histogram static method
Builds a histogram with the provided buckets as upper bounds.
Builds a histogram of values using the provided ascending buckets
as inclusive upper bounds. The final bucket is an open-ended tail.
Implementation
static Histogram histogram(
Iterable<Object?> values, {
required List<double> buckets,
}) {
if (buckets.isEmpty) {
throw ArgumentError('Histogram requires at least one bucket');
}
final sortedBounds = buckets.toList()..sort();
final counts = List<int>.filled(sortedBounds.length + 1, 0);
for (final value in values) {
final v = _toDouble(value);
var placed = false;
for (var i = 0; i < sortedBounds.length; i++) {
if (v <= sortedBounds[i]) {
counts[i]++;
placed = true;
break;
}
}
if (!placed) {
counts[sortedBounds.length]++;
}
}
final bins = <HistogramBucket>[];
for (var i = 0; i < sortedBounds.length; i++) {
bins.add(HistogramBucket(upperBound: sortedBounds[i], count: counts[i]));
}
bins.add(HistogramBucket(count: counts.last));
return Histogram(List.unmodifiable(bins));
}