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

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

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

        count++;
    }

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

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

    public List<Entry> entries() {
        return new ArrayList<>(this.reservoir);
    }

    public List<T> samples() {
        List<T> l = new ArrayList<>(this.size());
        for (Entry e : this.reservoir) {
            l.add(e.value);
        }
        return l;
    }

    public double[] keys() {
        double[] k = new double[this.size()];
        for (int i = 0; i < this.size(); ++i) {
            k[i] = this.reservoir.get(i).key;
        }
        return k;
    }

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

    }
}

@@ -159,30 +159,8 @@
 ir%3CT
- extends Comparable%3CT%3E
 %3E %7B%0A
@@ -1130,24 +1130,25 @@
 id = count;%0A
+%0A
         if (
@@ -1439,18 +1439,16 @@
 %0A    %7D%0A%0A
-//
     publ
@@ -1478,38 +1478,471 @@
 %3CT%3E 
-res, int size) %7B%0A//%0A//    %7D%0A//
+that, int size) %7B%0A        List%3CEntry%3E combinedEntries = new ArrayList%3C%3E(size);%0A        combinedEntries.addAll(this.reservoir);%0A        combinedEntries.addAll(that.reservoir);%0A        Collections.sort(combinedEntries);%0A%0A        this.reservoir = new ArrayList%3C%3E(size);%0A        for (int i = 0; i %3C size; ++i) %7B%0A            this.reservoir.add(combinedEntries.get(i));%0A        %7D%0A    %7D%0A%0A    public void merge(Reservoir%3CT%3E that) %7B%0A        merge(that, this.size());%0A    %7D
 %0A%0A  

Implement merging