Changeset 10300 for branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/DirectedGraph.cs
- Timestamp:
- 01/07/14 21:44:36 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/DirectedGraph.cs
r10278 r10300 31 31 [Item("DirectedGraph", "Generic class representing a directed graph with custom vertices and content")] 32 32 [StorableClass] 33 public class DirectedGraph <TVertex> : Item, IDirectedGraph<TVertex> where TVertex : class,IVertex{33 public class DirectedGraph : Item, IDirectedGraph { 34 34 [Storable] 35 protected readonly List< TVertex> nodes; // for performance reasons, maybe this should be a linked list (fast remove)36 public List< TVertex> Nodes {35 protected readonly List<IVertex> nodes; // for performance reasons, maybe this should be a linked list (fast remove) 36 public List<IVertex> Nodes { 37 37 get { return nodes; } 38 38 } 39 [Storable] 40 private readonly Dictionary<object, List<IVertex>> contentMap; 39 41 public DirectedGraph() { 40 nodes = new List<TVertex>(); 42 nodes = new List<IVertex>(); 43 contentMap = new Dictionary<object, List<IVertex>>(); 41 44 } 42 45 [StorableConstructor] … … 44 47 : base(serializing) { 45 48 } 46 protected DirectedGraph(DirectedGraph <TVertex>original, Cloner cloner)49 protected DirectedGraph(DirectedGraph original, Cloner cloner) 47 50 : base(original, cloner) { 48 nodes = new List<TVertex>(original.Nodes); 51 nodes = new List<IVertex>(original.Nodes); 52 contentMap = new Dictionary<object, List<IVertex>>(original.contentMap); 49 53 } 50 54 public override IDeepCloneable Clone(Cloner cloner) { 51 return new DirectedGraph <TVertex>(this, cloner);55 return new DirectedGraph(this, cloner); 52 56 } 53 public bool Contains( TVertex t) {57 public bool Contains(IVertex t) { 54 58 return nodes.Contains(t); 55 59 } 56 60 57 public virtual bool Any(Func<TVertex, bool> predicate) { 61 public bool Contains(object content) { 62 return contentMap.ContainsKey(content); 63 } 64 public List<IVertex> this[object key] { 65 get { 66 List<IVertex> result; 67 contentMap.TryGetValue(key, out result); 68 return result; 69 } 70 } 71 public virtual bool Any(Func<IVertex, bool> predicate) { 58 72 return nodes.Any(predicate); 59 73 } … … 61 75 get { return !nodes.Any(); } 62 76 } 63 64 77 public virtual void Clear() { 65 78 nodes.Clear(); 66 79 } 67 68 public virtual void AddVertex(TVertex vertex) { 80 public virtual void AddVertex(IVertex vertex) { 69 81 Nodes.Add(vertex); 82 if (!contentMap.ContainsKey(vertex.Content)) 83 contentMap[vertex.Content] = new List<IVertex>(); 84 contentMap[vertex.Content].Add(vertex); 70 85 } 71 86 72 public virtual void RemoveVertex( TVertex vertex) {87 public virtual void RemoveVertex(IVertex vertex) { 73 88 nodes.Remove(vertex); 74 89 } … … 77 92 } 78 93 } 79 80 [Item("Directed graph", "Generic directed graph base class.")]81 [StorableClass]82 public class DirectedGraph<TVertex, TContent> : DirectedGraph<TVertex>, IDirectedGraph<TVertex, TContent>83 where TVertex : class,IVertex<TContent>84 where TContent : class,IItem {85 86 [StorableConstructor]87 protected DirectedGraph(bool deserializing) : base(deserializing) { }88 89 [Storable]90 private readonly Dictionary<TContent, List<TVertex>> contentMap; // reverse mapping of content to the actual vertices91 public DirectedGraph() {92 contentMap = new Dictionary<TContent, List<TVertex>>();93 }94 public override IDeepCloneable Clone(Cloner cloner) {95 return new DirectedGraph<TVertex, TContent>(this, cloner);96 }97 protected DirectedGraph(DirectedGraph<TVertex, TContent> original, Cloner cloner)98 : base(original, cloner) {99 contentMap = new Dictionary<TContent, List<TVertex>>(original.contentMap);100 }101 public bool Contains(TContent content) {102 return contentMap.ContainsKey(content);103 }104 public List<TVertex> this[TContent key] {105 get {106 List<TVertex> result = null;107 contentMap.TryGetValue(key, out result);108 return result;109 }110 // set {111 // contentMap[key] = value;112 // }113 }114 public override void Clear() {115 contentMap.Clear();116 base.Clear();117 }118 public override void AddVertex(TVertex vertex) {119 if (contentMap.ContainsKey(vertex.Content)) {120 contentMap[vertex.Content].Add(vertex);121 } else {122 contentMap[vertex.Content] = new List<TVertex> { vertex };123 }124 base.AddVertex(vertex);125 }126 127 public override void RemoveVertex(TVertex vertex) {128 if (contentMap.ContainsKey(vertex.Content)) {129 contentMap[vertex.Content].Remove(vertex);130 }131 base.RemoveVertex(vertex);132 }133 }134 94 }
Note: See TracChangeset
for help on using the changeset viewer.