CompiledWeightedTable<T> constructor

CompiledWeightedTable<T>(
  1. List<T> items,
  2. List<double> weights
)

Implementation

factory CompiledWeightedTable(List<T> items, List<double> weights) {
  if (items.length != weights.length) {
    throw ArgumentError(
      'CompiledWeightedTable: items (${items.length}) and weights '
      '(${weights.length}) must be the same length',
    );
  }
  if (items.isEmpty) {
    throw ArgumentError('CompiledWeightedTable: items must not be empty');
  }
  final cumulative = <double>[];
  var running = 0.0;
  for (final w in weights) {
    if (!w.isFinite || w < 0) {
      throw ArgumentError(
        'CompiledWeightedTable: every weight must be finite and >= 0, '
        'got $w',
      );
    }
    running += w;
    cumulative.add(running);
  }
  if (!running.isFinite || running <= 0) {
    throw ArgumentError(
      'CompiledWeightedTable: weights must sum to a finite value > 0, '
      'got $running',
    );
  }
  return CompiledWeightedTable._(
    List.unmodifiable(items),
    List.unmodifiable(cumulative),
  );
}