2012-05-23 14:56:01 +01:00
|
|
|
#include <queue>
|
|
|
|
|
2013-02-07 20:09:10 +00:00
|
|
|
#include <iostream>
|
2013-03-09 19:41:17 +00:00
|
|
|
#include <ostream>
|
2012-05-23 14:56:01 +01:00
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
#include "ConstraintTree.h"
|
|
|
|
#include "Util.h"
|
|
|
|
|
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
namespace Horus {
|
2013-02-07 23:53:13 +00:00
|
|
|
|
2013-03-09 19:41:17 +00:00
|
|
|
class CTNode {
|
|
|
|
public:
|
|
|
|
CTNode (const CTNode& n, const CTChilds& chs = CTChilds())
|
|
|
|
: symbol_(n.symbol()), childs_(chs), level_(n.level()) { }
|
|
|
|
|
|
|
|
CTNode (Symbol s, unsigned l, const CTChilds& chs = CTChilds())
|
|
|
|
: symbol_(s), childs_(chs), level_(l) { }
|
|
|
|
|
|
|
|
unsigned level() const { return level_; }
|
|
|
|
|
|
|
|
void setLevel (unsigned level) { level_ = level; }
|
|
|
|
|
|
|
|
Symbol symbol() const { return symbol_; }
|
|
|
|
|
|
|
|
void setSymbol (Symbol s) { symbol_ = s; }
|
|
|
|
|
|
|
|
CTChilds& childs() { return childs_; }
|
|
|
|
|
|
|
|
const CTChilds& childs() const { return childs_; }
|
|
|
|
|
|
|
|
size_t nrChilds() const { return childs_.size(); }
|
|
|
|
|
|
|
|
bool isRoot() const { return level_ == 0; }
|
|
|
|
|
|
|
|
bool isLeaf() const { return childs_.empty(); }
|
|
|
|
|
|
|
|
CTChilds::iterator findSymbol (Symbol symb);
|
|
|
|
|
|
|
|
void mergeSubtree (CTNode*, bool = true);
|
|
|
|
|
|
|
|
void removeChild (CTNode*);
|
|
|
|
|
|
|
|
void removeChilds();
|
|
|
|
|
|
|
|
void removeAndDeleteChild (CTNode*);
|
|
|
|
|
|
|
|
void removeAndDeleteAllChilds();
|
|
|
|
|
|
|
|
SymbolSet childSymbols() const;
|
|
|
|
|
|
|
|
static CTNode* copySubtree (const CTNode*);
|
|
|
|
|
|
|
|
static void deleteSubtree (CTNode*);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void updateChildLevels (CTNode*, unsigned);
|
|
|
|
|
|
|
|
Symbol symbol_;
|
|
|
|
CTChilds childs_;
|
|
|
|
unsigned level_;
|
|
|
|
|
|
|
|
DISALLOW_ASSIGN (CTNode);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline CTChilds::iterator
|
|
|
|
CTNode::findSymbol (Symbol symb)
|
|
|
|
{
|
|
|
|
CTNode tmp (symb, 0);
|
|
|
|
return childs_.find (&tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
CmpSymbol::operator() (const CTNode* n1, const CTNode* n2) const
|
|
|
|
{
|
|
|
|
return n1->symbol() < n2->symbol();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
void
|
|
|
|
CTNode::mergeSubtree (CTNode* n, bool updateLevels)
|
|
|
|
{
|
|
|
|
if (updateLevels) {
|
|
|
|
updateChildLevels (n, level_ + 1);
|
|
|
|
}
|
|
|
|
CTChilds::iterator chIt = childs_.find (n);
|
|
|
|
if (chIt != childs_.end()) {
|
|
|
|
assert ((*chIt)->symbol() == n->symbol());
|
|
|
|
const CTChilds& childsToAdd = n->childs();
|
|
|
|
for (CTChilds::const_iterator it = childsToAdd.begin();
|
|
|
|
it != childsToAdd.end(); ++ it) {
|
|
|
|
(*chIt)->mergeSubtree (*it, false);
|
|
|
|
}
|
|
|
|
delete n;
|
|
|
|
} else {
|
|
|
|
childs_.insert (n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
CTNode::removeChild (CTNode* child)
|
|
|
|
{
|
|
|
|
assert (childs_.contains (child));
|
|
|
|
childs_.remove (child);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2013-02-28 19:45:37 +00:00
|
|
|
CTNode::removeChilds()
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
childs_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
CTNode::removeAndDeleteChild (CTNode* child)
|
|
|
|
{
|
|
|
|
removeChild (child);
|
|
|
|
CTNode::deleteSubtree (child);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2013-02-28 19:45:37 +00:00
|
|
|
CTNode::removeAndDeleteAllChilds()
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
for (CTChilds::const_iterator chIt = childs_.begin();
|
|
|
|
chIt != childs_.end(); ++ chIt) {
|
|
|
|
deleteSubtree (*chIt);
|
|
|
|
}
|
|
|
|
childs_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SymbolSet
|
2013-02-28 19:45:37 +00:00
|
|
|
CTNode::childSymbols() const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
SymbolSet symbols;
|
|
|
|
for (CTChilds::const_iterator chIt = childs_.begin();
|
|
|
|
chIt != childs_.end(); ++ chIt) {
|
|
|
|
symbols.insert ((*chIt)->symbol());
|
|
|
|
}
|
|
|
|
return symbols;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
CTNode::updateChildLevels (CTNode* n, unsigned level)
|
|
|
|
{
|
|
|
|
CTNodes stack;
|
|
|
|
stack.push_back (n);
|
|
|
|
n->setLevel (level);
|
|
|
|
while (stack.empty() == false) {
|
|
|
|
CTNode* node = stack.back();
|
|
|
|
stack.pop_back();
|
|
|
|
for (CTChilds::const_iterator chIt = node->childs().begin();
|
|
|
|
chIt != node->childs().end(); ++ chIt) {
|
|
|
|
(*chIt)->setLevel (node->level() + 1);
|
|
|
|
}
|
|
|
|
stack.insert (stack.end(), node->childs().begin(),
|
|
|
|
node->childs().end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CTNode*
|
|
|
|
CTNode::copySubtree (const CTNode* root1)
|
|
|
|
{
|
|
|
|
if (root1->childs().empty()) {
|
|
|
|
return new CTNode (*root1);
|
|
|
|
}
|
|
|
|
CTNode* root2 = new CTNode (*root1);
|
2013-02-07 13:37:15 +00:00
|
|
|
typedef std::pair<const CTNode*, CTNode*> StackPair;
|
|
|
|
std::vector<StackPair> stack = { StackPair (root1, root2) };
|
2012-05-23 14:56:01 +01:00
|
|
|
while (stack.empty() == false) {
|
|
|
|
const CTNode* n1 = stack.back().first;
|
|
|
|
CTNode* n2 = stack.back().second;
|
|
|
|
stack.pop_back();
|
2013-02-07 13:37:15 +00:00
|
|
|
// std::cout << "n2 childs: " << n2->childs();
|
|
|
|
// std::cout << "n1 childs: " << n1->childs();
|
2012-05-23 14:56:01 +01:00
|
|
|
n2->childs().reserve (n1->nrChilds());
|
|
|
|
stack.reserve (n1->nrChilds());
|
|
|
|
for (CTChilds::const_iterator chIt = n1->childs().begin();
|
|
|
|
chIt != n1->childs().end(); ++ chIt) {
|
|
|
|
CTNode* chCopy = new CTNode (**chIt);
|
|
|
|
n2->childs().insert_sorted (chCopy);
|
2012-12-27 12:54:58 +00:00
|
|
|
if ((*chIt)->nrChilds() > 0) {
|
2012-05-23 14:56:01 +01:00
|
|
|
stack.push_back (StackPair (*chIt, chCopy));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return root2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
CTNode::deleteSubtree (CTNode* n)
|
|
|
|
{
|
|
|
|
assert (n);
|
|
|
|
const CTChilds& childs = n->childs();
|
|
|
|
for (CTChilds::const_iterator chIt = childs.begin();
|
|
|
|
chIt != childs.end(); ++ chIt) {
|
|
|
|
deleteSubtree (*chIt);
|
|
|
|
}
|
|
|
|
delete n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-07 22:37:45 +00:00
|
|
|
std::ostream&
|
|
|
|
operator<< (std::ostream& out, const CTNode& n)
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
out << "(" << n.level() << ") " ;
|
|
|
|
out << n.symbol();
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ConstraintTree::ConstraintTree (unsigned nrLvs)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < nrLvs; i++) {
|
|
|
|
logVars_.push_back (LogVar (i));
|
|
|
|
}
|
|
|
|
root_ = new CTNode (0, 0);
|
|
|
|
logVarSet_ = LogVarSet (logVars_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ConstraintTree::ConstraintTree (const LogVars& logVars)
|
|
|
|
{
|
|
|
|
root_ = new CTNode (0, 0);
|
|
|
|
logVars_ = logVars;
|
|
|
|
logVarSet_ = LogVarSet (logVars);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ConstraintTree::ConstraintTree (
|
|
|
|
const LogVars& logVars,
|
|
|
|
const Tuples& tuples)
|
|
|
|
{
|
|
|
|
root_ = new CTNode (0, 0);
|
|
|
|
logVars_ = logVars;
|
|
|
|
logVarSet_ = LogVarSet (logVars);
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < tuples.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
addTuple (tuples[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
ConstraintTree::ConstraintTree (
|
|
|
|
std::vector<std::vector<std::string>> names)
|
2012-10-31 23:43:39 +00:00
|
|
|
{
|
|
|
|
assert (names.empty() == false);
|
2012-12-20 23:19:10 +00:00
|
|
|
assert (names.front().empty() == false);
|
2012-10-31 23:43:39 +00:00
|
|
|
unsigned nrLvs = names[0].size();
|
|
|
|
for (size_t i = 0; i < nrLvs; i++) {
|
|
|
|
logVars_.push_back (LogVar (i));
|
|
|
|
}
|
|
|
|
root_ = new CTNode (0, 0);
|
|
|
|
logVarSet_ = LogVarSet (logVars_);
|
|
|
|
for (size_t i = 0; i < names.size(); i++) {
|
|
|
|
Tuple t;
|
|
|
|
for (size_t j = 0; j < names[i].size(); j++) {
|
|
|
|
assert (names[i].size() == nrLvs);
|
2012-12-20 23:19:10 +00:00
|
|
|
t.push_back (LiftedUtils::getSymbol (names[i][j]));
|
2012-10-31 23:43:39 +00:00
|
|
|
}
|
|
|
|
addTuple (t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
ConstraintTree::ConstraintTree (const ConstraintTree& ct)
|
|
|
|
{
|
2012-10-30 00:15:40 +00:00
|
|
|
*this = ct;
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-06 00:24:02 +00:00
|
|
|
ConstraintTree::ConstraintTree (
|
|
|
|
const CTChilds& rootChilds,
|
|
|
|
const LogVars& logVars)
|
2013-03-09 19:41:17 +00:00
|
|
|
: root_(new CTNode (Symbol (0), unsigned (0), rootChilds)),
|
2013-02-06 00:24:02 +00:00
|
|
|
logVars_(logVars),
|
|
|
|
logVarSet_(logVars)
|
|
|
|
{
|
2013-03-09 19:41:17 +00:00
|
|
|
|
2013-02-06 00:24:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-28 19:45:37 +00:00
|
|
|
ConstraintTree::~ConstraintTree()
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
CTNode::deleteSubtree (root_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-09 19:41:17 +00:00
|
|
|
bool
|
|
|
|
ConstraintTree::empty() const
|
|
|
|
{
|
|
|
|
return root_->childs().empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
void
|
|
|
|
ConstraintTree::addTuple (const Tuple& tuple)
|
|
|
|
{
|
|
|
|
CTNode* prevNode = root_;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < tuple.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
CTChilds::const_iterator it = prevNode->findSymbol (tuple[i]);
|
|
|
|
if (it == prevNode->childs().end()) {
|
|
|
|
CTNode* newNode = new CTNode (tuple[i], i + 1);
|
|
|
|
prevNode->mergeSubtree (newNode, false);
|
|
|
|
prevNode = newNode;
|
|
|
|
} else {
|
|
|
|
prevNode = *it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
ConstraintTree::containsTuple (const Tuple& tuple)
|
|
|
|
{
|
|
|
|
CTNode* prevNode = root_;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < tuple.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
CTChilds::const_iterator it = prevNode->findSymbol (tuple[i]);
|
|
|
|
if (it == prevNode->childs().end()) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
prevNode = *it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ConstraintTree::moveToTop (const LogVars& lvs)
|
|
|
|
{
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < lvs.size(); i++) {
|
2013-02-08 21:01:53 +00:00
|
|
|
size_t pos = Util::indexOf (logVars_, lvs[i]);
|
2012-05-24 22:55:20 +01:00
|
|
|
assert (pos != logVars_.size());
|
|
|
|
for (size_t j = pos; j-- > i; ) {
|
|
|
|
swapLogVar (logVars_[j]);
|
2012-12-20 23:19:10 +00:00
|
|
|
}
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ConstraintTree::moveToBottom (const LogVars& lvs)
|
|
|
|
{
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = lvs.size(); i-- > 0; ) {
|
2013-02-08 21:01:53 +00:00
|
|
|
size_t pos = Util::indexOf (logVars_, lvs[i]);
|
2012-05-24 22:55:20 +01:00
|
|
|
assert (pos != logVars_.size());
|
|
|
|
size_t stop = logVars_.size() - (lvs.size() - i - 1);
|
|
|
|
for (size_t j = pos; j < stop - 1; j++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
swapLogVar (logVars_[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ConstraintTree::join (ConstraintTree* ct, bool oneTwoOne)
|
|
|
|
{
|
|
|
|
if (logVarSet_.empty()) {
|
|
|
|
CTNode::deleteSubtree (root_);
|
|
|
|
root_ = CTNode::copySubtree (ct->root());
|
|
|
|
logVars_ = ct->logVars();
|
|
|
|
logVarSet_ = ct->logVarSet();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (oneTwoOne) {
|
|
|
|
if (logVarSet_.contains (ct->logVarSet())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ct->logVarSet().contains (logVarSet_)) {
|
|
|
|
CTNode::deleteSubtree (root_);
|
|
|
|
root_ = CTNode::copySubtree (ct->root());
|
|
|
|
logVars_ = ct->logVars();
|
|
|
|
logVarSet_ = ct->logVarSet();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LogVarSet intersect = logVarSet_ & ct->logVarSet_;
|
|
|
|
if (intersect.empty()) {
|
2012-05-24 22:55:20 +01:00
|
|
|
// cartesian product
|
2012-05-23 14:56:01 +01:00
|
|
|
appendOnBottom (root_, ct->root()->childs());
|
2013-02-08 21:01:53 +00:00
|
|
|
Util::addToVector (logVars_, ct->logVars_);
|
2012-05-23 14:56:01 +01:00
|
|
|
logVarSet_ |= ct->logVarSet_;
|
|
|
|
} else {
|
|
|
|
moveToTop (intersect.elements());
|
|
|
|
ct->moveToTop (intersect.elements());
|
2012-12-20 23:19:10 +00:00
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
Tuples tuples;
|
|
|
|
CTNodes appendNodes;
|
|
|
|
getTuples (ct->root(), Tuples(), intersect.size(),
|
|
|
|
tuples, appendNodes);
|
|
|
|
|
|
|
|
CTNodes::const_iterator appendIt = appendNodes.begin();
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < tuples.size(); ++ i, ++ appendIt) {
|
2012-05-23 14:56:01 +01:00
|
|
|
bool tupleFounded = join (root_, tuples[i], 0, *appendIt);
|
|
|
|
if (oneTwoOne && tupleFounded == false) {
|
|
|
|
assert (false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LogVars newLvs (ct->logVars().begin() + intersect.size(),
|
|
|
|
ct->logVars().end());
|
2013-02-08 21:01:53 +00:00
|
|
|
Util::addToVector (logVars_, newLvs);
|
2012-05-23 14:56:01 +01:00
|
|
|
logVarSet_ |= LogVarSet (newLvs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
ConstraintTree::getLevel (LogVar X) const
|
|
|
|
{
|
2013-02-08 21:01:53 +00:00
|
|
|
unsigned level = Util::indexOf (logVars_, X);
|
2012-05-23 14:56:01 +01:00
|
|
|
level += 1; // root is in level 0, first logVar is in level 1
|
|
|
|
return level;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ConstraintTree::rename (LogVar X_old, LogVar X_new)
|
|
|
|
{
|
|
|
|
assert (logVarSet_.contains (X_old));
|
|
|
|
assert (logVarSet_.contains (X_new) == false);
|
|
|
|
logVarSet_ -= X_old;
|
|
|
|
logVarSet_ |= X_new;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < logVars_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
if (logVars_[i] == X_old) {
|
|
|
|
logVars_[i] = X_new;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ConstraintTree::applySubstitution (const Substitution& theta)
|
|
|
|
{
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < logVars_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
logVars_[i] = theta.newNameFor (logVars_[i]);
|
|
|
|
}
|
|
|
|
logVarSet_ = LogVarSet (logVars_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ConstraintTree::project (const LogVarSet& X)
|
|
|
|
{
|
|
|
|
assert (logVarSet_.contains (X));
|
|
|
|
remove ((logVarSet_ - X));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-29 21:37:58 +00:00
|
|
|
ConstraintTree
|
|
|
|
ConstraintTree::projectedCopy (const LogVarSet& X)
|
|
|
|
{
|
|
|
|
ConstraintTree copy = *this;
|
|
|
|
copy.project (X);
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
void
|
|
|
|
ConstraintTree::remove (const LogVarSet& X)
|
|
|
|
{
|
|
|
|
assert (logVarSet_.contains (X));
|
|
|
|
if (X.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
moveToBottom (X.elements());
|
|
|
|
unsigned level = getLevel (X.front()) - 1;
|
|
|
|
CTNodes nodes = getNodesAtLevel (level);
|
|
|
|
for (CTNodes::const_iterator it = nodes.begin();
|
|
|
|
it != nodes.end(); ++ it) {
|
|
|
|
(*it)->removeAndDeleteAllChilds();
|
|
|
|
}
|
|
|
|
logVars_.resize (logVars_.size() - X.size());
|
|
|
|
logVarSet_ -= X;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
ConstraintTree::ConstraintTree::isSingleton (LogVar X)
|
|
|
|
{
|
|
|
|
Symbol symb;
|
|
|
|
unsigned level = getLevel (X);
|
|
|
|
CTNodes stack;
|
|
|
|
stack.push_back (root_);
|
|
|
|
while (stack.empty() == false) {
|
|
|
|
CTNode* node = stack.back();
|
|
|
|
stack.pop_back();
|
|
|
|
if (node->level() == level) {
|
|
|
|
if (symb.valid()) {
|
|
|
|
if (node->symbol() != symb) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
symb = node->symbol();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
stack.insert (stack.end(), node->childs().begin(),
|
|
|
|
node->childs().end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LogVarSet
|
2013-02-28 19:45:37 +00:00
|
|
|
ConstraintTree::singletons()
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
LogVarSet singletons;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < logVars_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
if (isSingleton (logVars_[i])) {
|
|
|
|
singletons.insert (logVars_[i]);
|
|
|
|
}
|
2012-12-20 23:19:10 +00:00
|
|
|
}
|
2012-05-23 14:56:01 +01:00
|
|
|
return singletons;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TupleSet
|
|
|
|
ConstraintTree::tupleSet (unsigned stopLevel) const
|
|
|
|
{
|
|
|
|
assert (root_->isRoot());
|
|
|
|
Tuples tuples;
|
|
|
|
if (stopLevel == 0) {
|
|
|
|
stopLevel = logVars_.size();
|
|
|
|
}
|
|
|
|
getTuples (root_, Tuples(), stopLevel, tuples, CTNodes() = {});
|
|
|
|
return TupleSet (tuples);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TupleSet
|
|
|
|
ConstraintTree::tupleSet (const LogVars& originalLvs)
|
|
|
|
{
|
|
|
|
LogVars uniqueLvs;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < originalLvs.size(); i++) {
|
2013-02-08 21:01:53 +00:00
|
|
|
if (Util::contains (uniqueLvs, originalLvs[i]) == false) {
|
2012-05-23 14:56:01 +01:00
|
|
|
uniqueLvs.push_back (originalLvs[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Tuples tuples;
|
|
|
|
moveToTop (uniqueLvs);
|
|
|
|
unsigned stopLevel = uniqueLvs.size();
|
|
|
|
getTuples (root_, Tuples(), stopLevel, tuples, CTNodes() = {});
|
|
|
|
|
|
|
|
if (originalLvs.size() != uniqueLvs.size()) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<size_t> indexes;
|
2012-05-23 14:56:01 +01:00
|
|
|
indexes.reserve (originalLvs.size());
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < originalLvs.size(); i++) {
|
2013-02-08 21:01:53 +00:00
|
|
|
indexes.push_back (Util::indexOf (uniqueLvs, originalLvs[i]));
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
Tuples tuples2;
|
|
|
|
tuples2.reserve (tuples.size());
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < tuples.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
Tuple t;
|
|
|
|
t.reserve (originalLvs.size());
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t j = 0; j < originalLvs.size(); j++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
t.push_back (tuples[i][indexes[j]]);
|
|
|
|
}
|
|
|
|
tuples2.push_back (t);
|
|
|
|
}
|
|
|
|
return TupleSet (tuples2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TupleSet (tuples);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ConstraintTree::exportToGraphViz (
|
|
|
|
const char* fileName,
|
|
|
|
bool showLogVars) const
|
|
|
|
{
|
2013-02-07 13:37:15 +00:00
|
|
|
std::ofstream out (fileName);
|
2012-05-23 14:56:01 +01:00
|
|
|
if (!out.is_open()) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::cerr << "Error: couldn't open file '" << fileName << "'." ;
|
2013-02-13 19:18:55 +00:00
|
|
|
std::cerr << std::endl;
|
2012-12-20 17:11:11 +00:00
|
|
|
return;
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
2013-02-07 13:37:15 +00:00
|
|
|
out << "digraph {" << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
ConstraintTree copy (*this);
|
2012-12-20 16:20:38 +00:00
|
|
|
copy.moveToTop (copy.logVarSet_.elements());
|
2012-05-23 14:56:01 +01:00
|
|
|
CTNodes nodes = getNodesBelow (copy.root_);
|
2013-02-07 13:37:15 +00:00
|
|
|
out << "\"" << copy.root_ << "\"" << " [label=\"R\"]" << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
for (CTNodes::const_iterator it = ++ nodes.begin();
|
|
|
|
it != nodes.end(); ++ it) {
|
|
|
|
out << "\"" << *it << "\"";
|
|
|
|
out << " [label=\"" << **it << "\"]" ;
|
2013-02-07 13:37:15 +00:00
|
|
|
out << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
for (CTNodes::const_iterator it = nodes.begin();
|
|
|
|
it != nodes.end(); ++ it) {
|
|
|
|
const CTChilds& childs = (*it)->childs();
|
|
|
|
for (CTChilds::const_iterator chIt = childs.begin();
|
|
|
|
chIt != childs.end(); ++ chIt) {
|
|
|
|
out << "\"" << *it << "\"" ;
|
|
|
|
out << " -> " ;
|
2013-02-07 13:37:15 +00:00
|
|
|
out << "\"" << *chIt << "\"" << std::endl ;
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (showLogVars) {
|
2013-02-07 13:37:15 +00:00
|
|
|
out << "Root [label=\"\", shape=plaintext]" << std::endl;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < copy.logVars_.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
out << copy.logVars_[i] << " [label=" ;
|
|
|
|
out << copy.logVars_[i] << ", " ;
|
2013-02-07 13:37:15 +00:00
|
|
|
out << "shape=plaintext, fontsize=14]" << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
out << "Root -> " << copy.logVars_[0];
|
2013-02-07 13:37:15 +00:00
|
|
|
out << " [style=invis]" << std::endl;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < copy.logVars_.size() - 1; i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
out << copy.logVars_[i] << " -> " << copy.logVars_[i + 1];
|
2013-02-07 13:37:15 +00:00
|
|
|
out << " [style=invis]" << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
}
|
2013-02-07 13:37:15 +00:00
|
|
|
out << "}" <<std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
out.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
ConstraintTree::isCountNormalized (const LogVarSet& Ys)
|
|
|
|
{
|
|
|
|
assert (logVarSet_.contains (Ys));
|
|
|
|
if (Ys.empty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (Ys.size() == logVars_.size()) {
|
|
|
|
assert (LogVarSet (logVars_) == LogVarSet (Ys));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
LogVarSet Zs = logVarSet_ - LogVarSet (Ys);
|
|
|
|
moveToTop (Zs.elements());
|
|
|
|
CTNodes nodes = getNodesAtLevel (Zs.size());
|
|
|
|
unsigned count = countTuples (*nodes.begin());
|
|
|
|
for (CTNodes::const_iterator it = nodes.begin();
|
|
|
|
it != nodes.end(); ++ it) {
|
|
|
|
if (countTuples (*it) != count) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-20 23:19:10 +00:00
|
|
|
}
|
2012-05-23 14:56:01 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-20 23:19:10 +00:00
|
|
|
unsigned
|
2012-05-23 14:56:01 +01:00
|
|
|
ConstraintTree::getConditionalCount (const LogVarSet& Ys)
|
|
|
|
{
|
|
|
|
assert (isCountNormalized (Ys));
|
|
|
|
if (Ys.empty()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (Ys.size() == logVars_.size()) {
|
|
|
|
assert (LogVarSet (Ys) == LogVarSet (logVars_));
|
|
|
|
return countTuples (root_);
|
|
|
|
}
|
|
|
|
LogVarSet Zs = logVarSet_ - Ys;
|
|
|
|
moveToTop (Zs.elements());
|
|
|
|
CTNode* n = root_;
|
|
|
|
unsigned l = 0;
|
|
|
|
while (l != Zs.size()) {
|
|
|
|
n = *(n->childs().begin());
|
|
|
|
l ++;
|
|
|
|
}
|
|
|
|
return countTuples (n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TinySet<unsigned>
|
|
|
|
ConstraintTree::getConditionalCounts (const LogVarSet& Ys)
|
|
|
|
{
|
|
|
|
TinySet<unsigned> counts;
|
|
|
|
assert (logVarSet_.contains (Ys));
|
|
|
|
if (Ys.empty()) {
|
|
|
|
counts.insert (1);
|
|
|
|
} else if (Ys.size() == logVars_.size()) {
|
|
|
|
assert (LogVarSet (logVars_) == LogVarSet (Ys));
|
|
|
|
counts.insert (countTuples (root_));
|
|
|
|
} else {
|
|
|
|
LogVarSet Zs = logVarSet_ - LogVarSet (Ys);
|
|
|
|
moveToTop (Zs.elements());
|
|
|
|
CTNodes nodes = getNodesAtLevel (Zs.size());
|
|
|
|
for (CTNodes::const_iterator it = nodes.begin();
|
|
|
|
it != nodes.end(); ++ it) {
|
|
|
|
counts.insert (countTuples (*it));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return counts;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
ConstraintTree::isCartesianProduct (const LogVarSet& Xs)
|
|
|
|
{
|
|
|
|
assert (logVarSet_.contains (Xs));
|
|
|
|
if (Xs.size() <= 1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
moveToTop (Xs.elements());
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 1; i < Xs.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
CTNodes nodes = getNodesAtLevel (i);
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t j = 1; j < nodes.size(); j++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
if (nodes[j-1]->nrChilds() != nodes[ j ]->nrChilds()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (nodes[j-1]->childSymbols() != nodes[ j ]->childSymbols()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::pair<ConstraintTree*,ConstraintTree*>
|
|
|
|
ConstraintTree::split (const Tuple& tuple)
|
|
|
|
{
|
|
|
|
// assumes that my log vars are already on top
|
|
|
|
LogVars lvs (logVars_.begin(), logVars_.begin() + tuple.size());
|
|
|
|
ConstraintTree tempCt (logVars_, {tuple});
|
|
|
|
return split (lvs, &tempCt, lvs);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::pair<ConstraintTree*, ConstraintTree*>
|
|
|
|
ConstraintTree::split (
|
|
|
|
const LogVars& lvs1,
|
|
|
|
ConstraintTree* ct,
|
|
|
|
const LogVars& lvs2)
|
|
|
|
{
|
|
|
|
assert (lvs1.size() == lvs2.size());
|
|
|
|
assert (lvs1.size() == LogVarSet (lvs1).size());
|
|
|
|
assert (lvs2.size() == LogVarSet (lvs2).size());
|
|
|
|
assert (logVarSet_.contains (lvs1));
|
|
|
|
assert (ct->logVarSet().contains (lvs2));
|
|
|
|
CTChilds commChilds, exclChilds;
|
|
|
|
unsigned stopLevel = lvs1.size();
|
|
|
|
split (root_, ct->root(), commChilds, exclChilds, stopLevel);
|
|
|
|
ConstraintTree* commCt = new ConstraintTree (commChilds, logVars_);
|
|
|
|
ConstraintTree* exclCt = new ConstraintTree (exclChilds, logVars_);
|
2013-02-07 13:37:15 +00:00
|
|
|
// std::cout << commCt->tupleSet() << " + " ;
|
|
|
|
// std::cout << exclCt->tupleSet() << " = " ;
|
|
|
|
// std::cout << tupleSet() << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
assert ((commCt->tupleSet() | exclCt->tupleSet()) == tupleSet());
|
|
|
|
assert ((exclCt->tupleSet (stopLevel) & ct->tupleSet (stopLevel)).empty());
|
|
|
|
return {commCt, exclCt};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ConstraintTrees
|
|
|
|
ConstraintTree::countNormalize (const LogVarSet& Ys)
|
|
|
|
{
|
|
|
|
assert (logVarSet_.contains (Ys));
|
|
|
|
LogVarSet Zs = logVarSet_ - Ys;
|
|
|
|
if (Ys.empty() || Zs.empty()) {
|
|
|
|
return { new ConstraintTree (*this) };
|
|
|
|
}
|
|
|
|
moveToTop (Zs.elements());
|
|
|
|
ConstraintTrees cts;
|
2013-02-07 13:37:15 +00:00
|
|
|
std::unordered_map<unsigned, ConstraintTree*> countMap;
|
2012-05-23 14:56:01 +01:00
|
|
|
unsigned stopLevel = getLevel (Zs.back());
|
|
|
|
const CTChilds& childs = root_->childs();
|
|
|
|
|
|
|
|
for (CTChilds::const_iterator chIt = childs.begin();
|
|
|
|
chIt != childs.end(); ++ chIt) {
|
2013-02-07 13:37:15 +00:00
|
|
|
const std::vector<std::pair<CTNode*, unsigned>>& res =
|
2012-05-23 14:56:01 +01:00
|
|
|
countNormalize (*chIt, stopLevel);
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t j = 0; j < res.size(); j++) {
|
2013-02-07 13:37:15 +00:00
|
|
|
std::unordered_map<unsigned, ConstraintTree*>::iterator it
|
2012-05-23 14:56:01 +01:00
|
|
|
= countMap.find (res[j].second);
|
|
|
|
if (it == countMap.end()) {
|
|
|
|
ConstraintTree* newCt = new ConstraintTree (logVars_);
|
2013-02-07 13:37:15 +00:00
|
|
|
it = countMap.insert (std::make_pair (res[j].second, newCt)).first;
|
2012-05-23 14:56:01 +01:00
|
|
|
cts.push_back (newCt);
|
|
|
|
}
|
|
|
|
it->second->root_->mergeSubtree (res[j].first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cts;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ConstraintTrees
|
|
|
|
ConstraintTree::jointCountNormalize (
|
|
|
|
ConstraintTree* commCt,
|
|
|
|
ConstraintTree* exclCt,
|
|
|
|
LogVar X,
|
|
|
|
LogVar X_new1,
|
|
|
|
LogVar X_new2)
|
|
|
|
{
|
|
|
|
unsigned N = getConditionalCount (X);
|
2013-02-07 13:37:15 +00:00
|
|
|
// std::cout << "My tuples: " << tupleSet() << std::endl;
|
|
|
|
// std::cout << "CommCt tuples: " << commCt->tupleSet() << std::endl;
|
|
|
|
// std::cout << "ExclCt tuples: " << exclCt->tupleSet() << std::endl;
|
|
|
|
// std::cout << "Counted Lv: " << X << std::endl;
|
|
|
|
// std::cout << "X_new1: " << X_new1 << std::endl;
|
|
|
|
// std::cout << "X_new2: " << X_new2 << std::endl;
|
|
|
|
// std::cout << "Original N: " << N << std::endl;
|
|
|
|
// std::cout << endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
ConstraintTrees normCts1 = commCt->countNormalize (X);
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<unsigned> counts1 (normCts1.size());
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < normCts1.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
counts1[i] = normCts1[i]->getConditionalCount (X);
|
2013-02-07 13:37:15 +00:00
|
|
|
// std::cout << "normCts1[" << i << "] #" << counts1[i] ;
|
|
|
|
// std::cout << " " << normCts1[i]->tupleSet() << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ConstraintTrees normCts2 = exclCt->countNormalize (X);
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<unsigned> counts2 (normCts2.size());
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < normCts2.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
counts2[i] = normCts2[i]->getConditionalCount (X);
|
2013-02-07 13:37:15 +00:00
|
|
|
// std::cout << "normCts2[" << i << "] #" << counts2[i] ;
|
|
|
|
// std::cout << " " << normCts2[i]->tupleSet() << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
2013-02-07 13:37:15 +00:00
|
|
|
// std::cout << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
ConstraintTree* excl1 = 0;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < normCts1.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
if (counts1[i] == N) {
|
|
|
|
excl1 = normCts1[i];
|
|
|
|
normCts1.erase (normCts1.begin() + i);
|
|
|
|
counts1.erase (counts1.begin() + i);
|
2013-02-07 13:37:15 +00:00
|
|
|
// std::cout << "joint-count(" << N << ",0)" << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstraintTree* excl2 = 0;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < normCts2.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
if (counts2[i] == N) {
|
|
|
|
excl2 = normCts2[i];
|
|
|
|
normCts2.erase (normCts2.begin() + i);
|
|
|
|
counts2.erase (counts2.begin() + i);
|
2013-02-07 13:37:15 +00:00
|
|
|
// std::cout << "joint-count(0," << N << ")" << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < normCts1.size(); i++) {
|
2012-12-20 23:19:10 +00:00
|
|
|
unsigned j;
|
2012-05-23 14:56:01 +01:00
|
|
|
for (j = 0; counts1[i] + counts2[j] != N; j++) ;
|
2013-02-07 13:37:15 +00:00
|
|
|
// std::cout << "joint-count(" << counts1[i] ;
|
|
|
|
// std::cout << "," << counts2[j] << ")" << std::endl;
|
2012-05-23 14:56:01 +01:00
|
|
|
const CTChilds& childs = normCts2[j]->root_->childs();
|
|
|
|
for (CTChilds::const_iterator chIt = childs.begin();
|
|
|
|
chIt != childs.end(); ++ chIt) {
|
|
|
|
normCts1[i]->root_->mergeSubtree (CTNode::copySubtree (*chIt));
|
|
|
|
}
|
|
|
|
delete normCts2[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstraintTrees cts = normCts1;
|
|
|
|
commCt->rename (X, X_new1);
|
|
|
|
exclCt->rename (X, X_new2);
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < cts.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
cts[i]->remove (X);
|
|
|
|
cts[i]->join (commCt);
|
|
|
|
cts[i]->join (exclCt);
|
|
|
|
}
|
|
|
|
|
2012-12-27 12:54:58 +00:00
|
|
|
if (excl1) {
|
2012-05-23 14:56:01 +01:00
|
|
|
cts.push_back (excl1);
|
|
|
|
}
|
2012-12-27 12:54:58 +00:00
|
|
|
if (excl2) {
|
2012-05-23 14:56:01 +01:00
|
|
|
cts.push_back (excl2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cts;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LogVars
|
|
|
|
ConstraintTree::expand (LogVar X)
|
|
|
|
{
|
|
|
|
moveToBottom ({X});
|
|
|
|
assert (isCountNormalized (X));
|
|
|
|
CTNodes nodes = getNodesAtLevel (logVars_.size() - 1);
|
|
|
|
unsigned nrSymbols = getConditionalCount (X);
|
|
|
|
for (CTNodes::const_iterator it = nodes.begin();
|
|
|
|
it != nodes.end(); ++ it) {
|
|
|
|
Symbols symbols;
|
|
|
|
const CTChilds& childs = (*it)->childs();
|
|
|
|
for (CTChilds::const_iterator chIt = childs.begin();
|
|
|
|
chIt != childs.end(); ++ chIt) {
|
|
|
|
symbols.push_back ((*chIt)->symbol());
|
|
|
|
}
|
|
|
|
(*it)->removeAndDeleteAllChilds();
|
|
|
|
CTNode* prev = *it;
|
|
|
|
assert (symbols.size() == nrSymbols);
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t j = 0; j < nrSymbols; j++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
CTNode* newNode = new CTNode (symbols[j], (*it)->level() + j);
|
|
|
|
prev->mergeSubtree (newNode);
|
|
|
|
prev = newNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LogVars newLvs;
|
|
|
|
logVars_.pop_back();
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < nrSymbols; i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
logVars_.push_back (LogVar (logVarSet_.back() + 1));
|
|
|
|
newLvs.push_back (LogVar (logVarSet_.back() + 1));
|
|
|
|
logVarSet_.insert (LogVar (logVarSet_.back() + 1));
|
|
|
|
}
|
|
|
|
logVarSet_ -= X;
|
|
|
|
return newLvs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ConstraintTrees
|
|
|
|
ConstraintTree::ground (LogVar X)
|
|
|
|
{
|
|
|
|
moveToTop ({X});
|
|
|
|
ConstraintTrees cts;
|
|
|
|
const CTChilds& nodes = root_->childs();
|
|
|
|
for (CTChilds::const_iterator it = nodes.begin();
|
|
|
|
it != nodes.end(); ++ it) {
|
|
|
|
CTNode* copy = CTNode::copySubtree (*it);
|
|
|
|
copy->setSymbol ((*it)->symbol());
|
|
|
|
ConstraintTree* newCt = new ConstraintTree (logVars_);
|
|
|
|
newCt->root()->mergeSubtree (copy);
|
|
|
|
cts.push_back (newCt);
|
|
|
|
}
|
|
|
|
return cts;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2012-12-18 23:51:51 +00:00
|
|
|
ConstraintTree::cloneLogVar (LogVar X_1, LogVar X_2)
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
moveToBottom ({X_1});
|
|
|
|
CTNodes leafs = getNodesAtLevel (logVars_.size());
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < leafs.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
leafs[i]->childs().insert_sorted (
|
|
|
|
new CTNode (leafs[i]->symbol(), leafs[i]->level() + 1));
|
|
|
|
}
|
|
|
|
logVars_.push_back (X_2);
|
|
|
|
logVarSet_.insert (X_2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-30 00:15:40 +00:00
|
|
|
ConstraintTree&
|
|
|
|
ConstraintTree::operator= (const ConstraintTree& ct)
|
|
|
|
{
|
|
|
|
if (this != &ct) {
|
|
|
|
root_ = CTNode::copySubtree (ct.root_);
|
|
|
|
logVars_ = ct.logVars_;
|
|
|
|
logVarSet_ = ct.logVarSet_;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
unsigned
|
|
|
|
ConstraintTree::countTuples (const CTNode* n) const
|
|
|
|
{
|
|
|
|
if (n->isLeaf()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
unsigned sum = 0;
|
|
|
|
const CTChilds& childs = n->childs();
|
|
|
|
for (CTChilds::const_iterator chIt = childs.begin();
|
|
|
|
chIt != childs.end(); ++ chIt) {
|
|
|
|
sum += countTuples (*chIt);
|
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CTNodes
|
|
|
|
ConstraintTree::getNodesBelow (CTNode* fromHere) const
|
|
|
|
{
|
|
|
|
CTNodes nodes;
|
2013-02-07 13:37:15 +00:00
|
|
|
std::queue<CTNode*> queue;
|
2012-05-23 14:56:01 +01:00
|
|
|
queue.push (fromHere);
|
|
|
|
while (queue.empty() == false) {
|
|
|
|
CTNode* node = queue.front();
|
|
|
|
nodes.push_back (node);
|
|
|
|
for (CTChilds::const_iterator chIt = node->childs().begin();
|
|
|
|
chIt != node->childs().end(); ++ chIt) {
|
|
|
|
queue.push (*chIt);
|
|
|
|
}
|
|
|
|
queue.pop();
|
|
|
|
}
|
|
|
|
return nodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CTNodes
|
2012-12-20 23:19:10 +00:00
|
|
|
ConstraintTree::getNodesAtLevel (unsigned level) const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
assert (level <= logVars_.size());
|
|
|
|
if (level == 0) {
|
|
|
|
return { root_ };
|
|
|
|
}
|
|
|
|
CTNodes stack;
|
|
|
|
CTNodes nodes;
|
|
|
|
stack.push_back (root_);
|
|
|
|
while (stack.empty() == false) {
|
|
|
|
CTNode* node = stack.back();
|
|
|
|
stack.pop_back();
|
|
|
|
if (node->level() + 1 == level) {
|
|
|
|
nodes.insert (nodes.end(), node->childs().begin(),
|
|
|
|
node->childs().end());
|
|
|
|
} else {
|
|
|
|
stack.insert (stack.end(), node->childs().begin(),
|
|
|
|
node->childs().end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
ConstraintTree::nrNodes (const CTNode* n) const
|
|
|
|
{
|
|
|
|
unsigned nr = 0;
|
|
|
|
if (n->isLeaf() == false) {
|
|
|
|
for (CTChilds::const_iterator chIt = n->childs().begin();
|
|
|
|
chIt != n->childs().end(); ++ chIt) {
|
|
|
|
nr += nrNodes (*chIt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ConstraintTree::appendOnBottom (CTNode* n, const CTChilds& childs)
|
|
|
|
{
|
|
|
|
if (childs.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CTNodes stack { n };
|
|
|
|
while (stack.empty() == false) {
|
|
|
|
CTNode* node = stack.back();
|
|
|
|
stack.pop_back();
|
|
|
|
if (node->isLeaf()) {
|
|
|
|
for (CTChilds::const_iterator chIt = childs.begin();
|
|
|
|
chIt != childs.end(); ++ chIt) {
|
|
|
|
node->mergeSubtree (CTNode::copySubtree (*chIt));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
stack.insert (stack.end(), node->childs().begin(),
|
|
|
|
node->childs().end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ConstraintTree::swapLogVar (LogVar X)
|
|
|
|
{
|
2013-02-08 21:01:53 +00:00
|
|
|
size_t pos = Util::indexOf (logVars_, X);
|
2012-05-24 22:55:20 +01:00
|
|
|
assert (pos != logVars_.size());
|
2013-02-16 18:58:22 +00:00
|
|
|
CTNodes nodes = getNodesAtLevel (pos);
|
2012-05-23 14:56:01 +01:00
|
|
|
for (CTNodes::const_iterator nodeIt = nodes.begin();
|
|
|
|
nodeIt != nodes.end(); ++ nodeIt) {
|
|
|
|
CTChilds childsCopy = (*nodeIt)->childs();
|
|
|
|
(*nodeIt)->removeChilds();
|
|
|
|
for (CTChilds::const_iterator ccIt = childsCopy.begin();
|
|
|
|
ccIt != childsCopy.end(); ++ ccIt) {
|
|
|
|
const CTChilds& grandsons = (*ccIt)->childs();
|
|
|
|
for (CTChilds::const_iterator gsIt = grandsons.begin();
|
|
|
|
gsIt != grandsons.end(); ++ gsIt) {
|
|
|
|
CTNode* childCopy = new CTNode (
|
|
|
|
(*ccIt)->symbol(), (*ccIt)->level() + 1, (*gsIt)->childs());
|
|
|
|
(*gsIt)->removeChilds();
|
|
|
|
(*gsIt)->childs().insert_sorted (childCopy);
|
|
|
|
(*gsIt)->setLevel ((*gsIt)->level() - 1);
|
|
|
|
(*nodeIt)->mergeSubtree ((*gsIt), false);
|
|
|
|
}
|
|
|
|
delete (*ccIt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::swap (logVars_[pos], logVars_[pos + 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
ConstraintTree::join (
|
|
|
|
CTNode* currNode,
|
|
|
|
const Tuple& tuple,
|
2012-05-24 22:55:20 +01:00
|
|
|
size_t currIdx,
|
2012-05-23 14:56:01 +01:00
|
|
|
CTNode* appendNode)
|
|
|
|
{
|
|
|
|
bool tupleFounded = false;
|
|
|
|
CTChilds::const_iterator it = currNode->findSymbol (tuple[currIdx]);
|
|
|
|
if (it != currNode->childs().end()) {
|
|
|
|
if (currIdx == tuple.size() - 1) {
|
|
|
|
appendOnBottom (*it, appendNode->childs());
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
tupleFounded = join (*it, tuple, currIdx + 1, appendNode);
|
|
|
|
}
|
2012-12-20 23:19:10 +00:00
|
|
|
}
|
2012-05-23 14:56:01 +01:00
|
|
|
return tupleFounded;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ConstraintTree::getTuples (
|
2012-12-20 23:19:10 +00:00
|
|
|
CTNode* n,
|
2012-05-23 14:56:01 +01:00
|
|
|
Tuples currTuples,
|
|
|
|
unsigned stopLevel,
|
|
|
|
Tuples& tuplesCollected,
|
|
|
|
CTNodes& continuationNodes) const
|
|
|
|
{
|
|
|
|
if (n->isRoot() == false) {
|
2012-12-27 12:54:58 +00:00
|
|
|
if (currTuples.empty()) {
|
2012-05-23 14:56:01 +01:00
|
|
|
currTuples.push_back ({ n->symbol()});
|
|
|
|
} else {
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < currTuples.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
currTuples[i].push_back (n->symbol());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (n->level() == stopLevel) {
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < currTuples.size(); i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
tuplesCollected.push_back (currTuples[i]);
|
|
|
|
continuationNodes.push_back (n);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const CTChilds& childs = n->childs();
|
|
|
|
for (CTChilds::const_iterator chIt = childs.begin();
|
|
|
|
chIt != childs.end(); ++ chIt) {
|
|
|
|
getTuples (*chIt, currTuples, stopLevel, tuplesCollected,
|
|
|
|
continuationNodes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned
|
2013-02-28 19:45:37 +00:00
|
|
|
ConstraintTree::size() const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
return countTuples (root_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
ConstraintTree::nrSymbols (LogVar X)
|
|
|
|
{
|
|
|
|
moveToTop ({X});
|
|
|
|
return root_->childs().size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<std::pair<CTNode*, unsigned>>
|
2012-05-23 14:56:01 +01:00
|
|
|
ConstraintTree::countNormalize (
|
|
|
|
const CTNode* n,
|
|
|
|
unsigned stopLevel)
|
|
|
|
{
|
|
|
|
if (n->level() == stopLevel) {
|
2013-02-07 13:37:15 +00:00
|
|
|
return std::vector<std::pair<CTNode*, unsigned>>() = {
|
|
|
|
std::make_pair (CTNode::copySubtree (n), countTuples (n))
|
2012-05-23 14:56:01 +01:00
|
|
|
};
|
|
|
|
}
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<std::pair<CTNode*, unsigned>> res;
|
2012-05-23 14:56:01 +01:00
|
|
|
const CTChilds& childs = n->childs();
|
|
|
|
for (CTChilds::const_iterator chIt = childs.begin();
|
|
|
|
chIt != childs.end(); ++ chIt) {
|
2013-02-07 13:37:15 +00:00
|
|
|
const std::vector<std::pair<CTNode*, unsigned>>& lowerRes =
|
2012-05-23 14:56:01 +01:00
|
|
|
countNormalize (*chIt, stopLevel);
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t j = 0; j < lowerRes.size(); j++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
CTNode* newNode = new CTNode (*n);
|
|
|
|
newNode->mergeSubtree (lowerRes[j].first);
|
2013-02-07 13:37:15 +00:00
|
|
|
res.push_back (std::make_pair (newNode, lowerRes[j].second));
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ConstraintTree::split (
|
|
|
|
CTNode* n1,
|
|
|
|
CTNode* n2,
|
|
|
|
CTChilds& commChilds,
|
|
|
|
CTChilds& exclChilds,
|
2012-12-20 23:19:10 +00:00
|
|
|
unsigned stopLevel)
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
CTChilds& childs1 = n1->childs();
|
|
|
|
for (CTChilds::const_iterator chIt1 = childs1.begin();
|
|
|
|
chIt1 != childs1.end(); ++ chIt1) {
|
|
|
|
CTChilds::iterator chIt2 = n2->findSymbol ((*chIt1)->symbol());
|
|
|
|
if (chIt2 == n2->childs().end()) {
|
|
|
|
exclChilds.insert_sorted (CTNode::copySubtree (*chIt1));
|
|
|
|
} else {
|
|
|
|
if ((*chIt1)->level() == stopLevel) {
|
|
|
|
commChilds.insert_sorted (CTNode::copySubtree (*chIt1));
|
|
|
|
} else {
|
|
|
|
CTChilds lowerCommChilds, lowerExclChilds;
|
|
|
|
split (*chIt1, *chIt2, lowerCommChilds, lowerExclChilds, stopLevel);
|
|
|
|
if (lowerCommChilds.empty() == false) {
|
|
|
|
commChilds.insert_sorted (new CTNode (**chIt1, lowerCommChilds));
|
|
|
|
}
|
|
|
|
if (lowerExclChilds.empty() == false) {
|
|
|
|
exclChilds.insert_sorted (new CTNode (**chIt1, lowerExclChilds));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
} // namespace Horus
|
2013-02-07 23:53:13 +00:00
|
|
|
|