Represents a mathematical variable set; is used to associate a unique code to a given number of literals described intuitively by 2 or 3 indices. More...
#include <variableset.h>
Public Member Functions | |
VariableSet (unsigned int, unsigned int) | |
Constructor for 2D literals (x_ij). | |
VariableSet (unsigned int, unsigned int, unsigned int) | |
Constructor for 3D literals (x_ijk). | |
void | addVars (unsigned int, unsigned int) |
Adds a new subset of variables to this instance. | |
void | addVars (unsigned int, unsigned int, unsigned int) |
Adds a new subset of variables to this instance. | |
unsigned int | getVarCode (unsigned int, unsigned int, unsigned int) |
Returns the code of the 2D variable designed by the coordinates given as argument in the l_th subset. | |
unsigned int | getVarCode (unsigned int, unsigned int, unsigned int, unsigned int) |
Returns the code of the 3D variable designed by the coordinates given as argument. | |
bool | getVarValue (unsigned int, unsigned int, unsigned int) |
Returns the value of the variable described by the given 2D coordinates. | |
bool | getVarValue (unsigned int, unsigned int, unsigned int, unsigned int) |
Returns the value of the variable described by the given 3D coordinates. | |
unsigned int | getCard () |
Returns the total number of variables in this set. | |
void | parseDimacs (std::istream *) |
Parses a dimacs results formatted stream and assigns its variables accordingly. | |
Private Attributes | |
std::vector< std::vector < unsigned int > > | dim |
The dimensions of the matrices to which the variables correspond. Each std::vector< unsigned int> corresponds to one subset of variables. | |
std::vector< unsigned int > | sizes |
Contains the length of the std::vector used by each variables subset. It allows a faster computation of the index in varValue of a given literal from its coordinates. | |
std::vector< bool > | varValue |
Contains the value of each variable in the set; is initialized so as to contain only "false". |
Represents a mathematical variable set; is used to associate a unique code to a given number of literals described intuitively by 2 or 3 indices.
In order for formula to be written in DIMACS files and to be manipulated by Clause and Formula instance, they must have a unique integer valued code. This class ensures the corresponds between the code and the coordinates of a variable.
Definition at line 25 of file variableset.h.
VariableSet::VariableSet | ( | unsigned int | i, |
unsigned int | j | ||
) |
Constructor for 2D literals (x_ij).
Initializes the VariableSet instance by creating a varValue std::vector attribute of the correct size and filling it with zeros.
i | First dimension describing the literals. |
j | The second dimension. |
Definition at line 18 of file variableset.cpp.
{ addVars(i,j); }
VariableSet::VariableSet | ( | unsigned int | i, |
unsigned int | j, | ||
unsigned int | k | ||
) |
Constructor for 3D literals (x_ijk).
Initializes the VariableSet instance by creating a varValue std::vector attribute of the correct size and filling it with zeros.
i | First dimension describing the literals. |
j | The second dimension. |
k | The third one. |
Definition at line 24 of file variableset.cpp.
{ addVars(i,j,k); }
void VariableSet::addVars | ( | unsigned int | i, |
unsigned int | j | ||
) |
Adds a new subset of variables to this instance.
Their coordinates are indepedents from any other subset.
i | First coordinate. |
j | First coordinate. |
Definition at line 30 of file variableset.cpp.
void VariableSet::addVars | ( | unsigned int | i, |
unsigned int | j, | ||
unsigned int | k | ||
) |
Adds a new subset of variables to this instance.
Their coordinates are indepedents from any other subset.
i | First coordinate. |
j | First coordinate. |
k | First coordinate. |
Definition at line 43 of file variableset.cpp.
unsigned int VariableSet::getCard | ( | ) |
Returns the total number of variables in this set.
Definition at line 157 of file variableset.cpp.
unsigned int VariableSet::getVarCode | ( | unsigned int | s, |
unsigned int | i, | ||
unsigned int | j | ||
) |
Returns the code of the 2D variable designed by the coordinates given as argument in the l_th subset.
s | subset index |
i | First coordinate. |
j | Second coordinate. |
Definition at line 57 of file variableset.cpp.
{ if (s > sizes.size()) { std::cout<<"error in VariableSet: wrong subset index"<<" (" <<s<<">="<<sizes.size()<<")"<< std::endl; exit(100); } else if( (i>=dim[s][0]) || (j>=dim[s][1]) ) { std::cout<<"error in VariableSet: indices out of bound!"<<" (" <<i<<">="<<dim[s][0]<<" or" <<j<<">="<<dim[s][1]<<")" << std::endl; exit(100); } else { unsigned int varCode=0; for (unsigned int subset=0; subset<s; subset++) varCode += sizes[subset]; varCode += i + (dim[s][0])*j; return varCode +1; } }
unsigned int VariableSet::getVarCode | ( | unsigned int | s, |
unsigned int | i, | ||
unsigned int | j, | ||
unsigned int | k | ||
) |
Returns the code of the 3D variable designed by the coordinates given as argument.
s | subset index |
i | First coordinate. |
j | Second coordinate. |
k | Third one. |
Definition at line 84 of file variableset.cpp.
{ if (s > sizes.size()) { std::cout<<"error in VariableSet: wrong subset index"<<" (" <<s<<">="<<sizes.size()<<")"<< std::endl; exit(100); } else if ( (i>=dim[s][0]) || (j>=dim[s][1]) || (k>=dim[s][2]) ) { std::cout<<"error in VariableSet: indices out of bound!"<<" (" <<i<<">="<<dim[s][0]<<" or" <<j<<">="<<dim[s][1]<<" or" <<k<<">="<<dim[s][2]<<")"<< std::endl; exit(100); } else { unsigned int varCode=0; for (unsigned int subset=0; subset<s; subset++) varCode += sizes[subset]; varCode += i + (dim[s][0])*(j + dim[s][1]*k); // std::cout<<"("<<i<<","<<j<<","<<k<<" => "<<varCode+1<<")@t"; return varCode+1; } }
bool VariableSet::getVarValue | ( | unsigned int | s, |
unsigned int | i, | ||
unsigned int | j | ||
) |
Returns the value of the variable described by the given 2D coordinates.
s | subset index |
i | First coordinate. |
j | Second coordinate. |
Definition at line 115 of file variableset.cpp.
{ return varValue[getVarCode(s,i,j) -1]; }
bool VariableSet::getVarValue | ( | unsigned int | s, |
unsigned int | i, | ||
unsigned int | j, | ||
unsigned int | k | ||
) |
Returns the value of the variable described by the given 3D coordinates.
s | subset index |
i | First coordinate. |
j | Second coordinate. |
k | Third one. |
Definition at line 121 of file variableset.cpp.
{ return varValue[getVarCode(s,i,j,k) -1]; }
void VariableSet::parseDimacs | ( | std::istream * | input | ) |
Parses a dimacs results formatted stream and assigns its variables accordingly.
Any previous assignment is erased, in particular that of the construction.
input | The input stream to the file to parse. |
Definition at line 127 of file variableset.cpp.
{ if (!(*input)) { std::cout<<"error 100: No such file!"<<std::endl; exit(100); } else { std::string res; (*input) >> res; if (res.compare("SAT") != 0) { std::cout<<"error 101: The file does not correspond " <<"to a satisfiable formula."<<std::endl; exit(101); } else { int i=1; while ( ((*input) >> i) && (i!=0) ) if (i>0) varValue[i-1] = true; else varValue[-i+1] = false; } } }
std::vector< std::vector<unsigned int> > cnf::VariableSet::dim [private] |
The dimensions of the matrices to which the variables correspond. Each std::vector< unsigned int> corresponds to one subset of variables.
There can be as many dimensions as necessary in each subset. For instance, if "x" variables are in a 3x3 matrix and "y" variables in a 2x4 one, then it should hold that dim = [ [3,3], [2,4] ].
Definition at line 38 of file variableset.h.
std::vector<unsigned int> cnf::VariableSet::sizes [private] |
Contains the length of the std::vector used by each variables subset. It allows a faster computation of the index in varValue of a given literal from its coordinates.
Definition at line 46 of file variableset.h.
std::vector<bool> cnf::VariableSet::varValue [private] |
Contains the value of each variable in the set; is initialized so as to contain only "false".
Definition at line 52 of file variableset.h.