Class LimitedSizeIntSet

java.lang.Object
org.plumelib.util.LimitedSizeIntSet
All Implemented Interfaces:
Serializable, Cloneable

public class LimitedSizeIntSet extends Object implements Serializable, Cloneable
LimitedSizeIntSet stores up to some maximum number of unique values. If more than that many elements are added, then functionality is degraded: most operations return a conservative estimate (because the internal representation is nulled, in order to save space).

The advantage of this class over LimitedSizeSet<Integer> is that it does not autobox the int values, so it takes less memory.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static boolean
    Whether assertions are enabled.
    (package private) @org.checkerframework.checker.index.qual.IndexOrHigh({"values"}) int
    The number of active elements (equivalently, the first unused index).
    (package private) static final long
    Unique identifier for serialization.
    protected int @Nullable @MinLen(1) []
    If null, then at least numValues distinct values have been seen.
  • Constructor Summary

    Constructors
    Constructor
    Description
    LimitedSizeIntSet(@org.checkerframework.checker.index.qual.Positive int maxValues)
    Create a new LimitedSizeIntSet that can hold maxValues values.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(int elt)
    Add an element to this set.
    void
    Add all elements of s to this set.
     
    boolean
    contains(int elt)
    Returns true if this set contains the given element.
    @org.checkerframework.checker.index.qual.Positive int
    An upper bound on how many distinct elements can be individually represented in the set.
    merge(@org.checkerframework.checker.index.qual.Positive int maxValues, List<LimitedSizeIntSet> slist)
    Merges a list of LimitedSizeIntSet objects into a single object that represents the values seen by the entire list.
    private void
    Null the representation, which happens when a client tries to add more elements to this set than it can contain (which is the integer that was passed to the constructor when creating this set).
    boolean
    Returns true if more elements have been added than this set can contain (which is the integer that was passed to the constructor when creating this set).
    int
    A lower bound on the number of elements in the set.
     

    Methods inherited from class java.lang.Object

    equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • serialVersionUID

      static final long serialVersionUID
      Unique identifier for serialization. If you add or remove fields, change this number.
      See Also:
    • values

      protected int @Nullable @MinLen(1) [] values
      If null, then at least numValues distinct values have been seen. The size is not separately stored, because that would take extra space.
    • numValues

      @org.checkerframework.checker.index.qual.IndexOrHigh({"values"}) int numValues
      The number of active elements (equivalently, the first unused index).
    • assertsEnabled

      private static boolean assertsEnabled
      Whether assertions are enabled.
  • Constructor Details

    • LimitedSizeIntSet

      public LimitedSizeIntSet(@org.checkerframework.checker.index.qual.Positive int maxValues)
      Create a new LimitedSizeIntSet that can hold maxValues values.
      Parameters:
      maxValues - the maximum number of values this set will be able to hold; must be positive
  • Method Details

    • add

      public void add(int elt)
      Add an element to this set.
      Parameters:
      elt - the element to add to this set
    • addAll

      public void addAll(LimitedSizeIntSet s)
      Add all elements of s to this set.
      Parameters:
      s - the elements to add to this set
    • contains

      @Pure public boolean contains(int elt)
      Returns true if this set contains the given element.
      Parameters:
      elt - the element whose membership to test
      Returns:
      true if this set contains elt
    • size

      @Pure public int size(@GuardSatisfied LimitedSizeIntSet this)
      A lower bound on the number of elements in the set. Returns either the number of elements that have been inserted in the set, or maxSize(), whichever is less.
      Returns:
      a number that is a lower bound on the number of elements added to the set
    • maxSize

      public @org.checkerframework.checker.index.qual.Positive int maxSize()
      An upper bound on how many distinct elements can be individually represented in the set. Returns maxValues+1 (where maxValues is the argument to the constructor).
      Returns:
      maximum capacity of the set representation
    • repNulled

      @EnsuresNonNullIf(result=false, expression="values") @Pure public boolean repNulled(@GuardSatisfied LimitedSizeIntSet this)
      Returns true if more elements have been added than this set can contain (which is the integer that was passed to the constructor when creating this set).
      Returns:
      true if this set has been filled to capacity and its internal representation is nulled
    • nullRep

      private void nullRep()
      Null the representation, which happens when a client tries to add more elements to this set than it can contain (which is the integer that was passed to the constructor when creating this set).
    • clone

      @SideEffectFree public LimitedSizeIntSet clone(@GuardSatisfied LimitedSizeIntSet this)
      Overrides:
      clone in class Object
    • merge

      public static LimitedSizeIntSet merge(@org.checkerframework.checker.index.qual.Positive int maxValues, List<LimitedSizeIntSet> slist)
      Merges a list of LimitedSizeIntSet objects into a single object that represents the values seen by the entire list. Returns the new object, whose maxValues is the given integer.
      Parameters:
      maxValues - the maximum size for the returned LimitedSizeIntSet
      slist - a list of LimitedSizeIntSet, whose elements will be merged
      Returns:
      a LimitedSizeIntSet that merges the elements of slist
    • toString

      @SideEffectFree public String toString(@GuardSatisfied LimitedSizeIntSet this)
      Overrides:
      toString in class Object