forked from YaccConstructor/QuickGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBidirectionAdapterGraph.cs
More file actions
194 lines (166 loc) · 5.16 KB
/
BidirectionAdapterGraph.cs
File metadata and controls
194 lines (166 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics.Contracts;
using QuickGraph.Collections;
using System.Diagnostics;
namespace QuickGraph
{
/// <summary>
/// Wraps a vertex list graph (out-edges only) and caches the in-edge dictionary.
/// </summary>
/// <typeparam name="TVertex">type of the vertices</typeparam>
/// <typeparam name="TEdge">type of the edges</typeparam>
#if !SILVERLIGHT
[Serializable]
#endif
[DebuggerDisplay("VertexCount = {VertexCount}, EdgeCount = {EdgeCount}")]
public class BidirectionAdapterGraph<TVertex, TEdge>
: IBidirectionalGraph<TVertex, TEdge>
where TEdge : IEdge<TVertex>
{
private readonly IVertexAndEdgeListGraph<TVertex, TEdge> baseGraph;
private readonly Dictionary<TVertex, EdgeList<TVertex, TEdge>> inEdges;
public BidirectionAdapterGraph(IVertexAndEdgeListGraph<TVertex, TEdge> baseGraph)
{
Contract.Requires(baseGraph != null);
this.baseGraph = baseGraph;
this.inEdges = new Dictionary<TVertex, EdgeList<TVertex, TEdge>>(this.baseGraph.VertexCount);
foreach (var edge in this.baseGraph.Edges)
{
EdgeList<TVertex, TEdge> list;
if (!this.inEdges.TryGetValue(edge.Target, out list))
this.inEdges.Add(edge.Target, list = new EdgeList<TVertex, TEdge>());
list.Add(edge);
}
}
[Pure]
public bool IsInEdgesEmpty(TVertex v)
{
return this.InDegree(v) == 0;
}
[Pure]
public int InDegree(TVertex v)
{
EdgeList<TVertex, TEdge> edges;
if (this.inEdges.TryGetValue(v, out edges))
return edges.Count;
else
return 0;
}
static readonly TEdge[] emptyEdges = new TEdge[0];
[Pure]
public IEnumerable<TEdge> InEdges(TVertex v)
{
EdgeList<TVertex, TEdge> edges;
if (this.inEdges.TryGetValue(v, out edges))
return edges;
else
return emptyEdges;
}
[Pure]
public bool TryGetInEdges(TVertex v, out IEnumerable<TEdge> edges)
{
EdgeList<TVertex, TEdge> es;
if (this.inEdges.TryGetValue(v, out es))
{
edges = es;
return true;
}
edges = null;
return false;
}
[Pure]
public TEdge InEdge(TVertex v, int index)
{
return this.inEdges[v][index];
}
[Pure]
public int Degree(TVertex v)
{
return this.InDegree(v) + this.OutDegree(v);
}
[Pure]
public bool ContainsEdge(TVertex source, TVertex target)
{
return this.baseGraph.ContainsEdge(source, target);
}
[Pure]
public bool TryGetEdges(TVertex source, TVertex target, out IEnumerable<TEdge> edges)
{
return this.baseGraph.TryGetEdges(source, target, out edges);
}
[Pure]
public bool TryGetEdge(TVertex source, TVertex target, out TEdge edge)
{
return this.baseGraph.TryGetEdge(source, target, out edge);
}
[Pure] // InterfacePureBug
public bool IsOutEdgesEmpty(TVertex v)
{
return this.baseGraph.IsOutEdgesEmpty(v);
}
[Pure]
public int OutDegree(TVertex v)
{
return this.baseGraph.OutDegree(v);
}
[Pure]
public IEnumerable<TEdge> OutEdges(TVertex v)
{
return this.baseGraph.OutEdges(v);
}
[Pure]
public bool TryGetOutEdges(TVertex v, out IEnumerable<TEdge> edges)
{
return this.baseGraph.TryGetOutEdges(v, out edges);
}
[Pure]
public TEdge OutEdge(TVertex v, int index)
{
return this.baseGraph.OutEdge(v, index);
}
public bool IsDirected
{
get { return this.baseGraph.IsDirected; }
}
public bool AllowParallelEdges
{
get { return this.baseGraph.AllowParallelEdges; }
}
public bool IsVerticesEmpty
{
get { return this.baseGraph.IsVerticesEmpty; }
}
public int VertexCount
{
get { return this.baseGraph.VertexCount; }
}
public IEnumerable<TVertex> Vertices
{
get { return this.baseGraph.Vertices; }
}
[Pure]
public bool ContainsVertex(TVertex vertex)
{
return this.baseGraph.ContainsVertex(vertex);
}
public bool IsEdgesEmpty
{
get { return this.baseGraph.IsEdgesEmpty; }
}
public int EdgeCount
{
get { return this.baseGraph.EdgeCount; }
}
public virtual IEnumerable<TEdge> Edges
{
get { return this.baseGraph.Edges; }
}
[Pure]
public bool ContainsEdge(TEdge edge)
{
return this.baseGraph.ContainsEdge(edge);
}
}
}