1 /**************************************************************************************************
2 MiniSat -- Copyright (c) 2005, Niklas Sorensson
3 http://www.cs.chalmers.se/Cs/Research/FormalMethods/MiniSat/
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
6 associated documentation files (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge, publish, distribute,
8 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all copies or
12 substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
15 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
18 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 **************************************************************************************************/
20 // Modified to compile with MS Visual Studio 6.0 by Alan Mishchenko
21 
22 #ifndef ABC__sat__bsat__satVec_h
23 #define ABC__sat__bsat__satVec_h
24 
25 #include "misc/util/abc_global.h"
26 
27 ABC_NAMESPACE_HEADER_START
28 
29 
30 // vector of 32-bit intergers (added for 64-bit portability)
31 struct veci_t {
32     int    cap;
33     int    size;
34     int*   ptr;
35 };
36 typedef struct veci_t veci;
37 
veci_new(veci * v)38 static inline void veci_new (veci* v) {
39     v->cap  = 4;
40     v->size = 0;
41     v->ptr  = (int*)ABC_ALLOC( char, sizeof(int)*v->cap);
42 }
43 
veci_delete(veci * v)44 static inline void   veci_delete (veci* v)          { ABC_FREE(v->ptr);   }
veci_begin(veci * v)45 static inline int*   veci_begin  (veci* v)          { return v->ptr;  }
veci_size(veci * v)46 static inline int    veci_size   (veci* v)          { return v->size; }
veci_resize(veci * v,int k)47 static inline void   veci_resize (veci* v, int k)   {
48     assert(k <= v->size);
49 //    memset( veci_begin(v) + k, -1, sizeof(int) * (veci_size(v) - k) );
50     v->size = k;
51 } // only safe to shrink !!
veci_pop(veci * v)52 static inline int    veci_pop    (veci* v)          { assert(v->size); return v->ptr[--v->size]; }
veci_push(veci * v,int e)53 static inline void   veci_push   (veci* v, int e)
54 {
55     if (v->size == v->cap) {
56 //        int newsize = v->cap * 2;//+1;
57         int newsize = (v->cap < 4) ? v->cap * 2 : (v->cap / 2) * 3;
58         v->ptr = ABC_REALLOC( int, v->ptr, newsize );
59         if ( v->ptr == NULL )
60         {
61             printf( "Failed to realloc memory from %.1f MB to %.1f MB.\n",
62                 1.0 * v->cap / (1<<20), 1.0 * newsize / (1<<20) );
63             fflush( stdout );
64         }
65         v->cap = newsize; }
66     v->ptr[v->size++] = e;
67 }
veci_remove(veci * v,int e)68 static inline void   veci_remove(veci* v, int e)
69 {
70     int * ws = (int*)veci_begin(v);
71     int    j  = 0;
72     for (; ws[j] != e  ; j++);
73     assert(j < veci_size(v));
74     for (; j < veci_size(v)-1; j++) ws[j] = ws[j+1];
75     veci_resize(v,veci_size(v)-1);
76 }
77 
78 
79 // vector of 32- or 64-bit pointers
80 struct vecp_t {
81     int    cap;
82     int    size;
83     void** ptr;
84 };
85 typedef struct vecp_t vecp;
86 
vecp_new(vecp * v)87 static inline void vecp_new (vecp* v) {
88     v->size = 0;
89     v->cap  = 4;
90     v->ptr  = (void**)ABC_ALLOC( char, sizeof(void*)*v->cap);
91 }
92 
vecp_delete(vecp * v)93 static inline void   vecp_delete (vecp* v)          { ABC_FREE(v->ptr);   }
vecp_begin(vecp * v)94 static inline void** vecp_begin  (vecp* v)          { return v->ptr;  }
vecp_size(vecp * v)95 static inline int    vecp_size   (vecp* v)          { return v->size; }
vecp_resize(vecp * v,int k)96 static inline void   vecp_resize (vecp* v, int   k) { assert(k <= v->size); v->size = k;    } // only safe to shrink !!
vecp_push(vecp * v,void * e)97 static inline void   vecp_push   (vecp* v, void* e)
98 {
99     if (v->size == v->cap) {
100 //        int newsize = v->cap * 2;//+1;
101         int newsize = (v->cap < 4) ? v->cap * 2 : (v->cap / 2) * 3;
102         v->ptr = ABC_REALLOC( void*, v->ptr, newsize );
103         v->cap = newsize; }
104     v->ptr[v->size++] = e;
105 }
vecp_remove(vecp * v,void * e)106 static inline void   vecp_remove(vecp* v, void* e)
107 {
108     void** ws = vecp_begin(v);
109     int    j  = 0;
110     for (; ws[j] != e  ; j++);
111     assert(j < vecp_size(v));
112     for (; j < vecp_size(v)-1; j++) ws[j] = ws[j+1];
113     vecp_resize(v,vecp_size(v)-1);
114 }
115 
116 
117 
118 //=================================================================================================
119 // Simple types:
120 
121 #ifndef __cplusplus
122 #ifndef false
123 #  define false 0
124 #endif
125 #ifndef true
126 #  define true 1
127 #endif
128 #endif
129 
130 typedef int    lit;
131 typedef int    cla;
132 
133 typedef char               lbool;
134 
135 static const int   var_Undef = -1;
136 static const lit   lit_Undef = -2;
137 
138 static const lbool l_Undef   =  0;
139 static const lbool l_True    =  1;
140 static const lbool l_False   = -1;
141 
toLit(int v)142 static inline lit  toLit    (int v)        { return v + v; }
toLitCond(int v,int c)143 static inline lit  toLitCond(int v, int c) { return v + v + (c != 0); }
lit_neg(lit l)144 static inline lit  lit_neg  (lit l)        { return l ^ 1; }
lit_var(lit l)145 static inline int  lit_var  (lit l)        { return l >> 1; }
lit_sign(lit l)146 static inline int  lit_sign (lit l)        { return l & 1; }
lit_print(lit l)147 static inline int  lit_print(lit l)        { return lit_sign(l)? -lit_var(l)-1 : lit_var(l)+1; }
lit_read(int s)148 static inline lit  lit_read (int s)        { return s > 0 ? toLit(s-1) : lit_neg(toLit(-s-1)); }
lit_check(lit l,int n)149 static inline int  lit_check(lit l, int n) { return l >= 0 && lit_var(l) < n;                  }
150 
151 struct stats_t
152 {
153     unsigned starts, clauses, learnts;
154     ABC_INT64_T decisions, propagations, inspects, conflicts;
155     ABC_INT64_T clauses_literals, learnts_literals, tot_literals;
156 };
157 typedef struct stats_t stats_t;
158 
159 ABC_NAMESPACE_HEADER_END
160 
161 #endif
162