exactWeightErrorMasks function

List<LogicValue> exactWeightErrorMasks(
  1. int width,
  2. int weight, {
  3. int maxMasks = 100000,
})

Generates every mask with exactly weight set bits.

The result is bounded by maxMasks to prevent accidental construction of an impractically large exhaustive vector set.

Implementation

List<LogicValue> exactWeightErrorMasks(
  int width,
  int weight, {
  int maxMasks = 100000,
}) {
  _validateWidth(width);
  if (weight < 0 || weight > width) {
    throw RohdHclException('Error weight must be between zero and width.');
  }
  final result = <LogicValue>[];
  void visit(int nextBit, int remaining, BigInt mask) {
    if (result.length >= maxMasks) {
      throw RohdHclException(
          'Error mask generation exceeded maxMasks=$maxMasks.');
    }
    if (remaining == 0) {
      result.add(LogicValue.ofBigInt(mask, width));
      return;
    }
    for (var bit = nextBit; bit <= width - remaining; bit++) {
      visit(bit + 1, remaining - 1, mask | (BigInt.one << bit));
    }
  }

  visit(0, weight, BigInt.zero);
  return result;
}