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:
  • 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
  • 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