mayya-sharipova commented on code in PR #12962: URL: https://github.com/apache/lucene/pull/12962#discussion_r1454345777
########## lucene/core/src/java/org/apache/lucene/util/hnsw/BlockingFloatHeap.java: ########## @@ -0,0 +1,190 @@ +/* + * 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.util.hnsw; + +import java.util.concurrent.locks.ReentrantLock; + +/** + * A blocking bounded min heap that stores floats. The top element is the lowest value of the heap. + * + * <p>A primitive priority queue that maintains a partial ordering of its elements such that the + * least element can always be found in constant time. Implementation is based on {@link + * org.apache.lucene.util.LongHeap} + * + * @lucene.internal + */ +public final class BlockingFloatHeap { + private final int maxSize; + private final float[] heap; + private final ReentrantLock lock; + private int size; + + public BlockingFloatHeap(int maxSize) { + this.maxSize = maxSize; + this.heap = new float[maxSize + 1]; + this.lock = new ReentrantLock(); + this.size = 0; + } + + /** + * Inserts a value into this heap. + * + * <p>If the number of values would exceed the heap's maxSize, the least value is discarded + * + * @param value the value to add + * @return the new 'top' element in the queue. + */ + public float offer(float value) { + lock.lock(); + try { + if (size < maxSize) { + push(value); + return heap[1]; + } else { + if (value >= heap[1]) { + updateTop(value); + } + return heap[1]; + } + } finally { + lock.unlock(); + } + } + + /** + * Inserts array of values into this heap. + * + * <p>Values are expected to be sorted in ascending order. + * + * @param values a set of values to insert + * @return the new 'top' element in the queue. + */ + public float offer(float[] values) { + lock.lock(); + try { + for (int i = values.length - 1; i >= 0; i--) { + if (size < maxSize) { + push(values[i]); + } else { + if (values[i] >= heap[1]) { Review Comment: @tveasey Thanks for your feedback. I am not sure what do you meant by "break". In this method, it is good if the passed `values` are sorted in asc order, but it is not necessary. This method is for bulk update of the queue. For a faster performance it is better to start with larger values, so that if `values[i] < heap[1]`, we don't need to do anything. -- 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. To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org 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