java.util.PriorityQueue#clear ( )源码实例Demo

下面列出了java.util.PriorityQueue#clear ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: SwordToOffer   文件: MaxValueInWindows.java
public ArrayList<Integer> maxInWindows(int[] num, int size) {
    ArrayList<Integer> result = new ArrayList<Integer>();  //用一个列表存放每次窗口移动一位后窗口内的最大值
    if (num == null || num.length == 0 || size <= 0 || num.length < size) return result;
    PriorityQueue<Integer> queue = new PriorityQueue<Integer>(size, new Comparator<Integer>() {  //利用最大值排在前的优先队列自动比较放入队列的值
        public int compare(Integer i1, Integer i2) {
            return i2 - i1;  //这样重写比较器的比较函数,可以使前一个值小于后一个值时返回正数,与默认情况为负数相反
        }
    });
    int count = 0;  //每次加入窗口的元素数量的计数
    for (int i = 0; i < num.length - size + 1; i++) {  //num.length-size为最后一个窗口的开头位置,正如常见的i<array.length一样,这里的num.length-size+1是一个到不了的索引位置
        while (count < size) {  //当计数没达到窗口大小时,朝优先队列中添加元素,形成一个窗口
            queue.add(num[i + count]);
            count++;
        }
        result.add(queue.peek());  //经过优先队列排序后,最大元素排在前,因此队列第一个元素就是最大元素
        count = 0;  //一个窗口中的最大元素已经找到,清空计数和优先队列中的元素,进行下一个窗口多个元素的添加
        queue.clear();
    }
    return result;
}
 
源代码2 项目: incubator-hivemall   文件: MinHashUDTF.java
private void computeAndForwardSignatures(List<FeatureValue> features, Object[] forwardObjs)
        throws HiveException {
    final PriorityQueue<Integer> minhashes = new PriorityQueue<Integer>();
    // Compute N sets K minhash values
    for (int i = 0; i < num_hashes; i++) {
        float weightedMinHashValues = Float.MAX_VALUE;

        for (FeatureValue fv : features) {
            Object f = fv.getFeature();
            int hashIndex = Math.abs(hashFuncs[i].hash(f));
            float w = fv.getValueAsFloat();
            float hashValue = calcWeightedHashValue(hashIndex, w);
            if (hashValue < weightedMinHashValues) {
                weightedMinHashValues = hashValue;
                minhashes.offer(hashIndex);
            }
        }

        forwardObjs[0] = getSignature(minhashes, num_keygroups);
        forward(forwardObjs);

        minhashes.clear();
    }
}
 
源代码3 项目: incubator-hivemall   文件: MinHashesUDF.java
private static List<IntWritable> computeSignatures(final List<FeatureValue> features,
        final int numHashes, final int keyGroups, final int[] seeds) throws HiveException {
    final IntWritable[] hashes = new IntWritable[numHashes];
    final PriorityQueue<Integer> minhashes = new PriorityQueue<Integer>();
    // Compute N sets K minhash values
    for (int i = 0; i < numHashes; i++) {
        float weightedMinHashValues = Float.MAX_VALUE;
        for (FeatureValue fv : features) {
            Object f = fv.getFeature();
            assert (f != null);
            String fs = f.toString();
            int hashIndex = Math.abs(MurmurHash3.murmurhash3_x86_32(fs, seeds[i]));
            float w = fv.getValueAsFloat();
            float hashValue = calcWeightedHashValue(hashIndex, w);
            if (hashValue < weightedMinHashValues) {
                weightedMinHashValues = hashValue;
                minhashes.offer(hashIndex);
            }
        }

        hashes[i] = val(getSignature(minhashes, keyGroups));
        minhashes.clear();
    }

    return Arrays.asList(hashes);
}
 
源代码4 项目: openjdk-jdk9   文件: PriorityQueueTest.java
/**
 * clear removes all elements
 */
public void testClear() {
    PriorityQueue q = populatedQueue(SIZE);
    q.clear();
    assertTrue(q.isEmpty());
    assertEquals(0, q.size());
    q.add(new Integer(1));
    assertFalse(q.isEmpty());
    q.clear();
    assertTrue(q.isEmpty());
}
 
源代码5 项目: Algorithms   文件: MinDHeapTest.java
@Test
public void testPriorityRandomOperations() {
  for (int loop = 0; loop < LOOPS; loop++) {

    double p1 = Math.random();
    double p2 = Math.random();
    if (p2 < p1) {
      double tmp = p1;
      p1 = p2;
      p2 = tmp;
    }

    Integer[] ar = genRandArray(LOOPS);
    int d = 2 + (int) (Math.random() * 6);
    MinDHeap<Integer> pq = new MinDHeap<>(d, LOOPS);
    PriorityQueue<Integer> pq2 = new PriorityQueue<>(LOOPS);

    for (int i = 0; i < LOOPS; i++) {
      int e = ar[i];
      double r = Math.random();
      if (0 <= r && r <= p1) {
        pq.add(e);
        pq2.add(e);
      } else if (p1 < r && r <= p2) {
        if (!pq2.isEmpty()) assertEquals(pq.poll(), pq2.poll());
      } else {
        pq.clear();
        pq2.clear();
      }
    }

    assertEquals(pq.peek(), pq2.peek());
  }
}
 
源代码6 项目: data-structures   文件: MinDHeapTest.java
@Test
public void testPriorityRandomOperations() {
  for (int loop = 0; loop < LOOPS; loop++) {

    double p1 = Math.random();
    double p2 = Math.random();
    if (p2 < p1) {
      double tmp = p1;
      p1 = p2;
      p2 = p1;
    }

    Integer[] ar = genRandArray(LOOPS);
    int d = 2 + (int) (Math.random() * 6);
    MinDHeap<Integer> pq = new MinDHeap<>(d, LOOPS);
    PriorityQueue<Integer> pq2 = new PriorityQueue<>(LOOPS);

    for (int i = 0; i < LOOPS; i++) {
      int e = ar[i];
      double r = Math.random();
      if (0 <= r && r <= p1) {
        pq.add(e);
        pq2.add(e);
      } else if (p1 < r && r <= p2) {
        if (!pq2.isEmpty()) assertEquals(pq.poll(), pq2.poll());
      } else {
        pq.clear();
        pq2.clear();
      }
    }

    assertEquals(pq.peek(), pq2.peek());
  }
}
 
源代码7 项目: j2objc   文件: PriorityQueueTest.java
/**
 * java.util.PriorityQueue#Clear()
 */
public void test_clear() {
    PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
    int[] array = { 2, 45, 7, -12, 9 };
    for (int i = 0; i < array.length; i++) {
        integerQueue.offer(array[i]);
    }
    integerQueue.clear();
    assertTrue(integerQueue.isEmpty());
}
 
源代码8 项目: j2objc   文件: PriorityQueueTest.java
/**
 * clear removes all elements
 */
public void testClear() {
    PriorityQueue q = populatedQueue(SIZE);
    q.clear();
    assertTrue(q.isEmpty());
    assertEquals(0, q.size());
    q.add(new Integer(1));
    assertFalse(q.isEmpty());
    q.clear();
    assertTrue(q.isEmpty());
}
 
源代码9 项目: yolov3-android-tflite   文件: Classifier.java
protected ArrayList<Recognition> nms(ArrayList<Recognition> list) {
    ArrayList<Recognition> nmsList = new ArrayList<Recognition>();

    for (int k = 0; k < mLabelList.size(); k++) {
        //1.find max confidence per class
        PriorityQueue<Recognition> pq =
                new PriorityQueue<Recognition>(
                        10,
                        new Comparator<Recognition>() {
                            @Override
                            public int compare(final Recognition lhs, final Recognition rhs) {
                                // Intentionally reversed to put high confidence at the head of the queue.
                                return Float.compare(rhs.getConfidence(), lhs.getConfidence());
                            }
                        });

        for (int i = 0; i < list.size(); ++i) {
            if (list.get(i).detectedClass == k) {
                pq.add(list.get(i));
            }
        }
        Log.d("wangmin", "class[" + k + "] pq size: " + pq.size());

        //2.do non maximum suppression
        while(pq.size() > 0) {
            //insert detection with max confidence
            Recognition[] a = new Recognition[pq.size()];
            Recognition[] detections = pq.toArray(a);
            Recognition max = detections[0];
            nmsList.add(max);

            Log.d("wangmin", "before nms pq size: " + pq.size());

            //clear pq to do next nms
            pq.clear();

            for (int j = 1; j < detections.length; j++) {
                Recognition detection = detections[j];
                RectF b = detection.getLocation();
                if (box_iou(max.getLocation(), b) < mNmsThresh){
                    pq.add(detection);
                }
            }
            Log.d("wangmin", "after nms pq size: " + pq.size());
        }
    }
    return nmsList;
}
 
源代码10 项目: openjdk-jdk9   文件: ForgetMeNot.java
private static void realMain(String[] args) throws Throwable {
    final PriorityQueue<Integer> q = new PriorityQueue<>();
    Iterator<Integer> it;

    //----------------------------------------------------------------
    // Empty
    //----------------------------------------------------------------
    checkQ(q);
    check(q.isEmpty());
    check(! q.contains(1));
    it = q.iterator();
    removeIsCurrentlyIllegal(it);
    noMoreElements(it);
    q.clear();
    check(q.isEmpty());

    //----------------------------------------------------------------
    // Singleton
    //----------------------------------------------------------------
    q.add(1);
    checkQ(q, 1);
    check(! q.isEmpty());
    check(q.contains(1));
    it = q.iterator();
    removeIsCurrentlyIllegal(it);
    check(it.hasNext());
    equal(it.next(), 1);
    noMoreElements(it);
    remove(it, q);
    check(q.isEmpty());
    noMoreElements(it);
    checkQ(q);
    q.clear();

    //----------------------------------------------------------------
    // @see PriorityQueue.forgetMeNot
    //----------------------------------------------------------------
    final Integer[] a = {0, 4, 1, 6, 7, 2, 3}; // Carefully chosen!
    q.addAll(Arrays.asList(a));
    checkQ(q, a);
    it = q.iterator();
    checkQ(q, a);
    removeIsCurrentlyIllegal(it);
    checkQ(q, a);
    check(it.hasNext());
    removeIsCurrentlyIllegal(it);
    checkQ(q, a);
    check(it.hasNext());
    equal(it.next(), 0);
    equal(it.next(), 4);
    equal(it.next(), 1);
    equal(it.next(), 6);
    check(it.hasNext());
    checkQ(q, a);
    remove(it, q);
    checkQ(q, 0, 3, 1, 4, 7, 2);
    check(it.hasNext());
    removeIsCurrentlyIllegal(it);
    equal(it.next(), 7);
    remove(it, q);
    checkQ(q, 0, 2, 1, 4, 3);
    check(it.hasNext());
    removeIsCurrentlyIllegal(it);
    check(it.hasNext());
    equal(it.next(), 3);
    equal(it.next(), 2);
    check(! it.hasNext());
    remove(it, q);
    checkQ(q, 0, 3, 1, 4);
    check(! it.hasNext());
    noMoreElements(it);
    removeIsCurrentlyIllegal(it);
}
 
源代码11 项目: charts   文件: GraphCalculator.java
public NodeEdgeModel calculateClosenessCentrality(NodeEdgeModel nodeEdgeModel){
    PriorityQueue<GraphNode> queue = new PriorityQueue<>();

    ArrayList<GraphNode> visited = new ArrayList<>();
    int level;
    int counterUntilNextLevel;
    int counterOfNextLevel;
    double result;
    GraphNode currentNode;
    ArrayList<GraphNode> GraphNodes = nodeEdgeModel.getNodes();

    for(GraphNode node: GraphNodes){
        visited.clear();
        queue.clear();
        queue.add(node);
        result = 0;
        level = 0;
        counterUntilNextLevel = 1;
        counterOfNextLevel = 0;

        while(!queue.isEmpty()){
            currentNode = queue.poll();
            counterOfNextLevel += currentNode.getConnectedNodes().size();
            visited.add(currentNode);
            ArrayList<GraphNode> currentNodeList = new ArrayList<>(currentNode.getConnectedNodes());
            for(GraphNode gNode: currentNodeList){
                if(!visited.contains(gNode) && !queue.contains(gNode)){
                    queue.add(gNode);
                }
            }
            if(level>0) {
                result += 1.0 / (double) level;
            }
            counterUntilNextLevel--;
            if(counterUntilNextLevel == 0){
                level++;
                counterUntilNextLevel = counterOfNextLevel;
                counterOfNextLevel = 0;
            }
        }
        node.setNumericAttribute(closenessKey, new Double(result));
    }
    return nodeEdgeModel;
}
 
源代码12 项目: Algorithms   文件: BinaryHeapTest.java
@Test
public void testPQReusability() {

  List<Integer> SZs = genUniqueRandList(LOOPS);

  PriorityQueue<Integer> PQ = new PriorityQueue<>();
  BinaryHeap<Integer> pq = new BinaryHeap<>();

  for (int sz : SZs) {

    pq.clear();
    PQ.clear();

    List<Integer> nums = genRandList(sz);
    for (int n : nums) {
      pq.add(n);
      PQ.add(n);
    }

    Collections.shuffle(nums);

    for (int i = 0; i < sz / 2; i++) {

      // Sometimes add a new number into the BinaryHeap
      if (0.25 < Math.random()) {
        int randNum = (int) (Math.random() * 10000);
        PQ.add(randNum);
        pq.add(randNum);
      }

      int removeNum = nums.get(i);

      assertTrue(pq.isMinHeap(0));
      assertEquals(PQ.size(), pq.size());
      assertEquals(PQ.peek(), pq.peek());

      PQ.remove(removeNum);
      pq.remove(removeNum);

      assertEquals(PQ.peek(), pq.peek());
      assertEquals(PQ.size(), pq.size());
      assertTrue(pq.isMinHeap(0));
    }
  }
}
 
源代码13 项目: Algorithms   文件: BinaryHeapQuickRemovalsTest.java
@Test
public void testPQReusability() {

  List<Integer> SZs = genUniqueRandList(LOOPS);

  PriorityQueue<Integer> PQ = new PriorityQueue<>();
  BinaryHeapQuickRemovals<Integer> pq = new BinaryHeapQuickRemovals<>();

  for (int sz : SZs) {

    pq.clear();
    PQ.clear();

    List<Integer> nums = genRandList(sz);
    for (int n : nums) {
      pq.add(n);
      PQ.add(n);
    }

    Collections.shuffle(nums);

    for (int i = 0; i < sz / 2; i++) {

      // Sometimes add a new number into the BinaryHeapQuickRemovals
      if (0.25 < Math.random()) {
        int randNum = (int) (Math.random() * 10000);
        PQ.add(randNum);
        pq.add(randNum);
      }

      int removeNum = nums.get(i);

      assertTrue(pq.isMinHeap(0));
      assertEquals(PQ.size(), pq.size());
      assertEquals(PQ.peek(), pq.peek());

      PQ.remove(removeNum);
      pq.remove(removeNum);

      assertEquals(PQ.peek(), pq.peek());
      assertEquals(PQ.size(), pq.size());
      assertTrue(pq.isMinHeap(0));
    }
  }
}
 
源代码14 项目: data-structures   文件: BinaryHeapTest.java
@Test
public void testPQReusability() {

  List<Integer> SZs = genUniqueRandList(LOOPS);

  PriorityQueue<Integer> PQ = new PriorityQueue<>();
  BinaryHeap<Integer> pq = new BinaryHeap<>();

  for (int sz : SZs) {

    pq.clear();
    PQ.clear();

    List<Integer> nums = genRandList(sz);
    for (int n : nums) {
      pq.add(n);
      PQ.add(n);
    }

    Collections.shuffle(nums);

    for (int i = 0; i < sz / 2; i++) {

      // Sometimes add a new number into the BinaryHeap
      if (0.25 < Math.random()) {
        int randNum = (int) (Math.random() * 10000);
        PQ.add(randNum);
        pq.add(randNum);
      }

      int removeNum = nums.get(i);

      assertTrue(pq.isMinHeap(0));
      assertEquals(PQ.size(), pq.size());
      assertEquals(PQ.peek(), pq.peek());

      PQ.remove(removeNum);
      pq.remove(removeNum);

      assertEquals(PQ.peek(), pq.peek());
      assertEquals(PQ.size(), pq.size());
      assertTrue(pq.isMinHeap(0));
    }
  }
}
 
@Test
public void testPQReusability() {

  List<Integer> SZs = genUniqueRandList(LOOPS);

  PriorityQueue<Integer> PQ = new PriorityQueue<>();
  BinaryHeapQuickRemovals<Integer> pq = new BinaryHeapQuickRemovals<>();

  for (int sz : SZs) {

    pq.clear();
    PQ.clear();

    List<Integer> nums = genRandList(sz);
    for (int n : nums) {
      pq.add(n);
      PQ.add(n);
    }

    Collections.shuffle(nums);

    for (int i = 0; i < sz / 2; i++) {

      // Sometimes add a new number into the BinaryHeapQuickRemovals
      if (0.25 < Math.random()) {
        int randNum = (int) (Math.random() * 10000);
        PQ.add(randNum);
        pq.add(randNum);
      }

      int removeNum = nums.get(i);

      assertTrue(pq.isMinHeap(0));
      assertEquals(PQ.size(), pq.size());
      assertEquals(PQ.peek(), pq.peek());

      PQ.remove(removeNum);
      pq.remove(removeNum);

      assertEquals(PQ.peek(), pq.peek());
      assertEquals(PQ.size(), pq.size());
      assertTrue(pq.isMinHeap(0));
    }
  }
}