[10278] | 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 |
|
---|
[10267] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[10300] | 24 | using System.Linq;
|
---|
[10267] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
[10271] | 29 | namespace HeuristicLab.EvolutionTracking {
|
---|
[10267] | 30 | [StorableClass]
|
---|
[10300] | 31 | [Item("GenealogyGraph", "")]
|
---|
| 32 | public class GenealogyGraph : DirectedGraph, IGenealogyGraph {
|
---|
[10267] | 33 | [Storable]
|
---|
[10830] | 34 | private Dictionary<double, List<IGenealogyGraphNode>> ranks; // use a linked list for fast insertion/removal
|
---|
| 35 | public Dictionary<double, List<IGenealogyGraphNode>> Ranks {
|
---|
[10267] | 36 | get { return ranks; }
|
---|
| 37 | set { ranks = value; }
|
---|
| 38 | }
|
---|
[10677] | 39 | public new IEnumerable<IGenealogyGraphNode> Nodes {
|
---|
| 40 | get { return from n in base.Nodes select (IGenealogyGraphNode)n; }
|
---|
| 41 | }
|
---|
[10300] | 42 | protected GenealogyGraph(GenealogyGraph original, Cloner cloner)
|
---|
| 43 | : base(original, cloner) {
|
---|
| 44 | }
|
---|
[10267] | 45 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[10300] | 46 | return new GenealogyGraph(this, cloner);
|
---|
[10267] | 47 | }
|
---|
| 48 | [StorableConstructor]
|
---|
| 49 | protected GenealogyGraph(bool deserializing) : base(deserializing) { }
|
---|
| 50 | public GenealogyGraph() {
|
---|
[10830] | 51 | Ranks = new Dictionary<double, List<IGenealogyGraphNode>>();
|
---|
[10267] | 52 | }
|
---|
[10300] | 53 | public override void AddVertex(IVertex vertex) {
|
---|
[10302] | 54 | var node = (IGenealogyGraphNode)vertex;
|
---|
| 55 | if (!Ranks.ContainsKey(node.Rank))
|
---|
[10830] | 56 | Ranks[node.Rank] = new List<IGenealogyGraphNode>();
|
---|
| 57 | Ranks[node.Rank].Add(node);
|
---|
[10278] | 58 | base.AddVertex(vertex);
|
---|
| 59 | }
|
---|
[10300] | 60 | public override void RemoveVertex(IVertex vertex) {
|
---|
[10302] | 61 | var node = (IGenealogyGraphNode)vertex;
|
---|
| 62 | if (Ranks.ContainsKey(node.Rank)) {
|
---|
| 63 | Ranks[node.Rank].Remove(node);
|
---|
[10278] | 64 | }
|
---|
| 65 | base.RemoveVertex(vertex);
|
---|
| 66 | }
|
---|
| 67 | public event EventHandler GraphUpdated;
|
---|
| 68 | private void OnGraphUpdated(object sender, EventArgs args) {
|
---|
| 69 | var updated = GraphUpdated;
|
---|
| 70 | if (updated != null) updated(sender, args);
|
---|
| 71 | }
|
---|
[10267] | 72 |
|
---|
[10755] | 73 | public new IGenealogyGraphNode this[object content] {
|
---|
[10300] | 74 | get {
|
---|
| 75 | var result = base[content];
|
---|
[10755] | 76 | return result == null ? null : (IGenealogyGraphNode)result;
|
---|
[10278] | 77 | }
|
---|
[10833] | 78 | set { base[content] = value; }
|
---|
[10278] | 79 | }
|
---|
[10677] | 80 |
|
---|
| 81 | public override void Clear() {
|
---|
| 82 | base.Clear();
|
---|
| 83 | ranks.Clear();
|
---|
| 84 | }
|
---|
[10267] | 85 | }
|
---|
[10300] | 86 |
|
---|
[10347] | 87 | [StorableClass]
|
---|
| 88 | [Item("GenealogyGraph", "")]
|
---|
| 89 | public class GenealogyGraph<T> : DirectedGraph, IGenealogyGraph<T> where T : class, IItem {
|
---|
[10677] | 90 | // members and properties
|
---|
[10347] | 91 | [Storable]
|
---|
[10830] | 92 | private Dictionary<double, List<IGenealogyGraphNode>> ranks;
|
---|
| 93 | public Dictionary<double, List<IGenealogyGraphNode>> Ranks {
|
---|
[10347] | 94 | get { return ranks; }
|
---|
| 95 | set { ranks = value; }
|
---|
| 96 | }
|
---|
[10677] | 97 | public new IEnumerable<IGenealogyGraphNode<T>> Nodes {
|
---|
| 98 | get { return from n in base.Nodes select (IGenealogyGraphNode<T>)n; }
|
---|
| 99 | }
|
---|
| 100 | // contructors
|
---|
[10347] | 101 | protected GenealogyGraph(GenealogyGraph<T> original, Cloner cloner)
|
---|
| 102 | : base(original, cloner) {
|
---|
| 103 | }
|
---|
| 104 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 105 | return new GenealogyGraph<T>(this, cloner);
|
---|
| 106 | }
|
---|
| 107 | [StorableConstructor]
|
---|
| 108 | protected GenealogyGraph(bool deserializing) : base(deserializing) { }
|
---|
| 109 | public GenealogyGraph() {
|
---|
[10830] | 110 | Ranks = new Dictionary<double, List<IGenealogyGraphNode>>();
|
---|
[10347] | 111 | }
|
---|
[10677] | 112 | // methods
|
---|
[10347] | 113 | public override void AddVertex(IVertex vertex) {
|
---|
| 114 | var node = (IGenealogyGraphNode<T>)vertex;
|
---|
[10755] | 115 | if (!Ranks.ContainsKey(node.Rank)) {
|
---|
[10830] | 116 | Ranks[node.Rank] = new List<IGenealogyGraphNode>();
|
---|
[10755] | 117 | }
|
---|
[10830] | 118 | Ranks[node.Rank].Add(node);
|
---|
[10347] | 119 | base.AddVertex(vertex);
|
---|
| 120 | }
|
---|
| 121 | public override void RemoveVertex(IVertex vertex) {
|
---|
| 122 | var node = (IGenealogyGraphNode<T>)vertex;
|
---|
| 123 | if (Ranks.ContainsKey(node.Rank)) {
|
---|
| 124 | Ranks[node.Rank].Remove(node);
|
---|
| 125 | }
|
---|
| 126 | base.RemoveVertex(vertex);
|
---|
| 127 | }
|
---|
[10677] | 128 |
|
---|
| 129 | IEnumerable<IGenealogyGraphNode> IGenealogyGraph.Nodes {
|
---|
| 130 | get { return Nodes; }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[10347] | 133 | public event EventHandler GraphUpdated;
|
---|
| 134 | private void OnGraphUpdated(object sender, EventArgs args) {
|
---|
| 135 | var updated = GraphUpdated;
|
---|
| 136 | if (updated != null) updated(sender, args);
|
---|
| 137 | }
|
---|
[10755] | 138 | public new IGenealogyGraphNode<T> this[object content] {
|
---|
[10347] | 139 | get {
|
---|
| 140 | var result = base[content];
|
---|
[10755] | 141 | return result == null ? null : (IGenealogyGraphNode<T>)result;
|
---|
[10347] | 142 | }
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
[10267] | 145 | }
|
---|