Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1772: Added license headers where they were missing. Introduced an id map to the DirectedGraph to get graph vertices based on the id injected in the scopes by the genealogy analyzer.

File size: 3.8 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    [Storable]
35    private readonly List<IVertex> nodes; // for performance reasons, maybe this should be a linked list (fast remove)
36    public IEnumerable<IVertex> Nodes {
37      get { return nodes; }
38    }
39
40    [Storable]
41    private readonly Dictionary<string, IVertex> idMap;
42
43    [Storable]
44    private readonly Dictionary<object, IVertex> contentMap;
45    public DirectedGraph() {
46      nodes = new List<IVertex>();
47      contentMap = new Dictionary<object, IVertex>();
48      idMap = new Dictionary<string, IVertex>();
49    }
50    [StorableConstructor]
51    protected DirectedGraph(bool serializing)
52      : base(serializing) {
53    }
54    protected DirectedGraph(DirectedGraph original, Cloner cloner)
55      : base(original, cloner) {
56      nodes = new List<IVertex>(original.Nodes);
57      contentMap = new Dictionary<object, IVertex>(original.contentMap);
58      idMap = new Dictionary<string, IVertex>(original.idMap);
59    }
60    public override IDeepCloneable Clone(Cloner cloner) {
61      return new DirectedGraph(this, cloner);
62    }
63    public bool Contains(IVertex t) {
64      return nodes.Contains(t);
65    }
66
67    public bool Contains(object content) {
68      return contentMap.ContainsKey(content);
69    }
70
71    public IVertex GetVertex(string id) {
72      IVertex result;
73      idMap.TryGetValue(id, out result);
74      return result;
75    }
76
77    public IVertex this[object key] {
78      get {
79        IVertex result;
80        contentMap.TryGetValue(key, out result);
81        return result;
82      }
83      set {
84        if (contentMap.ContainsKey(key)) {
85          idMap.Remove(contentMap[key].Id);
86        }
87        contentMap[key] = value;
88        idMap[value.Id] = value;
89      }
90    }
91    public virtual bool Any(Func<IVertex, bool> predicate) {
92      return nodes.Any(predicate);
93    }
94    public virtual bool IsEmpty {
95      get { return !nodes.Any(); }
96    }
97    public virtual void Clear() {
98      nodes.Clear();
99      contentMap.Clear();
100      idMap.Clear();
101    }
102    public virtual void AddVertex(IVertex vertex) {
103      if (contentMap.ContainsKey(vertex.Content)) {
104        throw new Exception("Content already exists in the graph.");
105      }
106      contentMap[vertex.Content] = vertex;
107      idMap[vertex.Id] = vertex;
108      nodes.Add(vertex);
109    }
110
111    public virtual void RemoveVertex(IVertex vertex) {
112      nodes.Remove(vertex);
113    }
114    public override Image ItemImage {
115      get { return Common.Resources.VSImageLibrary.Graph; }
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.