Example #1
0
  // Remplit les cycles avec le generateur LFSR.
  // Le nombre de valeurs dans l'ensemble des cycles est de 2^(k1+k2).
  // Cette methode fonctionne pour tous les polynomes, meme ceux dont
  // la periode n'est pas maximale.
  private void fillCyclesLFSR() {
    int n = 1 << (k1 + k2); // Nombre de points dans l'ensemble.
    IntArrayList c; // Array used to store the current cycle.
    int i;
    boolean stateVisited[] = new boolean[n];

    // Indicates which states have been visited so far.
    for (i = 0; i < n; i++) stateVisited[i] = false;
    int startState = 0; // First state of the cycle currently considered.
    numPoints = 0;
    while (startState < n) {
      stateVisited[startState] = true;
      c = new IntArrayList();
      c.add(value);
      nextState();
      while (state != startState) {
        stateVisited[state] = true;
        c.add(value);
        nextState();
      }
      addCycle(c);
      //         System.out.println("Size of Cycle: " + c.size());
      for (i = startState + 1; i < n; i++) if (stateVisited[i] == false) break;
      startState = i;
      validateState(i);
    }
  }
Example #2
0
 public long[] getIDsForClass(Transaction trans, ClassMetadata clazz) {
   final IntArrayList ids = new IntArrayList();
   clazz
       .index()
       .traverseIds(
           trans,
           new Visitor4() {
             public void visit(Object obj) {
               ids.add(((Integer) obj).intValue());
             }
           });
   return ids.asLong();
 }
  @Test
  public void testStrangeRetainAllCase() {

    IntArrayList initialElements =
        IntArrayList.wrap(
            new int[] {
              586, 940, 1086, 1110, 1168, 1184, 1185, 1191, 1196, 1229, 1237, 1241, 1277, 1282,
              1284, 1299, 1308, 1309, 1310, 1314, 1328, 1360, 1366, 1370, 1378, 1388, 1392, 1402,
              1406, 1411, 1426, 1437, 1455, 1476, 1489, 1513, 1533, 1538, 1540, 1541, 1543, 1547,
              1548, 1551, 1557, 1568, 1575, 1577, 1582, 1583, 1584, 1588, 1591, 1592, 1601, 1610,
              1618, 1620, 1633, 1635, 1653, 1654, 1655, 1660, 1661, 1665, 1674, 1686, 1688, 1693,
              1700, 1705, 1717, 1720, 1732, 1739, 1740, 1745, 1746, 1752, 1754, 1756, 1765, 1766,
              1767, 1771, 1772, 1781, 1789, 1790, 1793, 1801, 1806, 1823, 1825, 1827, 1828, 1829,
              1831, 1832, 1837, 1839, 1844, 2962, 2969, 2974, 2990, 3019, 3023, 3029, 3030, 3052,
              3072, 3074, 3075, 3093, 3109, 3110, 3115, 3116, 3125, 3137, 3142, 3156, 3160, 3176,
              3180, 3188, 3193, 3198, 3207, 3209, 3210, 3213, 3214, 3221, 3225, 3230, 3231, 3236,
              3240, 3247, 3261, 4824, 4825, 4834, 4845, 4852, 4858, 4859, 4867, 4871, 4883, 4886,
              4887, 4905, 4907, 4911, 4920, 4923, 4924, 4925, 4934, 4942, 4953, 4957, 4965, 4973,
              4976, 4980, 4982, 4990, 4993, 6938, 6949, 6953, 7010, 7012, 7034, 7037, 7049, 7076,
              7094, 7379, 7384, 7388, 7394, 7414, 7419, 7458, 7459, 7466, 7467
            });

    IntArrayList retainElements = IntArrayList.wrap(new int[] {586});

    // Initialize both implementations with the same data
    IntOpenHashSet instance = new IntOpenHashSet(initialElements);
    IntRBTreeSet referenceInstance = new IntRBTreeSet(initialElements);

    instance.retainAll(retainElements);
    referenceInstance.retainAll(retainElements);

    // print the correct result {586}
    // System.out.println("ref: " + referenceInstance);

    // prints {586, 7379}, which is clearly wrong
    // System.out.println("ohm: " + instance);

    // Fails
    assertEquals(referenceInstance, instance);
  }
 @SuppressWarnings("unchecked")
 public void remove() {
   if (last == -1) throw new IllegalStateException();
   if (pos < -1) {
     // We're removing wrapped entries.
     Int2DoubleOpenHashMap.this.remove(wrapped.getInt(-pos - 2));
     last = -1;
     return;
   }
   size--;
   if (shiftKeys(last) == pos && c > 0) {
     c++;
     nextEntry();
   }
   last = -1; // You can no longer remove this entry.
   if (ASSERTS) checkTable();
 }
 /**
  * Shifts left entries with the specified hash code, starting at the specified position, and
  * empties the resulting free entry. If any entry wraps around the table, instantiates lazily
  * {@link #wrapped} and stores the entry key.
  *
  * @param pos a starting position.
  * @return the position cleared by the shifting process.
  */
 protected final int shiftKeys(int pos) {
   // Shift entries with the same hash.
   int last, slot;
   for (; ; ) {
     pos = ((last = pos) + 1) & mask;
     while (used[pos]) {
       slot = (it.unimi.dsi.fastutil.HashCommon.murmurHash3((key[pos]))) & mask;
       if (last <= pos ? last >= slot || slot > pos : last >= slot && slot > pos) break;
       pos = (pos + 1) & mask;
     }
     if (!used[pos]) break;
     if (pos < last) {
       // Wrapped entry.
       if (wrapped == null) wrapped = new IntArrayList();
       wrapped.add(key[pos]);
     }
     key[last] = key[pos];
     value[last] = value[pos];
   }
   used[last] = false;
   return last;
 }
 public int nextEntry() {
   if (!hasNext()) throw new NoSuchElementException();
   c--;
   // We are just enumerating elements from the wrapped list.
   if (pos < 0) {
     final int k = wrapped.getInt(-(last = --pos) - 2);
     // The starting point.
     int pos = (it.unimi.dsi.fastutil.HashCommon.murmurHash3((k))) & mask;
     // There's always an unused entry.
     while (used[pos]) {
       if (((key[pos]) == (k))) return pos;
       pos = (pos + 1) & mask;
     }
   }
   last = pos;
   // System.err.println( "Count: " + c );
   if (c != 0) {
     final boolean used[] = Int2DoubleOpenHashMap.this.used;
     while (pos-- != 0 && !used[pos]) ;
     // When here pos < 0 there are no more elements to be enumerated by scanning, but wrapped
     // might be nonempty.
   }
   return last;
 }
  /**
   * Returns the best cut of a graph w.r.t. the degree of dissimilarity between points of different
   * partitions and the degree of similarity between points of the same partition.
   *
   * @param W the weight matrix of the graph
   * @return an array of two elements, each of these contains the points of a partition
   */
  protected static int[][] bestCut(DoubleMatrix2D W) {
    int n = W.columns();
    // Builds the diagonal matrices D and D^(-1/2) (represented as their diagonals)
    DoubleMatrix1D d = DoubleFactory1D.dense.make(n);
    DoubleMatrix1D d_minus_1_2 = DoubleFactory1D.dense.make(n);
    for (int i = 0; i < n; i++) {
      double d_i = W.viewRow(i).zSum();
      d.set(i, d_i);
      d_minus_1_2.set(i, 1 / Math.sqrt(d_i));
    }
    DoubleMatrix2D D = DoubleFactory2D.sparse.diagonal(d);

    // System.out.println("DoubleMatrix2D :\n"+D.toString());

    DoubleMatrix2D X = D.copy();

    // System.out.println("DoubleMatrix2D copy :\n"+X.toString());

    // X = D^(-1/2) * (D - W) * D^(-1/2)
    X.assign(W, Functions.minus);
    // System.out.println("DoubleMatrix2D X: (D-W) :\n"+X.toString());
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        X.set(i, j, X.get(i, j) * d_minus_1_2.get(i) * d_minus_1_2.get(j));

    // Computes the eigenvalues and the eigenvectors of X
    EigenvalueDecomposition e = new EigenvalueDecomposition(X);
    DoubleMatrix1D lambda = e.getRealEigenvalues();

    // Selects the eigenvector z_2 associated with the second smallest eigenvalue
    // Creates a map that contains the pairs <index, eigenvalue>
    AbstractIntDoubleMap map = new OpenIntDoubleHashMap(n);
    for (int i = 0; i < n; i++) map.put(i, Math.abs(lambda.get(i)));
    IntArrayList list = new IntArrayList();
    // Sorts the map on the value
    map.keysSortedByValue(list);
    // Gets the index of the second smallest element
    int i_2 = list.get(1);

    // y_2 = D^(-1/2) * z_2
    DoubleMatrix1D y_2 = e.getV().viewColumn(i_2).copy();
    y_2.assign(d_minus_1_2, Functions.mult);

    // Creates a map that contains the pairs <i, y_2[i]>
    map.clear();
    for (int i = 0; i < n; i++) map.put(i, y_2.get(i));
    // Sorts the map on the value
    map.keysSortedByValue(list);
    // Search the element in the map previuosly ordered that minimizes the cut
    // of the partition
    double best_cut = Double.POSITIVE_INFINITY;
    int[][] partition = new int[2][];

    // The array v contains all the elements of the graph ordered by their
    // projection on vector y_2
    int[] v = list.elements();
    // For each admissible splitting point i
    for (int i = 1; i < n; i++) {
      // The array a contains all the elements that have a projection on vector
      // y_2 less or equal to the one of i-th element
      // The array b contains the remaining elements
      int[] a = new int[i];
      int[] b = new int[n - i];
      System.arraycopy(v, 0, a, 0, i);
      System.arraycopy(v, i, b, 0, n - i);
      double cut = Ncut(W, a, b, v);
      if (cut < best_cut) {
        best_cut = cut;
        partition[0] = a;
        partition[1] = b;
      }
    }

    // System.out.println("Partition:");
    // UtilsJS.printMatrix(partition);

    return partition;
  }