histogram static method

BigHistogram histogram(
  1. Iterable<Object?> values, {
  2. required List<BigInt> buckets,
})

Builds a histogram for BigInt magnitudes with buckets as upper bounds. Builds a histogram of values using BigInt buckets as inclusive upper bounds. The final bucket is an open-ended tail.

Implementation

static BigHistogram histogram(
  Iterable<Object?> values, {
  required List<BigInt> 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 = _toBigInt(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 = <BigHistogramBucket>[];
  for (var i = 0; i < sortedBounds.length; i++) {
    bins.add(
      BigHistogramBucket(upperBound: sortedBounds[i], count: counts[i]),
    );
  }
  bins.add(BigHistogramBucket(count: counts.last));
  return BigHistogram(List.unmodifiable(bins));
}