Class UniformReservoir

  • All Implemented Interfaces:
    Reservoir

    public class UniformReservoir
    extends java.lang.Object
    implements Reservoir
    A random sampling reservoir of a stream of longs. Uses Vitter's Algorithm R to produce a statistically representative sample.
    See Also:
    Random Sampling with a Reservoir
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static int BITS_PER_LONG  
      private java.util.concurrent.atomic.AtomicLong count  
      private static int DEFAULT_SIZE  
      private java.util.concurrent.atomic.AtomicLongArray values  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Snapshot getSnapshot()
      Returns a snapshot of the reservoir's values.
      private static long nextLong​(long n)
      Get a pseudo-random long uniformly between 0 and n-1.
      int size()
      Returns the number of values recorded.
      void update​(long value)
      Adds a new recorded value to the reservoir.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • count

        private final java.util.concurrent.atomic.AtomicLong count
      • values

        private final java.util.concurrent.atomic.AtomicLongArray values
    • Constructor Detail

      • UniformReservoir

        public UniformReservoir()
        Creates a new UniformReservoir of 1028 elements, which offers a 99.9% confidence level with a 5% margin of error assuming a normal distribution.
      • UniformReservoir

        public UniformReservoir​(int size)
        Creates a new UniformReservoir.
        Parameters:
        size - the number of samples to keep in the sampling reservoir
    • Method Detail

      • size

        public int size()
        Description copied from interface: Reservoir
        Returns the number of values recorded.
        Specified by:
        size in interface Reservoir
        Returns:
        the number of values recorded
      • update

        public void update​(long value)
        Description copied from interface: Reservoir
        Adds a new recorded value to the reservoir.
        Specified by:
        update in interface Reservoir
        Parameters:
        value - a new recorded value
      • nextLong

        private static long nextLong​(long n)
        Get a pseudo-random long uniformly between 0 and n-1. Stolen from Random.nextInt().
        Parameters:
        n - the bound
        Returns:
        a value select randomly from the range [0..n).
      • getSnapshot

        public Snapshot getSnapshot()
        Description copied from interface: Reservoir
        Returns a snapshot of the reservoir's values.
        Specified by:
        getSnapshot in interface Reservoir
        Returns:
        a snapshot of the reservoir's values