clause.cpp
Go to the documentation of this file.
00001 
00007 #include <stdlib.h>
00008 #include <vector>
00009 #include <string>
00010 #include <sstream>
00011 #include <iostream>
00012 
00013 #include "clause.h"
00014 
00015 using namespace cnf;
00016 
00017 
00018 Clause::Clause()
00019 {
00020         literals.clear();
00021 }
00022 
00023 void Clause::addLit(int code)
00024 {
00025         literals.push_back(code);
00026 }
00027 
00028 std::string Clause::toString()
00029 {
00030         std::stringstream s(std::stringstream::out);
00031         for (unsigned int i=0; i<literals.size(); i++)
00032         {
00033                 s << literals[i] << " ";
00034         }
00035         std::string res = s.str() + "0";
00036         s.flush();
00037         return res;
00038 }
00039 
00040 bool Clause::restriction(std::vector<int> assign)
00041 {
00042         for (unsigned int i=0; i<assign.size(); i++)
00043                 for (unsigned int j=0; j<literals.size(); j++)
00044                         if (assign[i] == (-1)*literals[j])
00045                                 // case where the literal in the clause is negated in
00046                                 // the assignment: we remove it from the clause
00047                                 literals.erase(literals.begin()+j);
00048                         else if (assign[i] == literals[j])
00049                                 // case where the literal is in the assignment: the CNF
00050                                 // clause is thus true
00051                                 return 1;
00052         if (literals.size() == 0)
00053         {
00054                 // case where the empty clause was derived using the assignment
00055                 std::cout<< "Error 200: The restriction falsifies the formula,"
00056                          << " no solution." << std::endl;
00057                 exit(200);
00058         }
00059         return 0;
00060 }
 All Classes Namespaces Files Functions Variables Defines