通用的数据结构在文件夹mtl中可以查询

基本的数据结构在SolverTypes.h中定义

     1. Lit

 
 1 struct Lit {
 2     int     x;
 3 
 4     // Use this as a constructor:
 5     friend Lit mkLit(Var var, bool sign );
 6 
 7     bool operator == (Lit p) const { return x == p.x; }
 8     bool operator != (Lit p) const { return x != p.x; }
 9     bool operator <  (Lit p) const { return x < p.x;  } 
// '<' makes p, ~p adjacent in the ordering. 10 }; 11 12 inline Lit mkLit (Var var, bool sign= false) { Lit p; p.x = var + var + (int)sign; return p; } 13 inline Lit operator ~(Lit p) { Lit q; q.x = p.x ^ 1; return q; } 14 inline Lit operator ^(Lit p, bool b) { Lit q; q.x = p.x ^ (unsigned int)b; return q; } 15 inline bool sign (Lit p) { return p.x & 1; } 16 inline int var (Lit p) { return p.x >> 1; } 17 18 // Mapping Literals to and from compact integers suitable for array indexing: 19 inline int toInt (Var v) { return v; } 20 inline int toInt (Lit p) { return p.x; } 21 inline Lit toLit (int i) { Lit p; p.x = i; return p; } 22 23 //const Lit lit_Undef = mkLit(var_Undef, false); // }- Useful special constants. 24 //const Lit lit_Error = mkLit(var_Undef, true ); // } 25 26 const Lit lit_Undef = { -2 }; // }- Useful special constants. 27 const Lit lit_Error = { -1 }; // } 28 29 30 inline int toFormal(Lit p) { return sign(p) ? -var(p) - 1 : var(p) + 1; } 31 32 33 inline std::ostream& operator<<(std::ostream& out, const Lit& val) 34 { 35 out << (sign(val) ? -var(val) : var(val)) << std::flush; 36 return out; 37 }

 

 

2. Clause

 
  1 class Clause;
  2 typedef RegionAllocator<uint32_t>::Ref CRef;
  3 
  4 class Clause {
  5     struct {
  6         unsigned mark      : 2;
  7         unsigned learnt    : 1;
  8         unsigned has_extra : 1;
  9         unsigned reloced   : 1;
 10         unsigned lbd       : 26;
 11         unsigned removable : 1;
 12         unsigned size      : 32;
 13         //simplify
 14         unsigned simplified : 1;}                            header;
 15     union { Lit lit; float act; uint32_t abs; uint32_t touched; CRef rel; } data[0];
 16 
 17     friend class ClauseAllocator;
 18 
 19     // NOTE: This constructor cannot be used directly (doesn't allocate enough memory).
 20     template<class V>
 21     Clause(const V& ps, bool use_extra, bool learnt) {
 22         header.mark      = 0;
 23         header.learnt    = learnt;
 24         header.has_extra = learnt | use_extra;
 25         header.reloced   = 0;
 26         header.size      = ps.size();
 27         header.lbd       = 0;
 28         header.removable = 1;
 29         //simplify
 30         //
 31         header.simplified = 0;
 32 
 33         for (int i = 0; i < ps.size(); i++)
 34             data[i].lit = ps[i];
 35 
 36         if (header.has_extra){
 37             if (header.learnt){
 38                 data[header.size].act = 0;
 39                 data[header.size+1].touched = 0;
 40             }else
 41                 calcAbstraction(); }
 42     }
 43 
 44 public:
 45     void calcAbstraction() {
 46         assert(header.has_extra);
 47         uint32_t abstraction = 0;
 48         for (int i = 0; i < size(); i++)
 49             abstraction |= 1 << (var(data[i].lit) & 31);
 50         data[header.size].abs = abstraction;  }
 51 
 52 
 53     int          size        ()      const   { return header.size; }
 54     void         shrink      (int i)         { assert(i <= size()); 
if (header.has_extra) data[header.size-i] = data[header.size]; header.size -= i; } 55 void pop () { shrink(1); } 56 bool learnt () const { return header.learnt; } 57 bool has_extra () const { return header.has_extra; } 58 uint32_t mark () const { return header.mark; } 59 void mark (uint32_t m) { header.mark = m; } 60 const Lit& last () const { return data[header.size-1].lit; } 61 62 bool reloced () const { return header.reloced; } 63 CRef relocation () const { return data[0].rel; } 64 void relocate (CRef c) { header.reloced = 1; data[0].rel = c; } 65 66 int lbd () const { return header.lbd; } 67 void set_lbd (int lbd) { header.lbd = lbd; } 68 bool removable () const { return header.removable; } 69 void removable (bool b) { header.removable = b; } 70 71 // NOTE: somewhat unsafe to change the clause in-place!
//Must manually call 'calcAbstraction' afterwards for
72 // subsumption operations to behave correctly. 73 Lit& operator [] (int i) { return data[i].lit; } 74 Lit operator [] (int i) const { return data[i].lit; } 75 operator const Lit* (void) const { return (Lit*)data; } 76 77 uint32_t& touched () { assert(header.has_extra && header.learnt);
return data[header.size+1].touched; } 78 float& activity () { assert(header.has_extra); return data[header.size].act; } 79 uint32_t abstraction () const { assert(header.has_extra); return data[header.size].abs; } 80 81 Lit subsumes (const Clause& other) const; 82 void strengthen (Lit p); 83 // simplify 84 // 85 void setSimplified(bool b) { header.simplified = b; } 86 bool simplified() { return header.simplified; } 87 }; 88 89 90 //================================================================================================= 91 // ClauseAllocator -- a simple class for allocating memory for clauses: 92 93 94 const CRef CRef_Undef = RegionAllocator<uint32_t>::Ref_Undef; 95 class ClauseAllocator : public RegionAllocator<uint32_t> 96 { 97 static int clauseWord32Size(int size, int extras){ 98 return (sizeof(Clause) + (sizeof(Lit) * (size + extras))) / sizeof(uint32_t); } 99 public: 100 bool extra_clause_field; 101 102 ClauseAllocator(uint32_t start_cap) : RegionAllocator<uint32_t>(start_cap), extra_clause_field(false){} 103 ClauseAllocator() : extra_clause_field(false){} 104 105 void moveTo(ClauseAllocator& to){ 106 to.extra_clause_field = extra_clause_field; 107 RegionAllocator<uint32_t>::moveTo(to); } 108 109 template<class Lits> 110 CRef alloc(const Lits& ps, bool learnt = false) 111 { 112 assert(sizeof(Lit) == sizeof(uint32_t)); 113 assert(sizeof(float) == sizeof(uint32_t)); 114 int extras = learnt ? 2 : (int)extra_clause_field; 115 116 CRef cid = RegionAllocator<uint32_t>::alloc(clauseWord32Size(ps.size(), extras)); 117 new (lea(cid)) Clause(ps, extra_clause_field, learnt); 118 119 return cid; 120 } 121 122 // Deref, Load Effective Address (LEA), Inverse of LEA (AEL): 123 Clause& operator[](Ref r) { return (Clause&)RegionAllocator<uint32_t>::operator[](r); } 124 const Clause& operator[](Ref r) const { return (Clause&)RegionAllocator<uint32_t>::operator[](r); } 125 Clause* lea (Ref r) { return (Clause*)RegionAllocator<uint32_t>::lea(r); } 126 const Clause* lea (Ref r) const { return (Clause*)RegionAllocator<uint32_t>::lea(r); } 127 Ref ael (const Clause* t){ return RegionAllocator<uint32_t>::ael((uint32_t*)t); } 128 129 void free(CRef cid) 130 { 131 Clause& c = operator[](cid); 132 int extras = c.learnt() ? 2 : (int)c.has_extra(); 133 RegionAllocator<uint32_t>::free(clauseWord32Size(c.size(), extras)); 134 } 135 136 void reloc(CRef& cr, ClauseAllocator& to) 137 { 138 Clause& c = operator[](cr); 139 140 if (c.reloced()) { cr = c.relocation(); return; } 141 142 cr = to.alloc(c, c.learnt()); 143 c.relocate(cr); 144 145 // Copy extra data-fields: 146 // (This could be cleaned-up. Generalize Clause-constructor to be applicable here instead?) 147 to[cr].mark(c.mark()); 148 if (to[cr].learnt()){ 149 to[cr].touched() = c.touched(); 150 to[cr].activity() = c.activity(); 151 to[cr].set_lbd(c.lbd()); 152 to[cr].removable(c.removable()); 153 // simplify 154 // 155 to[cr].setSimplified(c.simplified()); 156 } 157 else if (to[cr].has_extra()) to[cr].calcAbstraction(); 158 } 159 }; 160 161 162 inline std::ostream& operator<<(std::ostream& out, const Clause& cls) 163 { 164 for (int i = 0; i < cls.size(); ++i) 165 { 166 out << cls[i] << " "; 167 } 168 169 return out; 170 }

 

   

 

3.  OccLists

 
 1 //=================================================================================================
 2 // OccLists -- a class for maintaining occurence lists with lazy deletion:
 3 
 4 template<class Idx, class Vec, class Deleted>
 5 class OccLists
 6 {
 7     vec<Vec>  occs;
 8     vec<char> dirty;
 9     vec<Idx>  dirties;
10     Deleted   deleted;
11 
12 public:
13     OccLists(const Deleted& d) : deleted(d) {}
14     
15     void  init      (const Idx& idx){ occs.growTo(toInt(idx)+1); dirty.growTo(toInt(idx)+1, 0); }
16     // Vec&  operator[](const Idx& idx){ return occs[toInt(idx)]; }
17     Vec&  operator[](const Idx& idx){ return occs[toInt(idx)]; }
18     Vec&  lookup    (const Idx& idx){ if (dirty[toInt(idx)]) clean(idx); return occs[toInt(idx)]; }
19 
20     void  cleanAll  ();
21     void  clean     (const Idx& idx);
22     void  smudge    (const Idx& idx){
23         if (dirty[toInt(idx)] == 0){
24             dirty[toInt(idx)] = 1;
25             dirties.push(idx);
26         }
27     }
28 
29     void  clear(bool free = true){
30         occs   .clear(free);
31         dirty  .clear(free);
32         dirties.clear(free);
33     }
34 
35     //-----------------------------------------------------
36     //将观察体系各个观察中的观察元随机排列
37     //update on 2020-8-8
38     void randomlizeAll();
39     void randomlize(const Idx& idx);
40     //void randomlizeAll();
41 };
42 
43 
44 template<class Idx, class Vec, class Deleted>
45 void OccLists<Idx,Vec,Deleted>::cleanAll()
46 {
47     for (int i = 0; i < dirties.size(); i++)
48         // Dirties may contain duplicates so check here if a variable is already cleaned:
49         if (dirty[toInt(dirties[i])])
50             clean(dirties[i]);
51     dirties.clear();
52 }
53 
54 
55 template<class Idx, class Vec, class Deleted>
56 void OccLists<Idx,Vec,Deleted>::clean(const Idx& idx)
57 {
58     Vec& vec = occs[toInt(idx)];
59     int  i, j;
60     for (i = j = 0; i < vec.size(); i++)
61         if (!deleted(vec[i]))
62             vec[j++] = vec[i];
63     vec.shrink(i - j);
64     dirty[toInt(idx)] = 0;
65 }
66 
67 template<class Idx, class Vec, class Deleted>
68 void OccLists<Idx,Vec,Deleted>::randomlizeAll()
69 {
70     for (int i = 0; i < occs.size(); i++){
71         Vec& vec = occs[i];    
72         randomlizeT(vec);
73     }        
74 }
75 
76 template<class Idx, class Vec, class Deleted>
77 void OccLists<Idx,Vec,Deleted>::randomlize(const Idx& idx)
78 {
79     Vec& vec = occs[toInt(idx)];    
80     randomlizeT(vec);
81 }

 

   

 

posted on 2021-07-09 22:06  海阔凭鱼跃越  阅读(280)  评论(0编辑  收藏  举报