rubenada commented on a change in pull request #2080: URL: https://github.com/apache/calcite/pull/2080#discussion_r460162896
########## File path: linq4j/src/main/java/org/apache/calcite/linq4j/TopNHeap.java ########## @@ -0,0 +1,242 @@ +/* + * 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.calcite.linq4j; + +import org.apache.calcite.linq4j.function.Function1; + +import java.util.Comparator; + +/** + * Implementation of a stable binary heap with a fetch and an offset. + * Stable means that if two items are considered equal, + * they will appear in the same order as they were offered to the heap. + * + * @param <TSource> type of the element that will be added to the heap + * @param <TKey> type of the key, which the comparator will use for comparisons + */ +public class TopNHeap<TSource, TKey> { + static final int MAX_INIT_ARRAY_SIZE = 1024; + static final int ROOT = 1; + + private final Function1<TSource, TKey> keyFn; + private final Comparator<TKey> cmp; + private final int fetch; + private final int offset; + private final int maxSize; + + TKey headKey = null; + int size = 0; + int time = -Integer.MIN_VALUE; + + /** Heap with 1-based index, heap[0] is not used */ + TSource[] heap; + /** Stores the order of arrival of the elements in the heap */ + int[] order; + + public TopNHeap( + Function1<TSource, TKey> keySelector, + Comparator<TKey> comparator, + int fetch, + int offset) { + this.keyFn = keySelector; + this.cmp = comparator; + long tmp = (long) fetch + offset; + this.maxSize = tmp > Integer.MAX_VALUE - ROOT ? Integer.MAX_VALUE - ROOT : (int) tmp; + + // Review comment: This comment seems unnecessary (or the actual comment is missing) ---------------------------------------------------------------- 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: [email protected]
