Class ReferenceArraySet<K>

java.lang.Object
java.util.AbstractCollection<K>
it.unimi.dsi.fastutil.objects.AbstractReferenceCollection<K>
it.unimi.dsi.fastutil.objects.AbstractReferenceSet<K>
it.unimi.dsi.fastutil.objects.ReferenceArraySet<K>
All Implemented Interfaces:
it.unimi.dsi.fastutil.objects.ObjectIterable<K>, it.unimi.dsi.fastutil.objects.ReferenceCollection<K>, ReferenceSet<K>, Serializable, Cloneable, Iterable<K>, Collection<K>, Set<K>

public class ReferenceArraySet<K> extends AbstractReferenceSet<K> implements Serializable, Cloneable
A simple, brute-force implementation of a set based on a backing array.

The main purpose of this implementation is that of wrapping cleanly the brute-force approach to the storage of a very small number of items: just put them into an array and scan linearly to find an item.

See Also:
  • Field Details

    • a

      protected transient Object[] a
      The backing array (valid up to size, excluded).
    • size

      protected int size
      The number of valid entries in a.
  • Constructor Details

    • ReferenceArraySet

      public ReferenceArraySet(Object[] a)
      Creates a new array set using the given backing array. The resulting set will have as many elements as the array.

      It is the responsibility of the caller to ensure that the elements of a are distinct.

      Parameters:
      a - the backing array.
    • ReferenceArraySet

      public ReferenceArraySet()
      Creates a new empty array set.
    • ReferenceArraySet

      public ReferenceArraySet(int capacity)
      Creates a new empty array set of given initial capacity.
      Parameters:
      capacity - the initial capacity.
    • ReferenceArraySet

      public ReferenceArraySet(it.unimi.dsi.fastutil.objects.ReferenceCollection<K> c)
      Creates a new array set copying the contents of a given collection.
      Parameters:
      c - a collection.
    • ReferenceArraySet

      public ReferenceArraySet(Collection<? extends K> c)
      Creates a new array set copying the contents of a given set.
      Parameters:
      c - a collection.
    • ReferenceArraySet

      public ReferenceArraySet(ReferenceSet<K> c)
      Creates a new array set copying the contents of a given collection.
      Parameters:
      c - a collection.
    • ReferenceArraySet

      public ReferenceArraySet(Set<? extends K> c)
      Creates a new array set copying the contents of a given set.
      Parameters:
      c - a collection.
    • ReferenceArraySet

      public ReferenceArraySet(Object[] a, int size)
      Creates a new array set using the given backing array and the given number of elements of the array.

      It is the responsibility of the caller to ensure that the first size elements of a are distinct.

      Parameters:
      a - the backing array.
      size - the number of valid elements in a.
  • Method Details

    • of

      public static <K> ReferenceArraySet<K> of()
      Creates a new empty array set.
      Returns:
      a new empty array set.
    • of

      public static <K> ReferenceArraySet<K> of(K e)
      Creates a new array set using the element given.
      Parameters:
      e - the element that the returned set will contain.
      Returns:
      a new array set containing e.
    • of

      @SafeVarargs public static <K> ReferenceArraySet<K> of(K... a)
      Creates a new array set using an array of elements.

      Unlike the array accepting constructors, this method will throw if duplicate elements are encountered. This adds a non-trivial validation burden. Use ofUnchecked() if you know your input has no duplicates, which will skip this validation.

      Parameters:
      a - the backing array of the returned array set.
      Returns:
      a new array set containing the elements in a.
      Throws:
      IllegalArgumentException - if there were duplicate entries.
    • ofUnchecked

      public static <K> ReferenceArraySet<K> ofUnchecked()
      Creates a new empty array set.
      Returns:
      a new empty array set.
    • ofUnchecked

      @SafeVarargs public static <K> ReferenceArraySet<K> ofUnchecked(K... a)
      Creates a new array set using an array of elements.

      It is the responsibility of the caller to ensure that the elements of a are distinct.

      Parameters:
      a - the backing array of the returned array set.
      Returns:
      a new array set containing the elements in a.
    • iterator

      public it.unimi.dsi.fastutil.objects.ObjectIterator<K> iterator()
      Description copied from interface: ReferenceSet
      Returns a type-specific iterator on the elements of this set.
      Specified by:
      iterator in interface Collection<K>
      Specified by:
      iterator in interface Iterable<K>
      Specified by:
      iterator in interface it.unimi.dsi.fastutil.objects.ObjectIterable<K>
      Specified by:
      iterator in interface it.unimi.dsi.fastutil.objects.ReferenceCollection<K>
      Specified by:
      iterator in interface ReferenceSet<K>
      Specified by:
      iterator in interface Set<K>
      Specified by:
      iterator in class AbstractReferenceSet<K>
      Returns:
      a type-specific iterator on the elements of this set.
    • spliterator

      public it.unimi.dsi.fastutil.objects.ObjectSpliterator<K> spliterator()
      Returns a type-specific spliterator on the elements of this set.

      Set spliterators must report at least Spliterator.DISTINCT.

      See Set.spliterator() for more documentation on the requirements of the returned spliterator.

      In addition to the usual trait of DISTINCT for sets, the returned spliterator will also report the trait ORDERED.

      The returned spliterator is late-binding; it will track structural changes after the current item, up until the first trySplit(), at which point the maximum index will be fixed.
      Structural changes before the current item or after the first trySplit() will result in unspecified behavior.

      Specified by:
      spliterator in interface Collection<K>
      Specified by:
      spliterator in interface Iterable<K>
      Specified by:
      spliterator in interface it.unimi.dsi.fastutil.objects.ObjectIterable<K>
      Specified by:
      spliterator in interface it.unimi.dsi.fastutil.objects.ReferenceCollection<K>
      Specified by:
      spliterator in interface ReferenceSet<K>
      Specified by:
      spliterator in interface Set<K>
      Returns:
    • contains

      public boolean contains(Object k)
      Specified by:
      contains in interface Collection<K>
      Specified by:
      contains in interface Set<K>
      Overrides:
      contains in class AbstractCollection<K>
    • size

      public int size()
      Specified by:
      size in interface Collection<K>
      Specified by:
      size in interface Set<K>
      Specified by:
      size in class AbstractCollection<K>
    • remove

      public boolean remove(Object k)
      Specified by:
      remove in interface Collection<K>
      Specified by:
      remove in interface Set<K>
      Overrides:
      remove in class AbstractCollection<K>
    • add

      public boolean add(K k)
      Specified by:
      add in interface Collection<K>
      Specified by:
      add in interface Set<K>
      Overrides:
      add in class AbstractCollection<K>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<K>
      Specified by:
      clear in interface Set<K>
      Overrides:
      clear in class AbstractCollection<K>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<K>
      Specified by:
      isEmpty in interface Set<K>
      Overrides:
      isEmpty in class AbstractCollection<K>
    • toArray

      public Object[] toArray()
      Specified by:
      toArray in interface Collection<K>
      Specified by:
      toArray in interface Set<K>
      Overrides:
      toArray in class AbstractCollection<K>
    • toArray

      public <T> T[] toArray(T[] a)
      Specified by:
      toArray in interface Collection<K>
      Specified by:
      toArray in interface Set<K>
      Overrides:
      toArray in class AbstractCollection<K>
    • clone

      public ReferenceArraySet<K> clone()
      Returns a deep copy of this set.

      This method performs a deep copy of this array set; the data stored in the set, however, is not cloned. Note that this makes a difference only for object keys.

      Overrides:
      clone in class Object
      Returns:
      a deep copy of this set.