addPattern method

int addPattern(
  1. String pattern
)

Adds a pattern to the dictionary.

Returns the code assigned to the pattern, or the existing code if the pattern is already in the dictionary.

Implementation

int addPattern(String pattern) {
  if (pattern.length < minPatternLength) return -1;
  if (_encodeMap.containsKey(pattern)) {
    return _encodeMap[pattern]!;
  }
  if (_encodeMap.length >= maxDictionarySize) {
    // Evict least recently used (first entry)
    final firstKey = _encodeMap.keys.first;
    final firstCode = _encodeMap.remove(firstKey)!;
    _decodeMap.remove(firstCode);
  }

  final code = _nextCode++;
  _encodeMap[pattern] = code;
  _decodeMap[code] = pattern;
  return code;
}