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