public TrieNode(SupportedCharacters supportedCharacters) { if (supportedCharacters == null) { throw new ArgumentNullException("supportedCharacters"); } this.supportedCharacters = supportedCharacters; children = new TrieNode[supportedCharacters.Count]; }
private void Initialize(IList <string> input) { // Identify the set of characters contained in the input supportedCharacters = new SupportedCharacters(input.Cast <IEnumerable <char> >().SelectMany(cs => cs)); // Create a node-based trie var rootNode = new TrieNode(supportedCharacters); foreach (var str in input) { rootNode.Add(str); } // Shrink the trie down to an acyclic word graph by sharing common suffixes. var canonicalNodes = CreateCanonicalNodeDictionary(rootNode); // Make sure that our array representation can contain the number of nodes if (canonicalNodes.Count >= byte.MaxValue) { throw new InvalidOperationException("Too many nodes - System.Byte may be too small."); } // Initialize the array representation of the node structure trie = new byte[canonicalNodes.Count + 1, supportedCharacters.Count + 1]; // Establish a mapping between canonical nodes an array indices. var numToNode = new TrieNode[canonicalNodes.Keys.Count + 1]; canonicalNodes.Keys.CopyTo(numToNode, 1); // Leave the first row (number 0) null. var nodeToNum = new Dictionary <TrieNode, byte>(new NodeToNumComparer()); for (var i = 1; i < numToNode.Length; ++i) { nodeToNum.Add(numToNode[i], (byte)i); } // Populate the array, and let the garbage collecter handle the object refs. Fill(rootNode, nodeToNum); root = nodeToNum[rootNode]; }