private int GetChildIndex(int node_i, char c) { if (c >= firstChar && c <= lastChar) { ushort charIndexPlusOne = charToIndexPlusOne[c - firstChar]; if (charIndexPlusOne != 0) { int firstChild_i = firstChildForNode[node_i]; int lastChild_i = firstChildForNode[node_i + 1]; int nChildren = lastChild_i - firstChild_i; var charIndex = (ushort)(charIndexPlusOne - 1); int child_i; if (nChildren == 1) { child_i = children[firstChild_i].CharIndex == charIndex ? firstChild_i : -1; } else { var searchValue = new YaleChild(-1, charIndex); child_i = Array.BinarySearch(children, firstChild_i, nChildren, searchValue, childComparer); } return(child_i); } } return(-1); }
public static void ReadChildren(char[] indexToChar, int nodeCount, BinaryReader reader, out int[] firstChildForNode, out YaleChild[] children) { firstChildForNode = new int[nodeCount + 1]; int firstChildForNode_i = 0; int totalChildCount = reader.ReadInt32(); children = new YaleChild [totalChildCount]; firstChildForNode[nodeCount] = totalChildCount; int globalChild_i = 0; for (int child1_i = 0; child1_i < nodeCount; ++child1_i) { firstChildForNode[firstChildForNode_i++] = globalChild_i; ushort childCount = ReadInt(reader, indexToChar.Length + 1); for (ushort child_i = 0; child_i < childCount; ++child_i) { ushort charIndex = ReadInt(reader, indexToChar.Length); int childNodeIndex = reader.ReadInt32(); children[globalChild_i++] = new YaleChild(childNodeIndex, charIndex); } } }
public IEnumerable <int> GetPath(IEnumerable <char> word) { int node_i = rootNodeIndex; if (node_i == -1) { yield return(-1); yield break; } yield return(node_i); foreach (char c in word) { ushort charIndexPlusOne; if (c < firstChar || c > lastChar || (charIndexPlusOne = charToIndexPlusOne[c - firstChar]) == 0) { yield return(-1); yield break; } int firstChild_i = firstChildForNode[node_i]; int lastChild_i = firstChildForNode[node_i + 1]; int nChildren = lastChild_i - firstChild_i; var charIndex = (ushort)(charIndexPlusOne - 1); int child_i; if (nChildren == 1) { child_i = children[firstChild_i].CharIndex == charIndex ? firstChild_i : -1; } else { var searchValue = new YaleChild(-1, charIndex); child_i = Array.BinarySearch(children, firstChild_i, nChildren, searchValue, childComparer); } if (child_i < 0) { yield return(-1); yield break; } node_i = children[child_i].Index; yield return(node_i); } }