/usr/include/boolstuff-0.1/boolstuff/BoolExpr.cpp is in boolstuff-dev 0.1.15-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /* $Id: BoolExpr.cpp,v 1.31 2014/03/01 17:30:58 sarrazip Exp $
BoolExpr.cpp - Boolean expression binary tree node
boolstuff - Disjunctive Normal Form boolean expression library
Copyright (C) 2002-2013 Pierre Sarrazin <http://sarrazip.com/>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef _H_BoolExpr
#error "This file is only meant to be included by BoolExpr.h"
#endif
#include <set>
#include <algorithm>
#include <stdlib.h>
// Define this to enable traces that help in leak detection
// when used with script leak-detector.pl.
//
//#define BOOLSTUFF_LEAK_DETECTION 1
template <class T>
BoolExpr<T>::BoolExpr(const T &initValue /*= T()*/)
: type(VALUE),
value(initValue),
left(NULL),
right(NULL)
{
#ifdef BOOLSTUFF_LEAK_DETECTION
std::cerr << "CONS " << (void *) this << " " << type << " " << value << std::endl;
#endif
}
template <class T>
BoolExpr<T>::BoolExpr(Type t, BoolExpr *l, BoolExpr *r)
: type(t),
value(),
left(l),
right(r)
{
#ifdef BOOLSTUFF_LEAK_DETECTION
std::cerr << "CONS " << (void *) this << " " << type << " " << value << std::endl;
#endif
assert(type == AND || type == OR || type == NOT);
assert((type == NOT) == (!l && r)); // only a right subtree for a negation
assert((type == AND || type == OR) == (l && r)); // two subtrees for a binary operator
}
template <class T>
BoolExpr<T>::~BoolExpr()
{
#ifdef BOOLSTUFF_LEAK_DETECTION
std::cerr << "DEST " << (void *) this << " " << type << " " << value << std::endl;
#endif
delete left;
delete right;
}
template <class T>
inline
typename BoolExpr<T>::Type
BoolExpr<T>::getType() const
{
return type;
}
template <class T>
inline
const BoolExpr<T> *
BoolExpr<T>::getLeft() const
{
return left;
}
template <class T>
inline
BoolExpr<T> *
BoolExpr<T>::getLeft()
{
return left;
}
template <class T>
inline
const BoolExpr<T> *
BoolExpr<T>::getRight() const
{
return right;
}
template <class T>
inline
BoolExpr<T> *
BoolExpr<T>::getRight()
{
return right;
}
template <class T>
inline
const T &
BoolExpr<T>::getValue() const
{
return value;
}
template <class T>
inline
void
BoolExpr<T>::setType(Type t)
{
type = t;
}
template <class T>
inline
void
BoolExpr<T>::setLeft(BoolExpr *subtree)
{
left = subtree;
}
template <class T>
inline
void
BoolExpr<T>::setRight(BoolExpr *subtree)
{
right = subtree;
}
template <class T>
inline
void
BoolExpr<T>::setValue(const T &v)
{
value = v;
}
template <class T>
inline
T &
BoolExpr<T>::getValue()
{
return value;
}
template <class T>
bool
BoolExpr<T>::isDisjunctiveNormalForm() const
{
if (type == VALUE)
return (left == NULL && right == NULL);
if (type == NOT)
{
if (left != NULL || right == NULL || !right->isDisjunctiveNormalForm())
return false;
return (right->type == VALUE || right->type == NOT);
}
if (type == AND)
{
if (left == NULL || right == NULL
|| !left->isDisjunctiveNormalForm()
|| !right->isDisjunctiveNormalForm())
return false;
return (left->type != OR && right->type != OR);
}
if (type == OR)
{
if (left == NULL || right == NULL
|| !left->isDisjunctiveNormalForm()
|| !right->isDisjunctiveNormalForm())
return false;
return true;
}
return false;
}
template <class T>
template <class OutputIter>
OutputIter
BoolExpr<T>::getDNFTermRoots(OutputIter dest) const
{
switch (type)
{
case OR:
assert(left != NULL);
assert(right != NULL);
dest = left->getDNFTermRoots(dest);
return right->getDNFTermRoots(dest);
case AND:
case NOT:
assert(right != NULL);
assert((left == NULL) == (type == NOT));
case VALUE:
*dest++ = const_cast<BoolExpr<T> *>(this);
return dest;
default:
assert(false);
return dest;
}
}
// This expression is not expected to contain OR nodes.
//
template <class T>
template <class OutputIter>
OutputIter
BoolExpr<T>::getDNFFactorRoots(OutputIter dest) const
{
switch (type)
{
case AND:
assert(left != NULL);
assert(right != NULL);
dest = left->getDNFFactorRoots(dest);
return right->getDNFFactorRoots(dest);
case OR:
return dest;
case NOT:
assert(right != NULL);
assert(left == NULL);
case VALUE:
*dest++ = const_cast<BoolExpr<T> *>(this);
return dest;
default:
assert(false);
return dest;
}
}
template <class T>
void
BoolExpr<T>::getTreeVariables(
std::set<T> &positives, std::set<T> &negatives) const
{
if (type == VALUE)
{
positives.insert(value);
return;
}
if (type == NOT)
{
assert(right != NULL);
negatives.insert(right->value);
return;
}
assert(type == OR || type == AND);
assert(left != NULL && right != NULL);
left->getTreeVariables(positives, negatives);
right->getTreeVariables(positives, negatives);
}
template <class T>
bool
BoolExpr<T>::isDNFTermUseful() const
{
std::set<T> positives, negatives;
getTreeVariables(positives, negatives);
return isDNFTermUseful(positives, negatives);
}
template <class T>
bool
BoolExpr<T>::isDNFTermUseful(const std::set<T> &positives,
const std::set<T> &negatives) const
{
std::set<T> intersection;
set_intersection(positives.begin(), positives.end(),
negatives.begin(), negatives.end(),
std::inserter(intersection, intersection.end()));
return intersection.empty();
}
template <class T>
void
BoolExpr<T>::print(std::ostream &out) const
{
switch (type)
{
case VALUE:
out << value;
break;
case NOT:
{
assert(right != NULL);
bool paren = (right->type == AND || right->type == OR);
out << '!';
if (paren)
out << '(';
right->print(out);
if (paren)
out << ')';
}
break;
case OR:
{
assert(left != NULL);
assert(right != NULL);
left->print(out);
out << "|";
right->print(out);
}
break;
case AND:
{
assert(left != NULL);
assert(right != NULL);
bool paren = (left->type == OR);
if (paren)
out << '(';
left->print(out);
if (paren)
out << ')';
out << '&';
paren = (right->type == OR);
if (paren)
out << '(';
right->print(out);
if (paren)
out << ')';
}
break;
default:
assert(false);
}
}
template <class T>
std::string
BoolExpr<T>::print() const
{
std::ostringstream buffer;
print(buffer);
return buffer.str();
}
template <class T>
/*static*/
BoolExpr<T> *
BoolExpr<T>::cloneTree(const BoolExpr<T> *root)
{
if (root == NULL)
return NULL;
BoolExpr<T> *leftClone = cloneTree(root->left);
BoolExpr<T> *rightClone = cloneTree(root->right);
BoolExpr<T> *cloneRoot = new BoolExpr<T>(root->value);
cloneRoot->type = root->type;
cloneRoot->left = leftClone;
cloneRoot->right = rightClone;
return cloneRoot;
}
// Returns -1 if term a comes before term b, +1 if term a comes after term b,
// or 0 if they are equal.
//
template <class T>
static int exprComparator(BoolExpr<T> *a, BoolExpr<T> *b)
{
if (a == NULL && b != NULL)
return -1;
if (b == NULL)
return a == NULL ? 0 : +1;
typename BoolExpr<T>::Type aType = a->getType();
typename BoolExpr<T>::Type bType = b->getType();
if (aType != bType)
return int(aType) < int(bType) ? -1 : +1;
switch (aType)
{
case BoolExpr<T>::VALUE:
{
const T &aValue = a->getValue();
const T &bValue = b->getValue();
if (aValue < bValue)
return -1;
if (bValue < aValue)
return +1;
return 0;
}
case BoolExpr<T>::AND:
case BoolExpr<T>::OR:
{
int leftComparison = exprComparator(a->getLeft(), b->getLeft());
if (leftComparison != 0)
return leftComparison;
}
/*** NO BREAK ***/
case BoolExpr<T>::NOT:
return exprComparator(a->getRight(), b->getRight());
}
return true;
}
// Returns true iff term a comes before term b.
//
template <class T>
static bool exprOrdering(BoolExpr<T> *a, BoolExpr<T> *b)
{
return exprComparator(a, b) < 0;
}
template <class T>
/*static*/
BoolExpr<T> *
BoolExpr<T>::getDisjunctiveNormalForm(BoolExpr<T> *root)
{
bool tooLarge = false;
BoolExpr<T> *dnfRoot = getRawDNF(root, tooLarge);
if (dnfRoot == NULL)
return NULL;
// Get the terms of the DNF:
std::vector<BoolExpr<T> *> termRoots;
dnfRoot->getDNFTermRoots(std::inserter(termRoots, termRoots.end()));
// Destroy the OR nodes.
destroyDNFBinaryOpNodes(dnfRoot, true);
dnfRoot = NULL; // dnfRoot is invalid at this point
simplifyConjunctionList(termRoots);
// If only one term, consider it "useful",
// because we must return something:
if (termRoots.size() <= 1)
return termRoots.front();
// Sort terms to find repetitions:
std::sort(termRoots.begin(), termRoots.end(), exprOrdering<T>);
// Determine which terms are useful (terms that don't always evaluate to false,
// and are not the same as a previous term):
std::vector<BoolExpr<T> *> usefulTerms;
std::vector<bool> isUsefulTerm;
typename std::vector<BoolExpr<T> *>::const_iterator it;
typename std::vector<BoolExpr<T> *>::const_iterator previous = termRoots.end();
for (it = termRoots.begin(); it != termRoots.end(); previous = it, it++)
{
BoolExpr<T> *t = *it;
assert(t != NULL);
bool u = t->isDNFTermUseful();
// If 'it' is not the first term in termRoots, and 'it' is equal to
// the previous term in termRoots, then 'it' is not useful.
//
if (previous != termRoots.end() && exprComparator(*previous, t) == 0)
u = false;
isUsefulTerm.push_back(u);
if (u)
usefulTerms.push_back(const_cast<BoolExpr<T> *>(t));
}
assert(isUsefulTerm.size() == termRoots.size());
if (usefulTerms.size() != termRoots.size()) // if not all terms useful
{
// If all the terms are useless (e.g., a&!a | b&!b),
// return the first one, because we must return something.
if (usefulTerms.size() == 0)
{
// Destroy all term trees except the first one.
for (it = termRoots.begin(), it++; it != termRoots.end(); it++)
delete *it;
// Return the first term.
return termRoots.front();
}
// Destroy the trees in 'termRoots' that are not useful:
for (it = termRoots.begin(); it != termRoots.end(); it++)
if (!isUsefulTerm[it - termRoots.begin()])
delete *it;
}
// Join the useful terms with OR nodes:
return joinTreesWithOrNodes(usefulTerms);
}
template <class T>
/*static*/
void
BoolExpr<T>::simplifyConjunctionList(std::vector<BoolExpr<T> *> &conjunctionList)
{
typename std::vector<BoolExpr<T> *>::iterator jt;
for (jt = conjunctionList.begin(); jt != conjunctionList.end(); jt++)
*jt = simplifyConjunction(*jt);
}
// Assumes that 'conj' is a term of a DNF, i.e., there are no OR nodes.
// Gets the factor roots from 'conj'.
// For example, if 'conj' represents a&b&c, those roots are a, b and c.
// The AND nodes are destroyed.
// Then the factor roots are sorted and duplicates are destroyed.
// A new conjunction is formed from the remaining factor roots.
// If a single factor root is left, it is returned as is.
//
template <class T>
/*static*/
BoolExpr<T> *
BoolExpr<T>::simplifyConjunction(BoolExpr<T> *conj)
{
if (conj == NULL)
return NULL;
// From a&!b&c, get a vector containing a, !b, c.
//
std::vector<BoolExpr<T> *> factorRoots;
conj->getDNFFactorRoots(std::inserter(factorRoots, factorRoots.end()));
// Destroy the AND nodes.
destroyDNFBinaryOpNodes(conj, false);
conj = NULL; // conj now invalid; factorRoots holds the term trees
// Sort the factors to find the duplicates.
//
std::sort(factorRoots.begin(), factorRoots.end(), exprOrdering<T>);
BoolExpr<T> *previous = NULL;
typename std::vector<BoolExpr<T> *>::iterator it;
for (it = factorRoots.begin(); it != factorRoots.end(); it++)
{
BoolExpr<T> *f = *it;
assert(f != NULL);
assert(f->type == VALUE || f->type == NOT); // expecting factor of a DNF
assert(f->type != NOT || (!f->left && f->right && f->right->type == VALUE));
if (previous && exprComparator(previous, f) == 0)
{
// This factor is the same as the previous.
// Destroy it and remove it from factorRoots[].
delete f;
*it = NULL;
}
else
previous = f;
}
// Build new conjunction from non null pointers in factorRoots[].
BoolExpr<T> *newConj = NULL;
for (it = factorRoots.begin(); it != factorRoots.end(); it++)
{
BoolExpr<T> *f = *it;
if (f == NULL)
continue;
if (newConj == NULL) // if first factor of new conjunction
newConj = f;
else
newConj = new BoolExpr<T>(AND, newConj, f);
}
return newConj;
}
template <class T>
/*static*/
void
BoolExpr<T>::simplifyXAndXTerms(std::vector<BoolExpr<T> *> &terms)
{
typename std::vector<BoolExpr<T> *>::iterator jt;
for (jt = terms.begin(); jt != terms.end(); jt++)
{
BoolExpr<T> *term = *jt;
if (term->type == AND
&& term->left->type == VALUE
&& term->right->type == VALUE
&& term->left->value == term->right->value)
{
*jt = term->left;
term->left = NULL;
delete term; // destroys AND, and VALUE at right
}
}
}
// destroyOrNodes: if true, OR nodes are destroyed; otherwise,
// AND nodes are destroyed.
//
template <class T>
/*static*/
void
BoolExpr<T>::destroyDNFBinaryOpNodes(BoolExpr<T> *root, bool destroyOrNodes)
{
if (root == NULL)
return;
Type targetedType = (destroyOrNodes ? OR : AND);
if (root->type != targetedType)
return;
// Detach the targeted node's subtrees, so that 'delete' does not affect them:
BoolExpr<T> *left = root->left, *right = root->right;
assert(left != NULL && right != NULL);
root->left = root->right = NULL;
delete root;
destroyDNFBinaryOpNodes(left, destroyOrNodes);
destroyDNFBinaryOpNodes(right, destroyOrNodes);
}
template <class T>
/*static*/
BoolExpr<T> *
BoolExpr<T>::joinTreesWithOrNodes(const std::vector<BoolExpr<T> *> &trees)
{
if (trees.size() == 0)
return NULL;
if (trees.size() == 1)
return trees.front();
typename std::vector<BoolExpr<T> *>::const_iterator it = trees.begin();
BoolExpr<T> *left = *it++;
BoolExpr<T> *right = *it++;
assert(left && right);
BoolExpr<T> *result = new BoolExpr<T>(OR, left, right);
for ( ; it != trees.end(); it++)
result = new BoolExpr<T>(OR, result, *it);
return result;
}
#if 0 // debugging code
template <class T>
static void printTree(const char *prefix, BoolExpr<T> *tree, size_t level)
{
std::cout << prefix << ": L" << level << ", tree@" << (void *) tree;
if (tree)
std::cout << ": " << tree->print();
std::cout << std::endl;
}
class Decrementer
{
public:
Decrementer(size_t &r) : counter(r) {}
~Decrementer() { --counter; }
size_t &counter;
};
#endif
template <class T>
/*static*/
BoolExpr<T> *
BoolExpr<T>::getRawDNF(BoolExpr<T> *root, bool &tooLarge)
{
#if 0 // debugging code
static size_t level = 0;
++level;
Decrementer d(level);
#endif
tooLarge = false;
if (root == NULL)
return NULL;
/* One-level trees:
*/
if (root->type == VALUE)
return root;
/* Two-level trees:
*/
assert(root->right != NULL);
switch (root->type)
{
case NOT:
assert(root->left == NULL);
if (root->right->type == VALUE)
return root;
break;
case OR:
case AND:
assert(root->left != NULL);
if (root->left->type == VALUE && root->right->type == VALUE)
return root;
break;
default:
assert(false);
}
/* Three or more levels:
*/
root->left = getRawDNF(root->left, tooLarge);
if (tooLarge)
return NULL;
assert(root->left == NULL || root->left->isDisjunctiveNormalForm());
root->right = getRawDNF(root->right, tooLarge);
if (tooLarge)
return NULL;
assert(root->right == NULL || root->right->isDisjunctiveNormalForm());
/* Here, because of some simplifications, we may now have
a two-level tree. For example, if the original tree was
(!(!a)) & (!(!m)). The double negations have been simplified
and we now have a&m.
*/
assert(root->right != NULL);
if (root->type == NOT)
{
assert(root->left == NULL);
if (root->right->type == NOT)
{
/* Two NOTs make a positive:
*/
assert(root->right->left == NULL);
assert(root->right->right != NULL);
BoolExpr<T> *newRoot = root->right->right;
assert(newRoot->isDisjunctiveNormalForm());
root->right->right = NULL;
delete root; // deletes two nodes
return newRoot;
}
BoolExpr<T> *x = root->right;
root->right = NULL;
delete root;
return negateDNF(x, tooLarge); // returns null if resulting expression would be too large
}
assert(root->type != NOT);
assert(root->left != NULL);
/* If one side is a value, make sure that this value is at the left:
*/
if (root->left->type != VALUE && root->right->type == VALUE)
std::swap(root->left, root->right);
/* Permutate the left and right subtrees if they are
not in our "conventional order":
*/
if (root->left->type == NOT && root->right->type == OR)
std::swap(root->left, root->right);
else if (root->left->type == NOT && root->right->type == AND)
std::swap(root->left, root->right);
else if (root->left->type == OR && root->right->type == AND)
std::swap(root->left, root->right);
/* Conventional order:
root->left->type and root->right->type are expected to be
equal or to be one of (OR, NOT), (AND, NOT), (AND, OR).
*/
if (root->type == OR)
{
if (root->left->type == VALUE && root->right->type == NOT)
{
// Expected because of recursion step:
assert(root->right->right->type == VALUE);
return root;
}
if (root->left->type == VALUE)
return root;
if (root->left->type != NOT && root->right->type != NOT)
return root;
if (root->left->type == NOT)
{
assert(root->left->left == NULL);
assert(root->right->type == NOT); // expected re: conv. order
assert(root->right->left == NULL);
// Expected because of recursion step:
assert(root->left->right->type == VALUE);
assert(root->right->right->type == VALUE);
return root;
}
if (root->right->type != NOT)
return root;
// Expected because of recursion step:
assert(root->right->right->type == VALUE);
return root;
}
if (root->type == AND)
{
if (root->left->type == VALUE && root->right->type == NOT)
{
// Expected because of recursion step:
assert(root->right->right->type == VALUE);
return root;
}
if (root->left->type == VALUE && root->right->type == AND)
return root;
if (root->left->type == VALUE && root->right->type == OR)
{
BoolExpr<T> *andNode = root;
BoolExpr<T> *x = root->left;
BoolExpr<T> *orNode = root->right;
BoolExpr<T> *a = root->right->left;
BoolExpr<T> *b = root->right->right;
andNode->right = a;
BoolExpr<T> *xClone = new BoolExpr<T>(x->getValue());
BoolExpr<T> *newAndNode = new BoolExpr<T>(AND, xClone, b);
orNode->left = andNode;
orNode->right = newAndNode;
return getRawDNF(orNode, tooLarge);
}
if (root->left->type == AND && root->right->type == AND)
return root;
if (root->left->type == VALUE && root->right->type == VALUE)
return root;
if (root->left->type == NOT)
{
assert(root->left->left == NULL);
assert(root->right->type == NOT); // expected re: conv. order
assert(root->right->left == NULL);
// Expected because of recursion step:
assert(root->left->right->type == VALUE);
assert(root->right->right->type == VALUE);
return root;
}
if (root->right->type == NOT)
{
assert(root->right->left == NULL);
assert(root->right->right != NULL);
assert(root->right->right->type == VALUE);
if (root->left->type == AND)
return root;
BoolExpr<T> *a = root->left->left;
BoolExpr<T> *b = root->left->right;
BoolExpr<T> *c = root->right->right;
BoolExpr<T> *andNode = root;
BoolExpr<T> *orNode = root->left;
BoolExpr<T> *notNode = root->right;
// We have (a|b) & !c, which becomes (a&!c) | (b&!c):
BoolExpr<T> *newCNode = new BoolExpr<T>(c->getValue());
BoolExpr<T> *newNotNode = new BoolExpr<T>(NOT, NULL, newCNode);
BoolExpr<T> *newAndNode = new BoolExpr<T>(AND, b, newNotNode);
orNode->left = andNode;
orNode->right = newAndNode;
andNode->left = a;
andNode->right = notNode;
notNode->right = c;
return getRawDNF(orNode, tooLarge);
}
assert(root->right->type == OR);
if (root->left->type == OR)
{
BoolExpr<T> *a = root->left->left;
BoolExpr<T> *b = root->left->right;
BoolExpr<T> *c = root->right->left;
BoolExpr<T> *d = root->right->right;
BoolExpr<T> *andNode = root;
BoolExpr<T> *leftOrNode = root->left;
BoolExpr<T> *rightOrNode = root->right;
// We have (a|b) & (c|d), which becomes a&b | a&c | b&c | b&d:
andNode->left = a;
andNode->right = c;
BoolExpr<T> *aClone = cloneTree(a);
BoolExpr<T> *firstNewAndNode = new BoolExpr<T>(AND, aClone, d);
BoolExpr<T> *cClone = cloneTree(c);
BoolExpr<T> *secondNewAndNode = new BoolExpr<T>(AND, b, cClone);
BoolExpr<T> *bClone = cloneTree(b);
BoolExpr<T> *dClone = cloneTree(d);
BoolExpr<T> *thirdNewAndNode = new BoolExpr<T>(AND, bClone, dClone);
leftOrNode->left = andNode;
leftOrNode->right = firstNewAndNode;
rightOrNode->left = secondNewAndNode;
rightOrNode->right = thirdNewAndNode;
BoolExpr<T> *newRoot = new BoolExpr<T>(OR, leftOrNode, rightOrNode);
return getRawDNF(newRoot, tooLarge);
}
if (root->left->type == AND)
{
BoolExpr<T> *a = root->left->left;
BoolExpr<T> *b = root->left->right;
BoolExpr<T> *c = root->right->left;
BoolExpr<T> *d = root->right->right;
BoolExpr<T> *topAndNode = root;
BoolExpr<T> *rightOrNode = root->right;
BoolExpr<T> *aClone = cloneTree(a);
BoolExpr<T> *bClone = cloneTree(b);
BoolExpr<T> *newLowAndNode = new BoolExpr<T>(AND, aClone, bClone);
BoolExpr<T> *newHighAndNode = new BoolExpr<T>(AND, newLowAndNode, d);
topAndNode->right = c;
rightOrNode->left = topAndNode;
rightOrNode->right = newHighAndNode;
return getRawDNF(rightOrNode, tooLarge);
}
assert(false);
return NULL;
}
return NULL;
}
template <class T>
inline std::ostream &
operator << (std::ostream &out, const std::set<T> &s)
{
out << "{ ";
for (typename std::set<T>::const_iterator jt = s.begin();
jt != s.end(); jt++)
out << *jt << " ";
out << "}";
return out;
}
// A "literal" is a variable name, possibly accompanied by a NOT operator.
template <class T>
class Literal
{
public:
Literal(const T &v, bool p) : value(v), pos(p) {}
T value;
bool pos;
bool operator == (const Literal &lit) const
{
return pos == lit.pos && value == lit.value;
}
bool operator < (const Literal &lit) const
{
if (pos != lit.pos)
return pos < lit.pos;
return value < lit.value;
}
};
/* tooLarge: indicates if the resulting expression would be too large;
check this when the returned pointer is null.
*/
template <class T>
/*static*/
BoolExpr<T> *
BoolExpr<T>::negateDNF(BoolExpr<T> *root, bool &tooLarge)
{
tooLarge = false;
if (root == NULL)
return NULL;
assert(root->isDisjunctiveNormalForm());
std::vector<const BoolExpr<T> *> termRoots;
root->getDNFTermRoots(std::inserter(termRoots, termRoots.end()));
assert(termRoots.size() > 0);
// Re-express the DNF as a list of lists of literals.
std::vector< std::vector< Literal<T> > > terms;
typename std::vector<const BoolExpr<T> *>::const_iterator it;
size_t product = 1; // will be the product of the sizes of the terms
const size_t max = 10000; // maximum allowed product (to avoid using excessive memory)
size_t indexOfFirstUselessTerm = size_t(-1);
for (it = termRoots.begin(); it != termRoots.end(); it++)
{
std::set<T> pos, neg;
(*it)->getTreeVariables(pos, neg);
bool useful = (*it)->isDNFTermUseful(pos, neg);
if (useful || indexOfFirstUselessTerm == size_t(-1))
{
if (!useful)
{
// Remember position of 1st useless term encountered.
// If all terms are useless, we will use it, because
// we must return something.
indexOfFirstUselessTerm = terms.size();
}
terms.push_back(std::vector< Literal<T> >());
std::vector< Literal<T> > &variables = terms.back();
typename std::set<T>::const_iterator jt;
for (jt = pos.begin(); jt != pos.end(); jt++)
variables.push_back(Literal<T>(*jt, true));
for (jt = neg.begin(); jt != neg.end(); jt++)
variables.push_back(Literal<T>(*jt, false));
assert(variables.size() > 0);
if (product > max / variables.size())
{
delete root;
tooLarge = true; // resulting expression would be too large
return NULL;
}
product *= variables.size();
}
}
// 'root' and 'termRoots' are not useful anymore.
// Only 'terms' is used from now on.
termRoots.clear();
delete root;
root = NULL;
// If at least one useless term has been seen, but a useful term
// has also been seen, then get rid of the useless term designated
// by 'indexOfFirstUselessTerm'.
if (indexOfFirstUselessTerm != size_t(-1) && terms.size() > 1)
{
terms.erase(terms.begin() + indexOfFirstUselessTerm);
}
// Create a disjunction that is the negation of the useful terms
// seen in the original expression.
//
std::vector<size_t> indexVec(terms.size(), 0);
size_t numTermsCreated = 0;
BoolExpr<T> *disjunction = NULL;
for (;;)
{
BoolExpr<T> *conjunction = NULL;
std::set< Literal<T> > usedLiterals;
for (size_t i = 0; i < indexVec.size(); i++)
{
assert(indexVec[i] < terms[i].size());
const Literal<T> &lit = terms[i][indexVec[i]];
// If 'lit' not already used in the conjunction:
if (usedLiterals.find(lit) == usedLiterals.end())
{
// Create a node (or two) for 'lit' and add it
// to the current conjunction.
BoolExpr<T> *literal = new BoolExpr<T>(lit.value);
if (lit.pos)
literal = new BoolExpr<T>(NOT, NULL, literal);
if (conjunction == NULL)
conjunction = literal;
else
conjunction = new BoolExpr<T>(AND, conjunction, literal);
usedLiterals.insert(lit);
}
}
numTermsCreated++;
if (disjunction == NULL)
disjunction = conjunction;
else
disjunction = new BoolExpr<T>(OR, disjunction, conjunction);
size_t i = indexVec.size() - 1;
for ( ; i != size_t(-1); --i)
{
indexVec[i]++;
if (indexVec[i] < terms[i].size())
break;
// Overflow:
indexVec[i] = 0;
}
if (i == size_t(-1))
break;
}
assert(disjunction != NULL);
return disjunction;
}
|