Class OrderedSet<T>

  • All Implemented Interfaces:
    java.lang.Iterable<T>

    public class OrderedSet<T>
    extends ObjectSet<T>
    A ObjectSet that also stores keys in an Array using the insertion order. Null keys are not allowed. No allocation is done except when growing the table size.

    Iteration is ordered and faster than an unordered set. Keys can also be accessed and the order changed using orderedItems(). There is some additional overhead for put and remove. When used for faster iteration versus ObjectSet and the order does not actually matter, copying during remove can be greatly reduced by setting Array.ordered to false for orderedItems().

    This class performs fast contains (typically O(1), worst case O(n) but that is rare in practice). Remove is somewhat slower due to orderedItems(). Add may be slightly slower, depending on hash collisions. Hashcodes are rehashed to reduce collisions and the need to resize. Load factors greater than 0.91 greatly increase the chances to resize to the next higher POT size.

    Unordered sets and maps are not designed to provide especially fast iteration. Iteration is faster with OrderedSet and OrderedMap.

    This implementation uses linear probing with the backward shift algorithm for removal. Hashcodes are rehashed using Fibonacci hashing, instead of the more common power-of-two mask, to better distribute poor hashCodes (see Malte Skarupke's blog post). Linear probing continues to work even when all hashCodes collide, just more slowly.

    • Constructor Detail

      • OrderedSet

        public OrderedSet()
      • OrderedSet

        public OrderedSet​(int initialCapacity,
                          float loadFactor)
      • OrderedSet

        public OrderedSet​(int initialCapacity)
      • OrderedSet

        public OrderedSet​(OrderedSet<? extends T> set)
    • Method Detail

      • add

        public boolean add​(T key)
        Description copied from class: ObjectSet
        Returns true if the key was added to the set or false if it was already in the set. If this set already contains the key, the call leaves the set unchanged and returns false.
        Overrides:
        add in class ObjectSet<T>
      • add

        public boolean add​(T key,
                           int index)
        Sets the key at the specfied index. Returns true if the key was added to the set or false if it was already in the set. If this set already contains the key, the existing key's index is changed if needed and false is returned.
      • remove

        public boolean remove​(T key)
        Description copied from class: ObjectSet
        Returns true if the key was removed.
        Overrides:
        remove in class ObjectSet<T>
      • removeIndex

        public T removeIndex​(int index)
      • alter

        public boolean alter​(T before,
                             T after)
        Changes the item before to after without changing its position in the order. Returns true if after has been added to the OrderedSet and before has been removed; returns false if after is already present or before is not present. If you are iterating over an OrderedSet and have an index, you should prefer alterIndex(int, Object), which doesn't need to search for an index like this does and so can be faster.
        Parameters:
        before - an item that must be present for this to succeed
        after - an item that must not be in this set for this to succeed
        Returns:
        true if before was removed and after was added, false otherwise
      • alterIndex

        public boolean alterIndex​(int index,
                                  T after)
        Changes the item at the given index in the order to after, without changing the ordering of other items. If after is already present, this returns false; it will also return false if index is invalid for the size of this set. Otherwise, it returns true. Unlike alter(Object, Object), this operates in constant time.
        Parameters:
        index - the index in the order of the item to change; must be non-negative and less than ObjectSet.size
        after - the item that will replace the contents at index; this item must not be present for this to succeed
        Returns:
        true if after successfully replaced the contents at index, false otherwise
      • clear

        public void clear​(int maximumCapacity)
        Description copied from class: ObjectSet
        Clears the set and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger. The reduction is done by allocating new arrays, though for large arrays this can be faster than clearing the existing array.
        Overrides:
        clear in class ObjectSet<T>
      • clear

        public void clear()
        Description copied from class: ObjectSet
        Clears the set, leaving the backing arrays at the current capacity. When the capacity is high and the population is low, iteration can be unnecessarily slow. ObjectSet.clear(int) can be used to reduce the capacity.
        Overrides:
        clear in class ObjectSet<T>
      • orderedItems

        public Array<T> orderedItems()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class ObjectSet<T>
      • toString

        public java.lang.String toString​(java.lang.String separator)
        Overrides:
        toString in class ObjectSet<T>
      • with

        public static <T> OrderedSet<T> with​(T... array)