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 : 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, List<IVertex>> contentMap;
|
---|
41 | public DirectedGraph() {
|
---|
42 | nodes = new List<IVertex>();
|
---|
43 | contentMap = new Dictionary<object, List<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, List<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 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) {
|
---|
72 | return nodes.Any(predicate);
|
---|
73 | }
|
---|
74 | public virtual bool IsEmpty {
|
---|
75 | get { return !nodes.Any(); }
|
---|
76 | }
|
---|
77 | public virtual void Clear() {
|
---|
78 | nodes.Clear();
|
---|
79 | contentMap.Clear();
|
---|
80 | }
|
---|
81 | public virtual void AddVertex(IVertex vertex) {
|
---|
82 | Nodes.Add(vertex);
|
---|
83 | if (!contentMap.ContainsKey(vertex.Content))
|
---|
84 | contentMap[vertex.Content] = new List<IVertex>();
|
---|
85 | contentMap[vertex.Content].Add(vertex);
|
---|
86 | }
|
---|
87 |
|
---|
88 | public virtual void RemoveVertex(IVertex vertex) {
|
---|
89 | nodes.Remove(vertex);
|
---|
90 | }
|
---|
91 | public override Image ItemImage {
|
---|
92 | get { return Common.Resources.VSImageLibrary.Graph; }
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|