1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.EvolutionTracking {
|
---|
31 | [Item("DirectedGraph", "Generic class representing a directed graph with custom vertices and content")]
|
---|
32 | [StorableClass]
|
---|
33 | public class DirectedGraph<TVertex> : Item, IDirectedGraph<TVertex> where TVertex : class,IVertex {
|
---|
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 {
|
---|
37 | get { return nodes; }
|
---|
38 | }
|
---|
39 | public DirectedGraph() {
|
---|
40 | nodes = new List<TVertex>();
|
---|
41 | }
|
---|
42 | [StorableConstructor]
|
---|
43 | protected DirectedGraph(bool serializing)
|
---|
44 | : base(serializing) {
|
---|
45 | }
|
---|
46 | protected DirectedGraph(DirectedGraph<TVertex> original, Cloner cloner)
|
---|
47 | : base(original, cloner) {
|
---|
48 | nodes = new List<TVertex>(original.Nodes);
|
---|
49 | }
|
---|
50 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
51 | return new DirectedGraph<TVertex>(this, cloner);
|
---|
52 | }
|
---|
53 | public bool Contains(TVertex t) {
|
---|
54 | return nodes.Contains(t);
|
---|
55 | }
|
---|
56 |
|
---|
57 | public virtual bool Any(Func<TVertex, bool> predicate) {
|
---|
58 | return nodes.Any(predicate);
|
---|
59 | }
|
---|
60 | public virtual bool IsEmpty {
|
---|
61 | get { return !nodes.Any(); }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public virtual void Clear() {
|
---|
65 | nodes.Clear();
|
---|
66 | }
|
---|
67 |
|
---|
68 | public virtual void AddVertex(TVertex vertex) {
|
---|
69 | Nodes.Add(vertex);
|
---|
70 | }
|
---|
71 |
|
---|
72 | public virtual void RemoveVertex(TVertex vertex) {
|
---|
73 | nodes.Remove(vertex);
|
---|
74 | }
|
---|
75 | public override Image ItemImage {
|
---|
76 | get { return Common.Resources.VSImageLibrary.Graph; }
|
---|
77 | }
|
---|
78 | }
|
---|
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 vertices
|
---|
91 | 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 | }
|
---|