Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1772: Fixed another minor display bug concerning elite individuals. Fixed bug when saving fragments from mutation. Displayed quality as well in the SymbolicExpressionTreeTile.

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