提交 8a600041 编写于 作者: C Cyrus Najmabadi

Add serialization support for BKTrees.

上级 a132816a
......@@ -21,6 +21,17 @@ public Edge(int editDistance, int childNodeIndex)
EditDistance = editDistance;
ChildNodeIndex = childNodeIndex;
}
internal void WriteTo(ObjectWriter writer)
{
writer.WriteInt32(EditDistance);
writer.WriteInt32(ChildNodeIndex);
}
internal static Edge ReadFrom(ObjectReader reader)
{
return new Edge(reader.ReadInt32(), reader.ReadInt32());
}
}
}
}
\ No newline at end of file
......@@ -27,6 +27,18 @@ public Node(char[] lowerCaseCharacters, int edgeCount, int firstEdgeIndex)
EdgeCount = edgeCount;
FirstEdgeIndex = firstEdgeIndex;
}
internal void WriteTo(ObjectWriter writer)
{
writer.WriteValue(LowerCaseCharacters);
writer.WriteInt32(EdgeCount);
writer.WriteInt32(FirstEdgeIndex);
}
internal static Node ReadFrom(ObjectReader reader)
{
return new Node((char[])reader.ReadValue(), reader.ReadInt32(), reader.ReadInt32());
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Roslyn.Utilities
{
internal partial class BKTree
{
internal void WriteTo(ObjectWriter writer)
{
writer.WriteInt32(this._nodes.Length);
foreach (var node in _nodes)
{
node.WriteTo(writer);
}
writer.WriteInt32(this._edges.Length);
foreach (var edge in _edges)
{
edge.WriteTo(writer);
}
}
internal static BKTree ReadFrom(ObjectReader reader)
{
var nodes = new Node[reader.ReadInt32()];
for (var i = 0; i < nodes.Length; i++)
{
nodes[i] = Node.ReadFrom(reader);
}
var edges = new Edge[reader.ReadInt32()];
for (var i = 0; i < edges.Length; i++)
{
edges[i] = Edge.ReadFrom(reader);
}
return new BKTree(nodes, edges);
}
}
}
......@@ -12,6 +12,10 @@ namespace Roslyn.Utilities
{
internal partial class BKTree
{
public static readonly BKTree Empty = new BKTree(
SpecializedCollections.EmptyArray<Node>(),
SpecializedCollections.EmptyArray<Edge>());
// We have two completely flat arrays of structs (except for the char[] values the nodes
// point to). These arrays fully represent the BK tree. The structure is as follows:
//
......
......@@ -418,6 +418,7 @@
<Compile Include="Simplification\SimplifyTypeNameCodeAction.cs" />
<Compile Include="Utilities\BKTree.Edge.cs" />
<Compile Include="Utilities\BKTree.Node.cs" />
<Compile Include="Utilities\BKTree.Serialization.cs" />
<Compile Include="Utilities\ForegroundThreadDataKind.cs" />
<Compile Include="Utilities\IReadOnlyDictionaryExtensions.cs" />
<Compile Include="Utilities\ValuesSources\CachedWeakValueSource.cs" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册