minisat_types.h

Go to the documentation of this file.
00001 /*****************************************************************************/
00002 /*!
00003  *\file minisat_types.h
00004  *\brief MiniSat internal types
00005  *
00006  * Author: Alexander Fuchs
00007  *
00008  * Created: Fri Sep 08 11:04:00 2006
00009  *
00010  * <hr>
00011  *
00012  * License to use, copy, modify, sell and/or distribute this software
00013  * and its documentation for any purpose is hereby granted without
00014  * royalty, subject to the terms and conditions defined in the \ref
00015  * LICENSE file provided with this distribution.
00016  * 
00017  * <hr>
00018  */
00019 /*****************************************************************************/
00020 
00021 /***********************************************************************************[SolverTypes.h]
00022 MiniSat -- Copyright (c) 2003-2005, Niklas Een, Niklas Sorensson
00023 
00024 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
00025 associated documentation files (the "Software"), to deal in the Software without restriction,
00026 including without limitation the rights to use, copy, modify, merge, publish, distribute,
00027 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00028 furnished to do so, subject to the following conditions:
00029 
00030 The above copyright notice and this permission notice shall be included in all copies or
00031 substantial portions of the Software.
00032 
00033 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
00034 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00035 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00036 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
00037 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00038 **************************************************************************************************/
00039 
00040 #ifndef _cvc3__minisat__types_h_
00041 #define _cvc3__minisat__types_h_
00042 
00043 //=================================================================================================
00044 // Variables, literals, clause IDs:
00045 
00046 #include "minisat_global.h"
00047 #include <vector>
00048 
00049 namespace MiniSat {
00050 
00051   // NOTE! Variables are just integers. No abstraction here. They should be chosen from 0..N,
00052   // so that they can be used as array indices.
00053   // CVC additionally requires that N >= 2.
00054   typedef int Var;
00055   const int var_Undef = -1;
00056 
00057 
00058   class Lit {
00059     int     x;
00060 
00061     explicit Lit(int index) : x(index) {}   // (lit_Undef)
00062   public:
00063     Lit() : x(2*var_Undef) {}   // (lit_Undef)
00064     explicit Lit(Var var, bool sgn) : x((var+var) + (int)sgn) {}
00065     Lit operator~ () const { return Lit(x ^ 1); };
00066 
00067     bool sign  () const { return x & 1; };
00068     int  var   () const { return x >> 1; };
00069     int  index () const { return x; };
00070     static Lit  toLit (int i) { return Lit(i); };
00071     Lit  unsign() const { return Lit(x & ~1); };
00072     static Lit  id  (Lit p, bool sgn) { return Lit(p.x ^ (int)sgn); };
00073 
00074     bool operator == (const Lit q) const { return index() == q.index(); };
00075     bool operator != (const Lit q) const { return !(operator==(q)); };
00076     // '<' guarantees that p, ~p are adjacent in the ordering.;
00077     bool operator <  (const Lit q) const { return index() < q.index(); }
00078 
00079     uint hash() const { return (uint)x; }
00080 
00081     std::string toString() const {
00082       std::ostringstream buffer;
00083       if (sign())
00084   buffer << "+";
00085       else
00086   buffer << "-";
00087       buffer << var();
00088       return buffer.str();
00089     }
00090 
00091     int toDimacs() const { return sign() ? -var() - 1 : var() + 1; }
00092   };
00093 
00094   const Lit lit_Undef(var_Undef, false);  // }- Useful special constants.
00095   const Lit lit_Error(var_Undef, true );  // }
00096 
00097 
00098 
00099   // Clause -- a simple class for representing a clause:
00100   class Clause {
00101     uint    size_learnt;
00102     Lit     data[1];
00103 
00104     static Clause* s_decision;
00105     static Clause* s_theoryImplication;
00106   public:
00107     // NOTE: This constructor cannot be used directly (doesn't allocate enough memory).
00108     // actual layout is:
00109     // uint size_learnt : learnt in lowest bit, size * 2 in other bits
00110     // Lit data[size]
00111     // int id
00112     // int pushID (lemma only)
00113     // float activity (lemma only)
00114     //
00115     // using the hand-made allocator allows to allocate the data[]
00116     // like a static array within clause instead of as a pointer to the array.
00117     // this shows significant performance improvements
00118     Clause(bool learnt, const std::vector<Lit>& ps, int id_) {
00119         size_learnt = (ps.size() << 1) | (int)learnt;
00120         for (std::vector<Lit>::size_type i = 0; i < ps.size(); i++) data[i] = ps[i];
00121   id() = id_;
00122         if (learnt) activity() = 0;
00123     }
00124 
00125     // -- use this function instead:
00126     friend Clause* Clause_new(const std::vector<Lit>& ps, int id);
00127     friend Clause* Lemma_new(const std::vector<Lit>& ps, int id, int pushID);
00128 
00129     int       size        ()      const { return size_learnt >> 1; }
00130     bool      learnt      ()      const { return size_learnt & 1; }
00131     Lit       operator [] (int i) const { return data[i]; }
00132     Lit&      operator [] (int i)       { return data[i]; }
00133     // intended to be unique id per clause, > 0, or clauseIDNull
00134     int&      id          ()      const { return *((int*)&data[size()]); }
00135     
00136     // used with Solver::push/pop:
00137     // this is the highest id of all clauses used in the regression /
00138     // resolution / creation of this lemma
00139     int&      pushID      ()      const {
00140       if (learnt())
00141   return *((int*)&data[size() + 2]);
00142       else
00143   return id();
00144     }
00145     float&    activity    ()      const {
00146       DebugAssert(learnt(), "MiniSat::Types:activity: not a lemma");
00147       return *((float*)&data[size() + 1]);
00148     }
00149     void      toLit       (std::vector<Lit>& literals) const;
00150 
00151     static int ClauseIDNull() { return 0; }
00152 
00153     // special Clause, used to mark that an implication is a decision, id = -1.
00154     static Clause* Decision();
00155     // special Clause, used to mark that an implication is a theory implication
00156     // and that the explanation has not been retrieved yet, id = -2.
00157     static Clause* TheoryImplication();
00158 
00159     std::string toString() const {
00160       if (size() == 0) return "";
00161 
00162       std::ostringstream buffer;
00163       buffer << data[0].toString();
00164       for (int j = 1; j < size(); ++j) {
00165   buffer << " " << data[j].toString();
00166       }
00167       return buffer.str();
00168     }
00169   };
00170 
00171   Clause* Clause_new(const std::vector<Lit>& ps, int id);
00172   Clause* Lemma_new(const std::vector<Lit>& ps, int id, int pushID);
00173 
00174 }
00175 
00176 
00177 
00178 //=================================================================================================
00179 #endif

Generated on Tue Jul 3 14:33:38 2007 for CVC3 by  doxygen 1.5.1