|
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.core; |
|
29 |
| |
|
30 |
| import java.io.Serializable; |
|
31 |
| import java.util.Comparator; |
|
32 |
| import java.util.Iterator; |
|
33 |
| import java.util.NoSuchElementException; |
|
34 |
| import java.util.Random; |
|
35 |
| |
|
36 |
| import org.sat4j.specs.IVec; |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| public class Vec<T> implements Serializable, IVec<T> { |
|
45 |
| |
|
46 |
| private static final long serialVersionUID = 1L; |
|
47 |
| |
|
48 |
| private static final int RANDOM_SEED = 91648253; |
|
49 |
| |
|
50 |
3381367
| public Vec() {
|
|
51 |
3381367
| this(5);
|
|
52 |
| } |
|
53 |
| |
|
54 |
5159648
| @SuppressWarnings("unchecked")
|
|
55 |
| public Vec(int size) { |
|
56 |
5159648
| myarray = (T[]) new Object[size];
|
|
57 |
| } |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
15
| @SuppressWarnings("unchecked")
|
|
69 |
| public Vec(int size, T pad) { |
|
70 |
15
| myarray = (T[]) new Object[size];
|
|
71 |
15
| for (int i = 0; i < size; i++) {
|
|
72 |
59
| myarray[i] = pad;
|
|
73 |
| } |
|
74 |
15
| nbelem = size;
|
|
75 |
| } |
|
76 |
| |
|
77 |
| public int size() { |
|
78 |
| return nbelem; |
|
79 |
| } |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
2
| public void shrink(int nofelems) {
|
|
90 |
| assert nofelems <= size(); |
|
91 |
2
| while (nofelems-- > 0) {
|
|
92 |
10
| myarray[--nbelem] = null;
|
|
93 |
| } |
|
94 |
| } |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
1983355
| public void shrinkTo(final int newsize) {
|
|
103 |
| assert newsize <= size(); |
|
104 |
1983355
| for (int i = nbelem; i > newsize; i--) {
|
|
105 |
18452626
| myarray[i - 1] = null;
|
|
106 |
| } |
|
107 |
1983355
| nbelem = newsize;
|
|
108 |
| assert size() == newsize; |
|
109 |
| } |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
1327689782
| public void pop() {
|
|
116 |
| assert size() > 0; |
|
117 |
1327689782
| myarray[--nbelem] = null;
|
|
118 |
| } |
|
119 |
| |
|
120 |
14
| public void growTo(final int newsize, final T pad) {
|
|
121 |
| assert newsize >= size(); |
|
122 |
14
| ensure(newsize);
|
|
123 |
14
| for (int i = nbelem; i < newsize; i++) {
|
|
124 |
94
| myarray[i] = pad;
|
|
125 |
| } |
|
126 |
14
| nbelem = newsize;
|
|
127 |
| } |
|
128 |
| |
|
129 |
| @SuppressWarnings("unchecked") |
|
130 |
| public final void ensure(final int nsize) { |
|
131 |
| if (nsize >= myarray.length) { |
|
132 |
89235485
| T[] narray = (T[]) new Object[Math.max(nsize, nbelem * 2)];
|
|
133 |
89235485
| System.arraycopy(myarray, 0, narray, 0, nbelem);
|
|
134 |
89235485
| myarray = narray;
|
|
135 |
| } |
|
136 |
| } |
|
137 |
| |
|
138 |
1469942100
| public IVec<T> push(final T elem) {
|
|
139 |
1469942100
| ensure(nbelem + 1);
|
|
140 |
1469942100
| myarray[nbelem++] = elem;
|
|
141 |
1469942100
| return this;
|
|
142 |
| } |
|
143 |
| |
|
144 |
0
| public void unsafePush(final T elem) {
|
|
145 |
0
| myarray[nbelem++] = elem;
|
|
146 |
| } |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
0
| public void insertFirst(final T elem) {
|
|
157 |
0
| if (nbelem > 0) {
|
|
158 |
0
| push(myarray[0]);
|
|
159 |
0
| myarray[0] = elem;
|
|
160 |
0
| return;
|
|
161 |
| } |
|
162 |
0
| push(elem);
|
|
163 |
| } |
|
164 |
| |
|
165 |
74305
| public void insertFirstWithShifting(final T elem) {
|
|
166 |
74305
| if (nbelem > 0) {
|
|
167 |
10668
| ensure(nbelem + 1);
|
|
168 |
10668
| for (int i = nbelem; i > 0; i--) {
|
|
169 |
48606
| myarray[i] = myarray[i - 1];
|
|
170 |
| } |
|
171 |
10668
| myarray[0] = elem;
|
|
172 |
10668
| nbelem++;
|
|
173 |
10668
| return;
|
|
174 |
| } |
|
175 |
63637
| push(elem);
|
|
176 |
| } |
|
177 |
| |
|
178 |
| public void clear() { |
|
179 |
| while (nbelem > 0) { |
|
180 |
196640622
| myarray[--nbelem] = null;
|
|
181 |
| } |
|
182 |
| } |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
1290630394
| public T last() {
|
|
191 |
| assert size() != 0; |
|
192 |
1290630394
| return myarray[nbelem - 1];
|
|
193 |
| } |
|
194 |
| |
|
195 |
820260948
| public T get(int i) {
|
|
196 |
820260948
| return myarray[i];
|
|
197 |
| } |
|
198 |
| |
|
199 |
179404627
| public void set(int i, T o) {
|
|
200 |
179404627
| myarray[i] = o;
|
|
201 |
| } |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
37059412
| public void remove(T elem) {
|
|
210 |
| assert size() > 0; |
|
211 |
37059412
| int j = 0;
|
|
212 |
37059412
| for (; myarray[j] != elem; j++) {
|
|
213 |
| assert j < size(); |
|
214 |
| } |
|
215 |
37059412
| for (; j < size() - 1; j++) {
|
|
216 |
762147408
| myarray[j] = myarray[j + 1];
|
|
217 |
| } |
|
218 |
37059412
| pop();
|
|
219 |
| } |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
2
| public T delete(int i) {
|
|
231 |
| assert i >= 0; |
|
232 |
| assert i < nbelem; |
|
233 |
2
| T ith = myarray[i];
|
|
234 |
2
| myarray[i] = myarray[--nbelem];
|
|
235 |
2
| myarray[nbelem] = null;
|
|
236 |
2
| return ith;
|
|
237 |
| } |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
1940913040
| public void copyTo(IVec<T> copy) {
|
|
246 |
1940913040
| Vec<T> ncopy = (Vec<T>) copy;
|
|
247 |
1940913040
| int nsize = nbelem + ncopy.nbelem;
|
|
248 |
1940913040
| copy.ensure(nsize);
|
|
249 |
1940913040
| for (int i = 0; i < nbelem; i++) {
|
|
250 |
105434892
| ncopy.myarray[i + ncopy.nbelem] = myarray[i];
|
|
251 |
| } |
|
252 |
1940913040
| ncopy.nbelem = nsize;
|
|
253 |
| } |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
1621870
| public <E> void copyTo(E[] dest) {
|
|
259 |
| assert dest.length >= nbelem; |
|
260 |
1621870
| System.arraycopy(myarray, 0, dest, 0, nbelem);
|
|
261 |
| } |
|
262 |
| |
|
263 |
| |
|
264 |
| |
|
265 |
| |
|
266 |
1939353631
| public void moveTo(IVec<T> dest) {
|
|
267 |
1939353631
| copyTo(dest);
|
|
268 |
1939353631
| clear();
|
|
269 |
| } |
|
270 |
| |
|
271 |
0
| public void moveTo(int dest, int source) {
|
|
272 |
0
| myarray[dest]=myarray[source];
|
|
273 |
0
| myarray[source]=null;
|
|
274 |
| } |
|
275 |
| |
|
276 |
| private int nbelem; |
|
277 |
| |
|
278 |
| private T[] myarray; |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
74
| @Override
|
|
286 |
| public String toString() { |
|
287 |
74
| StringBuffer stb = new StringBuffer();
|
|
288 |
74
| for (int i = 0; i < nbelem - 1; i++) {
|
|
289 |
1406
| stb.append(myarray[i]);
|
|
290 |
1406
| stb.append(",");
|
|
291 |
| } |
|
292 |
74
| if (nbelem > 0) {
|
|
293 |
74
| stb.append(myarray[nbelem - 1]);
|
|
294 |
| } |
|
295 |
74
| return stb.toString();
|
|
296 |
| } |
|
297 |
| |
|
298 |
| private static Random rand = new Random(RANDOM_SEED); |
|
299 |
| |
|
300 |
19110499
| void selectionSort(int from, int to, Comparator<T> cmp) {
|
|
301 |
19110499
| int i, j, best_i;
|
|
302 |
19110499
| T tmp;
|
|
303 |
| |
|
304 |
19110499
| for (i = from; i < to - 1; i++) {
|
|
305 |
169598670
| best_i = i;
|
|
306 |
169598670
| for (j = i + 1; j < to; j++) {
|
|
307 |
952372899
| if (cmp.compare(myarray[j], myarray[best_i]) < 0)
|
|
308 |
48219450
| best_i = j;
|
|
309 |
| } |
|
310 |
169598670
| tmp = myarray[i];
|
|
311 |
169598670
| myarray[i] = myarray[best_i];
|
|
312 |
169598670
| myarray[best_i] = tmp;
|
|
313 |
| } |
|
314 |
| } |
|
315 |
| |
|
316 |
36254278
| void sort(int from, int to, Comparator<T> cmp) {
|
|
317 |
36254278
| int width = to - from;
|
|
318 |
36254278
| if (to - from <= 15)
|
|
319 |
19110498
| selectionSort(from, to, cmp);
|
|
320 |
| |
|
321 |
| else { |
|
322 |
17143780
| T pivot = myarray[rand.nextInt(width) + from];
|
|
323 |
17143780
| T tmp;
|
|
324 |
17143780
| int i = from - 1;
|
|
325 |
17143780
| int j = to;
|
|
326 |
| |
|
327 |
17143780
| for (;;) {
|
|
328 |
538736423
| do
|
|
329 |
611276225
| i++;
|
|
330 |
611276225
| while (cmp.compare(myarray[i], pivot) < 0);
|
|
331 |
538736423
| do
|
|
332 |
611202347
| j--;
|
|
333 |
611202347
| while (cmp.compare(pivot, myarray[j]) < 0);
|
|
334 |
| |
|
335 |
538736423
| if (i >= j)
|
|
336 |
17143780
| break;
|
|
337 |
| |
|
338 |
521592643
| tmp = myarray[i];
|
|
339 |
521592643
| myarray[i] = myarray[j];
|
|
340 |
521592643
| myarray[j] = tmp;
|
|
341 |
| } |
|
342 |
| |
|
343 |
17143780
| sort(from, i, cmp);
|
|
344 |
17143780
| sort(i, to, cmp);
|
|
345 |
| } |
|
346 |
| } |
|
347 |
| |
|
348 |
| |
|
349 |
| |
|
350 |
| |
|
351 |
1966717
| public void sort(Comparator<T> comparator) {
|
|
352 |
1966717
| sort(0, nbelem, comparator);
|
|
353 |
| } |
|
354 |
| |
|
355 |
1
| public void sortUnique(Comparator<T> cmp) {
|
|
356 |
1
| int i, j;
|
|
357 |
1
| T last;
|
|
358 |
| |
|
359 |
1
| if (nbelem == 0)
|
|
360 |
0
| return;
|
|
361 |
| |
|
362 |
1
| sort(0, nbelem, cmp);
|
|
363 |
| |
|
364 |
1
| i = 1;
|
|
365 |
1
| last = myarray[0];
|
|
366 |
1
| for (j = 1; j < nbelem; j++) {
|
|
367 |
105
| if (cmp.compare(last, myarray[j]) < 0) {
|
|
368 |
100
| last = myarray[i] = myarray[j];
|
|
369 |
100
| i++;
|
|
370 |
| } |
|
371 |
| } |
|
372 |
| |
|
373 |
1
| nbelem = i;
|
|
374 |
| } |
|
375 |
| |
|
376 |
4
| public static final <A extends Comparable<A>> Comparator<A> defaultComparator() {
|
|
377 |
4
| return new Comparator<A>() {
|
|
378 |
2559
| public int compare(A a, A b) {
|
|
379 |
2559
| return a.compareTo(b);
|
|
380 |
| } |
|
381 |
| }; |
|
382 |
| } |
|
383 |
| |
|
384 |
| |
|
385 |
| |
|
386 |
| |
|
387 |
| |
|
388 |
| |
|
389 |
21
| @Override
|
|
390 |
| public boolean equals(Object obj) { |
|
391 |
21
| if (obj instanceof IVec) {
|
|
392 |
21
| IVec<?> v = (IVec<?>) obj;
|
|
393 |
21
| if (v.size() != size())
|
|
394 |
2
| return false;
|
|
395 |
19
| for (int i = 0; i < size(); i++) {
|
|
396 |
316
| if (!v.get(i).equals(get(i))) {
|
|
397 |
1
| return false;
|
|
398 |
| } |
|
399 |
| } |
|
400 |
18
| return true;
|
|
401 |
| } |
|
402 |
0
| return false;
|
|
403 |
| } |
|
404 |
| |
|
405 |
| |
|
406 |
| |
|
407 |
| |
|
408 |
| |
|
409 |
| |
|
410 |
0
| @Override
|
|
411 |
| public int hashCode() { |
|
412 |
0
| int sum = 0;
|
|
413 |
0
| for (int i = 0; i < nbelem; i++) {
|
|
414 |
0
| sum += myarray.hashCode() / nbelem;
|
|
415 |
| } |
|
416 |
0
| return sum;
|
|
417 |
| } |
|
418 |
| |
|
419 |
2
| public Iterator<T> iterator() {
|
|
420 |
2
| return new Iterator<T>() {
|
|
421 |
| private int i = 0; |
|
422 |
| |
|
423 |
5
| public boolean hasNext() {
|
|
424 |
5
| return i < nbelem;
|
|
425 |
| } |
|
426 |
| |
|
427 |
4
| public T next() {
|
|
428 |
4
| if (i == nbelem)
|
|
429 |
1
| throw new NoSuchElementException();
|
|
430 |
3
| return myarray[i++];
|
|
431 |
| } |
|
432 |
| |
|
433 |
0
| public void remove() {
|
|
434 |
0
| throw new UnsupportedOperationException();
|
|
435 |
| } |
|
436 |
| }; |
|
437 |
| } |
|
438 |
| } |