Class ExactSamplerPipe<T>

  • All Implemented Interfaces:
    Closeable, AutoCloseable, BasePipe, Pipe<T>

    public class ExactSamplerPipe<T>
    extends FilterBasePipe<T>
    A pipe sampling exactly m items of an input pipe containing n items, where n is known in advance. The algorithm is an online algorithm choosing a given item with probability {#remaining items to sample}/{#remaining items to visit}.
    Author:
    Eyal Schneider
    • Constructor Detail

      • ExactSamplerPipe

        public ExactSamplerPipe​(Pipe<T> input,
                                int n,
                                int m,
                                Random rnd)
        Constructor
        Parameters:
        input - The input pipe
        n - The exact number of items in the input pipe. If inaccurate, sampling won't be complete or won't be uniform.
        m - The number of items to sample
        rnd - The randomizer to use, if consistency is required
      • ExactSamplerPipe

        public ExactSamplerPipe​(Pipe<T> input,
                                int n,
                                int m)
        Constructor
        Parameters:
        input - The input pipe
        n - The exact number of items in the input pipe. If inaccurate, sampling won't be complete or won't be uniform.
        m - The number of items to sample
    • Method Detail

      • shouldSelect

        protected boolean shouldSelect​(T item)
        Specified by:
        shouldSelect in class FilterBasePipe<T>
        Parameters:
        item - The next item from the input stream
        Returns:
        true if and only if the item should be selected