jpountz commented on code in PR #15140: URL: https://github.com/apache/lucene/pull/15140#discussion_r2315542754
########## lucene/core/src/java/org/apache/lucene/util/TernaryLongHeap.java: ########## @@ -0,0 +1,187 @@ +/* + * 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; + +import java.util.Arrays; + +/** + * A ternary min heap that stores longs; a primitive priority queue that like all priority queues + * maintains a partial ordering of its elements such that the least element can always be found in + * constant time. Put()'s and pop()'s require log_3(size). This heap provides unbounded growth via + * {@link #push(long)}, and bounded-size insertion based on its nominal maxSize via {@link + * #insertWithOverflow(long)}. The heap is a min heap, meaning that the top element is the lowest + * value of the heap. TernaryLongHeap implements 3-ary heap. + * + * @lucene.internal + */ +public final class TernaryLongHeap { + + private final int maxSize; + + private long[] heap; + private int size = 0; + + /** + * Constructs a heap with specified size and initializes all elements with the given value. + * + * @param size the number of elements to initialize in the heap. + * @param initialValue the value to fill the heap with. + */ + public TernaryLongHeap(int size, long initialValue) { + this(size <= 0 ? 1 : size); + Arrays.fill(heap, 1, size + 1, initialValue); + this.size = size; + } + + /** + * Create an empty priority queue of the configured initial size. + * + * @param maxSize the maximum size of the heap, or if negative, the initial size of an unbounded + * heap + */ + public TernaryLongHeap(int maxSize) { + if (maxSize < 1 || maxSize >= ArrayUtil.MAX_ARRAY_LENGTH) { + // Throw exception to prevent confusing OOME: + throw new IllegalArgumentException( + "maxSize must be > 0 and < " + (ArrayUtil.MAX_ARRAY_LENGTH - 1) + "; got: " + maxSize); + } + // NOTE: we add +1 because all access to heap is 1-based not 0-based. heap[0] is unused. + final int heapSize = maxSize + 1; + this.maxSize = maxSize; + this.heap = new long[heapSize]; + } + + /** + * Adds a value in log(size) time. Grows unbounded as needed to accommodate new values. + * + * @return the new 'top' element in the queue. + */ + public long push(long element) { + size++; + if (size == heap.length) { + heap = ArrayUtil.grow(heap, (size * 3 + 1) / 2); + } + heap[size] = element; + LongHeap.upHeap(heap, size, 3); + return heap[1]; + } Review Comment: > Rename maxSize parameter to initialSize/initialCapacity to better reflect its behaviour. +1 -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
