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
29
30 package org.sat4j.core;
31
32 import java.util.Arrays;
33 import java.util.Comparator;
34 import java.util.Iterator;
35 import java.util.NoSuchElementException;
36
37 import org.sat4j.specs.IVec;
38
39
40
41
42
43
44
45
46 public final class Vec<T> implements IVec<T> {
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
71
72
73 public Vec() {
74 this(5);
75 }
76
77
78
79
80
81
82
83
84
85
86
87 public Vec(T[] elts) {
88 this.myarray = elts;
89 this.nbelem = elts.length;
90 }
91
92
93
94
95
96
97
98 @SuppressWarnings("unchecked")
99 public Vec(int size) {
100 this.myarray = (T[]) new Object[size];
101 }
102
103
104
105
106
107
108
109
110
111
112 @SuppressWarnings("unchecked")
113 public Vec(int size, T pad) {
114 this.myarray = (T[]) new Object[size];
115 for (int i = 0; i < size; i++) {
116 this.myarray[i] = pad;
117 }
118 this.nbelem = size;
119 }
120
121 public int size() {
122 return this.nbelem;
123 }
124
125
126
127
128
129
130
131
132
133 public void shrink(int nofelems) {
134
135 while (nofelems-- > 0) {
136 this.myarray[--this.nbelem] = null;
137 }
138 }
139
140
141
142
143
144
145
146 public void shrinkTo(final int newsize) {
147
148 for (int i = this.nbelem; i > newsize; i--) {
149 this.myarray[i - 1] = null;
150 }
151 this.nbelem = newsize;
152
153 }
154
155
156
157
158
159 public void pop() {
160
161 this.myarray[--this.nbelem] = null;
162 }
163
164 public void growTo(final int newsize, final T pad) {
165
166 ensure(newsize);
167 for (int i = this.nbelem; i < newsize; i++) {
168 this.myarray[i] = pad;
169 }
170 this.nbelem = newsize;
171 }
172
173 @SuppressWarnings("unchecked")
174 public void ensure(final int nsize) {
175 if (nsize >= this.myarray.length) {
176 T[] narray = (T[]) new Object[Math.max(nsize, this.nbelem * 2)];
177 System.arraycopy(this.myarray, 0, narray, 0, this.nbelem);
178 this.myarray = narray;
179 }
180 }
181
182 public IVec<T> push(final T elem) {
183 ensure(this.nbelem + 1);
184 this.myarray[this.nbelem++] = elem;
185 return this;
186 }
187
188 public void unsafePush(final T elem) {
189 this.myarray[this.nbelem++] = elem;
190 }
191
192
193
194
195
196
197
198
199
200 public void insertFirst(final T elem) {
201 if (this.nbelem > 0) {
202 push(this.myarray[0]);
203 this.myarray[0] = elem;
204 return;
205 }
206 push(elem);
207 }
208
209 public void insertFirstWithShifting(final T elem) {
210 if (this.nbelem > 0) {
211 ensure(this.nbelem + 1);
212 for (int i = this.nbelem; i > 0; i--) {
213 this.myarray[i] = this.myarray[i - 1];
214 }
215 this.myarray[0] = elem;
216 this.nbelem++;
217 return;
218 }
219 push(elem);
220 }
221
222 public void clear() {
223 Arrays.fill(this.myarray, 0, this.nbelem, null);
224 this.nbelem = 0;
225 }
226
227
228
229
230
231
232
233 public T last() {
234
235 return this.myarray[this.nbelem - 1];
236 }
237
238 public T get(final int index) {
239 return this.myarray[index];
240 }
241
242 public void set(int index, T elem) {
243 this.myarray[index] = elem;
244 }
245
246
247
248
249
250
251
252
253 public void remove(T elem) {
254
255 int j = 0;
256 for (; this.myarray[j] != elem; j++) {
257 if (j == size())
258 throw new NoSuchElementException();
259 }
260
261 System.arraycopy(this.myarray, j + 1, this.myarray, j, size() - j - 1);
262 this.myarray[--this.nbelem] = null;
263 }
264
265
266
267
268
269
270
271
272
273
274 public T delete(int index) {
275
276 T ith = this.myarray[index];
277 this.myarray[index] = this.myarray[--this.nbelem];
278 this.myarray[this.nbelem] = null;
279 return ith;
280 }
281
282
283
284
285
286
287
288 public void copyTo(IVec<T> copy) {
289 final Vec<T> ncopy = (Vec<T>) copy;
290 final int nsize = this.nbelem + ncopy.nbelem;
291 copy.ensure(nsize);
292 System.arraycopy(this.myarray, 0, ncopy.myarray, ncopy.nbelem,
293 this.nbelem);
294 ncopy.nbelem = nsize;
295 }
296
297
298
299
300 public <E> void copyTo(E[] dest) {
301
302 System.arraycopy(this.myarray, 0, dest, 0, this.nbelem);
303 }
304
305
306
307
308 public void moveTo(IVec<T> dest) {
309 copyTo(dest);
310 clear();
311 }
312
313 public void moveTo(int dest, int source) {
314 if (dest != source) {
315 this.myarray[dest] = this.myarray[source];
316 this.myarray[source] = null;
317 }
318 }
319
320 public T[] toArray() {
321
322 return this.myarray;
323 }
324
325 private int nbelem;
326
327 private T[] myarray;
328
329
330
331
332
333
334 @Override
335 public String toString() {
336 StringBuffer stb = new StringBuffer();
337 for (int i = 0; i < this.nbelem - 1; i++) {
338 stb.append(this.myarray[i]);
339 stb.append(",");
340 }
341 if (this.nbelem > 0) {
342 stb.append(this.myarray[this.nbelem - 1]);
343 }
344 return stb.toString();
345 }
346
347 void selectionSort(int from, int to, Comparator<T> cmp) {
348 int i, j, besti;
349 T tmp;
350
351 for (i = from; i < to - 1; i++) {
352 besti = i;
353 for (j = i + 1; j < to; j++) {
354 if (cmp.compare(this.myarray[j], this.myarray[besti]) < 0) {
355 besti = j;
356 }
357 }
358 tmp = this.myarray[i];
359 this.myarray[i] = this.myarray[besti];
360 this.myarray[besti] = tmp;
361 }
362 }
363
364 void sort(int from, int to, Comparator<T> cmp) {
365 int width = to - from;
366 if (width <= 15) {
367 selectionSort(from, to, cmp);
368 } else {
369 T pivot = this.myarray[width / 2 + from];
370 T tmp;
371 int i = from - 1;
372 int j = to;
373
374 for (;;) {
375 do {
376 i++;
377 } while (cmp.compare(this.myarray[i], pivot) < 0);
378 do {
379 j--;
380 } while (cmp.compare(pivot, this.myarray[j]) < 0);
381
382 if (i >= j) {
383 break;
384 }
385
386 tmp = this.myarray[i];
387 this.myarray[i] = this.myarray[j];
388 this.myarray[j] = tmp;
389 }
390
391 sort(from, i, cmp);
392 sort(i, to, cmp);
393 }
394 }
395
396
397
398
399 public void sort(Comparator<T> comparator) {
400 sort(0, this.nbelem, comparator);
401 }
402
403 public void sortUnique(Comparator<T> cmp) {
404 int i, j;
405 T last;
406
407 if (this.nbelem == 0) {
408 return;
409 }
410
411 sort(0, this.nbelem, cmp);
412
413 i = 1;
414 last = this.myarray[0];
415 for (j = 1; j < this.nbelem; j++) {
416 if (cmp.compare(last, this.myarray[j]) < 0) {
417 last = this.myarray[i] = this.myarray[j];
418 i++;
419 }
420 }
421
422 this.nbelem = i;
423 }
424
425
426
427
428
429
430 @Override
431 public boolean equals(Object obj) {
432 if (obj instanceof IVec<?>) {
433 IVec<?> v = (IVec<?>) obj;
434 if (v.size() != size()) {
435 return false;
436 }
437 for (int i = 0; i < size(); i++) {
438 if (!v.get(i).equals(get(i))) {
439 return false;
440 }
441 }
442 return true;
443 }
444 return false;
445 }
446
447
448
449
450
451
452 @Override
453 public int hashCode() {
454 int sum = 0;
455 for (int i = 0; i < this.nbelem; i++) {
456 sum += this.myarray[i].hashCode() / this.nbelem;
457 }
458 return sum;
459 }
460
461 public Iterator<T> iterator() {
462 return new Iterator<T>() {
463 private int i = 0;
464
465 public boolean hasNext() {
466 return this.i < Vec.this.nbelem;
467 }
468
469 public T next() {
470 if (this.i == Vec.this.nbelem) {
471 throw new NoSuchElementException();
472 }
473 return Vec.this.myarray[this.i++];
474 }
475
476 public void remove() {
477 throw new UnsupportedOperationException();
478 }
479 };
480 }
481
482 public boolean isEmpty() {
483 return this.nbelem == 0;
484 }
485
486
487
488
489 public boolean contains(T e) {
490 for (int i = 0; i < this.nbelem; i++) {
491 if (this.myarray[i].equals(e)) {
492 return true;
493 }
494 }
495 return false;
496 }
497
498
499
500
501 public int indexOf(T element) {
502 for (int i = 0; i < this.nbelem; i++) {
503 if (this.myarray[i].equals(element)) {
504 return i;
505 }
506 }
507 return -1;
508 }
509 }