Class ByteArraySet

java.lang.Object
java.util.AbstractCollection<Byte>
it.unimi.dsi.fastutil.bytes.AbstractByteCollection
it.unimi.dsi.fastutil.bytes.AbstractByteSet
it.unimi.dsi.fastutil.bytes.ByteArraySet
All Implemented Interfaces:
it.unimi.dsi.fastutil.bytes.ByteCollection, it.unimi.dsi.fastutil.bytes.ByteIterable, ByteSet, Serializable, Cloneable, Iterable<Byte>, Collection<Byte>, Set<Byte>

public class ByteArraySet extends AbstractByteSet 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 Summary

    Fields
    Modifier and Type
    Field
    Description
    protected byte[]
    The backing array (valid up to size, excluded).
    protected int
    The number of valid entries in a.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new empty array set.
    ByteArraySet(byte[] a)
    Creates a new array set using the given backing array.
    ByteArraySet(byte[] a, int size)
    Creates a new array set using the given backing array and the given number of elements of the array.
    ByteArraySet(int capacity)
    Creates a new empty array set of given initial capacity.
    ByteArraySet(it.unimi.dsi.fastutil.bytes.ByteCollection c)
    Creates a new array set copying the contents of a given collection.
    Creates a new array set copying the contents of a given collection.
    Creates a new array set copying the contents of a given set.
    ByteArraySet(Set<? extends Byte> c)
    Creates a new array set copying the contents of a given set.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    add(byte k)
     
    void
     
    Returns a deep copy of this set.
    boolean
    contains(byte k)
     
    boolean
     
    it.unimi.dsi.fastutil.bytes.ByteIterator
    Returns a type-specific iterator on the elements of this set.
    of()
    Creates a new empty array set.
    of(byte e)
    Creates a new array set using the element given.
    of(byte... a)
    Creates a new array set using an array of elements.
    Creates a new empty array set.
    ofUnchecked(byte... a)
    Creates a new array set using an array of elements.
    boolean
    remove(byte k)
    Removes an element from this set.
    int
     
    it.unimi.dsi.fastutil.bytes.ByteSpliterator
    Returns a type-specific spliterator on the elements of this set.
    byte[]
    toArray(byte[] a)
     
    byte[]
     

    Methods inherited from class it.unimi.dsi.fastutil.bytes.AbstractByteSet

    equals, hashCode, rem

    Methods inherited from class it.unimi.dsi.fastutil.bytes.AbstractByteCollection

    add, addAll, addAll, contains, containsAll, containsAll, remove, removeAll, removeAll, retainAll, retainAll, toByteArray, toString

    Methods inherited from class java.util.AbstractCollection

    toArray, toArray

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface it.unimi.dsi.fastutil.bytes.ByteCollection

    addAll, containsAll, intIterator, intParallelStream, intSpliterator, intStream, parallelStream, removeAll, removeIf, removeIf, removeIf, retainAll, stream, toByteArray

    Methods inherited from interface it.unimi.dsi.fastutil.bytes.ByteIterable

    forEach, forEach, forEach

    Methods inherited from interface it.unimi.dsi.fastutil.bytes.ByteSet

    add, contains, remove

    Methods inherited from interface java.util.Collection

    toArray

    Methods inherited from interface java.util.Set

    addAll, containsAll, removeAll, retainAll, toArray, toArray
  • Field Details

    • a

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

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

    • ByteArraySet

      public ByteArraySet(byte[] 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.
    • ByteArraySet

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

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

      public ByteArraySet(it.unimi.dsi.fastutil.bytes.ByteCollection c)
      Creates a new array set copying the contents of a given collection.
      Parameters:
      c - a collection.
    • ByteArraySet

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

      public ByteArraySet(ByteSet c)
      Creates a new array set copying the contents of a given collection.
      Parameters:
      c - a collection.
    • ByteArraySet

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

      public ByteArraySet(byte[] 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 ByteArraySet of()
      Creates a new empty array set.
      Returns:
      a new empty array set.
    • of

      public static ByteArraySet of(byte 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

      public static ByteArraySet of(byte... 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 ByteArraySet ofUnchecked()
      Creates a new empty array set.
      Returns:
      a new empty array set.
    • ofUnchecked

      public static ByteArraySet ofUnchecked(byte... 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.bytes.ByteIterator iterator()
      Description copied from interface: ByteSet
      Returns a type-specific iterator on the elements of this set.
      Specified by:
      iterator in interface it.unimi.dsi.fastutil.bytes.ByteCollection
      Specified by:
      iterator in interface it.unimi.dsi.fastutil.bytes.ByteIterable
      Specified by:
      iterator in interface ByteSet
      Specified by:
      iterator in interface Collection<Byte>
      Specified by:
      iterator in interface Iterable<Byte>
      Specified by:
      iterator in interface Set<Byte>
      Specified by:
      iterator in class AbstractByteSet
      Returns:
      a type-specific iterator on the elements of this set.
    • spliterator

      public it.unimi.dsi.fastutil.bytes.ByteSpliterator 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 it.unimi.dsi.fastutil.bytes.ByteCollection
      Specified by:
      spliterator in interface it.unimi.dsi.fastutil.bytes.ByteIterable
      Specified by:
      spliterator in interface ByteSet
      Specified by:
      spliterator in interface Collection<Byte>
      Specified by:
      spliterator in interface Iterable<Byte>
      Specified by:
      spliterator in interface Set<Byte>
      Returns:
    • contains

      public boolean contains(byte k)
      Specified by:
      contains in interface it.unimi.dsi.fastutil.bytes.ByteCollection
      Overrides:
      contains in class it.unimi.dsi.fastutil.bytes.AbstractByteCollection
    • size

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

      public boolean remove(byte k)
      Description copied from class: AbstractByteSet
      Removes an element from this set. Delegates to the type-specific rem() method implemented by type-specific abstract Collection superclass.
      Specified by:
      remove in interface ByteSet
      Overrides:
      remove in class AbstractByteSet
      See Also:
    • add

      public boolean add(byte k)
      Specified by:
      add in interface it.unimi.dsi.fastutil.bytes.ByteCollection
      Overrides:
      add in class it.unimi.dsi.fastutil.bytes.AbstractByteCollection
    • clear

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

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

      public byte[] toByteArray()
      Specified by:
      toByteArray in interface it.unimi.dsi.fastutil.bytes.ByteCollection
      Overrides:
      toByteArray in class it.unimi.dsi.fastutil.bytes.AbstractByteCollection
    • toArray

      public byte[] toArray(byte[] a)
      Specified by:
      toArray in interface it.unimi.dsi.fastutil.bytes.ByteCollection
      Overrides:
      toArray in class it.unimi.dsi.fastutil.bytes.AbstractByteCollection
    • clone

      public ByteArraySet 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.