Clover coverage report -
Coverage timestamp: jeu. juin 15 2006 08:24:33 CEST
file stats: LOC: 593   Methods: 64
NCLOC: 377   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
VecInt.java 67,2% 63,6% 46,9% 60,6%
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.Iterator;
 32    import java.util.NoSuchElementException;
 33    import java.util.Random;
 34   
 35    import org.sat4j.specs.IVecInt;
 36   
 37    /*
 38    * Created on 9 oct. 2003
 39    */
 40   
 41    /**
 42    * A vector specific for primitive integers, widely used in the solver.
 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 @Override
 61    public int size() {
 62  0 return 0;
 63    }
 64   
 65  0 @Override
 66    public void shrink(int nofelems) {
 67    }
 68   
 69  0 @Override
 70    public void shrinkTo(int newsize) {
 71    }
 72   
 73  0 @Override
 74    public IVecInt pop() {
 75  0 throw new UnsupportedOperationException();
 76    }
 77   
 78  0 @Override
 79    public void growTo(int newsize, int pad) {
 80    }
 81   
 82  0 @Override
 83    public void ensure(int nsize) {
 84    }
 85   
 86  0 @Override
 87    public IVecInt push(int elem) {
 88  0 throw new UnsupportedOperationException();
 89    }
 90   
 91  0 @Override
 92    public void unsafePush(int elem) {
 93  0 throw new UnsupportedOperationException();
 94    }
 95   
 96  0 @Override
 97    public void clear() {
 98    }
 99   
 100  0 @Override
 101    public int last() {
 102  0 throw new UnsupportedOperationException();
 103    }
 104   
 105  0 @Override
 106    public int get(int i) {
 107  0 throw new UnsupportedOperationException();
 108    }
 109   
 110  0 @Override
 111    public void set(int i, int o) {
 112  0 throw new UnsupportedOperationException();
 113    }
 114   
 115  0 @Override
 116    public boolean contains(int e) {
 117  0 return false;
 118    }
 119   
 120  0 @Override
 121    public void copyTo(IVecInt copy) {
 122  0 throw new UnsupportedOperationException();
 123    }
 124   
 125  0 @Override
 126    public void copyTo(int[] is) {
 127    }
 128   
 129  0 @Override
 130    public void moveTo(IVecInt dest) {
 131    }
 132   
 133  0 @Override
 134    public void moveTo2(IVecInt dest) {
 135    }
 136   
 137  0 @Override
 138    public void moveTo(int[] dest) {
 139    }
 140   
 141  0 @Override
 142    public void insertFirst(int elem) {
 143  0 throw new UnsupportedOperationException();
 144    }
 145   
 146  0 @Override
 147    public void remove(int elem) {
 148  0 throw new UnsupportedOperationException();
 149    }
 150   
 151  0 @Override
 152    public int delete(int i) {
 153  0 throw new UnsupportedOperationException();
 154    }
 155   
 156  0 @Override
 157    public void sort() {
 158    }
 159   
 160  0 @Override
 161    public void sortUnique() {
 162    }
 163    };
 164   
 165  3056471 public VecInt() {
 166  3056471 this(5);
 167    }
 168   
 169  4114656 public VecInt(int size) {
 170  4114656 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 outside the VecInt.
 177    * But it should not take much memory.The size of
 178    * the created VecInt is the length of the array.
 179    *
 180    * @param lits a filled array of int.
 181    */
 182  472042 public VecInt(int [] lits) {
 183  472042 myarray = lits;
 184  472042 nbelem = lits.length;
 185    }
 186   
 187    /**
 188    * Build a vector of a given initial size filled with an integer.
 189    *
 190    * @param size
 191    * the initial size of the vector
 192    * @param pad
 193    * the integer to fill the vector with
 194    */
 195  1003652 public VecInt(int size, int pad) {
 196  1003652 myarray = new int[size];
 197  1003652 for (int i = 0; i < size; i++) {
 198  3083848 myarray[i] = pad;
 199    }
 200  1003652 nbelem = size;
 201    }
 202   
 203  98186948 public int size() {
 204  98186948 return nbelem;
 205    }
 206   
 207    /**
 208    * Remove the latest nofelems elements from the vector
 209    *
 210    * @param nofelems
 211    */
 212  20735691 public void shrink(int nofelems) {
 213    assert nofelems >= 0;
 214    assert nofelems <= size();
 215  20735691 nbelem -= nofelems;
 216    }
 217   
 218  0 public void shrinkTo(int newsize) {
 219    assert newsize >= 0;
 220    assert newsize < nbelem;
 221  0 nbelem = newsize;
 222    }
 223   
 224    /**
 225    * dï¿œpile le dernier ï¿œlï¿œment du vecteur. Si le vecteur est vide, ne
 226    * fait rien.
 227    */
 228    public IVecInt pop() {
 229    assert size() != 0;
 230    --nbelem;
 231    return this;
 232    }
 233   
 234  1248 public void growTo(int newsize, final int pad) {
 235    assert newsize > size();
 236  1248 ensure(newsize);
 237  1248 while (--newsize >= 0) {
 238  808126 myarray[nbelem++] = pad;
 239    }
 240    }
 241   
 242    public void ensure(int nsize) {
 243    if (nsize >= myarray.length) {
 244  6422159 int[] narray = new int[Math.max(nsize, nbelem * 2)];
 245  6422156 System.arraycopy(myarray, 0, narray, 0, nbelem);
 246  6422156 myarray = narray;
 247    }
 248    }
 249   
 250    public IVecInt push(int elem) {
 251    ensure(nbelem + 1);
 252    myarray[nbelem++] = elem;
 253    return this;
 254    }
 255   
 256  24248988 public void unsafePush(int elem) {
 257  24248988 myarray[nbelem++] = elem;
 258    }
 259   
 260  1555387371 public void clear() {
 261  1555387371 nbelem = 0;
 262    }
 263   
 264    public int last() {
 265    assert nbelem > 0;
 266    return myarray[nbelem - 1];
 267    }
 268   
 269  1939061815 public int get(int i) {
 270    assert i >= 0;
 271    assert i < nbelem;
 272  1939061815 return myarray[i];
 273    }
 274   
 275  226158886 public int unsafeGet(int i) {
 276  226158886 return myarray[i];
 277    }
 278   
 279  140711382 public void set(int i, int o) {
 280    assert i >= 0;
 281    assert i < nbelem;
 282  140711382 myarray[i] = o;
 283    }
 284   
 285  12 public boolean contains(int e) {
 286  12 for (int i = 0; i < nbelem; i++) {
 287  22 if (myarray[i] == e)
 288  10 return true;
 289    }
 290  2 return false;
 291    }
 292   
 293    /**
 294    * C'est opï¿œrations devraient se faire en temps constant. Ce n'est pas le
 295    * cas ici.
 296    *
 297    * @param copy
 298    */
 299  1836963 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  1836963 VecInt ncopy = (VecInt) copy;
 306  1836963 int nsize = nbelem + ncopy.nbelem;
 307  1836963 ncopy.ensure(nsize);
 308  1836963 System.arraycopy(myarray, 0, ncopy.myarray, ncopy.nbelem, nbelem);
 309  1836963 ncopy.nbelem = nsize;
 310    }
 311   
 312    /**
 313    * @param is
 314    */
 315  2945770 public void copyTo(int[] is) {
 316    assert is.length >= nbelem;
 317  2945770 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 public void moveTo(IVecInt dest) {
 324  0 copyTo(dest);
 325  0 nbelem = 0;
 326    }
 327   
 328  0 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  457036862 public void moveTo(int dest, int source) {
 340  457036862 myarray[dest]=myarray[source];
 341    }
 342   
 343  142402510 public void moveTo(int[] dest) {
 344  142402510 System.arraycopy(myarray, 0, dest, 0, nbelem);
 345  142402510 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 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  0 public void remove(int elem) {
 372    assert size() > 0;
 373  0 int j = 0;
 374  0 for (; myarray[j] != elem; j++) {
 375    assert j < size();
 376    }
 377  0 for (; j < size() - 1; j++) {
 378  0 myarray[j] = myarray[j + 1];
 379    }
 380  0 pop();
 381    }
 382   
 383    /**
 384    * Delete the ith element of the vector. The latest element of the vector
 385    * replaces the removed element at the ith indexer.
 386    *
 387    * @param i
 388    * the indexer of the element in the vector
 389    * @return the former ith element of the vector that is now removed from the
 390    * vector
 391    */
 392  528 public int delete(int i) {
 393    assert i >= 0;
 394    assert i < nbelem;
 395  528 int ith = myarray[i];
 396  528 myarray[i] = myarray[--nbelem];
 397  528 return ith;
 398    }
 399   
 400    private int nbelem;
 401   
 402    private int[] myarray;
 403   
 404    /*
 405    * (non-Javadoc)
 406    *
 407    * @see java.lang.int#toString()
 408    */
 409  101 @Override
 410    public String toString() {
 411  101 StringBuffer stb = new StringBuffer();
 412  101 for (int i = 0; i < nbelem - 1; i++) {
 413  1732 stb.append(myarray[i]);
 414  1732 stb.append(","); //$NON-NLS-1$
 415    }
 416  101 if (nbelem > 0) {
 417  101 stb.append(myarray[nbelem - 1]);
 418    }
 419  101 return stb.toString();
 420    }
 421   
 422    private static Random rand = new Random(RANDOM_SEED);
 423   
 424  4139182 void selectionSort(int from, int to) {
 425  4139182 int i, j, best_i;
 426  4139182 int tmp;
 427   
 428  4139182 for (i = from; i < to - 1; i++) {
 429  8545448 best_i = i;
 430  8545448 for (j = i + 1; j < to; j++) {
 431  24450040 if (myarray[j] < myarray[best_i])
 432  87704 best_i = j;
 433    }
 434  8545448 tmp = myarray[i];
 435  8545448 myarray[i] = myarray[best_i];
 436  8545448 myarray[best_i] = tmp;
 437    }
 438    }
 439   
 440  4313153 void sort(int from, int to) {
 441  4313153 int width = to - from;
 442  4313153 if (to - from <= 15)
 443  4139182 selectionSort(from, to);
 444   
 445    else {
 446  173971 int pivot = myarray[rand.nextInt(width) + from];
 447  173971 int tmp;
 448  173971 int i = from - 1;
 449  173971 int j = to;
 450   
 451  173971 for (;;) {
 452  186769 do
 453  1733530 i++;
 454  1733530 while (myarray[i] < pivot);
 455  186769 do
 456  1729488 j--;
 457  1729488 while (pivot < myarray[j]);
 458   
 459  186769 if (i >= j)
 460  173971 break;
 461   
 462  12798 tmp = myarray[i];
 463  12798 myarray[i] = myarray[j];
 464  12798 myarray[j] = tmp;
 465    }
 466   
 467  173971 sort(from, i);
 468  173971 sort(i, to);
 469    }
 470    }
 471   
 472    /**
 473    * sort the vector using a custom quicksort.
 474    */
 475  0 public void sort() {
 476  0 sort(0, nbelem);
 477    }
 478   
 479  3965221 public void sortUnique() {
 480  3965221 int i, j;
 481  3965221 int last;
 482  3965221 if (nbelem == 0)
 483  10 return;
 484   
 485  3965211 sort(0, nbelem);
 486  3965211 i = 1;
 487  3965211 last = myarray[0];
 488  3965211 for (j = 1; j < nbelem; j++) {
 489  8710473 if (last < myarray[j]) {
 490  8710473 last = myarray[i] = myarray[j];
 491  8710473 i++;
 492    }
 493    }
 494   
 495  3965211 nbelem = i;
 496    }
 497   
 498    /*
 499    * (non-Javadoc)
 500    *
 501    * @see java.lang.Object#equals(java.lang.Object)
 502    */
 503  23 @Override
 504    public boolean equals(Object obj) {
 505  23 if (obj instanceof IVecInt) {
 506  23 IVecInt v = (IVecInt) obj;
 507  23 if (v.size() != size())
 508  0 return false;
 509  23 for (int i = 0; i < size(); i++) {
 510  319 if (v.get(i) != get(i)) {
 511  6 return false;
 512    }
 513    }
 514  17 return true;
 515    }
 516  0 return false;
 517    }
 518   
 519    /*
 520    * (non-Javadoc)
 521    *
 522    * @see java.lang.Object#hashCode()
 523    */
 524  0 @Override
 525    public int hashCode() {
 526  0 long sum = 0;
 527  0 for (int i = 0; i < nbelem; i++) {
 528  0 sum += myarray[i];
 529    }
 530  0 return (int)sum/nbelem;
 531    }
 532   
 533    /*
 534    * (non-Javadoc)
 535    *
 536    * @see org.sat4j.specs.IVecInt2#pushAll(org.sat4j.specs.IVecInt2)
 537    */
 538  0 public void pushAll(IVecInt vec) {
 539  0 VecInt nvec = (VecInt) vec;
 540  0 int nsize = nbelem + nvec.nbelem;
 541  0 ensure(nsize);
 542  0 System.arraycopy(nvec.myarray, 0, myarray, nbelem, nvec.nbelem);
 543  0 nbelem = nsize;
 544    }
 545   
 546    /*
 547    * (non-Javadoc)
 548    *
 549    * @see org.sat4j.specs.IVecInt2#isSubsetOf(org.sat4j.specs.IVecInt2)
 550    */
 551  0 public boolean isSubsetOf(IVecInt vec) {
 552  0 boolean isSubSet = (this.size() <= vec.size());
 553   
 554  0 int i = 0;
 555  0 int j = 0;
 556  0 while (isSubSet && (i < this.size()) && (j < vec.size())) {
 557  0 while ((j < vec.size()) && (vec.get(j) < this.get(i))) {
 558  0 j++;
 559    }
 560  0 if (j < vec.size()) {
 561  0 isSubSet = (this.get(i) == vec.get(j));
 562    } else {
 563  0 isSubSet = false;
 564    }
 565  0 i++;
 566    }
 567  0 return isSubSet;
 568    }
 569   
 570  2440 public Iterator<Integer> iterator() {
 571  2440 return new Iterator<Integer>() {
 572    private int i = 0;
 573   
 574  2496 public boolean hasNext() {
 575  2496 return i < nbelem;
 576    }
 577   
 578  59 public Integer next() {
 579  59 if (i == nbelem)
 580  0 throw new NoSuchElementException();
 581  59 return myarray[i++];
 582    }
 583   
 584  0 public void remove() {
 585  0 throw new UnsupportedOperationException();
 586    }
 587    };
 588    }
 589   
 590  0 public boolean isEmpty() {
 591  0 return nbelem==0;
 592    }
 593    }