Changeset 11752 for branches/HeuristicLab.EvolutionTracking
- Timestamp:
- 01/13/15 17:17:42 (10 years ago)
- Location:
- branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraph.cs
r11694 r11752 37 37 private Dictionary<string, IGenealogyGraphNode> idMap; 38 38 39 private Dictionary<double, List<IGenealogyGraphNode>> ranks; 40 public Dictionary<double, List<IGenealogyGraphNode>> Ranks { 41 get { return ranks; } 42 set { ranks = value; } 39 private readonly Comparison<IArc> compareArcs = (a, b) => { 40 if (a.Data == b.Data) 41 return 0; 42 if (a.Data == null) 43 return -1; 44 return 1; 45 }; 46 47 protected Dictionary<double, List<IGenealogyGraphNode>> ranks; 48 public IEnumerable<KeyValuePair<double, IEnumerable<IGenealogyGraphNode>>> Ranks { 49 get { return ranks.Select(x => new KeyValuePair<double, IEnumerable<IGenealogyGraphNode>>(x.Key, x.Value)); } 43 50 } 44 51 public IEnumerable<IGenealogyGraphNode> GetByRank(double rank) { 52 return ranks.ContainsKey(rank) ? ranks[rank] : Enumerable.Empty<IGenealogyGraphNode>(); 53 } 45 54 protected GenealogyGraph(GenealogyGraph original, Cloner cloner) 46 55 : base(original, cloner) { 47 56 RebuildDictionaries(); 48 57 49 foreach (var arcs in Vertices.Select(v => (List<IArc>)((Vertex)v).InArcs)) { 50 arcs.Sort((a, b) => { 51 if (a.Data == b.Data) 52 return 0; 53 if (a.Data == null) 54 return -1; 55 return 1; 56 }); 57 } 58 foreach (var arcs in Vertices.Select(v => (List<IArc>)((IVertex)v).InArcs)) { arcs.Sort(compareArcs); } 58 59 } 59 60 60 public override IDeepCloneable Clone(Cloner cloner) { 61 61 return new GenealogyGraph(this, cloner); … … 66 66 : base(deserializing) { 67 67 } 68 69 68 [StorableHook(HookType.AfterDeserialization)] 70 69 private void AfterDeserialization() { 71 70 RebuildDictionaries(); 72 73 foreach (var arcs in Vertices.Select(v => (List<IArc>)((Vertex)v).InArcs)) { 74 arcs.Sort((a, b) => { 75 if (a.Data == b.Data) 76 return 0; 77 if (a.Data == null) 78 return -1; 79 return 1; 80 }); 81 } 71 foreach (var arcs in Vertices.Select(v => (List<IArc>)((Vertex)v).InArcs)) { arcs.Sort(compareArcs); } 82 72 } 83 84 73 public GenealogyGraph() { 85 Ranks = new Dictionary<double, List<IGenealogyGraphNode>>();74 ranks = new Dictionary<double, List<IGenealogyGraphNode>>(); 86 75 contentMap = new Dictionary<object, IGenealogyGraphNode>(); 87 76 idMap = new Dictionary<string, IGenealogyGraphNode>(); 88 77 } 89 90 78 new public IEnumerable<IGenealogyGraphNode> Vertices { 91 79 get { return base.Vertices.Select(x => (IGenealogyGraphNode)x); } 92 80 } 93 94 81 new public IEnumerable<IGenealogyGraphArc> Arcs { 95 82 get { return base.Arcs.Select(x => (IGenealogyGraphArc)x); } 96 83 } 97 98 84 public override IArc AddArc(IVertex source, IVertex target) { 99 85 var arc = new GenealogyGraphArc((IGenealogyGraphNode)source, (IGenealogyGraphNode)target); … … 101 87 return arc; 102 88 } 103 104 89 public override void AddVertex(IVertex vertex) { 105 90 base.AddVertex(vertex); … … 115 100 ranks[genealogyGraphNode.Rank].Add(genealogyGraphNode); 116 101 } 117 118 102 public override void RemoveVertex(IVertex vertex) { 119 103 var node = (IGenealogyGraphNode)vertex; 120 104 contentMap.Remove(node.Data); 121 105 idMap.Remove(node.Id); 122 if ( Ranks.ContainsKey(node.Rank)) {123 Ranks[node.Rank].Remove(node);124 if (! Ranks[node.Rank].Any())125 Ranks.Remove(node.Rank);106 if (ranks.ContainsKey(node.Rank)) { 107 ranks[node.Rank].Remove(node); // this call will be slow, use with caution 108 if (!ranks[node.Rank].Any()) 109 ranks.Remove(node.Rank); 126 110 } 127 111 base.RemoveVertex(vertex); 128 112 } 129 130 113 public IGenealogyGraphNode GetByContent(object content) { 131 114 IGenealogyGraphNode result; … … 133 116 return result; 134 117 } 135 136 118 public IGenealogyGraphNode GetById(string id) { 137 119 IGenealogyGraphNode result; … … 139 121 return result; 140 122 } 141 142 123 public bool Contains(object content) { 143 124 return contentMap.ContainsKey(content); 144 125 } 145 146 126 public event EventHandler GraphUpdated; 147 127 private void OnGraphUpdated(object sender, EventArgs args) { … … 149 129 if (updated != null) updated(sender, args); 150 130 } 151 152 131 public override void Clear() { 153 132 base.Clear(); … … 156 135 idMap.Clear(); 157 136 } 158 159 137 private void RebuildDictionaries() { 160 138 contentMap = new Dictionary<object, IGenealogyGraphNode>(); … … 172 150 } 173 151 } 174 175 152 public void ExportDot(string path) { 176 153 var sb = new StringBuilder(); … … 186 163 File.WriteAllText(path, sb.ToString()); 187 164 } 188 189 165 private Color GetColor(IGenealogyGraphNode node) { 190 166 var colorIndex = (int)Math.Round(node.Quality * ColorGradient.Colors.Count); … … 213 189 return (IGenealogyGraphNode<T>)base.GetById(id); 214 190 } 191 new public IEnumerable<IGenealogyGraphNode<T>> GetByRank(double rank) { 192 return base.GetByRank(rank).Select(x => (IGenealogyGraphNode<T>)x); 193 } 194 public new IEnumerable<KeyValuePair<double, IEnumerable<IGenealogyGraphNode<T>>>> Ranks { 195 get { return base.Ranks.Select(x => new KeyValuePair<double, IEnumerable<IGenealogyGraphNode<T>>>(x.Key, x.Value.Select(n => (IGenealogyGraphNode<T>)n))); } 196 } 215 197 } 216 198 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/Interfaces/IGenealogyGraph.cs
r11390 r11752 25 25 namespace HeuristicLab.EvolutionTracking { 26 26 public interface IGenealogyGraph : IDirectedGraph { 27 Dictionary<double, List<IGenealogyGraphNode>> Ranks { get; } 27 IEnumerable<IGenealogyGraphNode> GetByRank(double rank); 28 IEnumerable<KeyValuePair<double, IEnumerable<IGenealogyGraphNode>>> Ranks { get; } 28 29 IGenealogyGraphNode GetByContent(object content); 29 30 IGenealogyGraphNode GetById(string id);
Note: See TracChangeset
for help on using the changeset viewer.