/// <summary> /// Recursively add Nodes(characters) to double array trie /// </summary> /// <param name="previous"></param> /// <param name="index"></param> /// <param name="node"></param> void Add(int previous, int index, Trie.Node node) { if (node.Children.Any() && node.HasSinglePath() && node.Children[0].Key != TerminatingCharacter) { // If node has only one path, put the rest in tail array BaseBuffer[index] = TailIndex; // current index of tail array AddToTail(node.Children[0]); CheckBuffer[index] = previous; return; // No more child to process } var startIndex = (Compact ? 0 : index); var baseIndex = FindBase(startIndex, node.Children); BaseBuffer[index] = baseIndex; if (previous >= 0) { CheckBuffer[index] = previous; // Set check value } foreach (var child in node.Children) { // For each child to double array trie if (Compact) { Add(index, baseIndex + child.Key, child); } else { Add(index, index + baseIndex + child.Key, child); } } }
/// <summary> /// Add characters(nodes) to tail array /// </summary> /// <param name="node"></param> void AddToTail(Trie.Node node) { while (true) { if (TailBuffer.Length < TailIndex - TailOffset + 1) { var tmp = TailBuffer; Array.Resize(ref tmp, TailBuffer.Length + (int)(TailBuffer.Length * BufferGrowthPercentage)); TailBuffer = tmp; } TailBuffer[TailIndex++ - TailOffset] = node.Key; // set character of current node if (!node.Children.Any()) { // if it reached the end of input, break. break; } node = node.Children[0]; // Move to next node } }