public void testClear() throws Exception {
   TIntSet set = new TIntHashSet();
   set.addAll(new int[] {1, 2, 3});
   assertEquals("size was not 3", 3, set.size());
   set.clear();
   assertEquals("initial size was not 0", 0, set.size());
 }
 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));
 }
  @Override
  protected void freeChunk(Point p) {
    int x = (int) p.getX() >> Chunk.BLOCKS.BITS;
    int y = (int) p.getY() >> Chunk.BLOCKS.BITS; // + SEALEVEL_CHUNK;
    int z = (int) p.getZ() >> Chunk.BLOCKS.BITS;

    if (y < 0 || y >= p.getWorld().getHeight() >> Chunk.BLOCKS.BITS) {
      return;
    }

    TIntSet column = initializedChunks.get(x, z);
    CompressedChunkMessage CCMsg;
    if (column != null) {
      column.remove(y);
      if (column.isEmpty()) {
        if (initializedChunks.remove(x, z) != null) {
          activeChunks.remove(x, z);
        }
        CCMsg = new CompressedChunkMessage(x, z, true, null, null, null, true);
      } else {
        CCMsg = new CompressedChunkMessage(x, z, false, null, null, null, true);
      }

      session.send(false, CCMsg);
    }
  }
 public void testRehashing() throws Exception {
   int size = 10000;
   TIntSet set = new TIntHashSet(10);
   for (int i = 0; i < size; i++) {
     set.add(i);
   }
   assertEquals(set.size(), size);
 }
 public void testToArray() {
   TIntSet set = new TIntHashSet();
   int[] ints = {42, 1138, 13, 86, 99};
   set.addAll(ints);
   int[] res = set.toArray();
   Arrays.sort(ints);
   Arrays.sort(res);
   assertTrue(Arrays.equals(ints, res));
 }
  public void testSize() throws Exception {
    TIntSet set = new TIntHashSet();
    assertEquals("initial size was not 0", 0, set.size());

    for (int i = 0; i < 99; i++) {
      set.add(i);
      assertEquals("size did not increase after add", i + 1, set.size());
    }
  }
 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));
 }
示例#8
0
 private int[] pick(int n, double p) {
   TIntSet set = new TIntHashSet();
   // Pick at least one of the numbers
   set.add(random.nextInt(n));
   // And add some additional ones randomly
   for (int i = 0; i < n; i++) {
     if (random.nextDouble() > p) {
       set.add(i);
     }
   }
   return set.toArray();
 }
示例#9
0
 private static TIntSet readIds(String path) throws IOException {
   TIntSet ids = new TIntHashSet();
   BufferedReader reader = WpIOUtils.openBufferedReader(new File(path));
   while (true) {
     String line = reader.readLine();
     if (line == null) {
       break;
     }
     ids.add(Integer.valueOf(line.trim()));
   }
   reader.close();
   return ids;
 }
  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));
    }
  }
示例#11
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 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 testToArrayMatchesIteratorOrder() {
    TIntSet set = new TIntHashSet();
    int[] ints = {42, 1138, 13, 86, 99};
    set.addAll(ints);
    int[] toarray_ints = set.toArray();

    int[] iter_ints = new int[5];
    TIntIterator iter = set.iterator();

    int index = 0;
    while (iter.hasNext()) {
      iter_ints[index++] = iter.next();
    }

    assertTrue(Arrays.equals(iter_ints, toarray_ints));
  }
  /**
   * 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;*/
  }
  public void testSerialize() throws Exception {
    int[] ints = {1138, 42, 86, 99, 101};

    TIntSet set = new TIntHashSet();
    set.addAll(ints);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(set);

    ByteArrayInputStream bias = new ByteArrayInputStream(baos.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(bias);

    TIntSet deserialized = (TIntSet) ois.readObject();

    assertEquals(set, deserialized);
  }
  @Override
  public void keyPressed(final KeyEvent e) {
    //		System.out.println( "MouseAndKeyHandler.keyPressed()" );
    update();

    if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
      shiftPressed = true;
    } else if (e.getKeyCode() == KeyEvent.VK_META) {
      metaPressed = true;
    } else if (e.getKeyCode() != KeyEvent.VK_ALT
        && e.getKeyCode() != KeyEvent.VK_CONTROL
        && e.getKeyCode() != KeyEvent.VK_ALT_GRAPH) {
      pressedKeys.add(e.getKeyCode());

      final int mask = getMask(e);

      for (final BehaviourEntry<DragBehaviour> drag : keyDrags) {
        if (!activeKeyDrags.contains(drag) && drag.buttons.matches(mask, pressedKeys)) {
          drag.behaviour.init(mouseX, mouseY);
          activeKeyDrags.add(drag);
        }
      }

      for (final BehaviourEntry<ClickBehaviour> click : keyClicks) {
        if (click.buttons.matches(mask, pressedKeys)) {
          click.behaviour.click(mouseX, mouseY);
        }
      }
    }
  }
  @Override
  public void preSnapshot() {
    super.preSnapshot();

    Long key;
    while ((key = this.emptyColumns.poll()) != null) {
      int x = IntPairHashed.key1(key);
      int z = IntPairHashed.key2(key);
      TIntSet column = initializedChunks.get(x, z);
      if (column.isEmpty()) {
        column = initializedChunks.remove(x, z);
        activeChunks.remove(x, z);
        session.send(
            false, new ChunkDataMessage(x, z, true, null, null, null, true, player.getSession()));
      }
    }
  }
  public void testForEach() {
    TIntSet set = new TIntHashSet(10, 0.5f);
    int[] ints = {1138, 42, 86, 99, 101};
    set.addAll(ints);

    class ForEach implements TIntProcedure {

      TIntSet built = new TIntHashSet();

      @Override
      public boolean execute(int value) {
        built.add(value);
        return true;
      }

      TIntSet getBuilt() {
        return built;
      }
    }

    ForEach procedure = new ForEach();

    set.forEach(procedure);
    TIntSet built = procedure.getBuilt();

    assertEquals("inequal sizes: " + set + ", " + built, set.size(), built.size());
    assertTrue("inequal sets: " + set + ", " + built, set.equals(built));
  }
  public void testHashcode() {
    int[] ints = {1138, 42, 86, 99, 101};
    TIntSet set = new TIntHashSet();
    set.addAll(ints);
    TIntSet other = new TIntHashSet();
    other.addAll(ints);

    assertTrue(
        "hashcodes incorrectly not equal: " + set + ", " + other,
        set.hashCode() == other.hashCode());

    int[] mismatched = {72, 49, 53, 1024, 999};
    TIntSet unequal = new TIntHashSet();
    unequal.addAll(mismatched);

    assertFalse(
        "hashcodes unlikely equal: " + set + ", " + unequal, set.hashCode() == unequal.hashCode());
  }
 @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;
 }
  public void testEquals() {
    int[] ints = {1138, 42, 86, 99, 101};
    TIntSet set = new TIntHashSet();
    set.addAll(ints);
    TIntSet other = new TIntHashSet();
    other.addAll(ints);

    assertTrue("sets incorrectly not equal: " + set + ", " + other, set.equals(other));

    int[] mismatched = {72, 49, 53, 1024, 999};
    TIntSet unequal = new TIntHashSet();
    unequal.addAll(mismatched);

    assertFalse("sets incorrectly equal: " + set + ", " + unequal, set.equals(unequal));

    // Change length, different code branch
    unequal.add(1);
    assertFalse("sets incorrectly equal: " + set + ", " + unequal, set.equals(unequal));
  }
  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;
  }
  public void testToArrayWithParams() {
    int no_entry_value = Integer.MIN_VALUE;
    TIntSet set = new TIntHashSet(10, 0.5f, no_entry_value);
    assertEquals(no_entry_value, set.getNoEntryValue());

    int[] ints = {42, 1138, 13, 86, 99};
    set.addAll(ints);

    int[] sink = new int[ints.length + 2];
    sink[sink.length - 1] = -1;
    sink[sink.length - 2] = -2;

    int[] res = set.toArray(sink);
    assertEquals(set.getNoEntryValue(), res[set.size()]);

    Set<Integer> copy = new HashSet<Integer>();
    for (int element : sink) {
      copy.add(Integer.valueOf(element));
    }

    Set<Integer> bogey = new HashSet<Integer>();
    for (int element : ints) {
      bogey.add(Integer.valueOf(element));
    }
    bogey.add(-1);
    bogey.add(no_entry_value);
    assertEquals(bogey, copy);
  }
示例#24
0
 private void sendEntities(NetData.NetMessage.Builder message) {
   TIntIterator dirtyIterator = netDirty.iterator();
   while (dirtyIterator.hasNext()) {
     int netId = dirtyIterator.next();
     EntityRef entity = networkSystem.getEntity(netId);
     if (isOwned(entity)) {
       Set<Class<? extends Component>> emptyComponentClassSet = Collections.emptySet();
       EntityData.PackedEntity entityData =
           entitySerializer.serialize(
               entity,
               emptyComponentClassSet,
               changedComponents.get(netId),
               emptyComponentClassSet,
               new ClientComponentFieldCheck());
       if (entityData != null) {
         message.addUpdateEntity(
             NetData.UpdateEntityMessage.newBuilder().setEntity(entityData).setNetId(netId));
       }
     }
   }
   netDirty.clear();
 }
  @Override
  protected void freeChunk(Point p) {
    int x = (int) p.getX() >> Chunk.BLOCKS.BITS;
    int y = (int) p.getY() >> Chunk.BLOCKS.BITS; // + SEALEVEL_CHUNK;
    int z = (int) p.getZ() >> Chunk.BLOCKS.BITS;

    RepositionManager rm = getRepositionManager();

    int cY = rm.convertChunkY(y);

    if (cY < 0 || cY >= p.getWorld().getHeight() >> Chunk.BLOCKS.BITS) {
      return;
    }

    TIntSet column = initializedChunks.get(x, z);
    if (column != null) {
      column.remove(y);
      if (column.isEmpty()) {
        emptyColumns.add(IntPairHashed.key(x, z));
      }
    }
  }
  @Override
  protected void freeChunk(Point p) {
    int x = (int) p.getX() >> Chunk.BLOCKS.BITS;
    int y = (int) p.getY() >> Chunk.BLOCKS.BITS; // + SEALEVEL_CHUNK;
    int z = (int) p.getZ() >> Chunk.BLOCKS.BITS;

    if (y < 0 || y >= p.getWorld().getHeight() >> Chunk.BLOCKS.BITS) {
      return;
    }

    TIntSet column = initializedChunks.get(x, z);
    if (column != null) {
      column.remove(y);
      if (column.isEmpty()) {
        emptyColumns.add(IntPairHashed.key(x, z));
      } // TODO - is this required?
      /*
      else {
      	CCMsg = new ChunkDataMessage(x, z, false, null, null, null, true);
      }*/
    }
  }
  public void testIsEmpty() throws Exception {
    TIntSet s = new TIntHashSet();
    assertTrue("new set wasn't empty", s.isEmpty());

    s.add(1);
    assertTrue("set with element reports empty", !s.isEmpty());
    s.clear();
    assertTrue("cleared set reports not-empty", s.isEmpty());
  }
  public void testConstructors() throws Exception {
    TIntSet set = new TIntHashSet();
    assertNotNull(set);

    int[] ints = {1138, 42, 86, 99, 101};
    set.addAll(ints);

    TIntSet copy = new TIntHashSet(set);
    assertTrue("set not a copy: " + set + ", " + copy, set.equals(copy));

    TIntSet another = new TIntHashSet(20);
    another.addAll(ints);
    assertTrue("set not equal: " + set + ", " + copy, set.equals(another));

    another = new TIntHashSet(2, 1.0f);
    another.addAll(ints);
    assertTrue("set not equal: " + set + ", " + copy, set.equals(another));

    another = new TIntHashSet(ints);
    assertTrue("set not equal: " + set + ", " + copy, set.equals(another));
  }
  @Override
  public void keyReleased(final KeyEvent e) {
    //		System.out.println( "MouseAndKeyHandler.keyReleased()" );
    update();

    if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
      shiftPressed = false;
    } else if (e.getKeyCode() == KeyEvent.VK_META) {
      metaPressed = false;
    } else if (e.getKeyCode() != KeyEvent.VK_ALT
        && e.getKeyCode() != KeyEvent.VK_CONTROL
        && e.getKeyCode() != KeyEvent.VK_ALT_GRAPH) {
      pressedKeys.remove(e.getKeyCode());

      for (final BehaviourEntry<DragBehaviour> drag : activeKeyDrags)
        drag.behaviour.end(mouseX, mouseY);
      activeKeyDrags.clear();
    }
  }
示例#30
0
  @Override
  public SRResultList predictMostSimilar(
      List<SRResultList> scores, int maxResults, TIntSet validIds) {
    if (2 * scores.size() + 1 != mostSimilarCoefficients.size()) {
      throw new IllegalStateException();
    }
    TIntSet allIds = new TIntHashSet(); // ids returned by at least one metric
    for (SRResultList resultList : scores) {
      if (resultList != null) {
        for (SRResult result : resultList) {
          allIds.add(result.getId());
        }
      }
    }

    TIntDoubleHashMap scoreMap = new TIntDoubleHashMap();
    for (int id : allIds.toArray()) {
      scoreMap.put(id, mostSimilarCoefficients.get(0));
    }
    int i = 1;
    for (SRResultList resultList : scores) {
      TIntSet unknownIds = new TIntHashSet(allIds);
      double c1 = mostSimilarCoefficients.get(i); // score coeff
      double c2 = mostSimilarCoefficients.get(i + 1); // rank coefficient
      if (resultList != null) {
        for (int j = 0; j < resultList.numDocs(); j++) {
          int rank = j + 1;
          // expand or contract ranks proportionately
          if (validIds != null) {
            double k = 1.0 * numTrainingCandidateArticles / validIds.size();
            rank = (int) (rank * k);
          }
          SRResult result = resultList.get(j);
          unknownIds.remove(result.getId());
          double value = c1 * result.getScore() + c2 * Math.log(rank);
          if (debug) {
            System.err.format(
                "%s %d. %.3f (id=%d), computing %.3f * %.3f + %.3f * (log(%d) = %.3f)\n",
                "m" + i, j, value, result.getId(), c1, result.getScore(), c2, rank, Math.log(rank));
          }
          scoreMap.adjustValue(result.getId(), value);
        }
      }

      // interpolate scores for unknown ids
      double value =
          c1 * mostSimilarInterpolator.getInterpolatedScore(i / 2)
              + c2 * Math.log(mostSimilarInterpolator.getInterpolatedRank(i / 2));
      for (int id : unknownIds.toArray()) {
        scoreMap.adjustValue(id, value);
      }
      i += 2;
    }
    List<SRResult> resultList = new ArrayList<SRResult>();
    for (int id : scoreMap.keys()) {
      resultList.add(new SRResult(id, scoreMap.get(id)));
    }
    Collections.sort(resultList);
    Collections.reverse(resultList);
    int size = maxResults > resultList.size() ? resultList.size() : maxResults;
    SRResultList result = new SRResultList(size);
    for (i = 0; i < size; i++) {
      result.set(i, resultList.get(i));
    }
    return result;
  }