Package generic.util

Class MergeSortingIterator<T>

java.lang.Object
generic.util.MergeSortingIterator<T>
Type Parameters:
T - the type of elements in each iterator
All Implemented Interfaces:
PeekableIterator<T>, Iterator<T>

public class MergeSortingIterator<T> extends Object implements PeekableIterator<T>
An iterator which merges sorted iterators according to a comparator
  • Field Details

  • Constructor Details

    • MergeSortingIterator

      public MergeSortingIterator(Iterable<? extends Iterator<? extends T>> iterators, Comparator<? super T> comparator)
      Construct a merge sorting iterator
      Parameters:
      iterators - a collection of iterators to merge
      comparator - the comparator defining how the input and output iterators are sorted
  • Method Details

    • withLabels

      public static <L, T> MergeSortingIterator<Map.Entry<L,T>> withLabels(Map<L,? extends Iterator<? extends T>> iterMap, Comparator<T> comparator)
      Construct a merge-sorting iterator which generates labeled values

      The map of iterators is a map of entries, each giving a label and an iterator to be merged. Each iterator must return values as sorted by the given comparator. The entries returned by the combined iterator give the values in sorted order, but each has a the key indicating which given iterator returned that value. Note that the returned entry may be re-used by the underlying implementation, so users needing to keep the entry should create a copy.

      The purpose of the iterator is to know which iterator provided a given entry in the merged result. While this has general utility, at the moment, it is only used in our tests to verify proper operation of the merge-sorting implementation.

      Parameters:
      iterMap - a map of labeled iterators
      comparator - the comparator of values
      Returns:
      an iterator which returns labeled values in sorted order
    • hasNext

      public boolean hasNext()
      Specified by:
      hasNext in interface Iterator<T>
    • next

      public T next()
      Specified by:
      next in interface Iterator<T>
    • peek

      public T peek() throws NoSuchElementException
      Description copied from interface: PeekableIterator
      Returns the item that would be returned by calling Iterator.next(), but without incrementing the iterator.
      Specified by:
      peek in interface PeekableIterator<T>
      Returns:
      the item that would be returned by calling Iterator.next()
      Throws:
      NoSuchElementException - if there is no element to peek at