本文整理汇总了C#中Graph.Compress方法的典型用法代码示例。如果您正苦于以下问题:C# Graph.Compress方法的具体用法?C# Graph.Compress怎么用?C# Graph.Compress使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Graph
的用法示例。
在下文中一共展示了Graph.Compress方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestGraphCompressVertices
public void TestGraphCompressVertices()
{
var graph = new Graph<Edge>();
var vertex1 = graph.AddVertex(51, 1);
var vertex2 = graph.AddVertex(51, 2);
var vertex3 = graph.AddVertex(51, 3);
var vertex4 = graph.AddVertex(51, 3);
graph.AddEdge(vertex1, vertex2, new Edge()
{
Forward = true,
Tags = 1
}, null);
graph.AddEdge(vertex2, vertex3, new Edge()
{
Forward = true,
Tags = 2
}, null);
graph.AddEdge(vertex3, vertex4, new Edge()
{
Forward = true,
Tags = 3
}, null);
graph.AddEdge(vertex4, vertex2, new Edge()
{
Forward = true,
Tags = 4
}, null);
// make vertex4 obsolete.
graph.RemoveEdges(vertex4);
graph.Compress();
Assert.AreEqual(3, graph.VertexCount);
Assert.AreEqual(graph.GetEdges(vertex1).ToKeyValuePairs().Length, 1);
Assert.AreEqual(graph.GetEdges(vertex2).ToKeyValuePairs().Length, 2);
Assert.AreEqual(graph.GetEdges(vertex3).ToKeyValuePairs().Length, 1);
}
示例2: TestGraphCompressEdges
public void TestGraphCompressEdges()
{
var graph = new Graph<Edge>();
var vertex1 = graph.AddVertex(51, 1);
var vertex2 = graph.AddVertex(51, 2);
var vertex3 = graph.AddVertex(51, 3);
var vertex4 = graph.AddVertex(51, 3);
graph.AddEdge(vertex1, vertex2, new Edge()
{
Forward = true,
Tags = 1
}, null);
graph.AddEdge(vertex2, vertex3, new Edge()
{
Forward = true,
Tags = 2
}, null);
graph.AddEdge(vertex3, vertex4, new Edge()
{
Forward = true,
Tags = 3
}, null);
graph.AddEdge(vertex4, vertex2, new Edge()
{
Forward = true,
Tags = 4
}, null);
graph.RemoveEdge(vertex2, vertex3);
graph.Compress();
Assert.IsFalse(graph.ContainsEdges(vertex2, vertex3));
Assert.IsFalse(graph.ContainsEdges(vertex3, vertex2));
Assert.AreEqual(graph.GetEdges(vertex1).ToKeyValuePairs().Length, 1);
Assert.AreEqual(graph.GetEdges(vertex2).ToKeyValuePairs().Length, 2);
Assert.AreEqual(graph.GetEdges(vertex3).ToKeyValuePairs().Length, 1);
Assert.AreEqual(graph.GetEdges(vertex4).ToKeyValuePairs().Length, 2);
graph = new Graph<Edge>();
vertex1 = graph.AddVertex(51, 1);
vertex2 = graph.AddVertex(51, 2);
vertex3 = graph.AddVertex(51, 3);
vertex4 = graph.AddVertex(51, 3);
graph.AddEdge(vertex1, vertex2, new Edge()
{
Forward = true,
Tags = 1
}, null);
graph.AddEdge(vertex2, vertex3, new Edge()
{
Forward = true,
Tags = 2
}, null);
graph.AddEdge(vertex3, vertex4, new Edge()
{
Forward = true,
Tags = 3
}, null);
graph.AddEdge(vertex4, vertex2, new Edge()
{
Forward = true,
Tags = 4
}, null);
graph.RemoveEdge(vertex3, vertex4);
graph.Compress();
Assert.IsFalse(graph.ContainsEdges(vertex3, vertex4));
Assert.IsFalse(graph.ContainsEdges(vertex4, vertex3));
Assert.AreEqual(graph.GetEdges(vertex1).ToKeyValuePairs().Length, 1);
Assert.AreEqual(graph.GetEdges(vertex2).ToKeyValuePairs().Length, 3);
Assert.AreEqual(graph.GetEdges(vertex3).ToKeyValuePairs().Length, 1);
Assert.AreEqual(graph.GetEdges(vertex4).ToKeyValuePairs().Length, 1);
Edge edge;
Assert.IsTrue(graph.GetEdge(vertex1, vertex2, out edge));
Assert.AreEqual(1, edge.Tags);
Assert.IsTrue(graph.GetEdge(vertex2, vertex3, out edge));
Assert.AreEqual(2, edge.Tags);
Assert.IsTrue(graph.GetEdge(vertex4, vertex2, out edge));
Assert.AreEqual(4, edge.Tags);
}
示例3: TestCompressEdges
public void TestCompressEdges()
{
using (var graph = new Graph<Edge>(new MemoryMappedStream(new MemoryStream()), 1000,
Edge.MapFromDelegate, Edge.MapToDelegate, Edge.SizeUints))
{
var vertex1 = graph.AddVertex(51, 1);
var vertex2 = graph.AddVertex(51, 2);
var vertex3 = graph.AddVertex(51, 3);
var vertex4 = graph.AddVertex(51, 3);
graph.AddEdge(vertex1, vertex2, new Edge()
{
Forward = true,
Tags = 1
}, null);
graph.AddEdge(vertex2, vertex3, new Edge()
{
Forward = true,
Tags = 2
}, null);
graph.AddEdge(vertex3, vertex4, new Edge()
{
Forward = true,
Tags = 3
}, null);
graph.AddEdge(vertex4, vertex2, new Edge()
{
Forward = true,
Tags = 4
}, null);
graph.RemoveEdge(vertex2, vertex3);
graph.Compress();
Assert.IsFalse(graph.ContainsEdges(vertex2, vertex3));
Assert.IsFalse(graph.ContainsEdges(vertex3, vertex2));
Assert.AreEqual(graph.GetEdges(vertex1).ToKeyValuePairs().Length, 1);
Assert.AreEqual(graph.GetEdges(vertex2).ToKeyValuePairs().Length, 2);
Assert.AreEqual(graph.GetEdges(vertex3).ToKeyValuePairs().Length, 1);
Assert.AreEqual(graph.GetEdges(vertex4).ToKeyValuePairs().Length, 2);
}
using (var graph = new Graph<Edge>(new MemoryMappedStream(new MemoryStream()), 1000,
Edge.MapFromDelegate, Edge.MapToDelegate, Edge.SizeUints))
{
uint vertex1 = graph.AddVertex(51, 1);
uint vertex2 = graph.AddVertex(51, 2);
uint vertex3 = graph.AddVertex(51, 3);
uint vertex4 = graph.AddVertex(51, 3);
graph.AddEdge(vertex1, vertex2, new Edge()
{
Forward = true,
Tags = 1
}, null);
graph.AddEdge(vertex2, vertex3, new Edge()
{
Forward = true,
Tags = 2
}, null);
graph.AddEdge(vertex3, vertex4, new Edge()
{
Forward = true,
Tags = 3
}, null);
graph.AddEdge(vertex4, vertex2, new Edge()
{
Forward = true,
Tags = 4
}, null);
graph.RemoveEdge(vertex3, vertex4);
graph.Compress();
Assert.IsFalse(graph.ContainsEdges(vertex3, vertex4));
Assert.IsFalse(graph.ContainsEdges(vertex4, vertex3));
Assert.AreEqual(graph.GetEdges(vertex1).ToKeyValuePairs().Length, 1);
Assert.AreEqual(graph.GetEdges(vertex2).ToKeyValuePairs().Length, 3);
Assert.AreEqual(graph.GetEdges(vertex3).ToKeyValuePairs().Length, 1);
Assert.AreEqual(graph.GetEdges(vertex4).ToKeyValuePairs().Length, 1);
Edge edge;
Assert.IsTrue(graph.GetEdge(vertex1, vertex2, out edge));
Assert.AreEqual(1, edge.Tags);
Assert.IsTrue(graph.GetEdge(vertex2, vertex3, out edge));
Assert.AreEqual(2, edge.Tags);
Assert.IsTrue(graph.GetEdge(vertex4, vertex2, out edge));
Assert.AreEqual(4, edge.Tags);
}
}
示例4: TestCompressVertices
public void TestCompressVertices()
{
using (var graph = new Graph<Edge>(new MemoryMappedStream(new MemoryStream()), 1000,
Edge.MapFromDelegate, Edge.MapToDelegate, Edge.SizeUints))
{
var vertex1 = graph.AddVertex(51, 1);
var vertex2 = graph.AddVertex(51, 2);
var vertex3 = graph.AddVertex(51, 3);
var vertex4 = graph.AddVertex(51, 3);
graph.AddEdge(vertex1, vertex2, new Edge()
{
Forward = true,
Tags = 1
}, null);
graph.AddEdge(vertex2, vertex3, new Edge()
{
Forward = true,
Tags = 2
}, null);
graph.AddEdge(vertex3, vertex4, new Edge()
{
Forward = true,
Tags = 3
}, null);
graph.AddEdge(vertex4, vertex2, new Edge()
{
Forward = true,
Tags = 4
}, null);
// make vertex4 obsolete.
graph.RemoveEdges(vertex4);
graph.Compress();
Assert.AreEqual(3, graph.VertexCount);
Assert.AreEqual(graph.GetEdges(vertex1).ToKeyValuePairs().Length, 1);
Assert.AreEqual(graph.GetEdges(vertex2).ToKeyValuePairs().Length, 2);
Assert.AreEqual(graph.GetEdges(vertex3).ToKeyValuePairs().Length, 1);
}
}
示例5: SortHilbertTest7
public void SortHilbertTest7()
{
var n = GraphExtensions.DefaultHilbertSteps;
// build locations.
var locations = new List<Tuple<GeoCoordinate, uint>>();
locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(-90, -180), 1));
locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(-90, -60), 2));
locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(-90, 60), 3));
locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(-90, 180), 4));
locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(-30, -180), 5));
locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(-30, -60), 6));
locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(-30, 60), 7));
locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(-30, 180), 8));
locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(30, -180), 9));
locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(30, -60), 10));
locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(30, 60), 11));
locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(30, 180), 12));
locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(90, -180), 13));
locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(90, -60), 14));
locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(90, 60), 15));
locations.Add(new Tuple<GeoCoordinate, uint>(new GeoCoordinate(90, 180), 16));
// build graph.
var graph = new Graph<Edge>();
for (var idx = 0; idx < locations.Count; idx++)
{
graph.AddVertex((float)locations[idx].Item1.Latitude,
(float)locations[idx].Item1.Longitude);
}
// add edges.
Func<uint, uint, uint> buildTagsId = (v1, v2) => { return v1 + (v2 * 16); };
graph.AddEdge(1, 2, new Edge() { Tags = buildTagsId(1, 2) });
graph.AddEdge(2, 3, new Edge() { Tags = buildTagsId(2, 3) });
graph.AddEdge(3, 4, new Edge() { Tags = buildTagsId(3, 4) });
graph.AddEdge(4, 5, new Edge() { Tags = buildTagsId(4, 5) });
graph.AddEdge(5, 6, new Edge() { Tags = buildTagsId(5, 6) });
graph.AddEdge(6, 7, new Edge() { Tags = buildTagsId(6, 7) });
graph.AddEdge(7, 8, new Edge() { Tags = buildTagsId(7, 8) });
graph.AddEdge(8, 9, new Edge() { Tags = buildTagsId(8, 9) });
graph.AddEdge(9, 10, new Edge() { Tags = buildTagsId(9, 10) });
graph.AddEdge(10, 11, new Edge() { Tags = buildTagsId(10, 11) });
graph.AddEdge(11, 12, new Edge() { Tags = buildTagsId(11, 12) });
graph.AddEdge(12, 13, new Edge() { Tags = buildTagsId(12, 13) });
graph.AddEdge(13, 14, new Edge() { Tags = buildTagsId(13, 14) });
graph.AddEdge(14, 15, new Edge() { Tags = buildTagsId(14, 15) });
// add 10 random edges.
for (var i = 0; i < 10; i++)
{
var from = Convert.ToUInt32(OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(15) + 1);
var to = Convert.ToUInt32(OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(14) + 1);
if(from <= to)
{
to = to + 1;
}
graph.AddEdge(from, to, new Edge() { Tags = buildTagsId(from, to) });
}
// sort vertices in-place.
graph.SortHilbert();
graph.Compress();
// sort locations.
locations.Sort((x, y) =>
{
return HilbertCurve.HilbertDistance((float)x.Item1.Latitude, (float)x.Item1.Longitude, n).CompareTo(
HilbertCurve.HilbertDistance((float)y.Item1.Latitude, (float)y.Item1.Longitude, n));
});
// confirm sort.
float latitude, longitude;
var convert = new Dictionary<uint, uint>();
for (uint vertex = 1; vertex <= graph.VertexCount; vertex++)
{
graph.GetVertex(vertex, out latitude, out longitude);
Assert.AreEqual(latitude, locations[(int)(vertex - 1)].Item1.Latitude);
Assert.AreEqual(longitude, locations[(int)(vertex - 1)].Item1.Longitude);
convert.Add(vertex, locations[(int)(vertex - 1)].Item2);
}
for (uint vertex = 1; vertex <= graph.VertexCount; vertex++)
{
var edges = graph.GetEdges(vertex);
var originalVertex = convert[vertex];
foreach (var edge in edges)
{
var originalNeighbour = convert[edges.Neighbour];
if (edge.EdgeData.Forward)
{ // edge is forward.
Assert.AreEqual(buildTagsId(originalVertex, originalNeighbour), edge.EdgeData.Tags);
}
else
{ // edge is backward.
Assert.AreEqual(buildTagsId(originalNeighbour, originalVertex), edge.EdgeData.Tags);
}
}
}
//.........这里部分代码省略.........