merlimat commented on a change in pull request #425: Add 
GrowablePriorityLongPairQueue and tests
URL: https://github.com/apache/incubator-pulsar/pull/425#discussion_r127365438
 
 

 ##########
 File path: 
pulsar-common/src/main/java/org/apache/pulsar/common/util/collections/GrowablePriorityLongPairQueue.java
 ##########
 @@ -0,0 +1,360 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.common.util.collections;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import io.netty.util.internal.MathUtil;
+
+/**
+ * An unbounded priority queue based on a min heap where values are composed 
of pairs of longs.
+ *
+ * When the capacity is reached, data will be moved to a bigger array.
+ * 
+ * <b>It also act as a set and doesn't store duplicate values if {@link 
#allowedDuplicate} flag is passed false</b>
+ * 
+ ** <p>
+ * (long,long)
+ * <p>
+ *
+ */
+public class GrowablePriorityLongPairQueue {
+
+    private long[] data;
+    private int capacity;
+    private volatile int size = 0;
+    private static final long EmptyItem = -1L;
+
+    public GrowablePriorityLongPairQueue() {
+        this(64);
+    }
+
+    public GrowablePriorityLongPairQueue(int initialCapacity) {
+        checkArgument(initialCapacity > 0);
+        this.capacity = MathUtil.findNextPositivePowerOfTwo(initialCapacity);
+        data = new long[2 * capacity];
+        fillEmptyValue(data, 0, data.length);
+    }
+
+    public interface LongPairPredicate {
+        boolean test(long v1, long v2);
+    }
+
+    public static interface LongPairConsumer {
+        void accept(long v1, long v2);
+    }
+
+    public synchronized void add(long item1, long item2) {
+
+        if (this.size >= this.capacity) {
+            expandArray();
+        }
+
+        int lastIndex = this.size << 1;
+        data[lastIndex] = item1;
+        data[lastIndex + 1] = item2;
+
+        int loc = lastIndex;
+
+        // Swap with parent until parent not larger
+        while (loc > 0 && compare(loc, parent(loc)) < 0) {
+            swap(loc, parent(loc));
+            loc = parent(loc);
+        }
+
+        this.size++;
+
+    }
+
+    public synchronized void forEach(LongPairConsumer processor) {
+        int index = 0;
+        for (int i = 0; i < this.size; i++) {
+            processor.accept(data[index], data[index + 1]);
+            index = index + 2;
+        }
+    }
+
+    /**
+     * @return a new list of all keys (makes a copy)
+     */
+    public Set<LongPair> items() {
+        Set<LongPair> items = new HashSet<>();
+        forEach((item1, item2) -> items.add(new LongPair(item1, item2)));
+        return items;
+    }
+
+    /**
+     * @return a new list of keys with max provided numberOfItems (makes a 
copy)
+     */
+    public Set<LongPair> items(int numberOfItems) {
+        Set<LongPair> items = new HashSet<>();
+        forEach((item1, item2) -> {
+            if (items.size() < numberOfItems) {
+                items.add(new LongPair(item1, item2));
+            }
+        });
+
+        return items;
+    }
+
+    /**
+     * Removes all of the elements of this collection that satisfy the given 
predicate.
+     * 
+     * @param filter
+     *            a predicate which returns {@code true} for elements to be 
removed
+     * @return {@code true} if any elements were removed
+     * 
+     * @return number of removed values
+     */
+    public int removeIf(LongPairPredicate filter) {
+        int removedValues = 0;
+        for (LongPair item : items()) {
 
 Review comment:
   Should we implement this by iterating over the underlying array, to avoid 
copying the whole queue and creating many `LongPair` instances?
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to