package io.sigpipe.sing.stat;

import java.util.ArrayList;
import java.util.Collections;
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();

    private class Entry implements Comparable<Entry> {
        public double key;
        public T value;
        public int id;

        public Entry(double key, T value) {
            this.key = key;
            this.value = value;
        }

        @Override
        public int compareTo(Entry that) {
            return Double.compare(this.key, that.key);
        }

        @Override
        public String toString() {
            return id + " [" + key + "] -> " + value;
        }
    }

    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);
        }

    }
}

@@ -244,17 +244,21 @@
 te List%3C
-T
+Entry
 %3E reserv
@@ -266,35 +266,8 @@
 ir;%0A
-    private double%5B%5D keys;%0A
     
@@ -907,41 +907,8 @@
 e);%0A
-        keys = new double%5Bsize%5D;%0A
     
@@ -1048,24 +1048,128 @@
 t(T item) %7B%0A
+        double key = random.nextDouble();%0A        Entry e = new Entry(key, item);%0A        e.id = count;%0A
         if (
@@ -1224,20 +1224,17 @@
 (count, 
-item
+e
 );%0A     
@@ -1249,54 +1249,8 @@
 e %7B%0A
-            double key = random.nextDouble();%0A
     
@@ -1411,51 +1411,10 @@
 on, 
-item);%0A                keys%5Bposition%5D = key
+e)
 ;%0A  

Switch to Entry implementation