1 /***********************************************************************************[SolverTypes.h]
2 MiniSat -- Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
4 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5 associated documentation files (the "Software"), to deal in the Software without restriction,
6 including without limitation the rights to use, copy, modify, merge, publish, distribute,
7 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8 furnished to do so, subject to the following conditions:
10 The above copyright notice and this permission notice shall be included in all copies or
11 substantial portions of the Software.
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
17 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18 **************************************************************************************************/
20 #ifndef CVC4_MiniSat_SolverTypes_h
21 #define CVC4_MiniSat_SolverTypes_h
29 //=================================================================================================
30 // Variables, literals, lifted booleans, clauses:
33 // NOTE! Variables are just integers. No abstraction here. They should be chosen from 0..N,
34 // so that they can be used as array indices.
37 #define var_Undef (-1)
43 Lit() : x(2*var_Undef
) { } // (lit_Undef)
44 explicit Lit(Var var
, bool sign
= false) : x((var
+var
) + (int)sign
) { }
46 // Don't use these for constructing/deconstructing literals. Use the normal constructors instead.
47 friend int toInt (Lit p
); // Guarantees small, positive integers suitable for array indexing.
48 friend Lit
toLit (int i
); // Inverse of 'toInt()'
49 friend Lit
operator ~(Lit p
);
50 friend bool sign (Lit p
);
51 friend int var (Lit p
);
52 friend Lit
unsign (Lit p
);
53 friend Lit
id (Lit p
, bool sgn
);
55 bool operator == (Lit p
) const { return x
== p
.x
; }
56 bool operator != (Lit p
) const { return x
!= p
.x
; }
57 bool operator < (Lit p
) const { return x
< p
.x
; } // '<' guarantees that p, ~p are adjacent in the ordering.
60 inline int toInt (Lit p
) { return p
.x
; }
61 inline Lit
toLit (int i
) { Lit p
; p
.x
= i
; return p
; }
62 inline Lit
operator ~(Lit p
) { Lit q
; q
.x
= p
.x
^ 1; return q
; }
63 inline bool sign (Lit p
) { return p
.x
& 1; }
64 inline int var (Lit p
) { return p
.x
>> 1; }
65 inline Lit
unsign (Lit p
) { Lit q
; q
.x
= p
.x
& ~1; return q
; }
66 inline Lit
id (Lit p
, bool sgn
) { Lit q
; q
.x
= p
.x
^ (int)sgn
; return q
; }
68 const Lit
lit_Undef(var_Undef
, false); // }- Useful special constants.
69 const Lit
lit_Error(var_Undef
, true ); // }
72 //=================================================================================================
78 explicit lbool(int v
) : value(v
) { }
81 lbool() : value(0) { }
82 lbool(bool x
) : value((int)x
*2-1) { }
83 int toInt(void) const { return value
; }
85 bool operator == (lbool b
) const { return value
== b
.value
; }
86 bool operator != (lbool b
) const { return value
!= b
.value
; }
87 lbool
operator ^ (bool b
) const { return b
? lbool(-value
) : lbool(value
); }
89 friend int toInt (lbool l
);
90 friend lbool
toLbool(int v
);
92 inline int toInt (lbool l
) { return l
.toInt(); }
93 inline lbool
toLbool(int v
) { return lbool(v
); }
95 const lbool l_True
= toLbool( 1);
96 const lbool l_False
= toLbool(-1);
97 const lbool l_Undef
= toLbool( 0);
99 //=================================================================================================
100 // Clause -- a simple class for representing a clause:
105 union { float act
; uint32_t abst
; } extra
;
109 void calcAbstraction() {
110 uint32_t abstraction
= 0;
111 for (int i
= 0; i
< size(); i
++)
112 abstraction
|= 1 << (var(data
[i
]) & 31);
113 extra
.abst
= abstraction
; }
115 // NOTE: This constructor cannot be used directly (doesn't allocate enough memory).
117 Clause(const V
& ps
, bool learnt
) {
118 size_etc
= (ps
.size() << 3) | (uint32_t)learnt
;
119 for (int i
= 0; i
< ps
.size(); i
++) data
[i
] = ps
[i
];
120 if (learnt
) extra
.act
= 0; else calcAbstraction(); }
122 // -- use this function instead:
124 friend Clause
* Clause_new(const V
& ps
, bool learnt
= false) {
125 assert(sizeof(Lit
) == sizeof(uint32_t));
126 assert(sizeof(float) == sizeof(uint32_t));
127 void* mem
= malloc(sizeof(Clause
) + sizeof(uint32_t)*(ps
.size()));
128 return new (mem
) Clause(ps
, learnt
); }
130 int size () const { return size_etc
>> 3; }
131 void shrink (int i
) { assert(i
<= size()); size_etc
= (((size_etc
>> 3) - i
) << 3) | (size_etc
& 7); }
132 void pop () { shrink(1); }
133 bool learnt () const { return size_etc
& 1; }
134 uint32_t mark () const { return (size_etc
>> 1) & 3; }
135 void mark (uint32_t m
) { size_etc
= (size_etc
& ~6) | ((m
& 3) << 1); }
136 const Lit
& last () const { return data
[size()-1]; }
138 // NOTE: somewhat unsafe to change the clause in-place! Must manually call 'calcAbstraction' afterwards for
139 // subsumption operations to behave correctly.
140 Lit
& operator [] (int i
) { return data
[i
]; }
141 Lit
operator [] (int i
) const { return data
[i
]; }
142 operator const Lit
* (void) const { return data
; }
144 float& activity () { return extra
.act
; }
145 uint32_t abstraction () const { return extra
.abst
; }
147 Lit
subsumes (const Clause
& other
) const;
148 void strengthen (Lit p
);
152 /*_________________________________________________________________________________________________
154 | subsumes : (other : const Clause&) -> Lit
157 | Checks if clause subsumes 'other', and at the same time, if it can be used to simplify 'other'
158 | by subsumption resolution.
161 | lit_Error - No subsumption or simplification
162 | lit_Undef - Clause subsumes 'other'
163 | p - The literal p can be deleted from 'other'
164 |________________________________________________________________________________________________@*/
165 inline Lit
Clause::subsumes(const Clause
& other
) const
167 if (other
.size() < size() || (extra
.abst
& ~other
.extra
.abst
) != 0)
171 const Lit
* c
= (const Lit
*)(*this);
172 const Lit
* d
= (const Lit
*)other
;
174 for (int i
= 0; i
< size(); i
++) {
175 // search for c[i] or ~c[i]
176 for (int j
= 0; j
< other
.size(); j
++)
179 else if (ret
== lit_Undef
&& c
[i
] == ~d
[j
]){
193 inline void Clause::strengthen(Lit p
)
199 }/* CVC4::MiniSat namespace */
200 }/* CVC4 namespace */
202 #endif /* CVC4_MiniSat_SolverTypes_h */