private readonly Child [] children; // size = NNZ public YaleDawg(BinaryReader reader, Func <BinaryReader, TPayload> readPayload) { // The nodes are grouped by (has payload, has children). nodeCount = reader.ReadInt32(); rootNodeIndex = reader.ReadInt32(); payloads = MatrixDawg <TPayload> .ReadArray(reader, readPayload); indexToChar = MatrixDawg <TPayload> .ReadArray(reader, r => r.ReadChar()); charToIndexPlusOne = MatrixDawg <TPayload> .GetCharToIndexPlusOneMap(indexToChar); firstChildForNode = new int[nodeCount + 1]; int firstChildForNode_i = 0; int totalChildCount = reader.ReadInt32(); children = new Child [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 Child(childNodeIndex, charIndex); } } firstChar = indexToChar.FirstOrDefault(); lastChar = indexToChar.LastOrDefault(); }
private static void WriteChildrenNoLength(BinaryWriter writer, IEnumerable <Node <TPayload> > nodes, Dictionary <Node <TPayload>, int> nodeIndex, char[] allChars) { var charToIndexPlusOne = MatrixDawg <TPayload> .GetCharToIndexPlusOneMap(allChars); char firstChar = allChars.FirstOrDefault(); foreach (var node in nodes) { WriteInt(writer, node.Children.Count, allChars.Length + 1); foreach (var child in node.Children.OrderBy(c => c.Key)) { int charIndex = charToIndexPlusOne [child.Key - firstChar] - 1; WriteInt(writer, charIndex, allChars.Length); writer.Write(nodeIndex [child.Value]); } } }