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