--- /dev/null
+#ifndef CLAUSELIST_H_
+#define CLAUSELIST_H_
+
+#include "../AST/AST.h"
+#include <cassert>
+
+namespace BEEV
+{
+
+typedef vector<const ASTNode*>* ClausePtr;
+typedef vector<const ASTNode*> ClauseNoPtr;
+typedef deque<ClausePtr> ClauseContainer;
+
+static bool vectorsizesort(const ClausePtr& a, const ClausePtr& b)
+{
+ return a->size() < b->size();
+}
+
+class ClauseList
+ {
+ ClauseContainer cont;
+ // No copy constructor or assignment.
+ ClauseList& operator = (const ClauseList& other);
+ ClauseList(const ClauseList& other);
+
+ public:
+
+ void appendToAllClauses(const ASTNode* n);
+ void productInPlace(const ClauseList& varphi2);
+
+ ClauseContainer* asList() {
+ return &cont;
+ }
+
+ bool isUnit() const
+ {
+ return size() ==1 && cont[0]->size() ==1;
+ }
+
+ int size() const {
+ return cont.size();
+ }
+
+ ClauseList()
+ {
+ }
+
+ void sort()
+ {
+ std::sort (cont.begin(), cont.end(), vectorsizesort);
+ }
+
+ //Putting: cont.reserve(l->size() + cont.size());
+ // in here is a terrible idea.
+ // reserve() will increase the size of the vector to the amount reserved.
+ // If you are adding 10 clauselists of size 2, then it will increase
+ // the base container by 2, 10 times.
+ // However if you use the default growth strategy, it will grow in chunks,
+ // perhaps doubling the backing arrays size. Requiring fewer memmoves.
+ void insert(ClauseList* l) {
+ cont.insert(cont.end(), l->cont.begin(), l->cont.end());
+ }
+
+ void insertAtFront(ClauseList* l) {
+ cont.insert(cont.begin(), l->cont.begin(), l->cont.end());
+ }
+
+ // This deletes all the clausePtrs this container points to.
+ // This isn't part of the destructor because for instance, in
+ // INPLACE_UNION, the clausePtrs are already pointed to by
+ // another ClauseContainer.
+ void deleteJustVectors() {
+ ClauseContainer::const_iterator it = cont.begin();
+ for (; it != cont.end(); it++) {
+ delete *it;
+ }
+ cont.clear();
+ }
+
+ void push_back(ClausePtr p) {
+ cont.push_back(p);
+ }
+
+ void reserve(int v) {
+ //cont.reserve(v);
+ }
+
+ static ClauseList* UNION(const ClauseList& varphi1, const ClauseList& varphi2)
+ {
+ ClauseList* psi1 = ClauseList::COPY(varphi1);
+ ClauseList* psi2 = ClauseList::COPY(varphi2);
+ if (psi1->size() < psi2->size())
+ {
+ psi2->insert(psi1);
+ delete psi1;
+ return psi2;
+ }
+ else
+ {
+ psi1->insert(psi2);
+ delete psi2;
+ return psi1;
+ }
+ }
+
+ static void INPLACE_UNION(ClauseList* varphi1, const ClauseList& varphi2)
+ {
+
+ ClauseList* psi2 = ClauseList::COPY(varphi2);
+ varphi1->insert( psi2);
+ delete psi2;
+ }
+
+ static void NOCOPY_INPLACE_UNION(ClauseList* varphi1, ClauseList* varphi2)
+ {
+ varphi1->insert(varphi2);
+ delete varphi2;
+ }
+
+ static ClauseList* PRODUCT(const ClauseList& varphi1,
+ const ClauseList& varphi2) {
+
+ ClauseList* psi = new ClauseList();
+ psi->reserve(varphi1.size() * varphi2.size());
+
+ ClauseContainer::const_iterator it1 = varphi1.cont.begin();
+ for (; it1 != varphi1.cont.end(); it1++) {
+ const ClausePtr& clause1 = *it1;
+ ClauseContainer::const_iterator it2 = varphi2.cont.begin();
+ for (; it2 != varphi2.cont.end(); it2++) {
+ const ClausePtr& clause2 = *it2;
+ ClausePtr clause = new ClauseNoPtr();
+ clause->reserve(clause1->size() + clause2->size());
+ clause->insert(clause->end(), clause1->begin(), clause1->end());
+ clause->insert(clause->end(), clause2->begin(), clause2->end());
+ psi->push_back(clause);
+ }
+ }
+ return psi;
+ }
+
+ static ClauseList* COPY(const ClauseList& varphi) {
+
+ ClauseList* psi = new ClauseList();
+ psi->reserve(varphi.size());
+
+ ClauseContainer::const_iterator it = varphi.cont.begin();
+ for (; it != varphi.cont.end(); it++) {
+ psi->push_back(new ClauseNoPtr(**it));
+ }
+
+ return psi;
+ }
+ };
+}
+#endif /* CLAUSELIST_H_ */
//############################################################
//############################################################
- void CNFMgr::DeleteClauseList(ClauseList cllp)
- {
- ClauseList::const_iterator iend = cllp.end();
- for (ClauseList::const_iterator i = cllp.begin(); i < iend; i++)
- {
- delete *i;
- }
- cllp.clear();
- } //End of DeleteClauseList
-
bool CNFMgr::onChildDoPos(const ASTNode& varphi, unsigned int idx)
{
bool result = true;
//########################################
//utilities for clause sets
-
- ClauseList* CNFMgr::COPY(const ClauseList& varphi)
- {
- ClauseList* psi = new ClauseList();
-
- ClauseList::const_iterator it = varphi.begin();
- for (; it != varphi.end(); it++)
- {
- psi->push_back(new vector<const ASTNode*> (**it));
- }
-
- return psi;
- } //End of COPY()
-
ClauseList* CNFMgr::SINGLETON(const ASTNode& varphi)
{
ASTNode* copy = ASTNodeToASTNodePtr(varphi);
static ASTNode GetNodeFrom_SINGLETON(ClauseList *cl)
{
- ClausePtr c = (*cl)[0];
+ ClausePtr c = (*(*cl).asList())[0];
const ASTNode * a = (*c)[0];
return *a;
}
- ClauseList* CNFMgr::UNION(const ClauseList& varphi1,
- const ClauseList& varphi2)
- {
- ClauseList* psi1 = COPY(varphi1);
- ClauseList* psi2 = COPY(varphi2);
- psi1->insert(psi1->end(), psi2->begin(), psi2->end());
- delete psi2;
-
- return psi1;
- } //End of UNION()
-
- void CNFMgr::INPLACE_UNION(ClauseList* varphi1,
- const ClauseList& varphi2)
- {
- ClauseList* psi2 = COPY(varphi2);
- varphi1->insert(varphi1->end(), psi2->begin(), psi2->end());
- delete psi2;
- } //End of INPLACE_UNION()
-
- void CNFMgr::NOCOPY_INPLACE_UNION(ClauseList* varphi1,
- ClauseList* varphi2)
- {
- varphi1->insert(varphi1->end(), varphi2->begin(), varphi2->end());
- delete varphi2;
- } //End of NOCOPY_INPLACE_UNION
-
- ClauseList* CNFMgr::PRODUCT(const ClauseList& varphi1,
- const ClauseList& varphi2)
- {
- ClauseList* psi = new ClauseList();
- psi->reserve(varphi1.size() * varphi2.size());
-
- ClauseList::const_iterator it1 = varphi1.begin();
- for (; it1 != varphi1.end(); it1++)
- {
- ClausePtr clause1 = *it1;
- ClauseList::const_iterator it2 = varphi2.begin();
- for (; it2 != varphi2.end(); it2++)
- {
- ClausePtr clause2 = *it2;
- ClausePtr clause = new vector<const ASTNode*> ();
- clause->reserve(clause1->size() + clause2->size());
- clause->insert(clause->end(), clause1->begin(), clause1->end());
- clause->insert(clause->end(), clause2->begin(), clause2->end());
- psi->push_back(clause);
- }
- }
-
- return psi;
- } //End of Product
-
//########################################
//########################################
//prep. for cnf conversion
//########################################
ClauseList* cl1 = SINGLETON(bm->CreateNode(EQ, psi, t1));
- ClauseList* cl2 = PRODUCT(*(info[varphi[0]]->clausesneg), *cl1);
+ ClauseList* cl2 = ClauseList::PRODUCT(*(info[varphi[0]]->clausesneg), *cl1);
DELETE(cl1);
- defs->insert(defs->end(), cl2->begin(), cl2->end());
+ defs->insert(cl2);
ClauseList* cl3 = SINGLETON(bm->CreateNode(EQ, psi, t2));
- ClauseList* cl4 = PRODUCT(*(info[varphi[0]]->clausespos), *cl3);
+ ClauseList* cl4 = ClauseList::PRODUCT(*(info[varphi[0]]->clausespos), *cl3);
DELETE(cl3);
- defs->insert(defs->end(), cl4->begin(), cl4->end());
+ defs->insert(cl4);
return ASTNodeToASTNodePtr(psi);
}//End of doRenameITE()
ClauseList* cl1;
cl1 = SINGLETON(bm->CreateNode(NOT, psi));
- ClauseList* cl2 = PRODUCT(*(info[varphi]->clausespos), *cl1);
- defs->insert(defs->end(), cl2->begin(), cl2->end());
+ ClauseList* cl2 = ClauseList::PRODUCT(*(info[varphi]->clausespos), *cl1);
+ defs->insert(cl2);
DELETE(info[varphi]->clausespos);
DELETE(cl1);
delete cl2;
ClauseList* cl1;
cl1 = SINGLETON(psi);
- ClauseList* cl2 = PRODUCT(*(info[varphi]->clausesneg), *cl1);
- defs->insert(defs->end(), cl2->begin(), cl2->end());
+ ClauseList* cl2 = ClauseList::PRODUCT(*(info[varphi]->clausesneg), *cl1);
+ defs->insert(cl2);
DELETE(info[varphi]->clausesneg);
DELETE(cl1);
delete cl2;
ClauseList* defs)
{
convertFormulaToCNF(varphi[0], defs);
- info[varphi]->clausespos = COPY(*(info[varphi[0]]->clausesneg));
+ info[varphi]->clausespos = ClauseList::COPY(*(info[varphi[0]]->clausesneg));
reduceMemoryFootprintNeg(varphi[0]);
} //End of convertFormulaToCNFPosNOT()
ASTVec::const_iterator it = varphi.GetChildren().begin();
convertFormulaToCNF(*it, defs);
- ClauseList* psi = COPY(*(info[*it]->clausespos));
+ ClauseList* psi = ClauseList::COPY(*(info[*it]->clausespos));
for (it++; it != varphi.GetChildren().end(); it++) {
convertFormulaToCNF(*it, defs);
CNFInfo* x = info[*it];
if (sharesPos(*x) == 1) {
- psi->insert(psi->end(),x->clausespos->begin(), x->clausespos->end());
+ psi->insert(x->clausespos);
delete (x->clausespos);
x->clausespos = NULL;
if (x->clausesneg == NULL) {
info.erase(*it);
}
} else {
- INPLACE_UNION(psi, *(x->clausespos));
+ ClauseList::INPLACE_UNION(psi, *(x->clausespos));
reduceMemoryFootprintPos(*it);
}
}
{
renamesibs = true;
}
- psi = COPY(*clauses);
+ psi = ClauseList::COPY(*clauses);
reduceMemoryFootprintNeg(*it);
for (it++; it != varphi.GetChildren().end(); it++)
renamesibs = true;
}
oldpsi = psi;
- psi = PRODUCT(*psi, *clauses);
+ psi = ClauseList::PRODUCT(*psi, *clauses);
reduceMemoryFootprintNeg(*it);
DELETE(oldpsi);
}
{
renamesibs = true;
}
- psi = COPY(*clauses);
+ psi = ClauseList::COPY(*clauses);
reduceMemoryFootprintPos(*it);
for (it++; it != varphi.GetChildren().end(); it++)
renamesibs = true;
}
oldpsi = psi;
- psi = PRODUCT(*psi, *clauses);
+ psi = ClauseList::PRODUCT(*psi, *clauses);
reduceMemoryFootprintPos(*it);
DELETE(oldpsi);
}
//****************************************
ASTVec::const_iterator it = varphi.GetChildren().begin();
convertFormulaToCNF(*it, defs);
- ClauseList* psi = COPY(*(info[*it]->clausesneg));
+ ClauseList* psi = ClauseList::COPY(*(info[*it]->clausesneg));
reduceMemoryFootprintNeg(*it);
for (it++; it != varphi.GetChildren().end(); it++)
{
convertFormulaToCNF(*it, defs);
- INPLACE_UNION(psi, *(info[*it]->clausesneg));
+ ClauseList::INPLACE_UNION(psi, *(info[*it]->clausesneg));
reduceMemoryFootprintNeg(*it);
}
setDoSibRenamingPos(*x1);
}
convertFormulaToCNF(varphi[1], defs);
- ClauseList* psi = PRODUCT(*(x0->clausesneg), *(x1->clausespos));
+ ClauseList* psi = ClauseList::PRODUCT(*(x0->clausesneg), *(x1->clausespos));
reduceMemoryFootprintNeg(varphi[0]);
reduceMemoryFootprintPos(varphi[1]);
info[varphi]->clausespos = psi;
setDoSibRenamingPos(*x2);
}
convertFormulaToCNF(varphi[2], defs);
- ClauseList* psi1 = PRODUCT(*(x0->clausesneg), *(x1->clausespos));
- ClauseList* psi2 = PRODUCT(*(x0->clausespos), *(x2->clausespos));
- NOCOPY_INPLACE_UNION(psi1, psi2);
+ ClauseList* psi1 = ClauseList::PRODUCT(*(x0->clausesneg), *(x1->clausespos));
+ ClauseList* psi2 = ClauseList::PRODUCT(*(x0->clausespos), *(x2->clausespos));
+ ClauseList::NOCOPY_INPLACE_UNION(psi1, psi2);
reduceMemoryFootprintNeg(varphi[0]);
reduceMemoryFootprintPos(varphi[1]);
reduceMemoryFootprintPos(varphi[0]);
}
psi1 =
- PRODUCT(*(info[varphi[idx]]->clausespos),
+ ClauseList::PRODUCT(*(info[varphi[idx]]->clausespos),
*(info[varphi[idx + 1]]->clausespos));
psi2 =
- PRODUCT(*(info[varphi[idx]]->clausesneg),
+ ClauseList::PRODUCT(*(info[varphi[idx]]->clausesneg),
*(info[varphi[idx + 1]]->clausesneg));
- NOCOPY_INPLACE_UNION(psi1, psi2);
+ ClauseList::NOCOPY_INPLACE_UNION(psi1, psi2);
psi = psi1;
}
setDoSibRenamingNeg(*info[varphi[idx]]);
}
- psi1 = PRODUCT(*(info[varphi[idx]]->clausespos), *theta1);
- psi2 = PRODUCT(*(info[varphi[idx]]->clausesneg), *theta2);
+ psi1 = ClauseList::PRODUCT(*(info[varphi[idx]]->clausespos), *theta1);
+ psi2 = ClauseList::PRODUCT(*(info[varphi[idx]]->clausesneg), *theta2);
DELETE(theta1);
DELETE(theta2);
- NOCOPY_INPLACE_UNION(psi1, psi2);
+ ClauseList::NOCOPY_INPLACE_UNION(psi1, psi2);
psi = psi1;
}
ClauseList* defs)
{
convertFormulaToCNF(varphi[0], defs);
- info[varphi]->clausesneg = COPY(*(info[varphi[0]]->clausespos));
+ info[varphi]->clausesneg = ClauseList::COPY(*(info[varphi[0]]->clausespos));
reduceMemoryFootprintPos(varphi[0]);
} //End of convertFormulaToCNFNegNOT()
{
renamesibs = true;
}
- psi = COPY(*clauses);
+ psi = ClauseList::COPY(*clauses);
reduceMemoryFootprintNeg(*it);
for (it++; it != varphi.GetChildren().end(); it++)
renamesibs = true;
}
oldpsi = psi;
- psi = PRODUCT(*psi, *clauses);
+ psi = ClauseList::PRODUCT(*psi, *clauses);
reduceMemoryFootprintNeg(*it);
DELETE(oldpsi);
}
//****************************************
ASTVec::const_iterator it = varphi.GetChildren().begin();
convertFormulaToCNF(*it, defs);
- ClauseList* psi = COPY(*(info[*it]->clausespos));
+ ClauseList* psi = ClauseList::COPY(*(info[*it]->clausespos));
reduceMemoryFootprintPos(*it);
for (it++; it != varphi.GetChildren().end(); it++)
{
convertFormulaToCNF(*it, defs);
- INPLACE_UNION(psi, *(info[*it]->clausespos));
+ ClauseList::INPLACE_UNION(psi, *(info[*it]->clausespos));
reduceMemoryFootprintPos(*it);
}
//****************************************
ASTVec::const_iterator it = varphi.GetChildren().begin();
convertFormulaToCNF(*it, defs);
- ClauseList* psi = COPY(*(info[*it]->clausesneg));
+ ClauseList* psi = ClauseList::COPY(*(info[*it]->clausesneg));
reduceMemoryFootprintNeg(*it);
for (it++; it != varphi.GetChildren().end(); it++) {
convertFormulaToCNF(*it, defs);
CNFInfo* x = info[*it];
if (sharesNeg(*x) != 1) {
- INPLACE_UNION(psi, *(x->clausesneg));
+ ClauseList::INPLACE_UNION(psi, *(x->clausesneg));
reduceMemoryFootprintNeg(*it);
} else {
// If this is the only use of "clausesneg", no reason to make a copy.
- psi->insert(psi->end(), x->clausesneg->begin(),
- x->clausesneg->end());
+ psi->insert(x->clausesneg);
// Copied from reduceMemoryFootprintNeg
delete x->clausesneg;
x->clausesneg = NULL;
{
renamesibs = true;
}
- psi = COPY(*clauses);
+ psi = ClauseList::COPY(*clauses);
reduceMemoryFootprintPos(*it);
for (it++; it != varphi.GetChildren().end(); it++)
renamesibs = true;
}
oldpsi = psi;
- psi = PRODUCT(*psi, *clauses);
+ psi = ClauseList::PRODUCT(*psi, *clauses);
reduceMemoryFootprintPos(*it);
DELETE(oldpsi);
}
CNFInfo* x1 = info[varphi[1]];
convertFormulaToCNF(varphi[0], defs);
convertFormulaToCNF(varphi[1], defs);
- ClauseList* psi = UNION(*(x0->clausespos), *(x1->clausesneg));
+ ClauseList* psi = ClauseList::UNION(*(x0->clausespos), *(x1->clausesneg));
info[varphi]->clausesneg = psi;
reduceMemoryFootprintPos(varphi[0]);
reduceMemoryFootprintNeg(varphi[1]);
setDoSibRenamingNeg(*x2);
}
convertFormulaToCNF(varphi[2], defs);
- ClauseList* psi1 = PRODUCT(*(x0->clausesneg), *(x1->clausesneg));
- ClauseList* psi2 = PRODUCT(*(x0->clausespos), *(x2->clausesneg));
- NOCOPY_INPLACE_UNION(psi1, psi2);
+ ClauseList* psi1 = ClauseList::PRODUCT(*(x0->clausesneg), *(x1->clausesneg));
+ ClauseList* psi2 = ClauseList::PRODUCT(*(x0->clausespos), *(x2->clausesneg));
+ ClauseList::NOCOPY_INPLACE_UNION(psi1, psi2);
reduceMemoryFootprintNeg(varphi[0]);
reduceMemoryFootprintNeg(varphi[1]);
reduceMemoryFootprintPos(varphi[0]);
}
psi1 =
- PRODUCT(*(info[varphi[idx]]->clausesneg),
+ ClauseList::PRODUCT(*(info[varphi[idx]]->clausesneg),
*(info[varphi[idx + 1]]->clausespos));
psi2 =
- PRODUCT(*(info[varphi[idx]]->clausespos),
+ ClauseList::PRODUCT(*(info[varphi[idx]]->clausespos),
*(info[varphi[idx + 1]]->clausesneg));
- NOCOPY_INPLACE_UNION(psi1, psi2);
+ ClauseList::NOCOPY_INPLACE_UNION(psi1, psi2);
psi = psi1;
}
setDoSibRenamingPos(*info[varphi[idx]]);
}
- psi1 = PRODUCT(*(info[varphi[idx]]->clausesneg), *theta1);
- psi2 = PRODUCT(*(info[varphi[idx]]->clausespos), *theta2);
+ psi1 = ClauseList::PRODUCT(*(info[varphi[idx]]->clausesneg), *theta1);
+ psi2 = ClauseList::PRODUCT(*(info[varphi[idx]]->clausespos), *theta2);
DELETE(theta1);
DELETE(theta2);
- NOCOPY_INPLACE_UNION(psi1, psi2);
+ ClauseList::NOCOPY_INPLACE_UNION(psi1, psi2);
psi = psi1;
}
ClauseList* defs = SINGLETON(dummy_true_var);
convertFormulaToCNF(varphi, defs);
ClauseList* top = info[varphi]->clausespos;
- defs->insert(defs->begin() + 1, top->begin(), top->end());
+ defs->insertAtFront(top);
cleanup(varphi);
bm->GetRunTimes()->stop(RunTimes::CNFConversion);
return clausesxor;
}
+ //
void CNFMgr::DELETE(ClauseList* varphi)
{
- DeleteClauseList(*varphi);
- delete varphi;
+ varphi->deleteJustVectors();
+ delete varphi;
+ varphi = NULL;
} //End of DELETE()
+ /*
void CNFMgr::PrintClauseList(ostream& os, ClauseList& cll)
{
int num_clauses = cll.size();
os << endl
<< "-------------------------------------------" << endl;
}
- } //end of PrintClauseList()
+ } //end of PrintClauseList()
+ */
} // end namespace BEEV