decode method

LZW decode (
  1. List<int> lzw
)

feeds a list of numbers into the decoder. This can be done as many times as needed. To finalize, the method finalizeDecoding() has to be called.

Implementation

LZW decode(List<int> lzw) {
  if (_isEncoding) throw IllegalStateException("still encoding.");
  _isDecoding = true;

  if (coder == null) {
    coder = LZW(
      alphabet: alphabet,
      codeSize: codeSize,
      debugEncoding: false,
    );
  }

  for (int i = 0; i < lzw.length; i++) {
    int s = lzw[i];

    if (s < blank) {
      _outString += alphabet[s];
      coder.addInput(alphabet[s]);

      if (debugDecoding) {
        print("$i: found $s -> '${alphabet[s]}' in the alphabet");
      }
    } else if (s > blank) {
      String c = coder.getInverseCodeWord(s);

      if (c != null) {
        _outString += c;
        if (debugDecoding) print("$i: found $s -> '${c}' in the encoder");
        coder.addInput(c);
      } else {
        String sNew = coder.praefix + coder.praefix[0];

        _outString += sNew;
        coder.addInput(sNew);
      }
    }
  }

  return this;
}