/** * Removes and returns the least element of the PriorityQueue in * log(size) time. * * @return The least element. */ public final T poll() { if (size > 0) { T result = heap[1]; // save first value heap[1] = heap[size]; // move last to first heap[size] = null; // permit GC of objects size--; downHeap(); // adjust heap return result; } else { return null; } }
/** * Adds element to the PriorityQueue in log(size) time if either the * PriorityQueue is not full, or not lessThan(element, top()). * * @param element * The element to insert, * @return True, if element is added, false otherwise. */ public boolean offer(T element) { if (size < capacity) { put(element); return true; } else if (size > 0 && !lessThan(element, peek())) { heap[1] = element; adjustTop(); return true; } else { return false; } }
final PartialOrderPriorityQueue<PartitionIndexStream> heap = new PartialOrderPriorityQueue<>( new PartitionIndexStreamComparator(), toBeMerged.size()); ioManager, toBeMerged.get(i).getChannelID(), segments.get(i), toBeMerged.get(i).getPartitionIndexList()); heap.add(new PartitionIndexStream(readerDelegate, toBeMerged.get(i).getPartitionIndexList())); while (heap.size() > 0) { final PartitionIndexStream headStream = heap.peek(); final PartitionIndex partitionIndex = headStream.getCurrentPartitionIndex(); heap.poll(); } else { heap.adjustTop();
public BinaryMergeIterator( List<MutableObjectIterator<Entry>> iterators, List<Entry> reusableEntries, Comparator<Entry> comparator) throws IOException { checkArgument(iterators.size() == reusableEntries.size()); this.heap = new PartialOrderPriorityQueue<>( (o1, o2) -> comparator.compare(o1.getHead(), o2.getHead()), iterators.size()); for (int i = 0; i < iterators.size(); i++) { this.heap.add(new HeadStream<>(iterators.get(i), reusableEntries.get(i))); } }
private void upHeap() { int i = size; T node = heap[i]; // save bottom node int j = i >>> 1; while (j > 0 && lessThan(node, heap[j])) { heap[i] = heap[j]; // shift parents down i = j; j = j >>> 1; } heap[i] = node; // install saved node }
/** * Adds a buffer to a PriorityQueue in log(size) time. If one tries to * add more objects than maxSize from initialize a RuntimeException * (ArrayIndexOutOfBound) is thrown. */ public final void put(T element) { size++; heap[size] = element; upHeap(); }
/** * @param iterators * @param comparator * @throws IOException */ public MergeIterator(List<MutableObjectIterator<E>> iterators, TypeComparator<E> comparator) throws IOException { this.heap = new PartialOrderPriorityQueue<HeadStream<E>>(new HeadStreamComparator<E>(), iterators.size()); for (MutableObjectIterator<E> iterator : iterators) { this.heap.add(new HeadStream<E>(iterator, comparator.duplicate())); } }
private void upHeap() { int i = size; T node = heap[i]; // save bottom node int j = i >>> 1; while (j > 0 && lessThan(node, heap[j])) { heap[i] = heap[j]; // shift parents down i = j; j = j >>> 1; } heap[i] = node; // install saved node }
/** * Adds a buffer to a PriorityQueue in log(size) time. If one tries to * add more objects than maxSize from initialize a RuntimeException * (ArrayIndexOutOfBound) is thrown. */ public final void put(T element) { size++; heap[size] = element; upHeap(); }
/** * Gets the next smallest element, with respect to the definition of order implied by * the {@link TypeSerializer} provided to this iterator. * * @return The next element if the iterator has another element, null otherwise. * * @see org.apache.flink.util.MutableObjectIterator#next() */ @Override public E next() throws IOException { if (this.heap.size() > 0) { // get the smallest element final HeadStream<E> top = this.heap.peek(); E result = top.getHead(); // read an element if (!top.nextHead()) { this.heap.poll(); } else { this.heap.adjustTop(); } return result; } else { return null; } }
/** * Adds element to the PriorityQueue in log(size) time if either the * PriorityQueue is not full, or not lessThan(element, top()). * * @param element * The element to insert, * @return True, if element is added, false otherwise. */ public boolean offer(T element) { if (size < capacity) { put(element); return true; } else if (size > 0 && !lessThan(element, peek())) { heap[1] = element; adjustTop(); return true; } else { return false; } }
/** * @param iterators * @param comparator * @throws IOException */ public MergeIterator(List<MutableObjectIterator<E>> iterators, TypeComparator<E> comparator) throws IOException { this.heap = new PartialOrderPriorityQueue<HeadStream<E>>(new HeadStreamComparator<E>(), iterators.size()); for (MutableObjectIterator<E> iterator : iterators) { this.heap.add(new HeadStream<E>(iterator, comparator.duplicate())); } }
private void upHeap() { int i = size; T node = heap[i]; // save bottom node int j = i >>> 1; while (j > 0 && lessThan(node, heap[j])) { heap[i] = heap[j]; // shift parents down i = j; j = j >>> 1; } heap[i] = node; // install saved node }
/** * Removes and returns the least element of the PriorityQueue in * log(size) time. * * @return The least element. */ public final T poll() { if (size > 0) { T result = heap[1]; // save first value heap[1] = heap[size]; // move last to first heap[size] = null; // permit GC of objects size--; downHeap(); // adjust heap return result; } else { return null; } }
/** * Adds a buffer to a PriorityQueue in log(size) time. If one tries to * add more objects than maxSize from initialize a RuntimeException * (ArrayIndexOutOfBound) is thrown. */ public final void put(T element) { size++; heap[size] = element; upHeap(); }
/** * Gets the next smallest element, with respect to the definition of order implied by * the {@link TypeSerializer} provided to this iterator. * * @return The next element if the iterator has another element, null otherwise. * * @see org.apache.flink.util.MutableObjectIterator#next() */ @Override public E next() throws IOException { if (this.heap.size() > 0) { // get the smallest element final HeadStream<E> top = this.heap.peek(); E result = top.getHead(); // read an element if (!top.nextHead()) { this.heap.poll(); } else { this.heap.adjustTop(); } return result; } else { return null; } }
/** * Adds element to the PriorityQueue in log(size) time if either the * PriorityQueue is not full, or not lessThan(element, top()). * * @param element * The element to insert, * @return True, if element is added, false otherwise. */ public boolean offer(T element) { if (size < capacity) { put(element); return true; } else if (size > 0 && !lessThan(element, peek())) { heap[1] = element; adjustTop(); return true; } else { return false; } }
/** * @param iterators * @param comparator * @throws IOException */ public MergeIterator(List<MutableObjectIterator<E>> iterators, TypeComparator<E> comparator) throws IOException { this.heap = new PartialOrderPriorityQueue<HeadStream<E>>(new HeadStreamComparator<E>(), iterators.size()); for (MutableObjectIterator<E> iterator : iterators) { this.heap.add(new HeadStream<E>(iterator, comparator.duplicate())); } }
private void upHeap() { int i = size; T node = heap[i]; // save bottom node int j = i >>> 1; while (j > 0 && lessThan(node, heap[j])) { heap[i] = heap[j]; // shift parents down i = j; j = j >>> 1; } heap[i] = node; // install saved node }