Class SortedHead<T>

  • Type Parameters:
    T - type of items to hold in the collection

    public class SortedHead<T>
    extends java.lang.Object
    A collection of items that are divided in a sorted head and an unsorted tail. Keeps the head sorted according to comparator. Other objects are unsorted in tail.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        SortedHead​(T[] head, java.util.Comparator<? super T> comparator)
      Create a new collection with an initial head.
      private SortedHead​(T[] head, SimpleArray<T> tail, java.util.Comparator<? super T> comparator)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addObject​(T o)
      Add an item to the collection.
      static <T> void addSorted​(java.util.Iterator<? extends T> sourceIterator, T[] fullHead, SimpleArray<T> tail, java.util.Comparator<? super T> comparator)
      Convenience method for sorting items into a head of fixed size and a growable tail.
      T[] getTail()
      Get the tail items.
      private void updateHead()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • head

        private final T[] head
      • candidates

        private final T[] candidates
      • candidateCount

        private int candidateCount
      • limit

        private T limit
      • comparator

        private final java.util.Comparator<? super T> comparator
    • Constructor Detail

      • SortedHead

        private SortedHead​(T[] head,
                           SimpleArray<T> tail,
                           java.util.Comparator<? super T> comparator)
      • SortedHead

        public SortedHead​(T[] head,
                          java.util.Comparator<? super T> comparator)
        Create a new collection with an initial head.
        Parameters:
        head - Head items. The array must contain at least one item but does not need to be sorted. This array will be sorted every time the collection is added to with addObject(Object).
        comparator - comparator to use when sorting
    • Method Detail

      • getTail

        public T[] getTail()
        Get the tail items.
        Returns:
        an array with the tail items
      • addObject

        public void addObject​(T o)
        Add an item to the collection.
        Parameters:
        o - item to add
      • updateHead

        private void updateHead()
      • addSorted

        public static <T> void addSorted​(java.util.Iterator<? extends T> sourceIterator,
                                         T[] fullHead,
                                         SimpleArray<T> tail,
                                         java.util.Comparator<? super T> comparator)
        Convenience method for sorting items into a head of fixed size and a growable tail. There is no return value from the method, the result is instead located in the head and tail arguments that are modified by the call.
        Parameters:
        sourceIterator - iterator that gives items to sort
        fullHead - An array with current head items. The array may be modified by this method.
        tail - A growable tail. It may contain items at the start of the method call and items that do not fit in the head are placed here.
        comparator - comparator to use when sorting