jpountz commented on a change in pull request #1912:
URL: https://github.com/apache/lucene-solr/pull/1912#discussion_r499180612



##########
File path: 
lucene/core/src/java/org/apache/lucene/index/ApproximatePriorityQueue.java
##########
@@ -0,0 +1,136 @@
+/*
+ * 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.lucene.index;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.function.Predicate;
+
+/**
+ * An approximate priority queue, which attempts to poll items by decreasing
+ * log of the weight, though exact ordering is not guaranteed.
+ * This class doesn't support null elements.
+ */
+final class ApproximatePriorityQueue<T> {
+
+  // Indexes between 0 and 63 are sparely populated, and indexes that are
+  // greater than or equal to 64 are densely populated
+  // Items closed to the beginning of this list are more likely to have a
+  // higher weight.
+  private final List<T> slots = new ArrayList<>(Long.SIZE);
+
+  // A bitset where ones indicate that the corresponding index in `slots` is 
taken.
+  private long usedSlots = 0L;
+
+  ApproximatePriorityQueue() {
+    for (int i = 0; i < Long.SIZE; ++i) {
+      slots.add(null);
+    }
+  }
+
+  /**
+   * Add an entry to this queue that has the provided weight.
+   */
+  void add(T entry, long weight) {
+    if (entry == null) {
+      throw new NullPointerException();
+    }
+
+    final int expectedSlot = Long.numberOfLeadingZeros(weight);
+
+    // If the slot is already taken, we take the next one that is free.
+    final long freeSlots = ~usedSlots;
+    int destinationSlot = expectedSlot + Long.numberOfTrailingZeros(freeSlots 
>>> expectedSlot);

Review comment:
       you understood it correctly, I'll add the suggested comment and assert




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to