#region License Information /* HeuristicLab * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.EvolutionTracking { [StorableClass] [Item("GenealogyGraph", "")] public class GenealogyGraph : DirectedGraph, IGenealogyGraph { [Storable] private Dictionary> ranks; // use a linked list for fast insertion/removal public Dictionary> Ranks { get { return ranks; } set { ranks = value; } } public new IEnumerable Nodes { get { return from n in base.Nodes select (IGenealogyGraphNode)n; } } protected GenealogyGraph(GenealogyGraph original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new GenealogyGraph(this, cloner); } [StorableConstructor] protected GenealogyGraph(bool deserializing) : base(deserializing) { } public GenealogyGraph() { Ranks = new Dictionary>(); } public override void AddVertex(IVertex vertex) { var node = (IGenealogyGraphNode)vertex; if (!Ranks.ContainsKey(node.Rank)) Ranks[node.Rank] = new List(); Ranks[node.Rank].Add(node); base.AddVertex(vertex); } public override void RemoveVertex(IVertex vertex) { var node = (IGenealogyGraphNode)vertex; if (Ranks.ContainsKey(node.Rank)) { Ranks[node.Rank].Remove(node); } base.RemoveVertex(vertex); } public event EventHandler GraphUpdated; private void OnGraphUpdated(object sender, EventArgs args) { var updated = GraphUpdated; if (updated != null) updated(sender, args); } public new IGenealogyGraphNode this[object content] { get { var result = base[content]; return result == null ? null : (IGenealogyGraphNode)result; } set { base[content] = value; } } public override void Clear() { base.Clear(); ranks.Clear(); } } [StorableClass] [Item("GenealogyGraph", "")] public class GenealogyGraph : DirectedGraph, IGenealogyGraph where T : class, IItem { // members and properties [Storable] private Dictionary> ranks; public Dictionary> Ranks { get { return ranks; } set { ranks = value; } } public new IEnumerable> Nodes { get { return from n in base.Nodes select (IGenealogyGraphNode)n; } } // contructors protected GenealogyGraph(GenealogyGraph original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new GenealogyGraph(this, cloner); } [StorableConstructor] protected GenealogyGraph(bool deserializing) : base(deserializing) { } public GenealogyGraph() { Ranks = new Dictionary>(); } // methods public override void AddVertex(IVertex vertex) { var node = (IGenealogyGraphNode)vertex; if (!Ranks.ContainsKey(node.Rank)) { Ranks[node.Rank] = new List(); } Ranks[node.Rank].Add(node); base.AddVertex(vertex); } public override void RemoveVertex(IVertex vertex) { var node = (IGenealogyGraphNode)vertex; if (Ranks.ContainsKey(node.Rank)) { Ranks[node.Rank].Remove(node); } base.RemoveVertex(vertex); } IEnumerable IGenealogyGraph.Nodes { get { return Nodes; } } public event EventHandler GraphUpdated; private void OnGraphUpdated(object sender, EventArgs args) { var updated = GraphUpdated; if (updated != null) updated(sender, args); } public new IGenealogyGraphNode this[object content] { get { var result = base[content]; return result == null ? null : (IGenealogyGraphNode)result; } } } }