public void testContains() throws Exception {
   TIntSet s = new TIntHashSet();
   int i = 100;
   s.add(i);
   assertTrue("contains failed", s.contains(i));
   assertFalse("contains failed", s.contains(1000));
 }
  public void testIteratorRemove() {

    TIntSet set = new TIntHashSet();
    set.add(1);
    set.add(2);
    set.add(3);
    set.add(4);

    TIntIterator iter = set.iterator();
    assertTrue("iterator should have a next item", iter.hasNext());

    int last = -1;
    while (iter.hasNext()) {
      int next = iter.next();
      assertTrue(next >= 1 && next <= 4);
      assertTrue(next != last);
      last = next;

      if (next == 3) {
        iter.remove();
      }
    }

    assertFalse("iterator should not have a next item", iter.hasNext());

    assertFalse("set should not contain 3", set.contains(3));
    assertTrue("set should contain 1", set.contains(1));
    assertTrue("set should contain 2", set.contains(2));
    assertTrue("set should contain 4", set.contains(4));
    assertEquals(3, set.size());
  }
  public void testAddAll() throws Exception {

    int[] ints = {1138, 42, 13, 86, 99, 101};

    TIntSet set;

    List<Integer> list = new ArrayList<Integer>();
    for (int element : ints) {
      list.add(Integer.valueOf(element));
    }

    set = new TIntHashSet();
    assertTrue("addAll(Collection<?>) failed: " + set, set.addAll(list));
    for (int element : ints) {
      assertTrue("contains failed: ", set.contains(element));
    }

    set = new TIntHashSet();
    assertTrue("addAll(int[]) failed: " + set, set.addAll(ints));
    for (int element : ints) {
      assertTrue("contains failed: ", set.contains(element));
    }

    TIntSet test_set = new TIntHashSet();
    assertTrue("addAll(TIntSet) failed: " + test_set, test_set.addAll(set));
    for (int element : ints) {
      assertTrue("contains failed: ", set.contains(element));
    }
  }
 public void testRemove() throws Exception {
   TIntSet set = new TIntHashSet();
   set.add(1);
   set.add(2);
   assertTrue("One was not added", set.contains(1));
   assertTrue("One was not removed", set.remove(1));
   assertFalse("One was not removed", set.contains(1));
   assertTrue("Two was also removed", set.contains(2));
 }
  /**
   * All input and output ids are client ids.
   *
   * @param clientId The client id to find most similar items for.
   * @param maxResults The maximum number of neighbors.
   * @param validIds The Wikipedia ids that should be considered in result sets. Null means all ids.
   * @return
   * @throws IOException
   */
  @Override
  public DocScoreList mostSimilar(int clientId, int maxResults, TIntSet validIds)
      throws IOException {
    Leaderboard board = new Leaderboard(maxResults);
    DenseMatrixRow row = similarityMatrix.getRow(clientId);
    if (row == null) {
      return new DocScoreList(0);
    }
    int colIds[] = similarityMatrix.getColIds();
    if (colIds.length != row.getNumCols()) {
      throw new IllegalStateException();
    }
    for (int i = 0; i < colIds.length; i++) {
      if (validIds == null || validIds.contains(colIds[i])) {
        board.tallyScore(colIds[i], row.getColValue(i));
      }
    }
    return board.getTop();

    /*SparseMatrixRow row = mostSimilarMatrix.getRow(clientId);
    if (row == null) {
        return new DocScoreList(0);
    }
    int n = 0;
    DocScoreList top = new DocScoreList(Math.min(maxResults, row.getNumCols()));
    for (int i = 0; i < row.getNumCols() && n < maxResults; i++) {
        int id = row.getColIndex(i);
        if (validIds == null || validIds.contains(id)) {
            top.set(n++, id, row.getColValue(i));
        }
    }
    top.truncate(n);
    return top;*/
  }
예제 #6
0
 @Test
 public void testTestCompleteness() {
   final TIntSet testedOpcodes = new TIntHashSet();
   for (Message message : testMessages) {
     MessageCodec<?> codec = codecLookup.find(message.getClass());
     if (codec != null) {
       testedOpcodes.add(codec.getOpcode());
     }
   }
   for (MessageCodec<?> codec : codecLookup.getCodecs()) {
     assertTrue(
         "Opcode " + codec.getOpcode() + " not tested", testedOpcodes.contains(codec.getOpcode()));
   }
 }
  public void testContainsAll() throws Exception {

    int[] ints = {1138, 42, 13, 86, 99};

    TIntSet set = new TIntHashSet();
    set.addAll(ints);

    TIntSet other = new TIntHashSet();
    other.addAll(ints);

    List<Number> ints_list = new ArrayList<Number>();
    for (int element : ints) {
      ints_list.add(element);
    }

    for (int index = 0; index < ints.length; index++) {
      assertTrue(Integer.valueOf(ints[index]).toString(), set.contains(ints[index]));
    }

    assertTrue("containsAll(Collection<?>) failed: " + set, set.containsAll(ints_list));
    ints_list.remove(Integer.valueOf(42));
    ints_list.add(Long.valueOf(42));
    assertFalse("containsAll(Collection<?>) failed: " + set, set.containsAll(ints_list));

    assertTrue("containsAll(TIntSet) failed (same set): " + set, set.containsAll(set));

    assertTrue("containsAll(TIntSet) failed (other set): " + set, set.containsAll(other));

    assertTrue("containsAll(int[]) failed: " + set, set.containsAll(ints));

    int[] failed = {42, 86, 99, 123456};

    TIntSet failed_set = new TIntHashSet();
    failed_set.addAll(failed);

    List<Integer> failed_list = new ArrayList<Integer>();
    for (int element : failed) {
      failed_list.add(element);
    }

    assertFalse(
        "containsAll(Collection<?>) failed (false positive): " + set, set.containsAll(failed_list));

    assertFalse(
        "containsAll(TIntSet) failed (false positive): " + set, set.containsAll(failed_set));

    assertFalse("containsAll(int[]) failed (false positive): " + set, set.containsAll(failed));
  }
예제 #8
0
 @Override
 public boolean equals(Object other) {
   if (!(other instanceof TIntSet)) {
     return false;
   }
   final TIntSet that = (TIntSet) other;
   if (that.size() != this.size()) {
     return false;
   }
   for (int i = _states.length; i-- > 0; ) {
     if (_states[i] == FULL) {
       if (!that.contains(_set[i])) {
         return false;
       }
     }
   }
   return true;
 }
  private float jaccardValue(Instance instA, Instance instB) {
    TIntSet indicesA = new TIntHashSet(instA.getNumIndices());

    for (int i = 0; i < instA.getNumIndices(); i++) {
      indicesA.add(instA.getIndex(i));
    }

    int sizeOfUnion = indicesA.size();
    int sizeOfIntersection = 0;

    for (int i = 0; i < instB.getNumIndices(); i++) {
      int hostIndex = instB.getIndex(i);
      if (indicesA.contains(hostIndex)) {
        sizeOfIntersection += 1;
      } else {
        sizeOfUnion += 1;
      }
    }

    return (float) sizeOfIntersection / (float) sizeOfUnion;
  }