1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.sat4j.minisat.constraints.cnf;
29
30 import java.io.Serializable;
31
32 import org.sat4j.core.LiteralsUtils;
33 import org.sat4j.core.Vec;
34 import org.sat4j.minisat.core.Constr;
35 import org.sat4j.minisat.core.ILits;
36 import org.sat4j.minisat.core.Propagatable;
37 import org.sat4j.minisat.core.Undoable;
38 import org.sat4j.specs.IVec;
39
40
41
42
43
44
45 public final class Lits implements Serializable, ILits {
46
47 private static final int DEFAULT_INIT_SIZE = 128;
48
49 private static final long serialVersionUID = 1L;
50
51 private boolean pool[] = new boolean[1];
52
53 private int realnVars = 0;
54
55 @SuppressWarnings("unchecked")
56 private IVec<Propagatable>[] watches = new IVec[0];
57
58 private int[] level = new int[0];
59
60 private Constr[] reason = new Constr[0];
61
62 private int maxvarid = 0;
63
64 @SuppressWarnings("unchecked")
65 private IVec<Undoable>[] undos = new IVec[0];
66
67 private boolean[] falsified = new boolean[0];
68
69 public Lits() {
70 init(DEFAULT_INIT_SIZE);
71 }
72
73 @SuppressWarnings({ "unchecked" })
74 public final void init(int nvar) {
75 if (nvar < pool.length) {
76 return;
77 }
78 assert nvar >= 0;
79
80 int nvars = nvar + 1;
81 boolean[] npool = new boolean[nvars];
82 System.arraycopy(pool, 0, npool, 0, pool.length);
83 pool = npool;
84
85 int[] nlevel = new int[nvars];
86 System.arraycopy(level, 0, nlevel, 0, level.length);
87 level = nlevel;
88
89 IVec<Propagatable>[] nwatches = new IVec[2 * nvars];
90 System.arraycopy(watches, 0, nwatches, 0, watches.length);
91 watches = nwatches;
92
93 IVec<Undoable>[] nundos = new IVec[nvars];
94 System.arraycopy(undos, 0, nundos, 0, undos.length);
95 undos = nundos;
96
97 Constr[] nreason = new Constr[nvars];
98 System.arraycopy(reason, 0, nreason, 0, reason.length);
99 reason = nreason;
100
101 boolean[] newFalsified = new boolean[2 * nvars];
102 System.arraycopy(falsified, 0, newFalsified, 0, falsified.length);
103 falsified = newFalsified;
104 }
105
106 public int getFromPool(int x) {
107 int var = Math.abs(x);
108 if (var >= pool.length) {
109 init(Math.max(var, pool.length << 1));
110 }
111 assert var < pool.length;
112 if (var > maxvarid) {
113 maxvarid = var;
114 }
115 int lit = LiteralsUtils.toInternal(x);
116 assert lit > 1;
117 if (!pool[var]) {
118 realnVars++;
119 pool[var] = true;
120 watches[var << 1] = new Vec<Propagatable>();
121 watches[(var << 1) | 1] = new Vec<Propagatable>();
122 undos[var] = new Vec<Undoable>();
123 level[var] = -1;
124 falsified[var << 1] = false;
125
126 falsified[var << 1 | 1] = false;
127
128 }
129 return lit;
130 }
131
132 public boolean belongsToPool(int x) {
133 assert x > 0;
134 if (x >= pool.length) {
135 return false;
136 }
137 return pool[x];
138 }
139
140 public void resetPool() {
141 for (int i = 0; i < pool.length; i++) {
142 if (pool[i]) {
143 reset(i << 1);
144 }
145 }
146 }
147
148 public void ensurePool(int howmany) {
149 if (howmany >= pool.length) {
150 init(Math.max(howmany, pool.length << 1));
151 }
152 maxvarid = howmany;
153 }
154
155 public void unassign(int lit) {
156 assert falsified[lit] || falsified[lit ^ 1];
157 falsified[lit] = false;
158 falsified[lit ^ 1] = false;
159 }
160
161 public void satisfies(int lit) {
162 assert !falsified[lit] && !falsified[lit ^ 1];
163 falsified[lit] = false;
164 falsified[lit ^ 1] = true;
165 }
166
167 public boolean isSatisfied(int lit) {
168 return falsified[lit ^ 1];
169 }
170
171 public final boolean isFalsified(int lit) {
172 return falsified[lit];
173 }
174
175 public boolean isUnassigned(int lit) {
176 return !falsified[lit] && !falsified[lit ^ 1];
177 }
178
179 public String valueToString(int lit) {
180 if (isUnassigned(lit)) {
181 return "?";
182 }
183 if (isSatisfied(lit)) {
184 return "T";
185 }
186 return "F";
187 }
188
189 public int nVars() {
190
191 return maxvarid;
192 }
193
194 public int not(int lit) {
195 return lit ^ 1;
196 }
197
198 public static String toString(int lit) {
199 return ((lit & 1) == 0 ? "" : "-") + (lit >> 1);
200 }
201
202 public void reset(int lit) {
203 watches[lit].clear();
204 watches[lit ^ 1].clear();
205 level[lit >> 1] = -1;
206 reason[lit >> 1] = null;
207 undos[lit >> 1].clear();
208 falsified[lit] = false;
209 falsified[lit ^ 1] = false;
210 }
211
212 public int getLevel(int lit) {
213 return level[lit >> 1];
214 }
215
216 public void setLevel(int lit, int l) {
217 level[lit >> 1] = l;
218 }
219
220 public Constr getReason(int lit) {
221 return reason[lit >> 1];
222 }
223
224 public void setReason(int lit, Constr r) {
225 reason[lit >> 1] = r;
226 }
227
228 public IVec<Undoable> undos(int lit) {
229 return undos[lit >> 1];
230 }
231
232 public void watch(int lit, Propagatable c) {
233 watches[lit].push(c);
234 }
235
236 public IVec<Propagatable> watches(int lit) {
237 return watches[lit];
238 }
239
240 public boolean isImplied(int lit) {
241 int var = lit >> 1;
242 assert reason[var] == null || falsified[lit] || falsified[lit ^ 1];
243
244
245 return pool[var] && (reason[var] != null || level[var] == 0);
246 }
247
248 public int realnVars() {
249 return realnVars;
250 }
251
252
253
254
255
256
257
258 protected int capacity() {
259 return pool.length - 1;
260 }
261
262
263
264
265 public int nextFreeVarId(boolean reserve) {
266 if (reserve) {
267 ensurePool(maxvarid + 1);
268
269 return maxvarid;
270 }
271 return maxvarid + 1;
272 }
273 }