insert method

dynamic insert(
  1. String word
)

Implementation

insert(String word) {
  TokenSet node;
  int commonPrefix = 0;

  if (word.compareTo(previousWord) < 0) {
    throw Exception("Out of order word insertion");
  }

  for (var i = 0; i < word.length && i < previousWord.length; i++) {
    if (word[i] != previousWord[i]) {
      break;
    }
    commonPrefix++;
  }

  minimize(commonPrefix);

  if (uncheckedNodes.isEmpty) {
    node = root;
  } else {
    node = uncheckedNodes[uncheckedNodes.length - 1].child;
  }

  for (var i = commonPrefix; i < word.length; i++) {
    var nextNode = TokenSet(), char = word[i];

    node.edges[char] = nextNode;

    uncheckedNodes.add(BuilderFrame(
      parent: node,
      char: char,
      child: nextNode,
    ));

    node = nextNode;
  }

  node.isFinal = true;
  previousWord = word;
}