/** * Create a histogram from a set of ranges with associated event counters. * * @throws IllegalArgumentException If the items contain mixed signs. */ public Histogram(Stream<RangeWithCount> items) { final List<RangeWithCount> iter = cleanup_(items.map(RangeWithCount::clone).collect(Collectors.toList())); if (iter.isEmpty()) { buckets_ = EMPTY_LIST; return; } if (iter.stream() .map(RangeWithCount::getCount) .map(Math::signum) .distinct() .count() > 1) { throw new IllegalArgumentException("mixed sign"); } final List<Bucket> buckets = new ArrayList<>(iter.size()); double running_total = 0; for (RangeWithCount rwc : iter) { running_total += rwc.getCount(); buckets.add(new Bucket(rwc.getRange(), rwc.getCount(), running_total)); } buckets_ = unmodifiableList(buckets); }
/** * Create a histogram from a set of ranges with associated event counters. * * @throws IllegalArgumentException If the items contain mixed signs. */ public Histogram(Stream<RangeWithCount> items) { final List<RangeWithCount> iter = cleanup_(items.map(RangeWithCount::clone).collect(Collectors.toList())); if (iter.isEmpty()) { buckets_ = EMPTY_LIST; return; } if (iter.stream() .map(RangeWithCount::getCount) .map(Math::signum) .distinct() .count() > 1) { throw new IllegalArgumentException("mixed sign"); } final List<Bucket> buckets = new ArrayList<>(iter.size()); double running_total = 0; for (RangeWithCount rwc : iter) { running_total += rwc.getCount(); buckets.add(new Bucket(rwc.getRange(), rwc.getCount(), running_total)); } buckets_ = unmodifiableList(buckets); }
/** * Create a new histogram, after applying the function on each of the event * counters. */ public Histogram modifyEventCounters(BiFunction<Range, Double, Double> fn) { return new Histogram(stream() .map(entry -> { entry.setCount(fn.apply(entry.getRange(), entry.getCount())); return entry; })); }
succ.setCount(succ.getCount() + head.getCount()); continue; head.setCount(head.getCount() + succ_left_fraction * succ.getCount()); succ.setCount(succ_right_fraction * succ.getCount()); succ.setRange(new Range(mid, ceil)); imed.add(0, head); if (Math.signum(head.getCount()) != 0) result.add(head); continue; final double head_right_fraction = 1 - head_left_fraction; imed.add(0, head); imed.add(0, new RangeWithCount(new Range(floor, ceil), head_left_fraction * head.getCount())); head.setRange(new Range(ceil, head.getRange().getCeil())); head.setCount(head_right_fraction * head.getCount()); sort(imed, cmp); .filter(rwc -> Math.signum(rwc.getCount()) != 0) .forEach(result::add); && pred.getCount() * succ_range == succ.getCount() * pred_range) { result.remove(i); succ.setRange(new Range(pred.getRange().getFloor(), succ.getRange().getCeil())); succ.setCount(succ.getCount() + pred.getCount()); } else { ++i;
succ.setCount(succ.getCount() + head.getCount()); continue; head.setCount(head.getCount() + succ_left_fraction * succ.getCount()); succ.setCount(succ_right_fraction * succ.getCount()); succ.setRange(new Range(mid, ceil)); imed.add(0, head); if (Math.signum(head.getCount()) != 0) result.add(head); continue; final double head_right_fraction = 1 - head_left_fraction; imed.add(0, head); imed.add(0, new RangeWithCount(new Range(floor, ceil), head_left_fraction * head.getCount())); head.setRange(new Range(ceil, head.getRange().getCeil())); head.setCount(head_right_fraction * head.getCount()); sort(imed, cmp); .filter(rwc -> Math.signum(rwc.getCount()) != 0) .forEach(result::add); && pred.getCount() * succ_range == succ.getCount() * pred_range) { result.remove(i); succ.setRange(new Range(pred.getRange().getFloor(), succ.getRange().getCeil())); succ.setCount(succ.getCount() + pred.getCount()); } else { ++i;
/** * Subtracts two histograms. * * @throws IllegalArgumentException If the result contains mixed signs. */ public static Histogram subtract(Histogram x, Histogram y) { return new Histogram(Stream.concat( x.stream(), y.stream().map(rwc -> { rwc.setCount(-rwc.getCount()); return rwc; }))); }
/** * Subtracts two histograms. * * @throws IllegalArgumentException If the result contains mixed signs. */ public static Histogram subtract(Histogram x, Histogram y) { return new Histogram(Stream.concat( x.stream(), y.stream().map(rwc -> { rwc.setCount(-rwc.getCount()); return rwc; }))); }
/** * Create a new histogram, after applying the function on each of the event * counters. */ public Histogram modifyEventCounters(BiFunction<Range, Double, Double> fn) { return new Histogram(stream() .map(entry -> { entry.setCount(fn.apply(entry.getRange(), entry.getCount())); return entry; })); }