Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/DirectedGraph.cs @ 10888

Last change on this file since 10888 was 10888, checked in by bburlacu, 10 years ago

#1772: Introduced separate class for FragmentNodes and adjusted tracing code. Fixed small bug creating the genealogy graph.

File size: 4.4 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.EvolutionTracking {
31  [Item("DirectedGraph", "Generic class representing a directed graph with custom vertices and content")]
32  [StorableClass]
33  public class DirectedGraph : Item, IDirectedGraph {
34    public bool AllowDuplicateContent { get; set; }
35
36    [Storable]
37    private readonly List<IVertex> nodes; // for performance reasons, maybe this should be a linked list (fast remove)
38    public IEnumerable<IVertex> Nodes {
39      get { return nodes; }
40    }
41
42    [Storable]
43    private readonly Dictionary<string, IVertex> idMap;
44
45    [Storable]
46    private readonly Dictionary<object, IVertex> contentMap;
47    public DirectedGraph() {
48      nodes = new List<IVertex>();
49      contentMap = new Dictionary<object, IVertex>();
50      idMap = new Dictionary<string, IVertex>();
51    }
52    [StorableConstructor]
53    protected DirectedGraph(bool serializing)
54      : base(serializing) {
55    }
56    protected DirectedGraph(DirectedGraph original, Cloner cloner)
57      : base(original, cloner) {
58      nodes = new List<IVertex>(original.Nodes);
59      contentMap = new Dictionary<object, IVertex>(original.contentMap);
60      idMap = new Dictionary<string, IVertex>(original.idMap);
61    }
62    public override IDeepCloneable Clone(Cloner cloner) {
63      return new DirectedGraph(this, cloner);
64    }
65    public bool Contains(IVertex t) {
66      return nodes.Contains(t);
67    }
68
69    public bool Contains(object content) {
70      return contentMap.ContainsKey(content);
71    }
72
73    public IVertex GetVertex(string id) {
74      IVertex result;
75      idMap.TryGetValue(id, out result);
76      return result;
77    }
78
79    public IVertex this[object key] {
80      get {
81        IVertex result;
82        contentMap.TryGetValue(key, out result);
83        return result;
84      }
85      set {
86        if (contentMap.ContainsKey(key)) {
87          idMap.Remove(contentMap[key].Id);
88        }
89        contentMap[key] = value;
90        idMap[value.Id] = value;
91      }
92    }
93    public virtual bool Any(Func<IVertex, bool> predicate) {
94      return nodes.Any(predicate);
95    }
96    public virtual bool IsEmpty {
97      get { return !nodes.Any(); }
98    }
99    public virtual void Clear() {
100      nodes.Clear();
101      contentMap.Clear();
102      idMap.Clear();
103    }
104    public virtual void AddVertex(IVertex vertex) {
105      if (!AllowDuplicateContent && contentMap.ContainsKey(vertex.Content)) {
106        throw new Exception("Content already exists in the graph.");
107      }
108      contentMap[vertex.Content] = vertex;
109      idMap[vertex.Id] = vertex;
110      nodes.Add(vertex);
111    }
112
113    public virtual IArc AddArc(IVertex source, IVertex target) {
114      var arc = new Arc(source, target);
115      source.AddForwardArc(arc);
116      target.AddReverseArc(arc);
117      return arc;
118    }
119
120    public virtual void RemoveVertex(IVertex vertex) {
121      nodes.Remove(vertex);
122    }
123
124    public override Image ItemImage {
125      get { return Common.Resources.VSImageLibrary.Graph; }
126    }
127
128    public void SetContent(IVertex vertex, object content) {
129      var oldContent = vertex.Content;
130      contentMap.Remove(oldContent);
131      vertex.Content = content;
132      contentMap[content] = vertex;
133    }
134
135    public void SetId(IVertex vertex, string id) {
136      var oldId = vertex.Id;
137      idMap.Remove(oldId);
138      vertex.Id = id;
139      idMap[id] = vertex;
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.