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