source: vis_dev/sharpSAT/src/src_sharpSAT/MainSolver/InstanceGraph/ComponentTypes.h @ 9

Last change on this file since 9 was 9, checked in by cecile, 13 years ago

add sharpSat

File size: 8.7 KB
Line 
1#ifndef COMPONENTTYPES_H
2#define COMPONENTTYPES_H
3
4#ifdef DEBUG
5#include <assert.h>
6#endif
7
8
9#include <vector>
10#include <math.h>
11
12#include <RealNumberTypes.h>
13
14#include "AtomsAndNodes.h"
15
16using namespace std;
17
18//
19//  the identifier of the components
20//  identifier for (truly) binary clauses are not stored, therefore we need
21//  trueClauseCount to kepp track of the number of binary clauses still active in the
22//  component, otherwise if theClauses is empty one might suspect that the whole component is
23//  empty which would not be true if there where binary clauses in the original formula
24//
25class CComponentId
26{ 
27  /// INVARIANT: content: vvvvv varsSENTINEL
28  vector<VarIdT> theVars;   
29  /// INVARIANT: content: cccccc clsSENTINEL
30  vector<ClauseIdT> theClauses;                   
31 
32  unsigned int trueClauseCount; 
33 
34  long unsigned int hashKeyVars;
35  long unsigned int hashKeyCls;
36 
37 public:
38 
39  /// INVARIANT:
40  ///  cachedChildren may be nonempty only if cachedAs == NIL_ENTRY
41  ///  if cachedAS != NIL_ENTRY then this component has been cached and
42  ///  its descendants can be found in xFormulaCache.entry(cachedAs).theDescendants
43  unsigned int cachedAs;
44  vector<unsigned int> cachedChildren;
45 
46  void addCachedChildren(const vector<unsigned int> & cc)
47  {
48     cachedChildren.insert(cachedChildren.end(),cc.begin(),cc.end()); 
49  }
50 
51 
52  #define varsSENTINEL  0
53  #define clsSENTINEL   NOT_A_CLAUSE
54 
55  void reserveSpace(unsigned int nVars, unsigned int nCls)
56  {
57       theVars.reserve(nVars + 1);
58       theClauses.reserve(nCls + 1);
59  }
60 
61  inline void addCl(const ClauseIdT & cl)
62  { 
63     theClauses.push_back(cl); 
64     
65     //Compute HashKey   
66     if(cl != clsSENTINEL) hashKeyCls = hashKeyCls*7 + cl;       
67  }
68 
69  inline void addVar(const VarIdT & var)
70  { 
71     theVars.push_back(var);     
72     
73     //Compute HashKey
74     if(var != varsSENTINEL) hashKeyVars = hashKeyVars*5 + var;
75     
76  }
77 
78  long unsigned int getHashKey() const
79  {
80    return hashKeyVars + (hashKeyCls<<9);
81  }
82 
83 
84  void clear()
85  {
86   theVars.clear(); 
87   theClauses.clear(); 
88   trueClauseCount = 0;
89   cachedAs = 0;
90  }
91 
92  CComponentId()
93  {   
94    clear();
95    trueClauseCount = 0;
96    cachedAs = 0;
97    hashKeyVars = 0;
98    hashKeyCls = 0;
99  }
100
101 
102  vector<VarIdT>::iterator varsBegin() 
103  {
104     return theVars.begin();
105  }
106 
107   
108  vector<ClauseIdT>::iterator clsBegin() 
109  {
110     return theClauses.begin();
111  }
112 
113  inline vector<VarIdT>::const_iterator  varsBegin()  const
114  {
115     return theVars.begin();
116  }
117 
118  inline vector<ClauseIdT>::const_iterator  clsBegin()  const
119  {
120     return theClauses.begin();
121  }
122 
123 
124  unsigned int countVars()  const
125  {
126     #ifdef DEBUG
127     assert(theVars.size() >= 1);
128     #endif
129     return theVars.size() - 1;
130  }
131 
132  unsigned int countCls() const
133  {
134     #ifdef DEBUG
135     assert(theClauses.size() >= 1);
136     #endif
137     return theClauses.size() - 1;
138  }
139 
140  bool empty() const
141  {
142    return theVars.empty();// || theClauses.empty();
143  }
144 
145  int memSize() const
146  {
147     return theVars.capacity()*sizeof(VarIdT) +
148            theClauses.capacity()*sizeof(ClauseIdT);
149  }
150 
151  void setTrueClauseCount(unsigned int count)
152  {
153     trueClauseCount = count;   
154  }
155 
156  unsigned int getClauseCount()
157  {
158    #ifdef DEBUG
159    assert(trueClauseCount >= countCls());
160    #endif   
161    return trueClauseCount;
162  }
163
164  bool containsBinCls() const
165  {
166    #ifdef DEBUG
167    assert(trueClauseCount >= countCls());
168    #endif
169    return (trueClauseCount > countCls());
170  }
171 
172};
173
174template <class _T, unsigned int _bitsPerBlock = (sizeof(_T)<<3)>
175class CPackedCompId
176{ 
177  static unsigned int bpeCls, bpeVars; // bitsperentry   
178  static unsigned int maskVars,maskCls; 
179protected: 
180  vector<_T> theVars;     
181  vector<_T> theClauses; 
182 
183 public:
184  static unsigned int bitsPerVar() { return bpeVars;}
185  static unsigned int bitsPerCl() { return bpeCls;}
186 
187  unsigned int sizeVarVec() {return theVars.size();}
188  unsigned int sizeClVec() {return theClauses.size();} 
189 
190  static void adjustPackSize(unsigned int maxVarId, unsigned int maxClId); 
191   
192  CPackedCompId()
193  {   
194  }
195 
196  void createFrom(const CComponentId &rComp); 
197 
198 
199  bool equals(const CPackedCompId<_T> &rComp) const
200  {
201    return theVars == rComp.theVars
202          && theClauses == rComp.theClauses;
203  }
204 
205  bool equals(const CComponentId &rComp) const;
206 
207  void clear()
208  {
209   theVars.clear();
210   theClauses.clear();
211  }
212 
213  bool empty() const
214  {
215    return theVars.empty() && theClauses.empty();
216  }
217 
218  int memSize() const
219  {
220     return (theVars.capacity() + theClauses.capacity())*sizeof(_T);
221  }
222
223};
224
225/////////////////////////////////////////////////////////////////////////////
226//BEGIN Implementation CPackedCompId
227/////////////////////////////////////////////////////////////////////////////
228
229template <class _T, unsigned int _bitsPerBlock>
230unsigned int CPackedCompId<_T,_bitsPerBlock>::bpeCls = 0;
231template <class _T, unsigned int _bitsPerBlock>
232unsigned int CPackedCompId<_T,_bitsPerBlock>::bpeVars = 0; // bitsperentry
233template <class _T, unsigned int _bitsPerBlock>
234unsigned int CPackedCompId<_T,_bitsPerBlock>::maskVars = 0;
235template <class _T, unsigned int _bitsPerBlock>
236unsigned int CPackedCompId<_T,_bitsPerBlock>::maskCls = 0; // bitsperentry
237
238template <class _T, unsigned int _bitsPerBlock>
239void CPackedCompId<_T,_bitsPerBlock>::adjustPackSize(unsigned int maxVarId, unsigned int maxClId)
240{
241  bpeVars = (unsigned int)ceil(log((double)maxVarId+1)/log(2.0)); 
242  bpeCls = (unsigned int)ceil(log((double)maxClId+1)/log(2.0)); 
243
244  maskVars = maskCls = 0;
245  for(unsigned int i=0; i < bpeVars;i++) maskVars = (maskVars<<1) +1;
246  for(unsigned int i=0; i < bpeCls;i++) maskCls = (maskCls<<1) +1; 
247} 
248
249template <class _T, unsigned int _bitsPerBlock>
250void CPackedCompId<_T,_bitsPerBlock>::createFrom(const CComponentId &rComp)
251{   
252    vector<VarIdT>::const_iterator it;
253    vector<ClauseIdT>::const_iterator jt;
254   
255    if(!theVars.empty()) theVars.clear();
256    if(!theClauses.empty()) theClauses.clear();   
257   
258    theVars.reserve( rComp.countVars()*bpeVars/_bitsPerBlock +1);   
259    theClauses.reserve(rComp.countCls()*bpeCls/_bitsPerBlock +1);
260   
261    unsigned int bitpos = 0;       
262    unsigned int h = 0;
263    _T * pBack; 
264   
265    pBack = &theVars.front();
266   
267    for(it = rComp.varsBegin(); *it != varsSENTINEL;it++)
268    {     
269      h |= ((*it)<< (bitpos));
270      bitpos+= bpeVars;     
271                 
272      if(bitpos >= _bitsPerBlock)
273      {       
274       bitpos -= _bitsPerBlock;     
275       #ifdef DEBUG
276       assert(theVars.size() < theVars.capacity());
277       #endif
278       theVars.push_back(h);
279       h = ((*it)>> (bpeVars - bitpos));     
280      }     
281    }
282    if(bitpos > 0) theVars.push_back(h);
283   
284    bitpos = 0;
285    h = 0;
286    pBack = &theClauses.front();
287    for(jt = rComp.clsBegin(); *jt != clsSENTINEL;jt++)
288    { 
289      h |= ((*jt)<< (bitpos));     
290      bitpos += bpeCls;     
291                 
292      if(bitpos >= _bitsPerBlock)
293      {       
294       bitpos -= _bitsPerBlock;         
295       #ifdef DEBUG
296       assert(theClauses.size() < theClauses.capacity());
297       #endif
298       theClauses.push_back(h);
299       h = ((*jt)>> (bpeCls - bitpos));     
300      }     
301    }
302    if(bitpos > 0) theClauses.push_back(h);
303} 
304
305
306template <class _T, unsigned int _bitsPerBlock>
307bool CPackedCompId<_T,_bitsPerBlock>::equals(const CComponentId &rComp) const
308{
309   
310   if( (theVars.capacity() !=  (uint) rComp.countVars()*bpeVars/_bitsPerBlock +1)
311     || theClauses.capacity() != (uint) rComp.countCls()*bpeCls/_bitsPerBlock +1) return false;
312   
313   unsigned int bitpos = 0;       
314   unsigned int h = 0;
315   const _T * pItA; 
316     
317   pItA = &theVars.front();
318   
319   for(vector<VarIdT>::const_iterator it = rComp.varsBegin(); *it != varsSENTINEL;it++)
320   {   
321      h = ((*pItA)>> (bitpos));
322      bitpos+= bpeVars;                       
323      if(bitpos >= _bitsPerBlock)
324      {       
325        bitpos -= _bitsPerBlock;     
326        pItA++;
327        h |= ((*pItA)<< (bpeVars - bitpos));     
328      }
329      if(*it != (maskVars & h)) return false;     
330   }   
331   
332   bitpos = 0;   
333   pItA = &theClauses.front();   
334   for(vector<ClauseIdT>::const_iterator jt = rComp.clsBegin(); *jt != clsSENTINEL;jt++)
335   { 
336      h = (*pItA)>> (bitpos);     
337      bitpos += bpeCls;     
338                 
339      if(bitpos >= _bitsPerBlock)
340      {       
341       bitpos -= _bitsPerBlock;         
342       pItA++;
343       h |= ((*pItA)<< (bpeCls - bitpos));     
344      }     
345      if(*jt != (maskCls & h)) return false;
346   }   
347   
348   return true;
349   
350     
351}
352
353/////////////////////////////////////////////////////////////////////////////
354//END Implementation CPackedCompId
355/////////////////////////////////////////////////////////////////////////////
356#endif
Note: See TracBrowser for help on using the repository browser.