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 assert j < size();
258 }
259
260 System.arraycopy(this.myarray, j + 1, this.myarray, j, size() - j - 1);
261 this.myarray[--this.nbelem] = null;
262 }
263
264
265
266
267
268
269
270
271
272
273 public T delete(int index) {
274
275 T ith = this.myarray[index];
276 this.myarray[index] = this.myarray[--this.nbelem];
277 this.myarray[this.nbelem] = null;
278 return ith;
279 }
280
281
282
283
284
285
286
287 public void copyTo(IVec<T> copy) {
288 final Vec<T> ncopy = (Vec<T>) copy;
289 final int nsize = this.nbelem + ncopy.nbelem;
290 copy.ensure(nsize);
291 System.arraycopy(this.myarray, 0, ncopy.myarray, ncopy.nbelem,
292 this.nbelem);
293 ncopy.nbelem = nsize;
294 }
295
296
297
298
299 public <E> void copyTo(E[] dest) {
300
301 System.arraycopy(this.myarray, 0, dest, 0, this.nbelem);
302 }
303
304
305
306
307 public void moveTo(IVec<T> dest) {
308 copyTo(dest);
309 clear();
310 }
311
312 public void moveTo(int dest, int source) {
313 if (dest != source) {
314 this.myarray[dest] = this.myarray[source];
315 this.myarray[source] = null;
316 }
317 }
318
319 public T[] toArray() {
320
321 return this.myarray;
322 }
323
324 private int nbelem;
325
326 private T[] myarray;
327
328
329
330
331
332
333 @Override
334 public String toString() {
335 StringBuffer stb = new StringBuffer();
336 for (int i = 0; i < this.nbelem - 1; i++) {
337 stb.append(this.myarray[i]);
338 stb.append(",");
339 }
340 if (this.nbelem > 0) {
341 stb.append(this.myarray[this.nbelem - 1]);
342 }
343 return stb.toString();
344 }
345
346 void selectionSort(int from, int to, Comparator<T> cmp) {
347 int i, j, best_i;
348 T tmp;
349
350 for (i = from; i < to - 1; i++) {
351 best_i = i;
352 for (j = i + 1; j < to; j++) {
353 if (cmp.compare(this.myarray[j], this.myarray[best_i]) < 0) {
354 best_i = j;
355 }
356 }
357 tmp = this.myarray[i];
358 this.myarray[i] = this.myarray[best_i];
359 this.myarray[best_i] = tmp;
360 }
361 }
362
363 void sort(int from, int to, Comparator<T> cmp) {
364 int width = to - from;
365 if (width <= 15) {
366 selectionSort(from, to, cmp);
367 } else {
368 T pivot = this.myarray[width / 2 + from];
369 T tmp;
370 int i = from - 1;
371 int j = to;
372
373 for (;;) {
374 do {
375 i++;
376 } while (cmp.compare(this.myarray[i], pivot) < 0);
377 do {
378 j--;
379 } while (cmp.compare(pivot, this.myarray[j]) < 0);
380
381 if (i >= j) {
382 break;
383 }
384
385 tmp = this.myarray[i];
386 this.myarray[i] = this.myarray[j];
387 this.myarray[j] = tmp;
388 }
389
390 sort(from, i, cmp);
391 sort(i, to, cmp);
392 }
393 }
394
395
396
397
398 public void sort(Comparator<T> comparator) {
399 sort(0, this.nbelem, comparator);
400 }
401
402 public void sortUnique(Comparator<T> cmp) {
403 int i, j;
404 T last;
405
406 if (this.nbelem == 0) {
407 return;
408 }
409
410 sort(0, this.nbelem, cmp);
411
412 i = 1;
413 last = this.myarray[0];
414 for (j = 1; j < this.nbelem; j++) {
415 if (cmp.compare(last, this.myarray[j]) < 0) {
416 last = this.myarray[i] = this.myarray[j];
417 i++;
418 }
419 }
420
421 this.nbelem = i;
422 }
423
424
425
426
427
428
429 @Override
430 public boolean equals(Object obj) {
431 if (obj instanceof IVec<?>) {
432 IVec<?> v = (IVec<?>) obj;
433 if (v.size() != size()) {
434 return false;
435 }
436 for (int i = 0; i < size(); i++) {
437 if (!v.get(i).equals(get(i))) {
438 return false;
439 }
440 }
441 return true;
442 }
443 return false;
444 }
445
446
447
448
449
450
451 @Override
452 public int hashCode() {
453 int sum = 0;
454 for (int i = 0; i < this.nbelem; i++) {
455 sum += this.myarray[i].hashCode() / this.nbelem;
456 }
457 return sum;
458 }
459
460 public Iterator<T> iterator() {
461 return new Iterator<T>() {
462 private int i = 0;
463
464 public boolean hasNext() {
465 return this.i < Vec.this.nbelem;
466 }
467
468 public T next() {
469 if (this.i == Vec.this.nbelem) {
470 throw new NoSuchElementException();
471 }
472 return Vec.this.myarray[this.i++];
473 }
474
475 public void remove() {
476 throw new UnsupportedOperationException();
477 }
478 };
479 }
480
481 public boolean isEmpty() {
482 return this.nbelem == 0;
483 }
484
485
486
487
488 public boolean contains(T e) {
489 for (int i = 0; i < this.nbelem; i++) {
490 if (this.myarray[i].equals(e)) {
491 return true;
492 }
493 }
494 return false;
495 }
496
497
498
499
500 public int indexOf(T element) {
501 for (int i = 0; i < this.nbelem; i++) {
502 if (this.myarray[i].equals(element)) {
503 return i;
504 }
505 }
506 return -1;
507 }
508 }