use iterators instead off indices
This commit is contained in:
parent
62fedf014b
commit
5e7b660f44
@ -13,11 +13,13 @@ CTNode::addChild (CTNode* child, bool updateLevels)
|
|||||||
updateChildLevels (child, level_ + 1);
|
updateChildLevels (child, level_ + 1);
|
||||||
}
|
}
|
||||||
bool found = false;
|
bool found = false;
|
||||||
for (unsigned i = 0; i < childs_.size(); i++) {
|
for (CTNodes::const_iterator chIt = childs_.begin();
|
||||||
if (child->symbol() == childs_[i]->symbol()) {
|
chIt != childs_.end(); ++ chIt) {
|
||||||
CTNodes childChilds = child->childs();
|
if (child->symbol() == (*chIt)->symbol()) {
|
||||||
for (unsigned j = 0; j < childChilds.size(); j++) {
|
CTNodes childChildsCopy = child->childs();
|
||||||
childs_[i]->addChild (childChilds[j], false);
|
for (CTNodes::const_iterator ccIt = childChildsCopy.begin();
|
||||||
|
ccIt != childChildsCopy.end(); ++ ccIt) {
|
||||||
|
(*chIt)->addChild (*ccIt, false);
|
||||||
}
|
}
|
||||||
found = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
@ -27,6 +29,7 @@ CTNode::addChild (CTNode* child, bool updateLevels)
|
|||||||
delete child;
|
delete child;
|
||||||
} else {
|
} else {
|
||||||
childs_.push_back (child);
|
childs_.push_back (child);
|
||||||
|
// childs_.insert (child);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,8 +38,8 @@ CTNode::addChild (CTNode* child, bool updateLevels)
|
|||||||
void
|
void
|
||||||
CTNode::removeChild (CTNode* child)
|
CTNode::removeChild (CTNode* child)
|
||||||
{
|
{
|
||||||
CTNodes::iterator it =
|
CTNodes::iterator it;
|
||||||
std::find (childs_.begin(), childs_.end(), child);
|
it = std::find (childs_.begin(), childs_.end(), child);
|
||||||
assert (it != childs_.end());
|
assert (it != childs_.end());
|
||||||
childs_.erase (it);
|
childs_.erase (it);
|
||||||
}
|
}
|
||||||
@ -63,8 +66,9 @@ CTNode::removeAndDeleteChild (CTNode* child)
|
|||||||
void
|
void
|
||||||
CTNode::removeAndDeleteAllChilds (void)
|
CTNode::removeAndDeleteAllChilds (void)
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < childs_.size(); i++) {
|
for (CTNodes::const_iterator chIt = childs_.begin();
|
||||||
deleteSubtree (childs_[i]);
|
chIt != childs_.end(); ++ chIt) {
|
||||||
|
deleteSubtree (*chIt);
|
||||||
}
|
}
|
||||||
childs_.clear();
|
childs_.clear();
|
||||||
}
|
}
|
||||||
@ -75,8 +79,9 @@ SymbolSet
|
|||||||
CTNode::childSymbols (void) const
|
CTNode::childSymbols (void) const
|
||||||
{
|
{
|
||||||
SymbolSet symbols;
|
SymbolSet symbols;
|
||||||
for (unsigned i = 0; i < childs_.size(); i++) {
|
for (CTNodes::const_iterator chIt = childs_.begin();
|
||||||
symbols.insert (childs_[i]->symbol());
|
chIt != childs_.end(); ++ chIt) {
|
||||||
|
symbols.insert ((*chIt)->symbol());
|
||||||
}
|
}
|
||||||
return symbols;
|
return symbols;
|
||||||
}
|
}
|
||||||
@ -88,8 +93,9 @@ CTNode::updateChildLevels (CTNode* n, unsigned level)
|
|||||||
{
|
{
|
||||||
n->setLevel (level);
|
n->setLevel (level);
|
||||||
const CTNodes& childs = n->childs();
|
const CTNodes& childs = n->childs();
|
||||||
for (unsigned i = 0; i < childs.size(); i++) {
|
for (CTNodes::const_iterator chIt = childs.begin();
|
||||||
updateChildLevels (childs[i], level + 1);
|
chIt != childs.end(); ++ chIt) {
|
||||||
|
updateChildLevels (*chIt, level + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,8 +106,9 @@ CTNode::copySubtree (const CTNode* n)
|
|||||||
{
|
{
|
||||||
CTNode* newNode = new CTNode (*n);
|
CTNode* newNode = new CTNode (*n);
|
||||||
const CTNodes& childs = n->childs();
|
const CTNodes& childs = n->childs();
|
||||||
for (unsigned i = 0; i < childs.size(); i++) {
|
for (CTNodes::const_iterator chIt = childs.begin();
|
||||||
newNode->addChild (copySubtree (childs[i]));
|
chIt != childs.end(); ++ chIt) {
|
||||||
|
newNode->addChild (copySubtree (*chIt));
|
||||||
}
|
}
|
||||||
return newNode;
|
return newNode;
|
||||||
}
|
}
|
||||||
@ -113,8 +120,9 @@ CTNode::deleteSubtree (CTNode* n)
|
|||||||
{
|
{
|
||||||
assert (n);
|
assert (n);
|
||||||
const CTNodes& childs = n->childs();
|
const CTNodes& childs = n->childs();
|
||||||
for (unsigned i = 0; i < childs.size(); i++) {
|
for (CTNodes::const_iterator chIt = childs.begin();
|
||||||
deleteSubtree (childs[i]);
|
chIt != childs.end(); ++ chIt) {
|
||||||
|
deleteSubtree (*chIt);
|
||||||
}
|
}
|
||||||
delete n;
|
delete n;
|
||||||
}
|
}
|
||||||
@ -185,21 +193,18 @@ ConstraintTree::addTuple (const Tuple& tuple)
|
|||||||
CTNode* prevNode = root_;
|
CTNode* prevNode = root_;
|
||||||
CTNodes currNodes = root_->childs();
|
CTNodes currNodes = root_->childs();
|
||||||
for (unsigned i = 0; i < tuple.size(); i++) {
|
for (unsigned i = 0; i < tuple.size(); i++) {
|
||||||
int idx = -1;
|
CTNodes::const_iterator it = currNodes.begin();
|
||||||
for (unsigned j = 0; j < currNodes.size(); j++) {
|
while (it != currNodes.end() && (*it)->symbol() != tuple[i]) {
|
||||||
if (currNodes[j]->symbol() == tuple[i]) {
|
++ it;
|
||||||
idx = j;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (idx == -1) {
|
if (it == currNodes.end()) {
|
||||||
CTNode* newNode = new CTNode (tuple[i], i + 1);
|
CTNode* newNode = new CTNode (tuple[i], i + 1);
|
||||||
prevNode->addChild (newNode);
|
prevNode->addChild (newNode);
|
||||||
prevNode = newNode;
|
prevNode = newNode;
|
||||||
currNodes.clear();
|
currNodes.clear();
|
||||||
} else {
|
} else {
|
||||||
prevNode = currNodes[idx];
|
prevNode = *it;
|
||||||
currNodes = currNodes[idx]->childs();
|
currNodes = (*it)->childs();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -218,9 +223,9 @@ ConstraintTree::containsTuple (const Tuple& tuple)
|
|||||||
if (n->level() == tuple.size()) {
|
if (n->level() == tuple.size()) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
CTNodes childs = n->childs();
|
for (CTNodes::const_iterator chIt = n->childs().begin();
|
||||||
for (unsigned i = 0; i < childs.size(); i++) {
|
chIt != n->childs().end(); ++ chIt) {
|
||||||
queue.push (childs[i]);
|
queue.push (*chIt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -277,11 +282,13 @@ ConstraintTree::join (ConstraintTree* ct, bool assertWhenNotFound)
|
|||||||
|
|
||||||
LogVarSet intersect = logVarSet_ & ct->logVarSet_;
|
LogVarSet intersect = logVarSet_ & ct->logVarSet_;
|
||||||
if (intersect.empty()) {
|
if (intersect.empty()) {
|
||||||
const CTNodes& childs = ct->root()->childs();
|
|
||||||
CTNodes leafs = getNodesAtLevel (getLevel (logVars_.back()));
|
CTNodes leafs = getNodesAtLevel (getLevel (logVars_.back()));
|
||||||
for (unsigned i = 0; i < leafs.size(); i++) {
|
const CTNodes& childs = ct->root()->childs();
|
||||||
for (unsigned j = 0; j < childs.size(); j++) {
|
for (CTNodes::const_iterator leafIt = leafs.begin();
|
||||||
leafs[i]->addChild (CTNode::copySubtree (childs[j]));
|
leafIt != leafs.end(); ++ leafIt) {
|
||||||
|
for (CTNodes::const_iterator chIt = childs.begin();
|
||||||
|
chIt != childs.end(); ++ chIt) {
|
||||||
|
(*leafIt)->addChild (CTNode::copySubtree (*chIt));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Util::addToVector (logVars_, ct->logVars_);
|
Util::addToVector (logVars_, ct->logVars_);
|
||||||
@ -302,10 +309,13 @@ ConstraintTree::join (ConstraintTree* ct, bool assertWhenNotFound)
|
|||||||
tuples,
|
tuples,
|
||||||
continNodes);
|
continNodes);
|
||||||
|
|
||||||
for (unsigned i = 0; i < tuples.size(); i++) {
|
CTNodes::const_iterator continIt;
|
||||||
|
continIt = continNodes.begin();
|
||||||
|
for (unsigned i = 0; i < tuples.size(); i++, ++ continIt) {
|
||||||
bool tupleFounded = false;
|
bool tupleFounded = false;
|
||||||
for (unsigned j = 0; j < nodes.size(); j++) {
|
for (CTNodes::const_iterator nodeIt = nodes.begin();
|
||||||
tupleFounded |= join (nodes[j], tuples[i], 0, continNodes[i]);
|
nodeIt != nodes.end(); ++ nodeIt) {
|
||||||
|
tupleFounded |= join (*nodeIt, tuples[i], 0, *continIt);
|
||||||
}
|
}
|
||||||
if (assertWhenNotFound) {
|
if (assertWhenNotFound) {
|
||||||
assert (tupleFounded);
|
assert (tupleFounded);
|
||||||
@ -386,8 +396,9 @@ ConstraintTree::remove (const LogVarSet& X)
|
|||||||
moveToBottom (X.elements());
|
moveToBottom (X.elements());
|
||||||
unsigned level = getLevel (X.front()) - 1;
|
unsigned level = getLevel (X.front()) - 1;
|
||||||
CTNodes nodes = getNodesAtLevel (level);
|
CTNodes nodes = getNodesAtLevel (level);
|
||||||
for (unsigned i = 0; i < nodes.size(); i++) {
|
for (CTNodes::const_iterator it = nodes.begin();
|
||||||
nodes[i]->removeAndDeleteAllChilds();
|
it != nodes.end(); ++ it) {
|
||||||
|
(*it)->removeAndDeleteAllChilds();
|
||||||
}
|
}
|
||||||
logVars_.resize (logVars_.size() - X.size());
|
logVars_.resize (logVars_.size() - X.size());
|
||||||
logVarSet_ -= X;
|
logVarSet_ -= X;
|
||||||
@ -400,8 +411,9 @@ ConstraintTree::ConstraintTree::isSingleton (LogVar X)
|
|||||||
{
|
{
|
||||||
SymbolSet symbols;
|
SymbolSet symbols;
|
||||||
CTNodes nodes = getNodesAtLevel (getLevel (X));
|
CTNodes nodes = getNodesAtLevel (getLevel (X));
|
||||||
for (unsigned i = 0; i < nodes.size(); i++) {
|
for (CTNodes::const_iterator it = nodes.begin();
|
||||||
symbols.insert (nodes[i]->symbol());
|
it != nodes.end(); ++ it) {
|
||||||
|
symbols.insert ((*it)->symbol());
|
||||||
}
|
}
|
||||||
return symbols.size() == 1;
|
return symbols.size() == 1;
|
||||||
}
|
}
|
||||||
@ -464,17 +476,20 @@ ConstraintTree::exportToGraphViz (
|
|||||||
// copy.moveToTop (copy.logVarSet_.elements());
|
// copy.moveToTop (copy.logVarSet_.elements());
|
||||||
CTNodes nodes = getNodesBelow (copy.root_);
|
CTNodes nodes = getNodesBelow (copy.root_);
|
||||||
out << "\"" << copy.root_ << "\"" << " [label=\"R\"]" << endl;
|
out << "\"" << copy.root_ << "\"" << " [label=\"R\"]" << endl;
|
||||||
for (unsigned i = 1; i < nodes.size(); i++) {
|
for (CTNodes::const_iterator it = nodes.begin();
|
||||||
out << "\"" << nodes[i] << "\"";
|
it != nodes.end(); ++ it) {
|
||||||
out << " [label=\"" << *nodes[i] << "\"]" ;
|
out << "\"" << *it << "\"";
|
||||||
|
out << " [label=\"" << **it << "\"]" ;
|
||||||
out << endl;
|
out << endl;
|
||||||
}
|
}
|
||||||
for (unsigned i = 0; i < nodes.size(); i++) {
|
for (CTNodes::const_iterator it = nodes.begin();
|
||||||
const CTNodes& childs = nodes[i]->childs();
|
it != nodes.end(); ++ it) {
|
||||||
for (unsigned j = 0; j < childs.size(); j++) {
|
const CTNodes& childs = (*it)->childs();
|
||||||
out << "\"" << nodes[i] << "\"" ;
|
for (CTNodes::const_iterator chIt = childs.begin();
|
||||||
|
chIt != childs.end(); ++ chIt) {
|
||||||
|
out << "\"" << **it << "\"" ;
|
||||||
out << " -> " ;
|
out << " -> " ;
|
||||||
out << "\"" << childs[j] << "\"" << endl ;
|
out << "\"" << *chIt << "\"" << endl ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (showLogVars) {
|
if (showLogVars) {
|
||||||
@ -511,9 +526,10 @@ ConstraintTree::isCountNormalized (const LogVarSet& Ys)
|
|||||||
LogVarSet Zs = logVarSet_ - LogVarSet (Ys);
|
LogVarSet Zs = logVarSet_ - LogVarSet (Ys);
|
||||||
moveToTop (Zs.elements());
|
moveToTop (Zs.elements());
|
||||||
CTNodes nodes = getNodesAtLevel (Zs.size());
|
CTNodes nodes = getNodesAtLevel (Zs.size());
|
||||||
unsigned count = countTuples (nodes[0]);
|
unsigned count = countTuples (*nodes.begin());
|
||||||
for (unsigned i = 1; i < nodes.size(); i++) {
|
for (CTNodes::const_iterator it = nodes.begin();
|
||||||
if (countTuples (nodes[i]) != count) {
|
it != nodes.end(); ++ it) {
|
||||||
|
if (countTuples (*it) != count) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -538,7 +554,7 @@ ConstraintTree::getConditionalCount (const LogVarSet& Ys)
|
|||||||
CTNode* n = root_;
|
CTNode* n = root_;
|
||||||
unsigned l = 0;
|
unsigned l = 0;
|
||||||
while (l != Zs.size()) {
|
while (l != Zs.size()) {
|
||||||
n = n->childs()[0];
|
n = *(n->childs().begin());
|
||||||
l ++;
|
l ++;
|
||||||
}
|
}
|
||||||
return countTuples (n);
|
return countTuples (n);
|
||||||
@ -560,8 +576,9 @@ ConstraintTree::getConditionalCounts (const LogVarSet& Ys)
|
|||||||
LogVarSet Zs = logVarSet_ - LogVarSet (Ys);
|
LogVarSet Zs = logVarSet_ - LogVarSet (Ys);
|
||||||
moveToTop (Zs.elements());
|
moveToTop (Zs.elements());
|
||||||
CTNodes nodes = getNodesAtLevel (Zs.size());
|
CTNodes nodes = getNodesAtLevel (Zs.size());
|
||||||
for (unsigned i = 0; i < nodes.size(); i++) {
|
for (CTNodes::const_iterator it = nodes.begin();
|
||||||
counts.insert (countTuples (nodes[i]));
|
it != nodes.end(); ++ it) {
|
||||||
|
counts.insert (countTuples (*it));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return counts;
|
return counts;
|
||||||
@ -577,7 +594,8 @@ ConstraintTree::isCarteesianProduct (const LogVarSet& Xs) const
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
for (unsigned i = 1; i < Xs.size(); i++) {
|
for (unsigned i = 1; i < Xs.size(); i++) {
|
||||||
CTNodes nodes = getNodesAtLevel (i);
|
CTNodes nodesit = getNodesAtLevel (i);
|
||||||
|
vector<CTNode*> nodes (nodesit.begin(), nodesit.end()); // TODO
|
||||||
for (unsigned j = 1; j < nodes.size(); j++) {
|
for (unsigned j = 1; j < nodes.size(); j++) {
|
||||||
if (nodes[j-1]->nrChilds() != nodes[ j ]->nrChilds()) {
|
if (nodes[j-1]->nrChilds() != nodes[ j ]->nrChilds()) {
|
||||||
return false;
|
return false;
|
||||||
@ -617,8 +635,9 @@ ConstraintTree::split (
|
|||||||
split (exclCt->root(), ct->root(), commNodes, stopLevel);
|
split (exclCt->root(), ct->root(), commNodes, stopLevel);
|
||||||
|
|
||||||
ConstraintTree* commCt = new ConstraintTree (logVars_);
|
ConstraintTree* commCt = new ConstraintTree (logVars_);
|
||||||
for (unsigned i = 0; i < commNodes.size(); i++) {
|
for (CTNodes::const_iterator it = commNodes.begin();
|
||||||
commCt->root()->addChild (commNodes[i]);
|
it != commNodes.end(); ++ it) {
|
||||||
|
commCt->root()->addChild (*it);
|
||||||
}
|
}
|
||||||
// cout << commCt->tupleSet() << " + " ;
|
// cout << commCt->tupleSet() << " + " ;
|
||||||
// cout << exclCt->tupleSet() << " = " ;
|
// cout << exclCt->tupleSet() << " = " ;
|
||||||
@ -649,9 +668,10 @@ ConstraintTree::countNormalize (const LogVarSet& Ys)
|
|||||||
unsigned stopLevel = getLevel (Zs.back());
|
unsigned stopLevel = getLevel (Zs.back());
|
||||||
const CTNodes& childs = root_->childs();
|
const CTNodes& childs = root_->childs();
|
||||||
|
|
||||||
for (unsigned i = 0; i < childs.size(); i++) {
|
for (CTNodes::const_iterator chIt = childs.begin();
|
||||||
|
chIt != childs.end(); ++ chIt) {
|
||||||
const vector<pair<CTNode*, unsigned>>& res
|
const vector<pair<CTNode*, unsigned>>& res
|
||||||
= countNormalize (childs[i], stopLevel);
|
= countNormalize (*chIt, stopLevel);
|
||||||
for (unsigned j = 0; j < res.size(); j++) {
|
for (unsigned j = 0; j < res.size(); j++) {
|
||||||
unordered_map<unsigned, ConstraintTree*>::iterator it
|
unordered_map<unsigned, ConstraintTree*>::iterator it
|
||||||
= countMap.find (res[j].second);
|
= countMap.find (res[j].second);
|
||||||
@ -731,8 +751,9 @@ ConstraintTree::jointCountNormalize (
|
|||||||
// cout << "joint-count(" << counts1[i] ;
|
// cout << "joint-count(" << counts1[i] ;
|
||||||
// cout << "," << counts2[j] << ")" << endl;
|
// cout << "," << counts2[j] << ")" << endl;
|
||||||
const CTNodes& childs = normCts2[j]->root_->childs();
|
const CTNodes& childs = normCts2[j]->root_->childs();
|
||||||
for (unsigned k = 0; k < childs.size(); k++) {
|
for (CTNodes::const_iterator chIt = childs.begin();
|
||||||
normCts1[i]->root_->addChild (CTNode::copySubtree (childs[k]));
|
chIt != childs.end(); ++ chIt) {
|
||||||
|
normCts1[i]->root_->addChild (CTNode::copySubtree (*chIt));
|
||||||
}
|
}
|
||||||
delete normCts2[j];
|
delete normCts2[j];
|
||||||
}
|
}
|
||||||
@ -779,9 +800,11 @@ ConstraintTree::overlap (
|
|||||||
{
|
{
|
||||||
const CTNodes& childs1 = ct1->root_->childs();
|
const CTNodes& childs1 = ct1->root_->childs();
|
||||||
const CTNodes& childs2 = ct2->root_->childs();
|
const CTNodes& childs2 = ct2->root_->childs();
|
||||||
for (unsigned i = 0; i < childs1.size(); i++) {
|
for (CTNodes::const_iterator chIt1 = childs1.begin();
|
||||||
for (unsigned j = 0; j < childs2.size(); j++) {
|
chIt1 != childs1.end(); ++ chIt1) {
|
||||||
if (overlap (childs1[i], childs2[j], stopLevel)) {
|
for (CTNodes::const_iterator chIt2 = childs2.begin();
|
||||||
|
chIt2 != childs2.end(); ++ chIt2) {
|
||||||
|
if (overlap (*chIt1, *chIt2, stopLevel)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -798,17 +821,19 @@ ConstraintTree::expand (LogVar X)
|
|||||||
assert (isCountNormalized (X));
|
assert (isCountNormalized (X));
|
||||||
CTNodes nodes = getNodesAtLevel (logVars_.size() - 1);
|
CTNodes nodes = getNodesAtLevel (logVars_.size() - 1);
|
||||||
unsigned nrSymbols = getConditionalCount (X);
|
unsigned nrSymbols = getConditionalCount (X);
|
||||||
for (unsigned i = 0; i < nodes.size(); i++) {
|
for (CTNodes::const_iterator it = nodes.begin();
|
||||||
|
it != nodes.end(); ++ it) {
|
||||||
Symbols symbols;
|
Symbols symbols;
|
||||||
const CTNodes& childs = nodes[i]->childs();
|
const CTNodes& childs = (*it)->childs();
|
||||||
for (unsigned j = 0; j < childs.size(); j++) {
|
for (CTNodes::const_iterator chIt = childs.begin();
|
||||||
symbols.push_back (childs[j]->symbol());
|
chIt != childs.end(); ++ chIt) {
|
||||||
|
symbols.push_back ((*chIt)->symbol());
|
||||||
}
|
}
|
||||||
nodes[i]->removeAndDeleteAllChilds();
|
(*it)->removeAndDeleteAllChilds();
|
||||||
CTNode* prev = nodes[i];
|
CTNode* prev = *it;
|
||||||
assert (symbols.size() == nrSymbols);
|
assert (symbols.size() == nrSymbols);
|
||||||
for (unsigned j = 0; j < nrSymbols; j++) {
|
for (unsigned j = 0; j < nrSymbols; j++) {
|
||||||
CTNode* newNode = new CTNode (symbols[j], nodes[i]->level() + j);
|
CTNode* newNode = new CTNode (symbols[j], (*it)->level() + j);
|
||||||
prev->addChild (newNode);
|
prev->addChild (newNode);
|
||||||
prev = newNode;
|
prev = newNode;
|
||||||
}
|
}
|
||||||
@ -832,9 +857,10 @@ ConstraintTree::ground (LogVar X)
|
|||||||
moveToTop ({X});
|
moveToTop ({X});
|
||||||
ConstraintTrees cts;
|
ConstraintTrees cts;
|
||||||
const CTNodes& nodes = root_->childs();
|
const CTNodes& nodes = root_->childs();
|
||||||
for (unsigned i = 0; i < nodes.size(); i++) {
|
for (CTNodes::const_iterator it = nodes.begin();
|
||||||
CTNode* copy = CTNode::copySubtree (nodes[i]);
|
it != nodes.end(); ++ it) {
|
||||||
copy->setSymbol (nodes[i]->symbol());
|
CTNode* copy = CTNode::copySubtree (*it);
|
||||||
|
copy->setSymbol ((*it)->symbol());
|
||||||
ConstraintTree* newCt = new ConstraintTree (logVars_);
|
ConstraintTree* newCt = new ConstraintTree (logVars_);
|
||||||
newCt->root()->addChild (copy);
|
newCt->root()->addChild (copy);
|
||||||
cts.push_back (newCt);
|
cts.push_back (newCt);
|
||||||
@ -852,8 +878,9 @@ ConstraintTree::countTuples (const CTNode* n) const
|
|||||||
}
|
}
|
||||||
unsigned sum = 0;
|
unsigned sum = 0;
|
||||||
const CTNodes& childs = n->childs();
|
const CTNodes& childs = n->childs();
|
||||||
for (unsigned i = 0; i < childs.size(); i++) {
|
for (CTNodes::const_iterator chIt = childs.begin();
|
||||||
sum += countTuples (childs[i]);
|
chIt != childs.end(); ++ chIt) {
|
||||||
|
sum += countTuples (*chIt);
|
||||||
}
|
}
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
@ -869,8 +896,9 @@ ConstraintTree::getNodesBelow (CTNode* fromHere) const
|
|||||||
while (queue.empty() == false) {
|
while (queue.empty() == false) {
|
||||||
CTNode* node = queue.front();
|
CTNode* node = queue.front();
|
||||||
nodes.push_back (node);
|
nodes.push_back (node);
|
||||||
for (unsigned i = 0; i < node->childs().size(); i++) {
|
for (CTNodes::const_iterator chIt = node->childs().begin();
|
||||||
queue.push (node->childs()[i]);
|
chIt != node->childs().end(); ++ chIt) {
|
||||||
|
queue.push (*chIt);
|
||||||
}
|
}
|
||||||
queue.pop();
|
queue.pop();
|
||||||
}
|
}
|
||||||
@ -891,8 +919,9 @@ ConstraintTree::getNodesAtLevel (unsigned level) const
|
|||||||
if (node->level() == level) {
|
if (node->level() == level) {
|
||||||
nodes.push_back (node);
|
nodes.push_back (node);
|
||||||
} else {
|
} else {
|
||||||
for (unsigned i = 0; i < node->childs().size(); i++) {
|
for (CTNodes::const_iterator chIt = node->childs().begin();
|
||||||
queue.push (node->childs()[i]);
|
chIt != node->childs().end(); ++ chIt) {
|
||||||
|
queue.push (*chIt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
queue.pop();
|
queue.pop();
|
||||||
@ -905,30 +934,33 @@ ConstraintTree::getNodesAtLevel (unsigned level) const
|
|||||||
void
|
void
|
||||||
ConstraintTree::swapLogVar (LogVar X)
|
ConstraintTree::swapLogVar (LogVar X)
|
||||||
{
|
{
|
||||||
TupleSet before = tupleSet();
|
LogVars::iterator it;
|
||||||
LogVars::iterator it =
|
it = std::find (logVars_.begin(),logVars_.end(), X);
|
||||||
std::find (logVars_.begin(),logVars_.end(), X);
|
|
||||||
assert (it != logVars_.end());
|
assert (it != logVars_.end());
|
||||||
unsigned pos = std::distance (logVars_.begin(), it);
|
unsigned pos = std::distance (logVars_.begin(), it);
|
||||||
const CTNodes& nodes = getNodesAtLevel (pos);
|
const CTNodes& nodes = getNodesAtLevel (pos);
|
||||||
for (unsigned i = 0; i < nodes.size(); i++) {
|
for (CTNodes::const_iterator nodeIt = nodes.begin();
|
||||||
CTNodes childsCopy = nodes[i]->childs();
|
nodeIt != nodes.end(); ++ nodeIt) {
|
||||||
nodes[i]->removeChilds();
|
CTNodes childsCopy = (*nodeIt)->childs();
|
||||||
for (unsigned j = 0; j < childsCopy.size(); j++) {
|
(*nodeIt)->removeChilds();
|
||||||
const CTNodes grandsons = childsCopy[j]->childs();
|
for (CTNodes::const_iterator ccIt = childsCopy.begin();
|
||||||
for (unsigned k = 0; k < grandsons.size(); k++) {
|
ccIt != childsCopy.end(); ++ ccIt) {
|
||||||
CTNode* childCopy = new CTNode (*childsCopy[j]);
|
const CTNodes& grandsons = (*ccIt)->childs();
|
||||||
const CTNodes greatGrandsons = grandsons[k]->childs();
|
for (CTNodes::const_iterator gsIt = grandsons.begin();
|
||||||
for (unsigned t = 0; t < greatGrandsons.size(); t++) {
|
gsIt != grandsons.end(); ++ gsIt) {
|
||||||
grandsons[k]->removeChild (greatGrandsons[t]);
|
CTNode* childCopy = new CTNode (**ccIt);
|
||||||
childCopy->addChild (greatGrandsons[t], false);
|
const CTNodes& greatGrandsons = (*gsIt)->childs();
|
||||||
|
for (CTNodes::const_iterator ggsIt = greatGrandsons.begin();
|
||||||
|
ggsIt != greatGrandsons.end(); ++ ggsIt) {
|
||||||
|
childCopy->addChild (*ggsIt, false);
|
||||||
}
|
}
|
||||||
childCopy->setLevel (childCopy->level() + 1);
|
childCopy->setLevel (childCopy->level() + 1);
|
||||||
grandsons[k]->addChild (childCopy, false);
|
(*gsIt)->removeChilds();
|
||||||
grandsons[k]->setLevel (grandsons[k]->level() - 1);
|
(*gsIt)->addChild (childCopy, false);
|
||||||
nodes[i]->addChild (grandsons[k], false);
|
(*gsIt)->setLevel ((*gsIt)->level() - 1);
|
||||||
|
(*nodeIt)->addChild ((*gsIt), false);
|
||||||
}
|
}
|
||||||
delete childsCopy[j];
|
delete (*ccIt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::swap (logVars_[pos], logVars_[pos + 1]);
|
std::swap (logVars_[pos], logVars_[pos + 1]);
|
||||||
@ -947,14 +979,16 @@ ConstraintTree::join (
|
|||||||
if (n->symbol() == tuple[currIdx]) {
|
if (n->symbol() == tuple[currIdx]) {
|
||||||
if (currIdx == tuple.size() - 1) {
|
if (currIdx == tuple.size() - 1) {
|
||||||
const CTNodes& childs = appendNode->childs();
|
const CTNodes& childs = appendNode->childs();
|
||||||
for (unsigned i = 0; i < childs.size(); i++) {
|
for (CTNodes::const_iterator chIt = childs.begin();
|
||||||
n->addChild (CTNode::copySubtree (childs[i]));
|
chIt != childs.end(); ++ chIt) {
|
||||||
|
n->addChild (CTNode::copySubtree (*chIt));
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
const CTNodes& childs = n->childs();
|
const CTNodes& childs = n->childs();
|
||||||
for (unsigned i = 0; i < childs.size(); i++) {
|
for (CTNodes::const_iterator chIt = childs.begin();
|
||||||
tupleFounded |= join (childs[i], tuple, currIdx + 1, appendNode);
|
chIt != childs.end(); ++ chIt) {
|
||||||
|
tupleFounded |= join (*chIt, tuple, currIdx + 1, appendNode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return tupleFounded;
|
return tupleFounded;
|
||||||
@ -978,10 +1012,14 @@ ConstraintTree::indenticalSubtrees (
|
|||||||
if (childs1.size() != childs2.size()) {
|
if (childs1.size() != childs2.size()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for (unsigned i = 0; i < childs1.size(); i++) {
|
CTNodes::const_iterator chIt1 = childs1.begin();
|
||||||
if (indenticalSubtrees (childs1[i], childs2[i], true) == false) {
|
CTNodes::const_iterator chIt2 = childs2.begin();
|
||||||
|
while (chIt1 != childs1.end()) {
|
||||||
|
if (indenticalSubtrees (*chIt1, *chIt2, true) == false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
++ chIt1;
|
||||||
|
++ chIt2;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -1014,12 +1052,10 @@ ConstraintTree::getTuples (
|
|||||||
}
|
}
|
||||||
|
|
||||||
const CTNodes& childs = n->childs();
|
const CTNodes& childs = n->childs();
|
||||||
for (unsigned i = 0; i < childs.size(); i++) {
|
for (CTNodes::const_iterator chIt = childs.begin();
|
||||||
getTuples (childs[i],
|
chIt != childs.end(); ++ chIt) {
|
||||||
currTuples,
|
getTuples (*chIt, currTuples, stopLevel, tuplesCollected,
|
||||||
stopLevel,
|
continuationNodes);
|
||||||
tuplesCollected,
|
|
||||||
continuationNodes);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1055,9 +1091,10 @@ ConstraintTree::countNormalize (
|
|||||||
|
|
||||||
vector<pair<CTNode*, unsigned>> res;
|
vector<pair<CTNode*, unsigned>> res;
|
||||||
const CTNodes& childs = n->childs();
|
const CTNodes& childs = n->childs();
|
||||||
for (unsigned i = 0; i < childs.size(); i++) {
|
for (CTNodes::const_iterator chIt = childs.begin();
|
||||||
|
chIt != childs.end(); ++ chIt) {
|
||||||
const vector<pair<CTNode*, unsigned>>& lowerRes =
|
const vector<pair<CTNode*, unsigned>>& lowerRes =
|
||||||
countNormalize (childs[i], stopLevel);
|
countNormalize (*chIt, stopLevel);
|
||||||
for (unsigned j = 0; j < lowerRes.size(); j++) {
|
for (unsigned j = 0; j < lowerRes.size(); j++) {
|
||||||
CTNode* newNode = new CTNode (*n);
|
CTNode* newNode = new CTNode (*n);
|
||||||
newNode->addChild (lowerRes[j].first);
|
newNode->addChild (lowerRes[j].first);
|
||||||
@ -1078,42 +1115,50 @@ ConstraintTree::split (
|
|||||||
{
|
{
|
||||||
CTNodes& childs1 = n1->childs();
|
CTNodes& childs1 = n1->childs();
|
||||||
CTNodes& childs2 = n2->childs();
|
CTNodes& childs2 = n2->childs();
|
||||||
for (unsigned i = 0; i < childs1.size(); i++) {
|
for (CTNodes::const_iterator chIt1 = childs1.begin();
|
||||||
|
chIt1 != childs1.end(); ++ chIt1) {
|
||||||
CTNode* intersectNode = 0;
|
CTNode* intersectNode = 0;
|
||||||
for (unsigned j = 0; j < childs2.size(); j++) {
|
for (CTNodes::const_iterator chIt2 = childs2.begin();
|
||||||
if (childs1[i]->symbol() == childs2[j]->symbol()) {
|
chIt2 != childs2.end(); ++ chIt2) {
|
||||||
intersectNode = childs2[j];
|
if ((*chIt1)->symbol() == (*chIt2)->symbol()) {
|
||||||
|
intersectNode = *chIt2;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (intersectNode == 0) {
|
if (intersectNode == 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (childs1[i]->level() == stopLevel) {
|
if ((*chIt1)->level() == stopLevel) {
|
||||||
CTNode* newNode = CTNode::copySubtree (childs1[i]);
|
CTNode* newNode = CTNode::copySubtree (*chIt1);
|
||||||
nodes.push_back (newNode);
|
nodes.push_back (newNode);
|
||||||
childs1[i]->setSymbol (Symbol::invalid());
|
(*chIt1)->setSymbol (Symbol::invalid());
|
||||||
} else {
|
} else {
|
||||||
CTNodes lowerNodes;
|
CTNodes lowerNodes;
|
||||||
split (childs1[i], intersectNode, lowerNodes, stopLevel);
|
split ((*chIt1), intersectNode, lowerNodes, stopLevel);
|
||||||
if (lowerNodes.empty() == false) {
|
if (lowerNodes.empty() == false) {
|
||||||
CTNode* newNode = new CTNode (*childs1[i]);
|
CTNode* newNode = new CTNode (**chIt1);
|
||||||
for (unsigned j = 0; j < lowerNodes.size(); j++) {
|
for (CTNodes::const_iterator lowerIt = lowerNodes.begin();
|
||||||
newNode->addChild (lowerNodes[j]);
|
lowerIt != lowerNodes.end(); ++ lowerIt) {
|
||||||
|
newNode->addChild (*lowerIt);
|
||||||
}
|
}
|
||||||
nodes.push_back (newNode);
|
nodes.push_back (newNode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < (int)childs1.size(); i++) {
|
CTNodes::const_iterator chIt = childs1.begin();
|
||||||
if (childs1[i]->symbol() == Symbol::invalid()) {
|
while (chIt != childs1.end()) {
|
||||||
n1->removeAndDeleteChild (childs1[i]);
|
bool inc = true;
|
||||||
i --;
|
if ((*chIt)->symbol() == Symbol::invalid()) {
|
||||||
} else if (childs1[i]->isLeaf() &&
|
n1->removeAndDeleteChild ((*chIt));
|
||||||
childs1[i]->level() != stopLevel) {
|
inc = false;
|
||||||
n1->removeAndDeleteChild (childs1[i]);
|
} else if ((*chIt)->isLeaf() &&
|
||||||
i --;
|
(*chIt)->level() != stopLevel) {
|
||||||
|
n1->removeAndDeleteChild ((*chIt));
|
||||||
|
inc = false;
|
||||||
|
}
|
||||||
|
if (inc) {
|
||||||
|
++ chIt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1135,10 +1180,12 @@ ConstraintTree::overlap (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const CTNodes& childsI = n1->childs();
|
const CTNodes& childsI = n1->childs();
|
||||||
const CTNodes& childsJ = n2->childs();
|
const CTNodes& childsJ = n2->childs();
|
||||||
for (unsigned i = 0; i < childsI.size(); i++) {
|
for (CTNodes::const_iterator chIt1 = childsI.begin();
|
||||||
for (unsigned j = 0; j < childsJ.size(); j++) {
|
chIt1 != childsI.end(); ++ chIt1) {
|
||||||
if (overlap (childsI[i], childsJ[j], stopLevel)) {
|
for (CTNodes::const_iterator chIt2 = childsJ.begin();
|
||||||
|
chIt2 != childsJ.end(); ++ chIt2) {
|
||||||
|
if (overlap (*chIt1, *chIt2, stopLevel)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user