Clover coverage report -
Coverage timestamp: jeu. sept. 29 2005 23:57:39 CEST
file stats: LOC: 438   Methods: 36
NCLOC: 265   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Vec.java 88,3% 88,7% 86,1% 88,2%
coverage coverage
 1    /*
 2    * SAT4J: a SATisfiability library for Java
 3    * Copyright (C) 2004 Daniel Le Berre
 4    *
 5    * Based on the original minisat specification from:
 6    *
 7    * An extensible SAT solver. Niklas Eï¿œn and Niklas Sï¿œrensson.
 8    * Proceedings of the Sixth International Conference on Theory
 9    * and Applications of Satisfiability Testing, LNCS 2919,
 10    * pp 502-518, 2003.
 11    *
 12    * This library is free software; you can redistribute it and/or
 13    * modify it under the terms of the GNU Lesser General Public
 14    * License as published by the Free Software Foundation; either
 15    * version 2.1 of the License, or (at your option) any later version.
 16    *
 17    * This library is distributed in the hope that it will be useful,
 18    * but WITHOUT ANY WARRANTY; without even the implied warranty of
 19    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 20    * Lesser General Public License for more details.
 21    *
 22    * You should have received a copy of the GNU Lesser General Public
 23    * License along with this library; if not, write to the Free Software
 24    * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 25    *
 26    */
 27   
 28    package org.sat4j.core;
 29   
 30    import java.io.Serializable;
 31    import java.util.Comparator;
 32    import java.util.Iterator;
 33    import java.util.NoSuchElementException;
 34    import java.util.Random;
 35   
 36    import org.sat4j.specs.IVec;
 37   
 38    /**
 39    * Simple but efficient vector implementation, based on the
 40    * vector implementation available in MiniSAT.
 41    *
 42    * @author leberre
 43    */
 44    public class Vec<T> implements Serializable, IVec<T> {
 45   
 46    private static final long serialVersionUID = 1L;
 47   
 48    private static final int RANDOM_SEED = 91648253;
 49   
 50  3381367 public Vec() {
 51  3381367 this(5);
 52    }
 53   
 54  5159648 @SuppressWarnings("unchecked")
 55    public Vec(int size) {
 56  5159648 myarray = (T[]) new Object[size];
 57    }
 58   
 59    /**
 60    * Construit un vecteur contenant de taille size rempli ï¿œ l'aide de size
 61    * pad.
 62    *
 63    * @param size
 64    * la taille du vecteur
 65    * @param pad
 66    * l'objet servant ï¿œ remplir le vecteur
 67    */
 68  15 @SuppressWarnings("unchecked")
 69    public Vec(int size, T pad) {
 70  15 myarray = (T[]) new Object[size];
 71  15 for (int i = 0; i < size; i++) {
 72  59 myarray[i] = pad;
 73    }
 74  15 nbelem = size;
 75    }
 76   
 77    public int size() {
 78    return nbelem;
 79    }
 80   
 81    /**
 82    * Remove nofelems from the Vector. It is assumed that the number of
 83    * elements to remove is smaller or equals to the current number of elements
 84    * in the vector
 85    *
 86    * @param nofelems
 87    * the number of elements to remove.
 88    */
 89  2 public void shrink(int nofelems) {
 90    assert nofelems <= size();
 91  2 while (nofelems-- > 0) {
 92  10 myarray[--nbelem] = null;
 93    }
 94    }
 95   
 96    /**
 97    * reduce the Vector to exactly newsize elements
 98    *
 99    * @param newsize
 100    * the new size of the vector.
 101    */
 102  1983355 public void shrinkTo(final int newsize) {
 103    assert newsize <= size();
 104  1983355 for (int i = nbelem; i > newsize; i--) {
 105  18452626 myarray[i - 1] = null;
 106    }
 107  1983355 nbelem = newsize;
 108    assert size() == newsize;
 109    }
 110   
 111    /**
 112    * Pop the last element on the stack. It is assumed that the stack is not
 113    * empty!
 114    */
 115  1327689782 public void pop() {
 116    assert size() > 0;
 117  1327689782 myarray[--nbelem] = null;
 118    }
 119   
 120  14 public void growTo(final int newsize, final T pad) {
 121    assert newsize >= size();
 122  14 ensure(newsize);
 123  14 for (int i = nbelem; i < newsize; i++) {
 124  94 myarray[i] = pad;
 125    }
 126  14 nbelem = newsize;
 127    }
 128   
 129    @SuppressWarnings("unchecked")
 130    public final void ensure(final int nsize) {
 131    if (nsize >= myarray.length) {
 132  89235485 T[] narray = (T[]) new Object[Math.max(nsize, nbelem * 2)];
 133  89235485 System.arraycopy(myarray, 0, narray, 0, nbelem);
 134  89235485 myarray = narray;
 135    }
 136    }
 137   
 138  1469942100 public IVec<T> push(final T elem) {
 139  1469942100 ensure(nbelem + 1);
 140  1469942100 myarray[nbelem++] = elem;
 141  1469942100 return this;
 142    }
 143   
 144  0 public void unsafePush(final T elem) {
 145  0 myarray[nbelem++] = elem;
 146    }
 147   
 148    /**
 149    * Insert an element at the very begining of the vector. The former first
 150    * element is appended to the end of the vector in order to have a constant
 151    * time operation.
 152    *
 153    * @param elem
 154    * the element to put first in the vector.
 155    */
 156  0 public void insertFirst(final T elem) {
 157  0 if (nbelem > 0) {
 158  0 push(myarray[0]);
 159  0 myarray[0] = elem;
 160  0 return;
 161    }
 162  0 push(elem);
 163    }
 164   
 165  74305 public void insertFirstWithShifting(final T elem) {
 166  74305 if (nbelem > 0) {
 167  10668 ensure(nbelem + 1);
 168  10668 for (int i = nbelem; i > 0; i--) {
 169  48606 myarray[i] = myarray[i - 1];
 170    }
 171  10668 myarray[0] = elem;
 172  10668 nbelem++;
 173  10668 return;
 174    }
 175  63637 push(elem);
 176    }
 177   
 178    public void clear() {
 179    while (nbelem > 0) {
 180  196640622 myarray[--nbelem] = null;
 181    }
 182    }
 183   
 184    /**
 185    * return the latest element on the stack. It is assumed that the stack is
 186    * not empty!
 187    *
 188    * @return the last element on the stack (the one on the top)
 189    */
 190  1290630394 public T last() {
 191    assert size() != 0;
 192  1290630394 return myarray[nbelem - 1];
 193    }
 194   
 195  820260948 public T get(int i) {
 196  820260948 return myarray[i];
 197    }
 198   
 199  179404627 public void set(int i, T o) {
 200  179404627 myarray[i] = o;
 201    }
 202   
 203    /**
 204    * Enleve un element qui se trouve dans le vecteur!!!
 205    *
 206    * @param elem
 207    * un element du vecteur
 208    */
 209  37059412 public void remove(T elem) {
 210    assert size() > 0;
 211  37059412 int j = 0;
 212  37059412 for (; myarray[j] != elem; j++) {
 213    assert j < size();
 214    }
 215  37059412 for (; j < size() - 1; j++) {
 216  762147408 myarray[j] = myarray[j + 1];
 217    }
 218  37059412 pop();
 219    }
 220   
 221    /**
 222    * Delete the ith element of the vector. The latest element of the vector
 223    * replaces the removed element at the ith indexer.
 224    *
 225    * @param i
 226    * the indexer of the element in the vector
 227    * @return the former ith element of the vector that is now removed from the
 228    * vector
 229    */
 230  2 public T delete(int i) {
 231    assert i >= 0;
 232    assert i < nbelem;
 233  2 T ith = myarray[i];
 234  2 myarray[i] = myarray[--nbelem];
 235  2 myarray[nbelem] = null;
 236  2 return ith;
 237    }
 238   
 239    /**
 240    * Ces opï¿œrations devraient se faire en temps constant. Ce n'est pas le
 241    * cas ici.
 242    *
 243    * @param copy
 244    */
 245  1940913040 public void copyTo(IVec<T> copy) {
 246  1940913040 Vec<T> ncopy = (Vec<T>) copy;
 247  1940913040 int nsize = nbelem + ncopy.nbelem;
 248  1940913040 copy.ensure(nsize);
 249  1940913040 for (int i = 0; i < nbelem; i++) {
 250  105434892 ncopy.myarray[i + ncopy.nbelem] = myarray[i];
 251    }
 252  1940913040 ncopy.nbelem = nsize;
 253    }
 254   
 255    /**
 256    * @param dest
 257    */
 258  1621870 public <E> void copyTo(E[] dest) {
 259    assert dest.length >= nbelem;
 260  1621870 System.arraycopy(myarray, 0, dest, 0, nbelem);
 261    }
 262   
 263    /*
 264    * Copie un vecteur dans un autre (en vidant le premier), en temps constant.
 265    */
 266  1939353631 public void moveTo(IVec<T> dest) {
 267  1939353631 copyTo(dest);
 268  1939353631 clear();
 269    }
 270   
 271  0 public void moveTo(int dest, int source) {
 272  0 myarray[dest]=myarray[source];
 273  0 myarray[source]=null;
 274    }
 275   
 276    private int nbelem;
 277   
 278    private T[] myarray;
 279   
 280    /*
 281    * (non-Javadoc)
 282    *
 283    * @see java.lang.Object#toString()
 284    */
 285  74 @Override
 286    public String toString() {
 287  74 StringBuffer stb = new StringBuffer();
 288  74 for (int i = 0; i < nbelem - 1; i++) {
 289  1406 stb.append(myarray[i]);
 290  1406 stb.append(",");
 291    }
 292  74 if (nbelem > 0) {
 293  74 stb.append(myarray[nbelem - 1]);
 294    }
 295  74 return stb.toString();
 296    }
 297   
 298    private static Random rand = new Random(RANDOM_SEED);
 299   
 300  19110499 void selectionSort(int from, int to, Comparator<T> cmp) {
 301  19110499 int i, j, best_i;
 302  19110499 T tmp;
 303   
 304  19110499 for (i = from; i < to - 1; i++) {
 305  169598670 best_i = i;
 306  169598670 for (j = i + 1; j < to; j++) {
 307  952372899 if (cmp.compare(myarray[j], myarray[best_i]) < 0)
 308  48219450 best_i = j;
 309    }
 310  169598670 tmp = myarray[i];
 311  169598670 myarray[i] = myarray[best_i];
 312  169598670 myarray[best_i] = tmp;
 313    }
 314    }
 315   
 316  36254278 void sort(int from, int to, Comparator<T> cmp) {
 317  36254278 int width = to - from;
 318  36254278 if (to - from <= 15)
 319  19110498 selectionSort(from, to, cmp);
 320   
 321    else {
 322  17143780 T pivot = myarray[rand.nextInt(width) + from];
 323  17143780 T tmp;
 324  17143780 int i = from - 1;
 325  17143780 int j = to;
 326   
 327  17143780 for (;;) {
 328  538736423 do
 329  611276225 i++;
 330  611276225 while (cmp.compare(myarray[i], pivot) < 0);
 331  538736423 do
 332  611202347 j--;
 333  611202347 while (cmp.compare(pivot, myarray[j]) < 0);
 334   
 335  538736423 if (i >= j)
 336  17143780 break;
 337   
 338  521592643 tmp = myarray[i];
 339  521592643 myarray[i] = myarray[j];
 340  521592643 myarray[j] = tmp;
 341    }
 342   
 343  17143780 sort(from, i, cmp);
 344  17143780 sort(i, to, cmp);
 345    }
 346    }
 347   
 348    /**
 349    * @param comparator
 350    */
 351  1966717 public void sort(Comparator<T> comparator) {
 352  1966717 sort(0, nbelem, comparator);
 353    }
 354   
 355  1 public void sortUnique(Comparator<T> cmp) {
 356  1 int i, j;
 357  1 T last;
 358   
 359  1 if (nbelem == 0)
 360  0 return;
 361   
 362  1 sort(0, nbelem, cmp);
 363   
 364  1 i = 1;
 365  1 last = myarray[0];
 366  1 for (j = 1; j < nbelem; j++) {
 367  105 if (cmp.compare(last, myarray[j]) < 0) {
 368  100 last = myarray[i] = myarray[j];
 369  100 i++;
 370    }
 371    }
 372   
 373  1 nbelem = i;
 374    }
 375   
 376  4 public static final <A extends Comparable<A>> Comparator<A> defaultComparator() {
 377  4 return new Comparator<A>() {
 378  2559 public int compare(A a, A b) {
 379  2559 return a.compareTo(b);
 380    }
 381    };
 382    }
 383   
 384    /*
 385    * (non-Javadoc)
 386    *
 387    * @see java.lang.Object#equals(java.lang.Object)
 388    */
 389  21 @Override
 390    public boolean equals(Object obj) {
 391  21 if (obj instanceof IVec) {
 392  21 IVec<?> v = (IVec<?>) obj;
 393  21 if (v.size() != size())
 394  2 return false;
 395  19 for (int i = 0; i < size(); i++) {
 396  316 if (!v.get(i).equals(get(i))) {
 397  1 return false;
 398    }
 399    }
 400  18 return true;
 401    }
 402  0 return false;
 403    }
 404   
 405    /*
 406    * (non-Javadoc)
 407    *
 408    * @see java.lang.Object#hashCode()
 409    */
 410  0 @Override
 411    public int hashCode() {
 412  0 int sum = 0;
 413  0 for (int i = 0; i < nbelem; i++) {
 414  0 sum += myarray.hashCode() / nbelem;
 415    }
 416  0 return sum;
 417    }
 418   
 419  2 public Iterator<T> iterator() {
 420  2 return new Iterator<T>() {
 421    private int i = 0;
 422   
 423  5 public boolean hasNext() {
 424  5 return i < nbelem;
 425    }
 426   
 427  4 public T next() {
 428  4 if (i == nbelem)
 429  1 throw new NoSuchElementException();
 430  3 return myarray[i++];
 431    }
 432   
 433  0 public void remove() {
 434  0 throw new UnsupportedOperationException();
 435    }
 436    };
 437    }
 438    }