Clover Coverage Report
Coverage timestamp: mer. juin 27 2007 07:27:16 CEST
162   595   36   2,53
82   371   0,61   64
64     1,55  
1    
 
  VecInt       Line # 46 162 36 58,8% 0.58766234
 
  (206)
 
1    /*
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2006 Daniel Le Berre
3    *
4    * Based on the original minisat specification from:
5    *
6    * An extensible SAT solver. Niklas E?n and Niklas S?rensson. Proceedings of the
7    * Sixth International Conference on Theory and Applications of Satisfiability
8    * Testing, LNCS 2919, pp 502-518, 2003.
9    *
10    * This library is free software; you can redistribute it and/or modify it under
11    * the terms of the GNU Lesser General Public License as published by the Free
12    * Software Foundation; either version 2.1 of the License, or (at your option)
13    * any later version.
14    *
15    * This library is distributed in the hope that it will be useful, but WITHOUT
16    * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17    * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18    * details.
19    *
20    * You should have received a copy of the GNU Lesser General Public License
21    * along with this library; if not, write to the Free Software Foundation, Inc.,
22    * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23    *
24    */
25   
26    package org.sat4j.core;
27   
28    import java.io.Serializable;
29    import java.util.Iterator;
30    import java.util.NoSuchElementException;
31    import java.util.Random;
32   
33    import org.sat4j.specs.IVecInt;
34   
35    /*
36    * Created on 9 oct. 2003
37    */
38   
39    /**
40    * A vector specific for primitive integers, widely used in the solver. Note
41    * that if the vector has a sort method, the operations on the vector DO NOT
42    * preserve sorting.
43    *
44    * @author leberre
45    */
 
46    public class VecInt implements Serializable, IVecInt {
47   
48    private static final long serialVersionUID = 1L;
49   
50    private static final int RANDOM_SEED = 91648253;
51   
52    @SuppressWarnings("PMD")
53    public static final IVecInt EMPTY = new VecInt() {
54   
55    /**
56    *
57    */
58    private static final long serialVersionUID = 1L;
59   
 
60  0 toggle @Override
61    public int size() {
62  0 return 0;
63    }
64   
 
65  0 toggle @Override
66    public void shrink(int nofelems) {
67    }
68   
 
69  0 toggle @Override
70    public void shrinkTo(int newsize) {
71    }
72   
 
73  0 toggle @Override
74    public IVecInt pop() {
75  0 throw new UnsupportedOperationException();
76    }
77   
 
78  0 toggle @Override
79    public void growTo(int newsize, int pad) {
80    }
81   
 
82  0 toggle @Override
83    public void ensure(int nsize) {
84    }
85   
 
86  0 toggle @Override
87    public IVecInt push(int elem) {
88  0 throw new UnsupportedOperationException();
89    }
90   
 
91  0 toggle @Override
92    public void unsafePush(int elem) {
93  0 throw new UnsupportedOperationException();
94    }
95   
 
96  0 toggle @Override
97    public void clear() {
98    }
99   
 
100  0 toggle @Override
101    public int last() {
102  0 throw new UnsupportedOperationException();
103    }
104   
 
105  0 toggle @Override
106    public int get(int i) {
107  0 throw new UnsupportedOperationException();
108    }
109   
 
110  0 toggle @Override
111    public void set(int i, int o) {
112  0 throw new UnsupportedOperationException();
113    }
114   
 
115  0 toggle @Override
116    public boolean contains(int e) {
117  0 return false;
118    }
119   
 
120  0 toggle @Override
121    public void copyTo(IVecInt copy) {
122  0 throw new UnsupportedOperationException();
123    }
124   
 
125  0 toggle @Override
126    public void copyTo(int[] is) {
127    }
128   
 
129  0 toggle @Override
130    public void moveTo(IVecInt dest) {
131    }
132   
 
133  0 toggle @Override
134    public void moveTo2(IVecInt dest) {
135    }
136   
 
137  0 toggle @Override
138    public void moveTo(int[] dest) {
139    }
140   
 
141  0 toggle @Override
142    public void insertFirst(int elem) {
143  0 throw new UnsupportedOperationException();
144    }
145   
 
146  0 toggle @Override
147    public void remove(int elem) {
148  0 throw new UnsupportedOperationException();
149    }
150   
 
151  0 toggle @Override
152    public int delete(int i) {
153  0 throw new UnsupportedOperationException();
154    }
155   
 
156  0 toggle @Override
157    public void sort() {
158    }
159   
 
160  0 toggle @Override
161    public void sortUnique() {
162    }
163    };
164   
 
165  23639988 toggle public VecInt() {
166  23639988 this(5);
167    }
168   
 
169  24700719 toggle public VecInt(int size) {
170  24700719 myarray = new int[size];
171    }
172   
173    /**
174    * Adapter method to translate an array of int into an IVecInt.
175    *
176    * The array is used inside the VecInt, so the elements may be modified
177    * outside the VecInt. But it should not take much memory.The size of the
178    * created VecInt is the length of the array.
179    *
180    * @param lits
181    * a filled array of int.
182    */
 
183  473702 toggle public VecInt(int[] lits) {
184  473702 myarray = lits;
185  473702 nbelem = lits.length;
186    }
187   
188    /**
189    * Build a vector of a given initial size filled with an integer.
190    *
191    * @param size
192    * the initial size of the vector
193    * @param pad
194    * the integer to fill the vector with
195    */
 
196  1005312 toggle public VecInt(int size, int pad) {
197  1005312 myarray = new int[size];
198  4092760 for (int i = 0; i < size; i++) {
199  3087448 myarray[i] = pad;
200    }
201  1005312 nbelem = size;
202    }
203   
 
204    toggle public int size() {
205    return nbelem;
206    }
207   
208    /**
209    * Remove the latest nofelems elements from the vector
210    *
211    * @param nofelems
212    */
 
213  286770197 toggle public void shrink(int nofelems) {
214  286770197 assert nofelems >= 0;
215  286770197 assert nofelems <= size();
216  286770197 nbelem -= nofelems;
217    }
218   
 
219  0 toggle public void shrinkTo(int newsize) {
220  0 assert newsize >= 0;
221  0 assert newsize < nbelem;
222  0 nbelem = newsize;
223    }
224   
225    /**
226    * depile le dernier element du vecteur. Si le vecteur est vide, ne
227    * fait rien.
228    */
 
229  618739306 toggle public IVecInt pop() {
230  618739306 assert size() != 0;
231  618739306 --nbelem;
232  618739306 return this;
233    }
234   
 
235  1283 toggle public void growTo(int newsize, final int pad) {
236  1283 assert newsize > size();
237  1283 ensure(newsize);
238  892708 while (--newsize >= 0) {
239  891425 myarray[nbelem++] = pad;
240    }
241    }
242   
 
243    toggle public void ensure(int nsize) {
244    if (nsize >= myarray.length) {
245  13119752 int[] narray = new int[Math.max(nsize, nbelem * 2)];
246  13119752 System.arraycopy(myarray, 0, narray, 0, nbelem);
247  13119752 myarray = narray;
248    }
249    }
250   
 
251    toggle public IVecInt push(int elem) {
252    ensure(nbelem + 1);
253    myarray[nbelem++] = elem;
254    return this;
255    }
256   
 
257  24415122 toggle public void unsafePush(int elem) {
258  24415122 myarray[nbelem++] = elem;
259    }
260   
 
261    toggle public void clear() {
262    nbelem = 0;
263    }
264   
 
265  283616762 toggle public int last() {
266  283616762 assert nbelem > 0;
267  283616762 return myarray[nbelem - 1];
268    }
269   
 
270  2108651424 toggle public int get(int i) {
271  2108651424 assert i >= 0 && i < nbelem;
272  2108651424 return myarray[i];
273    }
274   
 
275  266831296 toggle public int unsafeGet(int i) {
276  266831296 return myarray[i];
277    }
278   
 
279    toggle public void set(int i, int o) {
280    assert i >= 0 && i < nbelem;
281    myarray[i] = o;
282    }
283   
 
284  435116969 toggle public boolean contains(int e) {
285  435116969 final int[] workArray = myarray; // dvh, faster access
286  1341721545 for (int i = 0; i < nbelem; i++) {
287  1047883483 if (workArray[i] == e)
288  141278907 return true;
289    }
290  293838062 return false;
291    }
292   
293    /**
294    * C'est operations devraient se faire en temps constant. Ce n'est pas le
295    * cas ici.
296    *
297    * @param copy
298    */
 
299  22057650 toggle public void copyTo(IVecInt copy) {
300    /*
301    * int nsize = nbelem + copy.nbelem; copy.ensure(nsize); for (int i = 0;
302    * i < nbelem; i++) { copy.myarray[i + copy.nbelem] = myarray[i]; }
303    * copy.nbelem = nsize;
304    */
305  22057650 VecInt ncopy = (VecInt) copy;
306  22057650 int nsize = nbelem + ncopy.nbelem;
307  22057650 ncopy.ensure(nsize);
308  22057650 System.arraycopy(myarray, 0, ncopy.myarray, ncopy.nbelem, nbelem);
309  22057650 ncopy.nbelem = nsize;
310    }
311   
312    /**
313    * @param is
314    */
 
315  2897309 toggle public void copyTo(int[] is) {
316  2897309 assert is.length >= nbelem;
317  2897309 System.arraycopy(myarray, 0, is, 0, nbelem);
318    }
319   
320    /*
321    * Copie un vecteur dans un autre (en vidant le premier), en temps constant.
322    */
 
323  0 toggle public void moveTo(IVecInt dest) {
324  0 copyTo(dest);
325  0 nbelem = 0;
326    }
327   
 
328  0 toggle public void moveTo2(IVecInt dest) {
329  0 VecInt ndest = (VecInt) dest;
330  0 int s = ndest.nbelem;
331  0 int tmp[] = ndest.myarray;
332  0 ndest.myarray = myarray;
333  0 ndest.nbelem = nbelem;
334  0 myarray = tmp;
335  0 nbelem = s;
336  0 nbelem = 0;
337    }
338   
 
339  841994868 toggle public void moveTo(int dest, int source) {
340  841994868 myarray[dest] = myarray[source];
341    }
342   
 
343  186668397 toggle public void moveTo(int[] dest) {
344  186668397 System.arraycopy(myarray, 0, dest, 0, nbelem);
345  186668397 nbelem = 0;
346    }
347   
348    /**
349    * Insert an element at the very begining of the vector. The former first
350    * element is appended to the end of the vector in order to have a constant
351    * time operation.
352    *
353    * @param elem
354    * the element to put first in the vector.
355    */
 
356  0 toggle public void insertFirst(final int elem) {
357  0 if (nbelem > 0) {
358  0 push(myarray[0]);
359  0 myarray[0] = elem;
360  0 return;
361    }
362  0 push(elem);
363    }
364   
365    /**
366    * Enleve un element qui se trouve dans le vecteur!!!
367    *
368    * @param elem
369    * un element du vecteur
370    */
 
371  10310224 toggle public void remove(int elem) {
372  10310224 assert size() > 0;
373  10310224 int j = 0;
374  28515130 for (; myarray[j] != elem; j++) {
375  18204906 assert j < size();
376    }
377  10310224 System.arraycopy(myarray, j + 1, myarray, j, size() - j);
378  10310224 pop();
379    }
380   
381    /**
382    * Delete the ith element of the vector. The latest element of the vector
383    * replaces the removed element at the ith indexer.
384    *
385    * @param i
386    * the indexer of the element in the vector
387    * @return the former ith element of the vector that is now removed from the
388    * vector
389    */
 
390  504 toggle public int delete(int i) {
391  504 assert i >= 0 && i < nbelem;
392  504 int ith = myarray[i];
393  504 myarray[i] = myarray[--nbelem];
394  504 return ith;
395    }
396   
397    private int nbelem;
398   
399    private int[] myarray;
400   
401    /*
402    * (non-Javadoc)
403    *
404    * @see java.lang.int#toString()
405    */
 
406  101 toggle @Override
407    public String toString() {
408  101 StringBuffer stb = new StringBuffer();
409  1833 for (int i = 0; i < nbelem - 1; i++) {
410  1732 stb.append(myarray[i]);
411  1732 stb.append(","); //$NON-NLS-1$
412    }
413  101 if (nbelem > 0) {
414  101 stb.append(myarray[nbelem - 1]);
415    }
416  101 return stb.toString();
417    }
418   
419    private static Random rand = new Random(RANDOM_SEED);
420   
 
421  4143255 toggle void selectionSort(int from, int to) {
422  4143255 int i, j, best_i;
423  4143255 int tmp;
424   
425  12694792 for (i = from; i < to - 1; i++) {
426  8551537 best_i = i;
427  33008000 for (j = i + 1; j < to; j++) {
428  24456463 if (myarray[j] < myarray[best_i])
429  1339540 best_i = j;
430    }
431  8551537 tmp = myarray[i];
432  8551537 myarray[i] = myarray[best_i];
433  8551537 myarray[best_i] = tmp;
434    }
435    }
436   
 
437  4315904 toggle void sort(int from, int to) {
438  4315904 int width = to - from;
439  4315904 if (to - from <= 15)
440  4143255 selectionSort(from, to);
441   
442    else {
443  172649 final int[] locarray = myarray;
444  172649 int pivot = locarray[rand.nextInt(width) + from];
445  172649 int tmp;
446  172649 int i = from - 1;
447  172649 int j = to;
448   
449  172649 for (;;) {
450  389785 do
451  1743548 i++;
452  1743548 while (locarray[i] < pivot);
453  389785 do
454  1725618 j--;
455  1725618 while (pivot < locarray[j]);
456   
457  389785 if (i >= j)
458  172649 break;
459   
460  217136 tmp = locarray[i];
461  217136 locarray[i] = locarray[j];
462  217136 locarray[j] = tmp;
463    }
464   
465  172649 sort(from, i);
466  172649 sort(i, to);
467    }
468    }
469   
470    /**
471    * sort the vector using a custom quicksort.
472    */
 
473  0 toggle public void sort() {
474  0 sort(0, nbelem);
475    }
476   
 
477  3970616 toggle public void sortUnique() {
478  3970616 int i, j;
479  3970616 int last;
480  3970616 if (nbelem == 0)
481  10 return;
482   
483  3970606 sort(0, nbelem);
484  3970606 i = 1;
485  3970606 int[] locarray = myarray;
486  3970606 last = locarray[0];
487  12687384 for (j = 1; j < nbelem; j++) {
488  8716778 if (last < locarray[j]) {
489  8716778 last = locarray[i] = locarray[j];
490  8716778 i++;
491    }
492    }
493   
494  3970606 nbelem = i;
495    }
496   
497    /**
498    * Two vectors are equals iff they have the very same elements in the order.
499    *
500    * @param obj
501    * an object
502    * @return true iff obj is a VecInt and has the same elements as this vector
503    * at each index.
504    *
505    * @see java.lang.Object#equals(java.lang.Object)
506    */
 
507  23 toggle @Override
508    public boolean equals(Object obj) {
509  23 if (obj instanceof VecInt) {
510  23 VecInt v = (VecInt) obj;
511  23 if (v.nbelem != nbelem)
512  0 return false;
513  336 for (int i = 0; i < nbelem; i++) {
514  319 if (v.myarray[i] != myarray[i]) {
515  6 return false;
516    }
517    }
518  17 return true;
519    }
520  0 return false;
521    }
522   
523    /*
524    * (non-Javadoc)
525    *
526    * @see java.lang.Object#hashCode()
527    */
 
528  0 toggle @Override
529    public int hashCode() {
530  0 long sum = 0;
531  0 for (int i = 0; i < nbelem; i++) {
532  0 sum += myarray[i];
533    }
534  0 return (int) sum / nbelem;
535    }
536   
537    /*
538    * (non-Javadoc)
539    *
540    * @see org.sat4j.specs.IVecInt2#pushAll(org.sat4j.specs.IVecInt2)
541    */
 
542  0 toggle public void pushAll(IVecInt vec) {
543  0 VecInt nvec = (VecInt) vec;
544  0 int nsize = nbelem + nvec.nbelem;
545  0 ensure(nsize);
546  0 System.arraycopy(nvec.myarray, 0, myarray, nbelem, nvec.nbelem);
547  0 nbelem = nsize;
548    }
549   
550    /**
551    * to detect that the vector is a subset of another one. Note that the
552    * method assumes that the two vectors are sorted!
553    *
554    * @param vec
555    * a vector
556    * @return true iff the current vector is a subset of vec
557    */
 
558  0 toggle public boolean isSubsetOf(VecInt vec) {
559  0 int i = 0;
560  0 int j = 0;
561  0 while ((i < this.nbelem) && (j < vec.nbelem)) {
562  0 while ((j < vec.nbelem) && (vec.myarray[j] < this.myarray[i])) {
563  0 j++;
564    }
565  0 if (j == vec.nbelem || this.myarray[i] != vec.myarray[j])
566  0 return false;
567  0 i++;
568    }
569  0 return true;
570    }
571   
 
572  40407309 toggle public Iterator<Integer> iterator() {
573  40407309 return new Iterator<Integer>() {
574    private int i = 0;
575   
 
576  162570787 toggle public boolean hasNext() {
577  162570787 return i < nbelem;
578    }
579   
 
580  124726731 toggle public Integer next() {
581  124726731 if (i == nbelem)
582  0 throw new NoSuchElementException();
583  124726731 return myarray[i++];
584    }
585   
 
586  0 toggle public void remove() {
587  0 throw new UnsupportedOperationException();
588    }
589    };
590    }
591   
 
592  18427115 toggle public boolean isEmpty() {
593  18427115 return nbelem == 0;
594    }
595    }