package io.sigpipe.sing.stat;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Reservoir<T extends Comparable<T>> {

    private int count;
    private int size;
    private List<T> reservoir;
    private double[] keys;
    private Random random = new Random();

    public Reservoir(int size) {
        this.size = size;
        reservoir = new ArrayList<>(size);
        keys = new double[size];
    }

    public void put(Iterable<T> items) {
        for (T item : items) {
            put(item);
        }
    }

    public void put(T item) {
        if (count < this.size()) {
            reservoir.add(count, item);
        } else {
            double key = random.nextDouble();
            if (key < ((double) this.size() / (count + 1))) {
                int position = random.nextInt(this.size());
                reservoir.set(position, item);
                keys[position] = key;
            }
        }

        count++;
    }

//    public void merge(Reservoir<T> res, int size) {
//
//    }
//

    public int size() {
        return this.size;
    }

    public List<T> samples() {
        return new ArrayList<>(reservoir);
    }

    private double[] keys() {
        return this.keys;
    }

    public static void main(String[] args) {
        Reservoir<Double> rs = new Reservoir<>(20);

        Random r = new Random();
        r.doubles(1000).filter(val -> val < 0.5).forEach(rs::put);

        RunningStatistics stats = new RunningStatistics();
        for (double d : rs.samples()) {
            System.out.println(d);
            stats.put(d);
        }
        System.out.println(stats);

        for (double d : rs.keys()) {
            System.out.println(d);
        }

    }
}

@@ -48,24 +48,54 @@
 .ArrayList;%0A
+import java.util.Collections;%0A
 import java.
@@ -324,24 +324,520 @@
  Random();%0A%0A
+    private class Entry implements Comparable%3CEntry%3E %7B%0A        public double key;%0A        public T value;%0A        public int id;%0A%0A        public Entry(double key, T value) %7B%0A            this.key = key;%0A            this.value = value;%0A        %7D%0A%0A        @Override%0A        public int compareTo(Entry that) %7B%0A            return Double.compare(this.key, that.key);%0A        %7D%0A%0A        @Override%0A        public String toString() %7B%0A            return id + %22 %5B%22 + key + %22%5D -%3E %22 + value;%0A        %7D%0A    %7D%0A%0A
     public R

Add entry class