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());
  }
예제 #2
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();
 }
  @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);
        }
      }
    }
  }
 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 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 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 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));
 }
예제 #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;
 }
예제 #10
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 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;
  }
예제 #13
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;
  }
 @Override
 public boolean execute(int value) {
   built.add(value);
   return true;
 }
 public void testAdd() throws Exception {
   TIntSet set = new TIntHashSet();
   assertTrue("add failed", set.add(1));
   assertFalse("duplicated add modified set", set.add(1));
 }
예제 #16
0
 @Override
 public void setComponentDirty(int netId, Class<? extends Component> componentType) {
   netDirty.add(netId);
   changedComponents.put(netId, componentType);
 }