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.Iterator; |
32 |
| import java.util.NoSuchElementException; |
33 |
| import java.util.Random; |
34 |
| |
35 |
| import org.sat4j.specs.IVecInt; |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| public class VecInt implements Serializable, IVecInt { |
47 |
| |
48 |
| private static final long serialVersionUID = 1L; |
49 |
| |
50 |
| private static final int RANDOM_SEED = 91648253; |
51 |
| |
52 |
| public static final IVecInt EMPTY = new VecInt() { |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| private static final long serialVersionUID = 1L; |
58 |
| |
59 |
0
| @Override
|
60 |
| public int size() { |
61 |
0
| return 0;
|
62 |
| } |
63 |
| |
64 |
0
| @Override
|
65 |
| public void shrink(int nofelems) { |
66 |
| } |
67 |
| |
68 |
0
| @Override
|
69 |
| public void shrinkTo(int newsize) { |
70 |
| } |
71 |
| |
72 |
0
| @Override
|
73 |
| public IVecInt pop() { |
74 |
0
| throw new UnsupportedOperationException();
|
75 |
| } |
76 |
| |
77 |
0
| @Override
|
78 |
| public void growTo(int newsize, int pad) { |
79 |
| } |
80 |
| |
81 |
0
| @Override
|
82 |
| public void ensure(int nsize) { |
83 |
| } |
84 |
| |
85 |
0
| @Override
|
86 |
| public IVecInt push(int elem) { |
87 |
0
| throw new UnsupportedOperationException();
|
88 |
| } |
89 |
| |
90 |
0
| @Override
|
91 |
| public void unsafePush(int elem) { |
92 |
0
| throw new UnsupportedOperationException();
|
93 |
| } |
94 |
| |
95 |
0
| @Override
|
96 |
| public void clear() { |
97 |
| } |
98 |
| |
99 |
0
| @Override
|
100 |
| public int last() { |
101 |
0
| throw new UnsupportedOperationException();
|
102 |
| } |
103 |
| |
104 |
0
| @Override
|
105 |
| public int get(int i) { |
106 |
0
| throw new UnsupportedOperationException();
|
107 |
| } |
108 |
| |
109 |
0
| @Override
|
110 |
| public void set(int i, int o) { |
111 |
0
| throw new UnsupportedOperationException();
|
112 |
| } |
113 |
| |
114 |
0
| @Override
|
115 |
| public boolean contains(int e) { |
116 |
0
| return false;
|
117 |
| } |
118 |
| |
119 |
0
| @Override
|
120 |
| public void copyTo(IVecInt copy) { |
121 |
0
| throw new UnsupportedOperationException();
|
122 |
| } |
123 |
| |
124 |
0
| @Override
|
125 |
| public void copyTo(int[] is) { |
126 |
| } |
127 |
| |
128 |
0
| @Override
|
129 |
| public void moveTo(IVecInt dest) { |
130 |
| } |
131 |
| |
132 |
0
| @Override
|
133 |
| public void moveTo2(IVecInt dest) { |
134 |
| } |
135 |
| |
136 |
0
| @Override
|
137 |
| public void moveTo(int[] dest) { |
138 |
| } |
139 |
| |
140 |
0
| @Override
|
141 |
| public void insertFirst(int elem) { |
142 |
0
| throw new UnsupportedOperationException();
|
143 |
| } |
144 |
| |
145 |
0
| @Override
|
146 |
| public void remove(int elem) { |
147 |
0
| throw new UnsupportedOperationException();
|
148 |
| } |
149 |
| |
150 |
0
| @Override
|
151 |
| public int delete(int i) { |
152 |
0
| throw new UnsupportedOperationException();
|
153 |
| } |
154 |
| |
155 |
0
| @Override
|
156 |
| public void sort() { |
157 |
| } |
158 |
| |
159 |
0
| @Override
|
160 |
| public void sortUnique() { |
161 |
| } |
162 |
| }; |
163 |
| |
164 |
2309260
| public VecInt() {
|
165 |
2309260
| this(5);
|
166 |
| } |
167 |
| |
168 |
3088875
| public VecInt(int size) {
|
169 |
3088875
| myarray = new int[size];
|
170 |
| } |
171 |
| |
172 |
| |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
| |
179 |
| |
180 |
| |
181 |
998672
| public VecInt(int size, int pad) {
|
182 |
998672
| myarray = new int[size];
|
183 |
998672
| for (int i = 0; i < size; i++) {
|
184 |
3073048
| myarray[i] = pad;
|
185 |
| } |
186 |
998672
| nbelem = size;
|
187 |
| } |
188 |
| |
189 |
1364544963
| public int size() {
|
190 |
1364544963
| return nbelem;
|
191 |
| } |
192 |
| |
193 |
| |
194 |
| |
195 |
| |
196 |
| |
197 |
| |
198 |
4246374
| public void shrink(int nofelems) {
|
199 |
| assert nofelems >= 0; |
200 |
| assert nofelems <= size(); |
201 |
4246374
| nbelem -= nofelems;
|
202 |
| } |
203 |
| |
204 |
0
| public void shrinkTo(int newsize) {
|
205 |
| assert newsize >= 0; |
206 |
| assert newsize < nbelem; |
207 |
0
| nbelem = newsize;
|
208 |
| } |
209 |
| |
210 |
| |
211 |
| |
212 |
| |
213 |
| |
214 |
| public IVecInt pop() { |
215 |
| assert size() != 0; |
216 |
| --nbelem; |
217 |
| return this; |
218 |
| } |
219 |
| |
220 |
9
| public void growTo(int newsize, final int pad) {
|
221 |
| assert newsize > size(); |
222 |
9
| ensure(newsize);
|
223 |
9
| while (--newsize >= 0) {
|
224 |
561
| myarray[nbelem++] = pad;
|
225 |
| } |
226 |
| } |
227 |
| |
228 |
| public void ensure(int nsize) { |
229 |
| if (nsize >= myarray.length) { |
230 |
2642745
| int[] narray = new int[Math.max(nsize, nbelem * 2)];
|
231 |
2642745
| System.arraycopy(myarray, 0, narray, 0, nbelem);
|
232 |
2642745
| myarray = narray;
|
233 |
| } |
234 |
| } |
235 |
| |
236 |
| public IVecInt push(int elem) { |
237 |
| ensure(nbelem + 1); |
238 |
| myarray[nbelem++] = elem; |
239 |
| return this; |
240 |
| } |
241 |
| |
242 |
16393476
| public void unsafePush(int elem) {
|
243 |
16393476
| myarray[nbelem++] = elem;
|
244 |
| } |
245 |
| |
246 |
1077732848
| public void clear() {
|
247 |
1077732848
| nbelem = 0;
|
248 |
| } |
249 |
| |
250 |
685939059
| public int last() {
|
251 |
| assert nbelem > 0; |
252 |
685939059
| return myarray[nbelem - 1];
|
253 |
| } |
254 |
| |
255 |
2105875652
| public int get(int i) {
|
256 |
| assert i >= 0; |
257 |
| assert i < nbelem; |
258 |
2105875652
| return myarray[i];
|
259 |
| } |
260 |
| |
261 |
146803591
| public int unsafeGet(int i) {
|
262 |
146803591
| return myarray[i];
|
263 |
| } |
264 |
| |
265 |
1112755840
| public void set(int i, int o) {
|
266 |
| assert i >= 0; |
267 |
| assert i < nbelem; |
268 |
1112755840
| myarray[i] = o;
|
269 |
| } |
270 |
| |
271 |
0
| public boolean contains(int e) {
|
272 |
0
| for (int i = 0; i < nbelem; i++) {
|
273 |
0
| if (myarray[i] == e)
|
274 |
0
| return true;
|
275 |
| } |
276 |
0
| return false;
|
277 |
| } |
278 |
| |
279 |
| |
280 |
| |
281 |
| |
282 |
| |
283 |
| |
284 |
| |
285 |
1559408
| public void copyTo(IVecInt copy) {
|
286 |
| |
287 |
| |
288 |
| |
289 |
| |
290 |
| |
291 |
1559408
| VecInt ncopy = (VecInt) copy;
|
292 |
1559408
| int nsize = nbelem + ncopy.nbelem;
|
293 |
1559408
| ncopy.ensure(nsize);
|
294 |
1559408
| System.arraycopy(myarray, 0, ncopy.myarray, ncopy.nbelem, nbelem);
|
295 |
1559408
| ncopy.nbelem = nsize;
|
296 |
| } |
297 |
| |
298 |
| |
299 |
| |
300 |
| |
301 |
1621870
| public void copyTo(int[] is) {
|
302 |
| assert is.length >= nbelem; |
303 |
1621870
| System.arraycopy(myarray, 0, is, 0, nbelem);
|
304 |
| } |
305 |
| |
306 |
| |
307 |
| |
308 |
| |
309 |
0
| public void moveTo(IVecInt dest) {
|
310 |
0
| copyTo(dest);
|
311 |
0
| nbelem = 0;
|
312 |
| } |
313 |
| |
314 |
0
| public void moveTo2(IVecInt dest) {
|
315 |
0
| VecInt ndest = (VecInt) dest;
|
316 |
0
| int s = ndest.nbelem;
|
317 |
0
| int tmp[] = ndest.myarray;
|
318 |
0
| ndest.myarray = myarray;
|
319 |
0
| ndest.nbelem = nbelem;
|
320 |
0
| myarray = tmp;
|
321 |
0
| nbelem = s;
|
322 |
0
| nbelem = 0;
|
323 |
| } |
324 |
| |
325 |
95067182
| public void moveTo(int dest, int source) {
|
326 |
95067182
| myarray[dest]=myarray[source];
|
327 |
| } |
328 |
| |
329 |
95875745
| public void moveTo(int[] dest) {
|
330 |
95875745
| System.arraycopy(myarray, 0, dest, 0, nbelem);
|
331 |
95875745
| nbelem = 0;
|
332 |
| } |
333 |
| |
334 |
| |
335 |
| |
336 |
| |
337 |
| |
338 |
| |
339 |
| |
340 |
| |
341 |
| |
342 |
0
| public void insertFirst(final int elem) {
|
343 |
0
| if (nbelem > 0) {
|
344 |
0
| push(myarray[0]);
|
345 |
0
| myarray[0] = elem;
|
346 |
0
| return;
|
347 |
| } |
348 |
0
| push(elem);
|
349 |
| } |
350 |
| |
351 |
| |
352 |
| |
353 |
| |
354 |
| |
355 |
| |
356 |
| |
357 |
0
| public void remove(int elem) {
|
358 |
| assert size() > 0; |
359 |
0
| int j = 0;
|
360 |
0
| for (; myarray[j] != elem; j++) {
|
361 |
| assert j < size(); |
362 |
| } |
363 |
0
| for (; j < size() - 1; j++) {
|
364 |
0
| myarray[j] = myarray[j + 1];
|
365 |
| } |
366 |
0
| pop();
|
367 |
| } |
368 |
| |
369 |
| |
370 |
| |
371 |
| |
372 |
| |
373 |
| |
374 |
| |
375 |
| |
376 |
| |
377 |
| |
378 |
330
| public int delete(int i) {
|
379 |
| assert i >= 0; |
380 |
| assert i < nbelem; |
381 |
330
| int ith = myarray[i];
|
382 |
330
| myarray[i] = myarray[--nbelem];
|
383 |
330
| return ith;
|
384 |
| } |
385 |
| |
386 |
| private int nbelem; |
387 |
| |
388 |
| private int[] myarray; |
389 |
| |
390 |
| |
391 |
| |
392 |
| |
393 |
| |
394 |
| |
395 |
85
| @Override
|
396 |
| public String toString() { |
397 |
85
| StringBuffer stb = new StringBuffer();
|
398 |
85
| for (int i = 0; i < nbelem - 1; i++) {
|
399 |
1428
| stb.append(myarray[i]);
|
400 |
1428
| stb.append(",");
|
401 |
| } |
402 |
85
| if (nbelem > 0) {
|
403 |
85
| stb.append(myarray[nbelem - 1]);
|
404 |
| } |
405 |
85
| return stb.toString();
|
406 |
| } |
407 |
| |
408 |
| private static Random rand = new Random(RANDOM_SEED); |
409 |
| |
410 |
3635472
| void selectionSort(int from, int to) {
|
411 |
3635472
| int i, j, best_i;
|
412 |
3635472
| int tmp;
|
413 |
| |
414 |
3635472
| for (i = from; i < to - 1; i++) {
|
415 |
7196924
| best_i = i;
|
416 |
7196924
| for (j = i + 1; j < to; j++) {
|
417 |
20362674
| if (myarray[j] < myarray[best_i])
|
418 |
84196
| best_i = j;
|
419 |
| } |
420 |
7196924
| tmp = myarray[i];
|
421 |
7196924
| myarray[i] = myarray[best_i];
|
422 |
7196924
| myarray[best_i] = tmp;
|
423 |
| } |
424 |
| } |
425 |
| |
426 |
3765655
| void sort(int from, int to) {
|
427 |
3765655
| int width = to - from;
|
428 |
3765655
| if (to - from <= 15)
|
429 |
3635472
| selectionSort(from, to);
|
430 |
| |
431 |
| else { |
432 |
130183
| int pivot = myarray[rand.nextInt(width) + from];
|
433 |
130183
| int tmp;
|
434 |
130183
| int i = from - 1;
|
435 |
130183
| int j = to;
|
436 |
| |
437 |
130183
| for (;;) {
|
438 |
142981
| do
|
439 |
1119218
| i++;
|
440 |
1119218
| while (myarray[i] < pivot);
|
441 |
142981
| do
|
442 |
1109232
| j--;
|
443 |
1109232
| while (pivot < myarray[j]);
|
444 |
| |
445 |
142981
| if (i >= j)
|
446 |
130183
| break;
|
447 |
| |
448 |
12798
| tmp = myarray[i];
|
449 |
12798
| myarray[i] = myarray[j];
|
450 |
12798
| myarray[j] = tmp;
|
451 |
| } |
452 |
| |
453 |
130183
| sort(from, i);
|
454 |
130183
| sort(i, to);
|
455 |
| } |
456 |
| } |
457 |
| |
458 |
| |
459 |
| |
460 |
| |
461 |
0
| public void sort() {
|
462 |
0
| sort(0, nbelem);
|
463 |
| } |
464 |
| |
465 |
3505299
| public void sortUnique() {
|
466 |
3505299
| int i, j;
|
467 |
3505299
| int last;
|
468 |
3505299
| if (nbelem == 0)
|
469 |
10
| return;
|
470 |
| |
471 |
3505289
| sort(0, nbelem);
|
472 |
3505289
| i = 1;
|
473 |
3505289
| last = myarray[0];
|
474 |
3505289
| for (j = 1; j < nbelem; j++) {
|
475 |
7319831
| if (last < myarray[j]) {
|
476 |
7319831
| last = myarray[i] = myarray[j];
|
477 |
7319831
| i++;
|
478 |
| } |
479 |
| } |
480 |
| |
481 |
3505289
| nbelem = i;
|
482 |
| } |
483 |
| |
484 |
| |
485 |
| |
486 |
| |
487 |
| |
488 |
| |
489 |
23
| @Override
|
490 |
| public boolean equals(Object obj) { |
491 |
23
| if (obj instanceof IVecInt) {
|
492 |
23
| IVecInt v = (IVecInt) obj;
|
493 |
23
| if (v.size() != size())
|
494 |
0
| return false;
|
495 |
23
| for (int i = 0; i < size(); i++) {
|
496 |
319
| if (v.get(i) != get(i)) {
|
497 |
6
| return false;
|
498 |
| } |
499 |
| } |
500 |
17
| return true;
|
501 |
| } |
502 |
0
| return false;
|
503 |
| } |
504 |
| |
505 |
| |
506 |
| |
507 |
| |
508 |
| |
509 |
| |
510 |
0
| @Override
|
511 |
| public int hashCode() { |
512 |
0
| int sum = 0;
|
513 |
0
| for (int i = 0; i < nbelem; i++) {
|
514 |
0
| sum += myarray.hashCode() / nbelem;
|
515 |
| } |
516 |
0
| return sum;
|
517 |
| } |
518 |
| |
519 |
| |
520 |
| |
521 |
| |
522 |
| |
523 |
| |
524 |
0
| public void pushAll(IVecInt vec) {
|
525 |
0
| VecInt nvec = (VecInt) vec;
|
526 |
0
| int nsize = nbelem + nvec.nbelem;
|
527 |
0
| ensure(nsize);
|
528 |
0
| System.arraycopy(nvec.myarray, 0, myarray, nbelem, nvec.nbelem);
|
529 |
0
| nbelem = nsize;
|
530 |
| } |
531 |
| |
532 |
| |
533 |
| |
534 |
| |
535 |
| |
536 |
| |
537 |
0
| public boolean isSubsetOf(IVecInt vec) {
|
538 |
0
| boolean isSubSet = (this.size() <= vec.size());
|
539 |
| |
540 |
0
| int i = 0;
|
541 |
0
| int j = 0;
|
542 |
0
| while (isSubSet && (i < this.size()) && (j < vec.size())) {
|
543 |
0
| while ((j < vec.size()) && (vec.get(j) < this.get(i))) {
|
544 |
0
| j++;
|
545 |
| } |
546 |
0
| if (j < vec.size()) {
|
547 |
0
| isSubSet = (this.get(i) == vec.get(j));
|
548 |
| } else { |
549 |
0
| isSubSet = false;
|
550 |
| } |
551 |
0
| i++;
|
552 |
| } |
553 |
0
| return isSubSet;
|
554 |
| } |
555 |
| |
556 |
1985
| public Iterator<Integer> iterator() {
|
557 |
1985
| return new Iterator<Integer>() {
|
558 |
| private int i = 0; |
559 |
| |
560 |
1999
| public boolean hasNext() {
|
561 |
1999
| return i < nbelem;
|
562 |
| } |
563 |
| |
564 |
15
| public Integer next() {
|
565 |
15
| if (i == nbelem)
|
566 |
0
| throw new NoSuchElementException();
|
567 |
15
| return myarray[i++];
|
568 |
| } |
569 |
| |
570 |
0
| public void remove() {
|
571 |
0
| throw new UnsupportedOperationException();
|
572 |
| } |
573 |
| }; |
574 |
| } |
575 |
| } |