Exemplo n.º 1
0
        public Dawg <TPayload> BuildDawg()
        {
            var levels = LevelBuilder <TPayload> .BuildLevels(root);

            foreach (var level in levels)
            {
                foreach (var similarNodes in level.GroupBy(n => n, n => n, new NodeWrapperEqualityComparer <TPayload> ()))
                {
                    foreach (var similarNode in similarNodes)
                    {
                        similarNode.Super.Children [similarNode.Char] = similarNodes.Key.Node;
                    }
                }
            }

            return(new Dawg <TPayload> (root));
        }
Exemplo n.º 2
0
        public void SaveTo(Stream stream, Action <BinaryWriter, TPayload> writePayload)
        {
            var levels = LevelBuilder <TPayload> .BuildLevels(root);

            var allNodes = levels.SelectMany(level => level.Distinct(new NodeSuperEqualityComparerByNode()))
                           .Concat(new [] { new NodeWrapper <TPayload> (root, null, ' ') })
                           .ToArray();

            var nodeIndex = allNodes
                            .Select((n, i) => new KeyValuePair <Node <TPayload>, int> (n.Node, i))
                            .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

            using (var writer = new BinaryWriter(stream))
            {
                writer.Write(nodeIndex.Count);

                writer.Write(nodeIndex [root]);

                foreach (var nodeSuper in allNodes)
                {
                    writer.Write(nodeSuper.Char);
                }

                foreach (var nodeI in nodeIndex)
                {
                    var node = nodeI.Key;

                    writer.Write(checked ((ushort)node.Children.Count));

                    foreach (var child in node.Children.Values)
                    {
                        writer.Write(nodeIndex [child]);
                    }

                    writePayload(writer, node.Payload);
                }
            }
        }
Exemplo n.º 3
0
        public Dawg <TPayload> BuildDawg()
        {
            LevelBuilder <TPayload> .BuildLevelsExcludingRoot(root);

            return(new Dawg <TPayload>(new OldDawg <TPayload> (root)));
        }