|
| 1 | +package io.github.thibaultmeyer.cuid; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.MethodOrderer; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import org.junit.jupiter.api.TestMethodOrder; |
| 6 | + |
| 7 | +import java.util.DoubleSummaryStatistics; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.function.Function; |
| 11 | +import java.util.stream.Collectors; |
| 12 | +import java.util.stream.IntStream; |
| 13 | +import java.util.stream.Stream; |
| 14 | + |
| 15 | +@TestMethodOrder(MethodOrderer.MethodName.class) |
| 16 | +final class HistogramTest { |
| 17 | + |
| 18 | + @Test |
| 19 | + void histogramCUIDv1() { |
| 20 | + |
| 21 | + // Arrange |
| 22 | + final List<Double> data = Stream.generate(CUID::randomCUID1) |
| 23 | + .limit(1_000_000) |
| 24 | + .map(Object::hashCode) |
| 25 | + .map(o -> (double) o) |
| 26 | + .collect(Collectors.toList()); |
| 27 | + |
| 28 | + // Act |
| 29 | + final Histogram histogram = new Histogram(data); |
| 30 | + final Map<Integer, Integer> frequencies = histogram.histogram(20); |
| 31 | + |
| 32 | + // Assert |
| 33 | + System.out.println("CUID v1: " + frequencies); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void histogramCUIDv2() { |
| 38 | + |
| 39 | + // Arrange |
| 40 | + final List<Double> data = Stream.generate(CUID::randomCUID2) |
| 41 | + .limit(1_000_000) |
| 42 | + .map(Object::hashCode) |
| 43 | + .map(o -> (double) o) |
| 44 | + .collect(Collectors.toList()); |
| 45 | + |
| 46 | + // Act |
| 47 | + final Histogram histogram = new Histogram(data); |
| 48 | + final Map<Integer, Integer> frequencies = histogram.histogram(20); |
| 49 | + |
| 50 | + // Assert |
| 51 | + System.out.println("CUID v2: " + frequencies); |
| 52 | + } |
| 53 | + |
| 54 | + public static final class Histogram { |
| 55 | + |
| 56 | + private final List<Double> data; |
| 57 | + |
| 58 | + public Histogram(List<Double> data) { |
| 59 | + |
| 60 | + this.data = data; |
| 61 | + } |
| 62 | + |
| 63 | + private static Integer findBin(Double datum, int bins, double min, double max) { |
| 64 | + |
| 65 | + final double binWidth = (max - min) / bins; |
| 66 | + if (datum >= max) { |
| 67 | + return bins - 1; |
| 68 | + } else if (datum <= min) { |
| 69 | + return 0; |
| 70 | + } |
| 71 | + return (int) Math.floor((datum - min) / binWidth); |
| 72 | + } |
| 73 | + |
| 74 | + public Map<Integer, Integer> histogram(int bins) { |
| 75 | + |
| 76 | + final DoubleSummaryStatistics statistics = this.data.stream() |
| 77 | + .mapToDouble(x -> x) |
| 78 | + .summaryStatistics(); |
| 79 | + |
| 80 | + final double max = statistics.getMax(); |
| 81 | + final double min = statistics.getMin(); |
| 82 | + |
| 83 | + // Make sure that all bins are initialized |
| 84 | + final Map<Integer, Integer> histogram = IntStream.range(0, bins) |
| 85 | + .boxed() |
| 86 | + .collect(Collectors.toMap(Function.identity(), x -> 0)); |
| 87 | + |
| 88 | + histogram.putAll( |
| 89 | + this.data.stream().collect( |
| 90 | + Collectors.groupingBy( |
| 91 | + x -> findBin(x, bins, min, max), |
| 92 | + Collectors.mapping(x -> 1, Collectors.summingInt(x -> x))))); |
| 93 | + |
| 94 | + return histogram; |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments