Clover coverage report -
Coverage timestamp: jeu. juin 15 2006 08:24:33 CEST
file stats: LOC: 457   Methods: 36
NCLOC: 265   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Vec.java 88,3% 88,1% 83,3% 87,4%
coverage coverage
 1    /*
 2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004 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
 7    * the Sixth International Conference on Theory and Applications of
 8    * Satisfiability 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.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    * Simple but efficient vector implementation, based on the vector
 38    * implementation available in MiniSAT. Note that the elements are compared
 39    * using their references, not using the equals method.
 40    *
 41    * @author leberre
 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    * Create a Vector with an initial capacity of 5 elements.
 51    */
 52  6187597 public Vec() {
 53  6187597 this(5);
 54    }
 55   
 56    /**
 57    * Adapter method to translate an array of int into an IVec.
 58    *
 59    * The array is used inside the Vec, so the elements may be modified
 60    * outside the Vec. But it should not take much memory. The size of
 61    * the created Vec is the length of the array.
 62    *
 63    * @param elts
 64    * a filled array of T.
 65    */
 66  397410 public Vec(T[] elts) {
 67  397410 myarray = elts;
 68  397410 nbelem = elts.length;
 69    }
 70   
 71    /**
 72    * Create a Vector with a given capacity.
 73    *
 74    * @param size
 75    * the capacity of the vector.
 76    */
 77  8249411 @SuppressWarnings("unchecked")
 78    public Vec(int size) {
 79  8249411 myarray = (T[]) new Object[size];
 80    }
 81   
 82    /**
 83    * Construit un vecteur contenant de taille size rempli ï¿œ l'aide de size
 84    * pad.
 85    *
 86    * @param size
 87    * la taille du vecteur
 88    * @param pad
 89    * l'objet servant ï¿œ remplir le vecteur
 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    * Remove nofelems from the Vector. It is assumed that the number of
 106    * elements to remove is smaller or equals to the current number of elements
 107    * in the vector
 108    *
 109    * @param nofelems
 110    * the number of elements to remove.
 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    * reduce the Vector to exactly newsize elements
 121    *
 122    * @param newsize
 123    * the new size of the vector.
 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    * Pop the last element on the stack. It is assumed that the stack is not
 136    * empty!
 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    * Insert an element at the very begining of the vector. The former first
 173    * element is appended to the end of the vector in order to have a constant
 174    * time operation.
 175    *
 176    * @param elem
 177    * the element to put first in the vector.
 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    * return the latest element on the stack. It is assumed that the stack is
 209    * not empty!
 210    *
 211    * @return the last element on the stack (the one on the top)
 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    * Enleve un element qui se trouve dans le vecteur!!!
 228    *
 229    * @param elem
 230    * un element du vecteur
 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    * Delete the ith element of the vector. The latest element of the vector
 246    * replaces the removed element at the ith indexer.
 247    *
 248    * @param i
 249    * the indexer of the element in the vector
 250    * @return the former ith element of the vector that is now removed from the
 251    * vector
 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    * Ces opï¿œrations devraient se faire en temps constant. Ce n'est pas le
 264    * cas ici.
 265    *
 266    * @param copy
 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    * @param dest
 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    * Copie un vecteur dans un autre (en vidant le premier), en temps constant.
 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    * (non-Javadoc)
 305    *
 306    * @see java.lang.Object#toString()
 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(","); //$NON-NLS-1$
 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    * @param comparator
 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    * (non-Javadoc)
 401    *
 402    * @see java.lang.Object#equals(java.lang.Object)
 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    * (non-Javadoc)
 422    *
 423    * @see java.lang.Object#hashCode()
 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    }