Class MultiRandSelector<T extends @Nullable Object>

java.lang.Object
org.plumelib.util.MultiRandSelector<T>
Type Parameters:
T - the type of elements to be selected among

public class MultiRandSelector<T extends @Nullable Object> extends Object
Performs uniform random selection over an iterator, where the objects in the iterator may be partitioned so that the random selection chooses the same number from each group.

For example, given data about incomes by state, it may be desirable to select 1000 people from each state rather than 50,000 from the nation. As another example, for selecting invocations in a Daikon trace file, it may be desirable to select an equal number of samples per program point.

The performance is the same as running a set of RandomSelector Objects, one for each bucket, plus some overhead for determining which bucket to assign to each Object in the iteration.

To use this class, call accept(T) on every Object in the iteration to be sampled. Then, call valuesIter() to receive an iteration of all the values selected by the random selection.

See Also:
  • Field Details

    • coinTossMode

      private boolean coinTossMode
      Whether to toss a coin or select a given number of elements.
    • numElts

      private int numElts
      Number of elements to select. -1 if coinTossMode==true.
    • keepProbability

      private double keepProbability
      Likelihood to select each element. -1.0 if coinTossMode=false.
    • r

      private Random r
      The Random instance to use. Is not a seed. Gets side-effected.
    • eq

      private Partitioner<T extends @Nullable Object,T extends @Nullable Object> eq
      Partioner that determines how to partition the objects.
    • map

      private HashMap<T extends @Nullable Object,RandomSelector<T extends @Nullable Object>> map
      Maps from partition representatives to the RandomSelector to use on that partition.
  • Constructor Details

    • MultiRandSelector

      public MultiRandSelector(int numElts, Partitioner<T,T> eq)
      Create a MultiRandSelector that chooses numElts elements from each bucket.
      Parameters:
      numElts - the number of elements to select from each bucket
      eq - partioner that determines how to partition the objects
    • MultiRandSelector

      public MultiRandSelector(double keepProbability, Partitioner<T,T> eq)
      Create a MultiRandSelector that chooses each element with probability keepProbability.
      Parameters:
      keepProbability - the likelihood to select each element
      eq - partioner that determines how to partition the objects
    • MultiRandSelector

      public MultiRandSelector(int numElts, Random r, Partitioner<T,T> eq)
      Create a MultiRandSelector that chooses numElts from each partition, using the given Random.
      Parameters:
      numElts - the number of elements to select from each bucket
      r - the Random instance to use for making random choices
      eq - partioner that determines how to partition the objects
    • MultiRandSelector

      public MultiRandSelector(double keepProbability, Random r, Partitioner<T,T> eq)
      Create a MultiRandSelector that chooses each element with probability keepProbability., using the given Random.
      Parameters:
      keepProbability - likelihood to select each element
      r - the Random instance to use for making random choices
      eq - partioner that determines how to partition the objects
    • MultiRandSelector

      private MultiRandSelector(Random r, Partitioner<T,T> eq)
      Helper constructor to create a not-fully-initialized MultiRandSelector.
      Parameters:
      r - the Random instance to use for making random choices
      eq - partioner that determines how to partition the objects
  • Method Details

    • acceptIter

      public void acceptIter(Iterator<T> iter)
      Use all the iterator's elements in the pool to select from.
      Parameters:
      iter - contains elements that are added to the pool to select from
    • accept

      public void accept(T next)
      Use the given value as one of the objects in the pool to select from.
      Parameters:
      next - element that is added to the pool to select from
    • valuesIter

      public Iterator<T> valuesIter()
      Returns an iterator of all objects selected.
      Returns:
      an iterator of all objects selected