/// <summary> /// Creates a new suffix trie. /// </summary> public SuffixTrie() { // The root represents a special terminator character root = new TrieNode(0); lastCode = 0; }
/// <summary> /// Recursively updates the trie, given the current size (not counting the root) /// of the trie. To avoid generating unnecessary garbage, we pass a start index /// into key. /// </summary> /// <param name="key">the key to lookup/update, relative to <see cref="startIndex"/></param> /// <param name="startIndex">start location into <see cref="key"/></param> /// <param name="nextCode">the next code to be assigned</param> public void Update(byte[] key, int startIndex, ref uint nextCode) { // string is already known or a substring of a known string if (startIndex >= key.Length) { return; } byte navigateChar = key[startIndex]; TrieNode nextNode; // if the byte is not already known if (!children.TryGetValue(navigateChar, out nextNode)) { nextNode = new TrieNode(++nextCode); children.Add(navigateChar, nextNode); } nextNode.Update(key, startIndex + 1, ref nextCode); }