Class DenseIntSet8Bit

java.lang.Object
org.jhotdraw8.collection.primitive.DenseIntSet8Bit
All Implemented Interfaces:
IntSet
Direct Known Subclasses:
GrowableIntSet8Bit

public class DenseIntSet8Bit extends Object implements IntSet
A dense set of int-values which can be cleared in O(1); needs 8-bits storage space for each int-value.

This set is optimised for frequent setting and clearing.

Setting a boolean at an index is O(1).

Clearing the set is O(1) amortized. Every 255 times it takes O(n) time to clear the set.

Storage space is one byte per boolean.

This set has a fixed capacity. Attempting to access an element outside of the capacity range results in an ArrayIndexOutOfBoundsException.

  • Constructor Details

    • DenseIntSet8Bit

      public DenseIntSet8Bit(int capacity)
      Creates a set with the specified capacity.
  • Method Details

    • addAsInt

      public boolean addAsInt(int element)
      Adds an element to the set.
      Specified by:
      addAsInt in interface IntSet
      Parameters:
      element - the element
      Returns:
      true if the element was added, false if it was already in the set.
      Throws:
      ArrayIndexOutOfBoundsException - if element is outside of the capacity range.
    • removeAsInt

      public boolean removeAsInt(int element)
      Removes the specified element from the set.
      Specified by:
      removeAsInt in interface IntSet
      Parameters:
      element - an element
      Returns:
      true if the element was in the set, false otherwise
      Throws:
      ArrayIndexOutOfBoundsException - if element is outside of the capacity range.
    • containsAsInt

      public boolean containsAsInt(int e)
      Checks if the set contains the specified element.
      Specified by:
      containsAsInt in interface IntSet
      Parameters:
      e - an element
      Returns:
      true if the element is in the set.
      Throws:
      ArrayIndexOutOfBoundsException - if element is outside of the capacity range.
    • clear

      public void clear()
      Description copied from interface: IntSet
      Clears the set.
      Specified by:
      clear in interface IntSet
    • setCapacity

      public void setCapacity(int capacity)
      Sets the capacity of the set.
      Parameters:
      capacity - the new capacity
    • capacity

      public int capacity()
      Gets the capacity of the set.
      Returns:
      the capacity
    • equals

      public boolean equals(@Nullable Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      The hash code is the same as BitSet.hashCode().
      Overrides:
      hashCode in class Object
      Returns:
      hashcode
    • toLongArray

      public long @NonNull [] toLongArray()
      Returns a new long array containing all the bits in this int set.
      Returns:
      a new long array.
    • toString

      public @NonNull String toString()
      Overrides:
      toString in class Object