1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
|
/************************************************************************
************************************************************************
FAUST compiler
Copyright (C) 2003-2018 GRAME, Centre National de Creation Musicale
---------------------------------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
************************************************************************
************************************************************************/
/*****************************************************************************
HISTORY
22/01/05 : corrected bug on bool signals cached in float variables
2009-08-16 : First "doc" version (kb)
2009-11-22 : Some clean up (kb)
*****************************************************************************/
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <vector>
#include "compatibility.hh"
#include "doc.hh"
#include "doc_compile.hh"
#include "doc_notice.hh"
#include "exception.hh"
#include "floats.hh"
#include "global.hh"
#include "names.hh"
#include "ppsig.hh"
#include "prim2.hh"
#include "recursivness.hh"
#include "sigprint.hh"
#include "sigtype.hh"
#include "sigtyperules.hh"
#include "simplify.hh"
#include "tlib.hh"
#include "xtended.hh"
using namespace std;
extern bool getSigListNickName(Tree t, Tree& id);
/*****************************************************************************
getFreshID
*****************************************************************************/
string DocCompiler::getFreshID(const string& prefix)
{
if (gGlobal->gIDCounters.find(prefix) == gGlobal->gIDCounters.end()) {
gGlobal->gIDCounters[prefix] = 1;
}
int n = gGlobal->gIDCounters[prefix];
gGlobal->gIDCounters[prefix] = n + 1;
return subst("$0_{$1}", prefix, docT(n));
}
/*****************************************************************************
prepare
*****************************************************************************/
Tree DocCompiler::annotate(Tree LS)
{
recursivnessAnnotation(LS); // Annotate LS with recursivness information
typeAnnotation(LS, gGlobal->gLocalCausalityCheck); // Annotate LS with type information
sharingAnalysis(LS); // annotate LS with sharing count
fOccMarkup.mark(LS); // annotate LS with occurences analysis
return LS;
}
/*****************************************************************************
compileLateq
*****************************************************************************/
Lateq* DocCompiler::compileLateq(Tree L, Lateq* compiledEqn)
{
// cerr << "Documentator : compileLateq : L = "; printSignal(L, stdout, 0); cerr << endl;
fLateq = compiledEqn; ///< Dynamic field !
int priority = 0;
for (int i = 0; isList(L); L = tl(L), i++) {
Tree sig = hd(L);
Tree id;
if (getSigNickname(sig, id)) {
// cerr << "Documentator : compileLateq : NICKNAMEPROPERTY = " << tree2str(id) << endl;
fLateq->addOutputSigFormula(
subst("$0(t) = $1", tree2str(id), CS(sig, priority), docT(i)));
} else {
// cerr << "Documentator : compileLateq : NO NICKNAMEPROPERTY" << endl;
if (fLateq->outputs() == 1) {
fLateq->addOutputSigFormula(subst("y(t) = $0", CS(sig, priority)));
gGlobal->gDocNoticeFlagMap["outputsig"] = true;
} else {
fLateq->addOutputSigFormula(
subst("$0(t) = $1", getFreshID("y"), CS(sig, priority)));
gGlobal->gDocNoticeFlagMap["outputsigs"] = true;
}
}
}
return fLateq;
}
/*****************************************************************************
CS : compile a signal
*****************************************************************************/
/**
* Test if a signal is already compiled
* @param sig the signal expression to compile.
* @param name the string representing the compiled expression.
* @return true is already compiled
*/
bool DocCompiler::getCompiledExpression(Tree sig, string& cexp)
{
return fCompileProperty.get(sig, cexp);
}
/**
* Set the string of a compiled expression is already compiled
* @param sig the signal expression to compile.
* @param cexp the string representing the compiled expression.
* @return the cexp (for commodity)
*/
string DocCompiler::setCompiledExpression(Tree sig, const string& cexp)
{
fCompileProperty.set(sig, cexp);
return cexp;
}
/**
* Compile a signal
* @param sig the signal expression to compile.
* @return the C code translation of sig as a string
*/
string DocCompiler::CS(Tree sig, int priority)
{
string code;
if (!getCompiledExpression(sig, code)) { // not compiled yet.
code = generateCode(sig, priority);
setCompiledExpression(sig, code);
}
return code;
}
/*****************************************************************************
generateCode : dispatch according to signal
*****************************************************************************/
/**
* @brief Main code generator dispatch.
*
* According to the type of the input signal, generateCode calls
* the appropriate generator with appropriate arguments.
*
* @param sig The signal expression to compile.
* @param priority The environment priority of the expression.
* @return <string> The LaTeX code translation of the signal.
*/
string DocCompiler::generateCode(Tree sig, int priority)
{
int i;
double r;
Tree c, sel, x, y, z, u, label, ff, largs, type, name, file;
if (getUserData(sig)) {
printGCCall(sig, "generateXtended");
return generateXtended(sig, priority);
} else if (isSigInt(sig, &i)) {
printGCCall(sig, "generateNumber");
return generateNumber(sig, docT(i));
} else if (isSigReal(sig, &r)) {
printGCCall(sig, "generateNumber");
return generateNumber(sig, docT(r));
} else if (isSigInput(sig, &i)) {
printGCCall(sig, "generateInput");
return generateInput(sig, docT(i + 1));
} else if (isSigOutput(sig, &i, x)) {
printGCCall(sig, "generateOutput");
return generateOutput(sig, docT(i + 1), CS(x, priority));
}
else if (isSigDelay(sig, x, y)) {
printGCCall(sig, "generateDelayAccess");
return generateDelayAccess(sig, x, y, priority);
} else if (isSigPrefix(sig, x, y)) {
printGCCall(sig, "generatePrefix");
return generatePrefix(sig, x, y, priority);
} else if (isSigBinOp(sig, &i, x, y)) {
printGCCall(sig, "generateBinOp");
return generateBinOp(sig, i, x, y, priority);
} else if (isSigFFun(sig, ff, largs)) {
printGCCall(sig, "generateFFun");
return generateFFun(sig, ff, largs, priority);
} else if (isSigFConst(sig, type, name, file)) {
printGCCall(sig, "generateFConst");
return generateFConst(sig, tree2str(file), tree2str(name));
} else if (isSigFVar(sig, type, name, file)) {
printGCCall(sig, "generateFVar");
return generateFVar(sig, tree2str(file), tree2str(name));
}
// new special tables for documentation purposes
else if (isSigDocConstantTbl(sig, x, y)) {
printGCCall(sig, "generateDocConstantTbl");
return generateDocConstantTbl(sig, x, y);
} else if (isSigDocWriteTbl(sig, x, y, z, u)) {
printGCCall(sig, "generateDocWriteTbl");
return generateDocWriteTbl(sig, x, y, z, u);
} else if (isSigDocAccessTbl(sig, x, y)) {
printGCCall(sig, "generateDocAccessTbl");
return generateDocAccessTbl(sig, x, y);
}
else if (isSigSelect2(sig, sel, x, y)) {
printGCCall(sig, "generateSelect2");
return generateSelect2(sig, sel, x, y, priority);
}
else if (isProj(sig, &i, x)) {
printGCCall(sig, "generateRecProj");
return generateRecProj(sig, x, i, priority);
}
else if (isSigIntCast(sig, x)) {
printGCCall(sig, "generateIntCast");
return generateIntCast(sig, x, priority);
} else if (isSigFloatCast(sig, x)) {
printGCCall(sig, "generateFloatCast");
return generateFloatCast(sig, x, priority);
}
else if (isSigButton(sig, label)) {
printGCCall(sig, "generateButton");
return generateButton(sig, label);
} else if (isSigCheckbox(sig, label)) {
printGCCall(sig, "generateCheckbox");
return generateCheckbox(sig, label);
} else if (isSigVSlider(sig, label, c, x, y, z)) {
printGCCall(sig, "generateVSlider");
return generateVSlider(sig, label, c, x, y, z);
} else if (isSigHSlider(sig, label, c, x, y, z)) {
printGCCall(sig, "generateHSlider");
return generateHSlider(sig, label, c, x, y, z);
} else if (isSigNumEntry(sig, label, c, x, y, z)) {
printGCCall(sig, "generateNumEntry");
return generateNumEntry(sig, label, c, x, y, z);
}
else if (isSigVBargraph(sig, label, x, y, z)) {
printGCCall(sig, "generateVBargraph");
return CS(z, priority);
} // generateVBargraph (sig, label, x, y, CS(z, priority)); }
else if (isSigHBargraph(sig, label, x, y, z)) {
printGCCall(sig, "generateHBargraph");
return CS(z, priority);
} // generateHBargraph (sig, label, x, y, CS(z, priority)); }
else if (isSigAttach(sig, x, y)) {
printGCCall(sig, "generateAttach");
return generateAttach(sig, x, y, priority);
} else if (isSigEnable(sig, x, y)) {
printGCCall(sig, "generateControl");
return generateControl(sig, x, y, priority);
}
else {
cerr << "ASSERT : unrecognized signal : " << *sig << endl;
faustassert(false);
}
faustassert(false);
// Never reached
return "ASSERT : in generate code";
}
/**
* Print calling information of generateCode, for debug purposes.
*
* @remark
* To turn printing on, turn the 'printCalls' boolean to true.
*/
void DocCompiler::printGCCall(Tree sig, const string& calledFunction)
{
bool printCalls = false;
bool maskSigs = false;
if (printCalls) {
cerr << " -> generateCode calls " << calledFunction;
if (maskSigs) {
cerr << endl;
} else {
cerr << " on " << ppsig(sig) << endl;
}
}
}
/*****************************************************************************
NUMBERS
*****************************************************************************/
string DocCompiler::generateNumber(Tree sig, const string& exp)
{
string ctype, vname;
Occurrences* o = fOccMarkup.retrieve(sig);
// check for number occuring in delays
if (o->getMaxDelay() > 0) {
getTypedNames(getCertifiedSigType(sig), "r", ctype, vname);
gGlobal->gDocNoticeFlagMap["recursigs"] = true;
// cerr << "- r : generateNumber : \"" << vname << "\"" << endl;
generateDelayVec(sig, exp, ctype, vname, o->getMaxDelay());
}
return exp;
}
/*****************************************************************************
FOREIGN CONSTANTS
*****************************************************************************/
string DocCompiler::generateFConst(Tree sig, const string& file, const string& exp)
{
string ctype, vname;
Occurrences* o = fOccMarkup.retrieve(sig);
if (o->getMaxDelay() > 0) {
getTypedNames(getCertifiedSigType(sig), "r", ctype, vname);
gGlobal->gDocNoticeFlagMap["recursigs"] = true;
// cerr << "- r : generateFConst : \"" << vname << "\"" << endl;
generateDelayVec(sig, exp, ctype, vname, o->getMaxDelay());
}
if (exp == "fSampleRate" || exp == "fSamplingFreq") {
// gGlobal->gDocNoticeFlagMap["fsamp"] = true;
return "f_S";
}
return "\\mathrm{" + exp + "}";
}
/*****************************************************************************
FOREIGN VARIABLES
*****************************************************************************/
string DocCompiler::generateFVar(Tree sig, const string& file, const string& exp)
{
string ctype, vname;
Occurrences* o = fOccMarkup.retrieve(sig);
if (o->getMaxDelay() > 0) {
getTypedNames(getCertifiedSigType(sig), "r", ctype, vname);
gGlobal->gDocNoticeFlagMap["recursigs"] = true;
// cerr << "- r : generateFVar : \"" << vname << "\"" << endl;
setVectorNameProperty(sig, vname);
generateDelayVec(sig, exp, ctype, vname, o->getMaxDelay());
}
return generateCacheCode(sig, exp);
}
/*****************************************************************************
INPUTS - OUTPUTS
*****************************************************************************/
string DocCompiler::generateInput(Tree sig, const string& idx)
{
if (fLateq->inputs() == 1) {
setVectorNameProperty(sig, "x");
fLateq->addInputSigFormula("x(t)");
gGlobal->gDocNoticeFlagMap["inputsig"] = true;
return generateCacheCode(sig, "x(t)");
} else {
setVectorNameProperty(sig, subst("x_{$0}", idx));
fLateq->addInputSigFormula(subst("x_{$0}(t)", idx));
gGlobal->gDocNoticeFlagMap["inputsigs"] = true;
return generateCacheCode(sig, subst("x_{$0}(t)", idx));
}
}
/** Unused for the moment ! */
string DocCompiler::generateOutput(Tree sig, const string& idx, const string& arg)
{
string dst;
if (fLateq->outputs() == 1) {
dst = subst("y(t)", idx);
gGlobal->gDocNoticeFlagMap["outputsig"] = true;
} else {
dst = subst("y_{$0}(t)", idx);
gGlobal->gDocNoticeFlagMap["outputsigs"] = true;
}
fLateq->addOutputSigFormula(subst("$0 = $1", dst, arg));
return dst;
}
/*****************************************************************************
BINARY OPERATION
*****************************************************************************/
/**
* Generate binary operations, managing priority parenthesis.
* ((a*b)+c) can be written (a*b+c) if priority(*) > priority(+)
* ((a*b)*c) can be writteb (a*b*c) if * is associative
* Associative operation should have a distinc priority from other operations.
* Non associative operations can share the same priority.
*
* @param sig The signal expression to treat.
* @param opcode The operation code, as described in gBinOpLateqTable.
* @param arg1 The first operand.
* @param arg2 The second operand.
* @param priority The priority of the environment of the expression.
*
* @return <string> The LaTeX code translation of the signal, cached.
*
* @remark The case of LaTeX frac{}{} is special.
*
* @todo Handle integer arithmetics, by testing arguments type,
* and printing dedicated operators (\oplus, \odot, \ominus, \oslash).
*/
/// associative operations are + * | & xor
static bool associative(int opcode)
{
return (opcode == kAdd) || (opcode == kMul) || (opcode == kAND) || (opcode == kOR) ||
(opcode == kXOR);
}
string DocCompiler::generateControl(Tree sig, Tree arg1, Tree arg2, int priority)
{
return generateBinOp(sig, kMul, arg1, arg2, priority);
}
string DocCompiler::generateBinOp(Tree sig, int opcode, Tree arg1, Tree arg2, int priority)
{
string s;
int thisPriority = gBinOpLateqTable[opcode]->fPriority;
/* Priority parenthesis handling. */
string lpar = "";
string rpar = "";
if ((thisPriority < priority) || ((thisPriority == priority) && !associative(opcode))) {
// (a+b)*c or (a/b)/c need parenthesis
lpar = " \\left(";
rpar = "\\right) ";
}
Type t1 = getCertifiedSigType(arg1);
Type t2 = getCertifiedSigType(arg2);
bool intOpDetected = false;
if ((t1->nature() == kInt) && (t2->nature() == kInt)) {
intOpDetected = true;
}
string op;
if (!intOpDetected) {
op = gBinOpLateqTable[opcode]->fName;
} else {
switch (opcode) {
case kAdd:
op = "\\oplus";
gGlobal->gDocNoticeFlagMap["intplus"] = true;
break;
case kSub:
op = "\\ominus";
gGlobal->gDocNoticeFlagMap["intminus"] = true;
break;
case kMul:
op = "\\odot";
gGlobal->gDocNoticeFlagMap["intmult"] = true;
break;
case kDiv:
op = "\\oslash";
gGlobal->gDocNoticeFlagMap["intdiv"] = true;
gGlobal->gDocNoticeFlagMap["intcast"] =
true; // "$normalize(int(i/j))$" in the notice.
break;
default:
op = gBinOpLateqTable[opcode]->fName;
break;
}
}
/* LaTeX frac{}{} handling VS general case. */
if ((opcode == kDiv) && (!intOpDetected)) {
s = subst("$0\\frac{$1}{$2}$3", lpar, CS(arg1, 0), CS(arg2, 0), rpar);
} else {
s = subst("$0$1 $2 $3$4", lpar, CS(arg1, thisPriority), op, CS(arg2, thisPriority), rpar);
}
// if (opcode == kMul) {
// gGlobal->gDocNoticeFlagMap["cdot"] = true;
// }
return generateCacheCode(sig, s);
}
/*****************************************************************************
Primitive Operations
*****************************************************************************/
string DocCompiler::generateFFun(Tree sig, Tree ff, Tree largs, int priority)
{
string code = ffname(ff);
code += '(';
string sep = "";
for (int i = 0; i < ffarity(ff); i++) {
code += sep;
code += CS(nth(largs, i), priority);
sep = ", ";
}
code += ')';
gGlobal->gDocNoticeFlagMap["foreignfun"] = true;
return "\\mathrm{ff" + code + "}";
}
/*****************************************************************************
CACHE CODE
*****************************************************************************/
void DocCompiler::getTypedNames(Type t, const string& prefix, string& ctype, string& vname)
{
if (t->nature() == kInt) {
ctype = "int";
vname = subst("$0", getFreshID(prefix));
} else {
ctype = ifloat();
vname = subst("$0", getFreshID(prefix));
}
}
/**
* Test if exp is very simple that is it
* can't be considered a real component
* @param exp the signal we want to test
* @return true if it a very simple signal
*/
static bool isVerySimpleFormula(Tree sig)
{
int i;
double r;
Tree type, name, file, label, c, x, y, z;
return isSigInt(sig, &i) || isSigReal(sig, &r) || isSigInput(sig, &i) ||
isSigFConst(sig, type, name, file) || isSigButton(sig, label) ||
isSigCheckbox(sig, label) || isSigVSlider(sig, label, c, x, y, z) ||
isSigHSlider(sig, label, c, x, y, z) || isSigNumEntry(sig, label, c, x, y, z);
}
string DocCompiler::generateCacheCode(Tree sig, const string& exp)
{
// cerr << "!! entering generateCacheCode with sig=\"" << ppsig(sig) << "\"" << endl;
string vname, ctype, code, vectorname;
int sharing = getSharingCount(sig);
Occurrences* o = fOccMarkup.retrieve(sig);
// check reentrance
if (getCompiledExpression(sig, code)) {
// cerr << "!! generateCacheCode called a true getCompiledExpression" << endl;
return code;
}
// check for expression occuring in delays
if (o->getMaxDelay() > 0) {
if (getVectorNameProperty(sig, vectorname)) {
return exp;
}
getTypedNames(getCertifiedSigType(sig), "r", ctype, vname);
gGlobal->gDocNoticeFlagMap["recursigs"] = true;
// cerr << "- r : generateCacheCode : vame=\"" << vname << "\", for sig=\"" << ppsig(sig) <<
// "\"" << endl;
if (sharing > 1) {
// cerr << " generateCacheCode calls generateDelayVec(generateVariableStore) on
// vame=\"" << vname <<
// "\"" << endl;
return generateDelayVec(sig, generateVariableStore(sig, exp), ctype, vname,
o->getMaxDelay());
} else {
// cerr << " generateCacheCode calls generateDelayVec(exp) on vame=\"" << vname <<
// "\"" << endl;
return generateDelayVec(sig, exp, ctype, vname, o->getMaxDelay());
}
} else if (sharing == 1 || getVectorNameProperty(sig, vectorname) || isVerySimpleFormula(sig)) {
// cerr << "! generateCacheCode : sharing == 1 : return \"" << exp << "\"" << endl;
return exp;
} else if (sharing > 1) {
// cerr << "! generateCacheCode : sharing > 1 : return \"" << exp << "\"" << endl;
return generateVariableStore(sig, exp);
} else {
stringstream error;
error << "ERROR in sharing count (" << sharing << ") for " << *sig << endl;
throw faustexception(error.str());
}
return "Error in generateCacheCode";
}
string DocCompiler::generateVariableStore(Tree sig, const string& exp)
{
string vname, ctype;
Type t = getCertifiedSigType(sig);
switch (t->variability()) {
case kKonst:
getTypedNames(t, "k", ctype, vname); ///< "k" for constants.
fLateq->addConstSigFormula(subst("$0 = $1", vname, exp));
gGlobal->gDocNoticeFlagMap["constsigs"] = true;
return vname;
case kBlock:
getTypedNames(t, "p", ctype, vname); ///< "p" for "parameter".
fLateq->addParamSigFormula(subst("$0(t) = $1", vname, exp));
gGlobal->gDocNoticeFlagMap["paramsigs"] = true;
setVectorNameProperty(sig, vname);
return subst("$0(t)", vname);
case kSamp:
if (getVectorNameProperty(sig, vname)) {
return subst("$0(t)", vname);
} else {
getTypedNames(t, "s", ctype, vname);
// cerr << "- generateVariableStore : \"" << subst("$0(t) = $1", vname, exp) << "\""
// << endl;
fLateq->addStoreSigFormula(subst("$0(t) = $1", vname, exp));
gGlobal->gDocNoticeFlagMap["storedsigs"] = true;
setVectorNameProperty(sig, vname);
return subst("$0(t)", vname);
}
default:
faustassert(0);
return "";
}
}
/*****************************************************************************
CASTING
*****************************************************************************/
string DocCompiler::generateIntCast(Tree sig, Tree x, int priority)
{
gGlobal->gDocNoticeFlagMap["intcast"] = true;
return generateCacheCode(sig, subst("\\mathrm{int}\\left($0\\right)", CS(x, 0)));
}
/**
* @brief Don't generate float cast !
*
* It is just a kind of redirection.
* Calling generateCacheCode ensures to create a new
* variable name if the input signal expression is shared.
*/
string DocCompiler::generateFloatCast(Tree sig, Tree x, int priority)
{
return generateCacheCode(sig, subst("$0", CS(x, priority)));
}
/*****************************************************************************
user interface elements
*****************************************************************************/
string DocCompiler::generateButton(Tree sig, Tree path)
{
string vname = getFreshID("{u_b}");
string varname = vname + "(t)";
fLateq->addUISigFormula(getUIDir(path), prepareBinaryUI(varname, path));
gGlobal->gDocNoticeFlagMap["buttonsigs"] = true;
return generateCacheCode(sig, varname);
}
string DocCompiler::generateCheckbox(Tree sig, Tree path)
{
string vname = getFreshID("{u_c}");
string varname = vname + "(t)";
fLateq->addUISigFormula(getUIDir(path), prepareBinaryUI(varname, path));
gGlobal->gDocNoticeFlagMap["checkboxsigs"] = true;
return generateCacheCode(sig, varname);
}
string DocCompiler::generateVSlider(Tree sig, Tree path, Tree cur, Tree min, Tree max, Tree step)
{
string varname = getFreshID("{u_s}") + "(t)";
fLateq->addUISigFormula(getUIDir(path), prepareIntervallicUI(varname, path, cur, min, max));
gGlobal->gDocNoticeFlagMap["slidersigs"] = true;
return generateCacheCode(sig, varname);
}
string DocCompiler::generateHSlider(Tree sig, Tree path, Tree cur, Tree min, Tree max, Tree step)
{
string varname = getFreshID("{u_s}") + "(t)";
fLateq->addUISigFormula(getUIDir(path), prepareIntervallicUI(varname, path, cur, min, max));
gGlobal->gDocNoticeFlagMap["slidersigs"] = true;
return generateCacheCode(sig, varname);
}
string DocCompiler::generateNumEntry(Tree sig, Tree path, Tree cur, Tree min, Tree max, Tree step)
{
string varname = getFreshID("{u_n}") + "(t)";
fLateq->addUISigFormula(getUIDir(path), prepareIntervallicUI(varname, path, cur, min, max));
gGlobal->gDocNoticeFlagMap["nentrysigs"] = true;
return generateCacheCode(sig, varname);
}
string DocCompiler::generateVBargraph(Tree sig, Tree path, Tree min, Tree max, const string& exp)
{
string varname = getFreshID("{u_g}");
Type t = getCertifiedSigType(sig);
switch (t->variability()) {
case kKonst:
break;
case kBlock:
break;
case kSamp:
break;
}
return generateCacheCode(sig, varname);
}
string DocCompiler::generateHBargraph(Tree sig, Tree path, Tree min, Tree max, const string& exp)
{
string varname = getFreshID("{u_g}");
Type t = getCertifiedSigType(sig);
switch (t->variability()) {
case kKonst:
break;
case kBlock:
break;
case kSamp:
break;
}
return generateCacheCode(sig, varname);
}
string DocCompiler::generateAttach(Tree sig, Tree x, Tree y, int priority)
{
string vname;
string exp;
CS(y, priority);
exp = CS(x, priority);
if (getVectorNameProperty(x, vname)) {
setVectorNameProperty(sig, vname);
}
return generateCacheCode(sig, exp);
}
/*****************************************************************************
TABLES
(note : tables here are siplified versions different from the ones used to
generate c++ code)
*****************************************************************************/
/**
* Generate the equation of a constant table (its content is time constant).
* Returns the name of the table
*/
string DocCompiler::generateDocConstantTbl(Tree /*tbl*/, Tree size, Tree isig)
{
string vname, ctype;
string init = CS(isig, 0);
int n;
if (!isSigInt(size, &n)) {
cerr << "error in DocCompiler::generateDocConstantTbl() : " << *size
<< " is not an integer expression and can't be used as a table size' " << endl;
}
// allocate a name v_i for the table
getTypedNames(getCertifiedSigType(isig), "v", ctype, vname);
// add a comment on tables in the notice
gGlobal->gDocNoticeFlagMap["tablesigs"] = true;
// add equation v[t] = isig(t)
fLateq->addRDTblSigFormula(
subst("$0[t] = $1 \\condition{when $$t \\in [0,$2]$$} ", vname, init, T(n - 1)));
// note that the name of the table can never be used outside an sigDocTableAccess
return vname;
}
/**
* tests if a charactere is a word separator
*/
static bool isSeparator(char c)
{
bool w =
(((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')) || ((c >= '0') && (c <= '9')));
return !w;
}
/**
* Replaces the occurences of 't' in a formula with another character
*/
static string replaceTimeBy(const string& src, char r)
{
string dst;
char pre = 0;
for (size_t i = 0; i < src.size(); i++) {
char x = src[i];
if ((x == 't') && isSeparator(pre) && ((i == src.size() - 1) || isSeparator(src[i + 1]))) {
dst.push_back(r);
} else {
dst.push_back(x);
}
pre = x;
}
return dst;
}
/**
* Generate the equation of a write table, which content is time dependent.
* It is basically a signal of vectors.
*/
string DocCompiler::generateDocWriteTbl(Tree /*tbl*/, Tree size, Tree isig, Tree widx, Tree wsig)
{
string vname, ctype;
string init = CS(isig, 0);
int n;
if (!isSigInt(size, &n)) {
cerr << "error in DocCompiler::generateDocWriteTbl() : " << *size
<< " is not an integer expression and can't be used as a table size' " << endl;
}
// allocate a name w_i for the table
getTypedNames(getCertifiedSigType(isig), "w", ctype, vname);
// add a comment on tables in the notice
gGlobal->gDocNoticeFlagMap["tablesigs"] = true;
// describe the table equation
string ltqRWTableDef;
ltqRWTableDef += subst("$0(t)[i] = \n", vname);
ltqRWTableDef += "\\left\\{\\begin{array}{ll}\n";
ltqRWTableDef += subst("$0 & \\mbox{if \\,} t < 0 \\mbox{\\, and \\,} i \\in [0,$1] \\\\\n",
replaceTimeBy(init, 'i'), T(n - 1));
ltqRWTableDef += subst("$0 & \\mbox{if \\,} i = $1 \\\\\n", CS(wsig, 0), CS(widx, 0));
ltqRWTableDef += subst("$0(t\\!-\\!1)[i] & \\mbox{otherwise} \\\\\n", vname);
ltqRWTableDef += "\\end{array}\\right.";
// add the table equation
fLateq->addRWTblSigFormula(ltqRWTableDef); // w(t) = initsig(t)
// note that the name of the table can never be used outside an sigDocTableAccess
return vname;
}
/**
* Generate the equation of a write table, which content is time dependent.
* It is basically a signal of vectors.
*/
string DocCompiler::generateDocAccessTbl(Tree sig, Tree tbl, Tree ridx)
{
// the compilation of a table always returns its name
string vname = CS(tbl, 0);
string result = subst("$0[$1]", vname, CS(ridx, 0));
return generateCacheCode(sig, result);
}
bool DocCompiler::isShortEnough(string& s, unsigned int max)
{
return (s.length() <= max);
}
/*****************************************************************************
RECURSIONS
*****************************************************************************/
/**
* Generate code for a projection of a group of mutually recursive definitions
*/
string DocCompiler::generateRecProj(Tree sig, Tree r, int i, int priority)
{
string vname;
Tree var, le;
// cerr << "*** generateRecProj sig : \"" << ppsig(sig) << "\"" << endl;
if (!getVectorNameProperty(sig, vname)) {
faustassert(isRec(r, var, le));
// cerr << " generateRecProj has NOT YET a vname : " << endl;
// cerr << "--> generateRecProj calls generateRec on \"" << ppsig(sig) << "\"" << endl;
generateRec(r, var, le, priority);
faustassert(getVectorNameProperty(sig, vname));
// cerr << "<-- generateRecProj vname : \"" << subst("$0(t)", vname) << "\"" << endl;
} else {
// cerr << "(generateRecProj has already a vname : \"" << subst("$0(t)", vname) << "\")" <<
// endl;
}
return subst("$0(t)", vname);
}
/**
* Generate code for a group of mutually recursive definitions
*/
void DocCompiler::generateRec(Tree sig, Tree var, Tree le, int priority)
{
int N = len(le);
vector<bool> used(N);
vector<int> delay(N);
vector<string> vname(N);
vector<string> ctype(N);
// prepare each element of a recursive definition
for (int i = 0; i < N; i++) {
Tree e = sigProj(i, sig); // recreate each recursive definition
if (fOccMarkup.retrieve(e)) {
// this projection is used
used[i] = true;
// cerr << "generateRec : used[" << i << "] = true" << endl;
getTypedNames(getCertifiedSigType(e), "r", ctype[i], vname[i]);
gGlobal->gDocNoticeFlagMap["recursigs"] = true;
// cerr << "- r : generateRec setVectorNameProperty : \"" << vname[i] << "\"" << endl;
setVectorNameProperty(e, vname[i]);
delay[i] = fOccMarkup.retrieve(e)->getMaxDelay();
} else {
// this projection is not used therefore
// we should not generate code for it
used[i] = false;
// cerr << "generateRec : used[" << i << "] = false" << endl;
}
}
// generate delayline for each element of a recursive definition
for (int i = 0; i < N; i++) {
if (used[i]) {
generateDelayLine(ctype[i], vname[i], delay[i], CS(nth(le, i), priority));
}
}
}
/*****************************************************************************
PREFIX, DELAY A PREFIX VALUE
*****************************************************************************/
/**
* Generate LaTeX code for "prefix", a 1sample-delay explicitely initialized.
*
* @param sig The signal expression to treat.
* @param x The initial value for the delay line.
* @param e The value for the delay line, after initialization.
* @param priority The priority of the environment of the expression.
*
* @return <string> The LaTeX code translation of the signal, cached.
*/
string DocCompiler::generatePrefix(Tree sig, Tree x, Tree e, int priority)
{
string var = getFreshID("m");
string exp0 = CS(x, priority);
string exp1 = CS(e, priority); // ensure exp1 is compiled to have a vector name
string vecname;
if (!getVectorNameProperty(e, vecname)) {
cerr << "ASSERT : no vector name for : " << ppsig(e, MAX_ERROR_SIZE) << endl;
faustassert(false);
}
string ltqPrefixDef;
ltqPrefixDef += subst("$0(t) = \n", var);
ltqPrefixDef += "\\left\\{\\begin{array}{ll}\n";
ltqPrefixDef += subst("$0 & \\mbox{, when \\,} t = 0\\\\\n", exp0);
ltqPrefixDef += subst("$0 & \\mbox{, when \\,} t > 0\n", subst("$0(t\\!-\\!1)", vecname));
ltqPrefixDef += "\\end{array}\\right.";
fLateq->addPrefixSigFormula(ltqPrefixDef);
gGlobal->gDocNoticeFlagMap["prefixsigs"] = true;
return generateCacheCode(sig, subst("$0(t)", var));
}
// to be reviewed using table reading and sharing the construction of the value pair
/**
* Generate a select2 code
*/
string DocCompiler::generateSelect2(Tree sig, Tree sel, Tree s1, Tree s2, int priority)
{
string var = getFreshID("q");
string expsel = CS(sel, 0);
string exps1 = CS(s1, 0);
string exps2 = CS(s2, 0);
string ltqSelDef;
ltqSelDef += subst("$0(t) = \n", var);
ltqSelDef += "\\left\\{\\begin{array}{ll}\n";
ltqSelDef += subst("$0 & \\mbox{if \\,} $1 = 0\\\\\n", exps1, expsel);
ltqSelDef += subst("$0 & \\mbox{if \\,} $1 = 1\n", exps2, expsel);
ltqSelDef += "\\end{array}\\right.";
fLateq->addSelectSigFormula(ltqSelDef);
gGlobal->gDocNoticeFlagMap["selectionsigs"] = true;
// return generateCacheCode(sig, subst("$0(t)", var));
setVectorNameProperty(sig, var);
return subst("$0(t)", var);
}
/**
* retrieve the type annotation of sig
* @param sig the signal we want to know the type
*/
string DocCompiler::generateXtended(Tree sig, int priority)
{
xtended* p = (xtended*)getUserData(sig);
vector<string> args;
vector<Type> types;
for (int i = 0; i < sig->arity(); i++) {
args.push_back(CS(sig->branch(i), 0));
types.push_back(getCertifiedSigType(sig->branch(i)));
}
if (p->needCache()) {
// cerr << "!! generateXtended : <needCache> : calls generateCacheCode(sig,
// p->generateLateq(fLateq, args, types))" << endl;
return generateCacheCode(sig, p->generateLateq(fLateq, args, types));
} else {
// cerr << "!! generateXtended : <do not needCache> : calls p->generateLateq(fLateq, args,
// types)" << endl;
return p->generateLateq(fLateq, args, types);
}
}
//------------------------------------------------------------------------------------------------
/*****************************************************************************
vector name property
*****************************************************************************/
/**
* Set the vector name property of a signal, the name of the vector used to
* store the previous values of the signal to implement a delay.
* @param sig the signal expression.
* @param vecname the string representing the vector name.
* @return true is already compiled
*/
void DocCompiler::setVectorNameProperty(Tree sig, const string& vecname)
{
fVectorProperty.set(sig, vecname);
}
/**
* Get the vector name property of a signal, the name of the vector used to
* store the previous values of the signal to implement a delay.
* @param sig the signal expression.
* @param vecname the string where to store the vector name.
* @return true if the signal has this property, false otherwise
*/
bool DocCompiler::getVectorNameProperty(Tree sig, string& vecname)
{
return fVectorProperty.get(sig, vecname);
}
/*****************************************************************************
N-SAMPLE FIXED DELAY : sig = exp@delay
case 1-sample max delay :
Y(t-0) Y(t-1)
Temp Var gGlobal->gLessTempSwitch = false
V[0] V[1] gGlobal->gLessTempSwitch = true
case max delay < gGlobal->gMaxCopyDelay :
Y(t-0) Y(t-1) Y(t-2) ...
Temp V[0] V[1] ... gGlobal->gLessTempSwitch = false
V[0] V[1] V[2] ... gGlobal->gLessTempSwitch = true
case max delay >= gGlobal->gMaxCopyDelay :
Y(t-0) Y(t-1) Y(t-2) ...
Temp V[0] V[1] ...
V[0] V[1] V[2] ...
*****************************************************************************/
/**
* Generate code for accessing a delayed signal. The generated code depend of
* the maximum delay attached to exp and the gGlobal->gLessTempSwitch.
*
* @todo Priorités à revoir pour le parenthésage (associativité de - et /),
* avec gBinOpLateqTable dans binop.cpp.
*/
string DocCompiler::generateDelayAccess(Tree sig, Tree exp, Tree delay, int priority)
{
int d;
string vecname;
CS(exp, 0); // ensure exp is compiled to have a vector name
if (!getVectorNameProperty(exp, vecname)) {
cerr << "ASSERT : no vector name for : " << ppsig(exp, MAX_ERROR_SIZE) << endl;
faustassert(false);
}
if (isSigInt(delay, &d) && (d == 0)) {
// cerr << "@ generateDelayAccess : d = " << d << endl;
return subst("$0(t)", vecname);
} else {
// cerr << "@ generateDelayAccess : d = " << d << endl;
return subst("$0(t\\!-\\!$1)", vecname, CS(delay, 7));
}
}
/**
* Generate code for the delay mecchanism. The generated code depend of the
* maximum delay attached to exp and the "less temporaries" switch
*/
string DocCompiler::generateDelayVec(Tree sig, const string& exp, const string& ctype,
const string& vname, int mxd)
{
string s = generateDelayVecNoTemp(sig, exp, ctype, vname, mxd);
if (getCertifiedSigType(sig)->variability() < kSamp) {
return exp;
} else {
return s;
}
}
/**
* Generate code for the delay mecchanism without using temporary variables
*/
string DocCompiler::generateDelayVecNoTemp(Tree sig, const string& exp, const string& ctype,
const string& vname, int mxd)
{
faustassert(mxd > 0);
// cerr << " entering generateDelayVecNoTemp" << endl;
string vectorname;
// if generateVariableStore has already tagged sig, no definition is needed.
if (getVectorNameProperty(sig, vectorname)) {
return subst("$0(t)", vectorname);
} else {
fLateq->addRecurSigFormula(subst("$0(t) = $1", vname, exp));
setVectorNameProperty(sig, vname);
return subst("$0(t)", vname);
}
}
/**
* Generate code for the delay mecchanism without using temporary variables
*/
void DocCompiler::generateDelayLine(const string& ctype, const string& vname, int mxd,
const string& exp)
{
// faustassert(mxd > 0);
if (mxd == 0) {
fLateq->addRecurSigFormula(subst("$0(t) = $1", vname, exp));
} else {
fLateq->addRecurSigFormula(subst("$0(t) = $1", vname, exp));
}
}
/****************************************************************
User interface element utilities.
*****************************************************************/
/**
* @brief Get the directory of a user interface element.
*
* Convert the input reversed path tree into a string.
* The name of the UI is stripped (the head of the path tree),
* the rest of the tree is a list of pointed pairs, where the names
* are contained by the tail of these pointed pairs.
* Metadatas (begining by '[') are stripped.
*
* @param[in] pathname The path tree to convert.
* @return <string> A directory-like string.
*/
string DocCompiler::getUIDir(Tree pathname)
{
// cerr << "Documentator : getUIDir : print(pathname, stdout) = "; print(pathname, stdout); cerr
// << endl;
string s;
Tree dir = reverse(tl(pathname));
while (!isNil(dir)) {
string tmp = tree2str(tl(hd(dir)));
if ((tmp[0] != '[') && (!tmp.empty())) {
s += tmp + '/';
}
dir = tl(dir);
}
return s;
}
/**
* @brief Prepare binary user interface elements (button, checkbox).
*
* - Format a LaTeX output string as a supertabular row with 3 columns :
* "\begin{supertabular}{lll}". @see Lateq::printHierarchy
* - The UI range is only a set of two values : {0, 1}.
* - The UI current value is automatically 0.
*
* @param[in] name The LaTeX name of the UI signal (eg. "{u_b}_{i}(t)").
* @param[in] path The path tree to parse.
* @return <string> The LaTeX output string.
*/
string DocCompiler::prepareBinaryUI(const string& name, Tree path)
{
string label, unit;
getUIDocInfos(path, label, unit);
string s = "";
label = (label.size() > 0) ? ("\\textsf{\"" + label + "\"} ") : "";
unit = (unit.size() > 0) ? ("\\ (" + unit + ")") : "";
s += label + unit;
s += " & $" + name + "$";
s += " $\\in$ $\\left\\{\\,0, 1\\,\\right\\}$";
s += " & $(\\mbox{" + gGlobal->gDocMathStringMap["defaultvalue"] + "} = 0)$\\\\";
return s;
}
/**
* @brief Prepare "intervallic" user interface elements (sliders, nentry).
*
* - Format a LaTeX output string as a supertabular row with 3 columns :
* "\begin{supertabular}{lll}". @see Lateq::printHierarchy
* - The UI range is an bounded interval : [tmin, tmax].
* - The UI current value is tcur.
*
* @param[in] name The LaTeX name of the UI signal (eg. "{u_s}_{i}(t)").
* @param[in] path The path tree to parse.
* @param[in] tcur The current UI value tree to convert.
* @param[in] tmin The minimum UI value tree to convert.
* @param[in] tmax The maximum UI value tree to convert.
* @return <string> The LaTeX output string.
*/
string DocCompiler::prepareIntervallicUI(const string& name, Tree path, Tree tcur, Tree tmin,
Tree tmax)
{
string label, unit, cur, min, max;
getUIDocInfos(path, label, unit);
cur = docT(tree2double(tcur));
min = docT(tree2double(tmin));
max = docT(tree2double(tmax));
string s = "";
label = (label.size() > 0) ? ("\\textsf{\"" + label + "\"} ") : "";
unit = (unit.size() > 0) ? ("\\ (" + unit + ")") : "";
s += label + unit;
s += " & $" + name + "$";
s += " $\\in$ $\\left[\\," + min + ", " + max + "\\,\\right]$";
s += " & $(\\mbox{" + gGlobal->gDocMathStringMap["defaultvalue"] + "} = " + cur + ")$\\\\";
return s;
}
/**
* Get information on a user interface element for documentation.
*
* @param[in] path The UI full pathname to parse.
* @param[out] label The place to store the UI name.
* @param[out] unit The place to store the UI unit.
*/
void DocCompiler::getUIDocInfos(Tree path, string& label, string& unit)
{
label = "";
unit = "";
map<string, set<string> > metadata;
extractMetadata(tree2str(hd(path)), label, metadata);
set<string> myunits = metadata["unit"];
// for (set<string>::iterator i = myunits.begin(); i != myunits.end(); i++) {
// cerr << "Documentator : getUIDocInfos : metadata[\"unit\"] = " << *i << endl;
// }
for (map<string, set<string> >::iterator i = metadata.begin(); i != metadata.end(); i++) {
const string& key = i->first;
const set<string>& values = i->second;
for (set<string>::const_iterator j = values.begin(); j != values.end(); j++) {
if (key == "unit") {
unit += *j;
}
}
}
}
|