[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]
|
---|
[10300] | 34 | private Dictionary<double, LinkedList<IGenealogyGraphNode>> ranks; // use a linked list for fast insertion/removal
|
---|
| 35 | public Dictionary<double, LinkedList<IGenealogyGraphNode>> Ranks {
|
---|
[10267] | 36 | get { return ranks; }
|
---|
| 37 | set { ranks = value; }
|
---|
| 38 | }
|
---|
[10300] | 39 | protected GenealogyGraph(GenealogyGraph original, Cloner cloner)
|
---|
| 40 | : base(original, cloner) {
|
---|
| 41 | }
|
---|
[10267] | 42 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[10300] | 43 | return new GenealogyGraph(this, cloner);
|
---|
[10267] | 44 | }
|
---|
| 45 | [StorableConstructor]
|
---|
| 46 | protected GenealogyGraph(bool deserializing) : base(deserializing) { }
|
---|
| 47 | public GenealogyGraph() {
|
---|
[10300] | 48 | Ranks = new Dictionary<double, LinkedList<IGenealogyGraphNode>>();
|
---|
[10267] | 49 | }
|
---|
[10300] | 50 | public override void AddVertex(IVertex vertex) {
|
---|
[10302] | 51 | var node = (IGenealogyGraphNode)vertex;
|
---|
| 52 | if (!Ranks.ContainsKey(node.Rank))
|
---|
| 53 | Ranks[node.Rank] = new LinkedList<IGenealogyGraphNode>();
|
---|
| 54 | Ranks[node.Rank].AddLast(node);
|
---|
[10278] | 55 | base.AddVertex(vertex);
|
---|
| 56 | }
|
---|
[10300] | 57 | public override void RemoveVertex(IVertex vertex) {
|
---|
[10302] | 58 | var node = (IGenealogyGraphNode)vertex;
|
---|
| 59 | if (Ranks.ContainsKey(node.Rank)) {
|
---|
| 60 | Ranks[node.Rank].Remove(node);
|
---|
[10278] | 61 | }
|
---|
| 62 | base.RemoveVertex(vertex);
|
---|
| 63 | }
|
---|
| 64 | public event EventHandler GraphUpdated;
|
---|
| 65 | private void OnGraphUpdated(object sender, EventArgs args) {
|
---|
| 66 | var updated = GraphUpdated;
|
---|
| 67 | if (updated != null) updated(sender, args);
|
---|
| 68 | }
|
---|
[10267] | 69 |
|
---|
[10300] | 70 | public new List<IGenealogyGraphNode> this[object content] {
|
---|
| 71 | get {
|
---|
| 72 | var result = base[content];
|
---|
| 73 | return result != null ? result.Cast<IGenealogyGraphNode>().ToList() : null;
|
---|
[10278] | 74 | }
|
---|
| 75 | }
|
---|
[10267] | 76 | }
|
---|
[10300] | 77 |
|
---|
[10347] | 78 | [StorableClass]
|
---|
| 79 | [Item("GenealogyGraph", "")]
|
---|
| 80 | public class GenealogyGraph<T> : DirectedGraph, IGenealogyGraph<T> where T : class, IItem {
|
---|
| 81 | [Storable]
|
---|
[10650] | 82 | private Dictionary<double, LinkedList<IGenealogyGraphNode>> ranks;
|
---|
| 83 | public Dictionary<double, LinkedList<IGenealogyGraphNode>> Ranks {
|
---|
[10347] | 84 | get { return ranks; }
|
---|
| 85 | set { ranks = value; }
|
---|
| 86 | }
|
---|
| 87 | protected GenealogyGraph(GenealogyGraph<T> original, Cloner cloner)
|
---|
| 88 | : base(original, cloner) {
|
---|
| 89 | }
|
---|
| 90 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 91 | return new GenealogyGraph<T>(this, cloner);
|
---|
| 92 | }
|
---|
| 93 | [StorableConstructor]
|
---|
| 94 | protected GenealogyGraph(bool deserializing) : base(deserializing) { }
|
---|
| 95 | public GenealogyGraph() {
|
---|
[10650] | 96 | Ranks = new Dictionary<double, LinkedList<IGenealogyGraphNode>>();
|
---|
[10347] | 97 | }
|
---|
| 98 | public override void AddVertex(IVertex vertex) {
|
---|
| 99 | var node = (IGenealogyGraphNode<T>)vertex;
|
---|
| 100 | if (!Ranks.ContainsKey(node.Rank))
|
---|
[10650] | 101 | Ranks[node.Rank] = new LinkedList<IGenealogyGraphNode>();
|
---|
[10347] | 102 | Ranks[node.Rank].AddLast(node);
|
---|
| 103 | base.AddVertex(vertex);
|
---|
| 104 | }
|
---|
| 105 | public override void RemoveVertex(IVertex vertex) {
|
---|
| 106 | var node = (IGenealogyGraphNode<T>)vertex;
|
---|
| 107 | if (Ranks.ContainsKey(node.Rank)) {
|
---|
| 108 | Ranks[node.Rank].Remove(node);
|
---|
| 109 | }
|
---|
| 110 | base.RemoveVertex(vertex);
|
---|
| 111 | }
|
---|
| 112 | public event EventHandler GraphUpdated;
|
---|
| 113 | private void OnGraphUpdated(object sender, EventArgs args) {
|
---|
| 114 | var updated = GraphUpdated;
|
---|
| 115 | if (updated != null) updated(sender, args);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | public new List<IGenealogyGraphNode<T>> this[object content] {
|
---|
| 119 | get {
|
---|
| 120 | var result = base[content];
|
---|
| 121 | return result != null ? result.Cast<IGenealogyGraphNode<T>>().ToList() : null;
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
[10267] | 125 | }
|
---|