formula.cpp
Go to the documentation of this file.
00001 
00007 #include <vector>
00008 #include <iostream>
00009 #include <fstream>
00010 #include <stdlib.h>
00011 
00012 #include "formula.h"
00013 
00014 using namespace cnf;
00015 
00016 
00017 Formula::Formula(VariableSet * S,std::ostream * out,
00018                  std::string comment)
00019 {
00020         varSet = S;
00021         // initializing output
00022         output = out;
00023         (*output) << "c " << comment << std::endl
00024                << "p cnf " << varSet->getCard()
00025                << " 1 " << std::endl;
00026 }
00027 
00028 
00029 Formula::~Formula()
00030 {
00031         flushCNF();
00032 }
00033 
00034 
00035 void Formula::newClause()
00036 {
00037         bufferClause.clear();
00038 }
00039 
00040 
00041 void Formula::addLit(unsigned int s, bool sign, unsigned int i, unsigned int j)
00042 {
00043         if (sign)
00044                 bufferClause.addLit((int)varSet->getVarCode(s,i,j));
00045         else
00046                 bufferClause.addLit((-1)*(int)varSet->getVarCode(s,i,j));
00047 }
00048 
00049 
00050 void Formula::addLit(unsigned int s, bool sign, unsigned int i, unsigned int j, unsigned int k)
00051 {
00052         if (sign)
00053                 bufferClause.addLit((int)varSet->getVarCode(s,i,j,k));
00054         else
00055                 bufferClause.addLit((-1)*(int)varSet->getVarCode(s,i,j,k));
00056 }
00057 
00058 
00059 void Formula::addLitCode(int c)
00060 {
00061         if (
00062                 ((c < 0)  &&  ((-1)*c > (int)varSet->getCard()))
00063                 || ((c >= 0)  &&  (c > (int)varSet->getCard()))
00064                 )
00065         {
00066                 std::cout<<"ERROR in Formula::addLitCode(), the code is"
00067                         " too big: c="<<c<<" is greater than the varSet"
00068                         " size, "<<varSet->getCard()<<std::endl;
00069                 exit(1);
00070         }
00071         else
00072                 bufferClause.addLit(c);
00073 }
00074 
00075 
00076 void Formula::pushClause()
00077 {
00078         cnfs.push_back(bufferClause);
00079         if (cnfs.size() > CLAUSE_BUFFER_SIZE)
00080                 flushCNF();
00081 }
00082 
00083 
00084 void Formula::flushCNF()
00085 {
00086         for (unsigned int i=0; i<cnfs.size(); i++)
00087                 (*output) << cnfs[i].toString() << "\n";
00088         output->flush();
00089         cnfs.clear();
00090 }
 All Classes Namespaces Files Functions Variables Defines