Satsuma
a delicious .NET graph library
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Pages
RedirectedGraph.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 using System.Linq;
28 
29 namespace Satsuma
30 {
35  public sealed class RedirectedGraph : IGraph
36  {
37  public enum Direction
38  {
40  Forward,
42  Backward,
44  Edge
45  }
46 
47  private IGraph graph;
48  private Func<Arc, Direction> getDirection;
49 
53  public RedirectedGraph(IGraph graph, Func<Arc, Direction> getDirection)
54  {
55  this.graph = graph;
56  this.getDirection = getDirection;
57  }
58 
59  public Node U(Arc arc)
60  {
61  return getDirection(arc) == Direction.Backward ? graph.V(arc) : graph.U(arc);
62  }
63 
64  public Node V(Arc arc)
65  {
66  return getDirection(arc) == Direction.Backward ? graph.U(arc) : graph.V(arc);
67  }
68 
69  public bool IsEdge(Arc arc)
70  {
71  return getDirection(arc) == Direction.Edge;
72  }
73 
74  public IEnumerable<Node> Nodes()
75  {
76  return graph.Nodes();
77  }
78 
79  public IEnumerable<Arc> Arcs(ArcFilter filter = ArcFilter.All)
80  {
81  return filter == ArcFilter.All ? graph.Arcs() :
82  graph.Arcs().Where(x => getDirection(x) == Direction.Edge);
83  }
84 
85  private IEnumerable<Arc> FilterArcs(Node u, IEnumerable<Arc> arcs, ArcFilter filter)
86  {
87  switch (filter)
88  {
89  case ArcFilter.All: return arcs;
90  case ArcFilter.Edge: return arcs.Where(x => getDirection(x) == Direction.Edge);
91  case ArcFilter.Forward: return arcs.Where(x =>
92  {
93  var dir = getDirection(x);
94  switch (dir)
95  {
96  case Direction.Forward: return U(x) == u;
97  case Direction.Backward: return V(x) == u;
98  default: return true;
99  }
100  });
101  default: return arcs.Where(x =>
102  {
103  var dir = getDirection(x);
104  switch (dir)
105  {
106  case Direction.Forward: return V(x) == u;
107  case Direction.Backward: return U(x) == u;
108  default: return true;
109  }
110  });
111  }
112  }
113 
114  public IEnumerable<Arc> Arcs(Node u, ArcFilter filter = ArcFilter.All)
115  {
116  return FilterArcs(u, graph.Arcs(u), filter);
117  }
118 
119  public IEnumerable<Arc> Arcs(Node u, Node v, ArcFilter filter = ArcFilter.All)
120  {
121  return FilterArcs(u, graph.Arcs(u, v), filter);
122  }
123 
124  public int NodeCount()
125  {
126  return graph.NodeCount();
127  }
128 
129  public int ArcCount(ArcFilter filter = ArcFilter.All)
130  {
131  return filter == ArcFilter.All ? graph.ArcCount() : Arcs(filter).Count();
132  }
133 
134  public int ArcCount(Node u, ArcFilter filter = ArcFilter.All)
135  {
136  return Arcs(u, filter).Count();
137  }
138 
139  public int ArcCount(Node u, Node v, ArcFilter filter = ArcFilter.All)
140  {
141  return Arcs(u, v, filter).Count();
142  }
143 
144  public bool HasNode(Node node)
145  {
146  return graph.HasNode(node);
147  }
148 
149  public bool HasArc(Arc arc)
150  {
151  return graph.HasArc(arc);
152  }
153  }
154 }