public static MultiDawg <TPayload> LoadFrom(Stream stream, Func <BinaryReader, TPayload> readPayload) { var reader = new BinaryReader(stream); if (reader.ReadInt32() != Signature) { throw new Exception("Invalid signature."); } if (reader.ReadInt32() != Version) { throw new Exception("Invalid version."); } int nodeCount = reader.ReadInt32(); int rootNodeIndex = reader.ReadInt32(); TPayload[][] payloads = reader.ReadArray(r => r.ReadArray(readPayload)); char[] indexToChar = reader.ReadArray(r => r.ReadChar()); ushort[] charToIndexPlusOne = CharToIndexPlusOneMap.Get(indexToChar); YaleReader.ReadChildren(indexToChar, nodeCount, reader, out var firstChildForNode, out var children); var yaleGraph = new YaleGraph(children, firstChildForNode, charToIndexPlusOne, rootNodeIndex, indexToChar); return(new MultiDawg <TPayload>(yaleGraph, payloads)); }
public YaleDawg(BinaryReader reader, Func <BinaryReader, TPayload> readPayload) { nodeCount = reader.ReadInt32(); rootNodeIndex = reader.ReadInt32(); payloads = reader.ReadArray(readPayload); indexToChar = reader.ReadArray(r => r.ReadChar()); ushort[] charToIndexPlusOne = CharToIndexPlusOneMap.Get(indexToChar); YaleReader.ReadChildren(indexToChar, nodeCount, reader, out firstChildForNode, out children); yaleGraph = new YaleGraph(children, firstChildForNode, charToIndexPlusOne, rootNodeIndex, indexToChar); }
private static void WriteChildrenNoLength <T>(BinaryWriter writer, IEnumerable <Node <T> > nodes, Dictionary <Node <T>, int> nodeIndex, char[] allChars) { ushort[] charToIndexPlusOne = CharToIndexPlusOneMap.Get(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]); } } }