encode method

List<GfValue> encode(
  1. Iterable<GfValue> message
)

Encodes message systematically into a transmitted codeword.

Implementation

List<GfValue> encode(Iterable<GfValue> message) {
  final symbols = message.toList(growable: false);
  if (symbols.length != messageSymbols) {
    throw ArgumentError.value(
        symbols, 'message', 'Expected $messageSymbols symbols.');
  }
  _requireSymbols(symbols);

  final codeword = [
    ...List<GfValue>.filled(paritySymbols, field.zero),
    ...symbols,
  ];
  for (var index = codeword.length - 1;
      index >= generatorPolynomial.length - 1;
      index--) {
    final scale = codeword[index] / generatorPolynomial.last;
    if (scale.isZero) {
      continue;
    }
    for (var coefficient = 0;
        coefficient < generatorPolynomial.length;
        coefficient++) {
      codeword[index - coefficient] += scale *
          generatorPolynomial[generatorPolynomial.length - 1 - coefficient];
    }
  }
  for (var index = 0; index < messageSymbols; index++) {
    codeword[paritySymbols + index] = symbols[index];
  }
  return List<GfValue>.unmodifiable(codeword);
}