Satsuma
a delicious .NET graph library
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Pages
Graph.cs
Go to the documentation of this file.
1 #region License
2 /*This file is part of Satsuma Graph Library
3 Copyright © 2013 Balázs Szalkai
4 
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8 
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17 
18  2. Altered source versions must be plainly marked as such, and must not be
19  misrepresented as being the original software.
20 
21  3. This notice may not be removed or altered from any source
22  distribution.*/
23 #endregion
24 
25 using System;
26 using System.Collections.Generic;
27 
28 namespace Satsuma
29 {
31  public struct Node : IEquatable<Node>
32  {
35  public long Id { get; private set; }
36 
38  public Node(long id)
39  : this()
40  {
41  Id = id;
42  }
43 
46  public static Node Invalid
47  {
48  get { return new Node(0); }
49  }
50 
51  public bool Equals(Node other)
52  {
53  return Id == other.Id;
54  }
55 
56  public override bool Equals(object obj)
57  {
58  if (obj is Node) return Equals((Node)obj);
59  return false;
60  }
61 
62  public override int GetHashCode()
63  {
64  return Id.GetHashCode();
65  }
66 
67  public override string ToString()
68  {
69  return "#" + Id;
70  }
71 
72  public static bool operator ==(Node a, Node b)
73  {
74  return a.Equals(b);
75  }
76 
77  public static bool operator !=(Node a, Node b)
78  {
79  return !(a == b);
80  }
81  }
82 
87  public struct Arc : IEquatable<Arc>
88  {
91  public long Id { get; private set; }
92 
94  public Arc(long id)
95  : this()
96  {
97  Id = id;
98  }
99 
102  public static Arc Invalid
103  {
104  get { return new Arc(0); }
105  }
106 
107  public bool Equals(Arc other)
108  {
109  return Id == other.Id;
110  }
111 
112  public override bool Equals(object obj)
113  {
114  if (obj is Arc) return Equals((Arc)obj);
115  return false;
116  }
117 
118  public override int GetHashCode()
119  {
120  return Id.GetHashCode();
121  }
122 
123  public override string ToString()
124  {
125  return "|" + Id;
126  }
127 
128  public static bool operator ==(Arc a, Arc b)
129  {
130  return a.Equals(b);
131  }
132 
133  public static bool operator !=(Arc a, Arc b)
134  {
135  return !(a == b);
136  }
137  }
138 
141  public enum Directedness
142  {
144  Directed,
146  Undirected
147  }
148 
150  public interface IBuildableGraph : IClearable
151  {
153  Node AddNode();
158  Arc AddArc(Node u, Node v, Directedness directedness);
159  }
160 
162  public interface IDestroyableGraph : IClearable
163  {
166  bool DeleteNode(Node node);
169  bool DeleteArc(Arc arc);
170  }
171 
174  public interface IArcLookup
175  {
177  Node U(Arc arc);
179  Node V(Arc arc);
181  bool IsEdge(Arc arc);
182  }
183 
185  public static class ArcLookupExtensions
186  {
189  public static string ArcToString(this IArcLookup graph, Arc arc)
190  {
191  if (arc == Arc.Invalid) return "Arc.Invalid";
192  return graph.U(arc) + (graph.IsEdge(arc) ? "<-->" : "--->") + graph.V(arc);
193  }
194 
199  public static Node Other(this IArcLookup graph, Arc arc, Node node)
200  {
201  Node u = graph.U(arc);
202  if (u != node) return u;
203  return graph.V(arc);
204  }
205 
211  public static Node[] Nodes(this IArcLookup graph, Arc arc, bool allowDuplicates = true)
212  {
213  var u = graph.U(arc);
214  var v = graph.V(arc);
215  if (!allowDuplicates && u == v) return new Node[] { u };
216  return new Node[] { u, v };
217  }
218  }
219 
221  public enum ArcFilter
222  {
224  All,
226  Edge,
228  Forward,
230  Backward
231  }
232 
234  public interface IGraph : IArcLookup
235  {
237  IEnumerable<Node> Nodes();
242  IEnumerable<Arc> Arcs(ArcFilter filter = ArcFilter.All);
249  IEnumerable<Arc> Arcs(Node u, ArcFilter filter = ArcFilter.All);
256  IEnumerable<Arc> Arcs(Node u, Node v, ArcFilter filter = ArcFilter.All);
257 
259  int NodeCount();
262  int ArcCount(ArcFilter filter = ArcFilter.All);
265  int ArcCount(Node u, ArcFilter filter = ArcFilter.All);
268  int ArcCount(Node u, Node v, ArcFilter filter = ArcFilter.All);
269 
274  bool HasNode(Node node);
279  bool HasArc(Arc arc);
280  }
281 
285  public sealed class CustomGraph : Supergraph
286  {
287  public CustomGraph()
288  : base(null) { }
289  }
290 }