Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/Collections/DirectedGraph/DirectedGraph.cs @ 11250

Last change on this file since 11250 was 11250, checked in by mkommend, 10 years ago

#2223: Adapted events signatures in the directed graph.

File size: 5.8 KB
RevLine 
[10267]1#region License Information
2/* HeuristicLab
[10278]3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[10267]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
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using HeuristicLab.Common;
[11241]27using HeuristicLab.Common.Resources;
[10267]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
[11241]30namespace HeuristicLab.Core {
[10278]31  [Item("DirectedGraph", "Generic class representing a directed graph with custom vertices and content")]
[10267]32  [StorableClass]
[10300]33  public class DirectedGraph : Item, IDirectedGraph {
[11241]34    public override Image ItemImage { get { return VSImageLibrary.Graph; } }
35
36    private HashSet<IVertex> vertices;
[10267]37    [Storable]
[10903]38    public IEnumerable<IVertex> Vertices {
39      get { return vertices; }
[11229]40      private set { vertices = new HashSet<IVertex>(value); }
[10267]41    }
[10884]42
[11241]43    private HashSet<IArc> arcs;
[10300]44    [Storable]
[10903]45    public IEnumerable<IArc> Arcs {
46      get { return arcs; }
[11229]47      private set { arcs = new HashSet<IArc>(value); }
[10903]48    }
49
[11241]50    public DirectedGraph() {
51      vertices = new HashSet<IVertex>();
52      arcs = new HashSet<IArc>();
53    }
54
55    protected DirectedGraph(DirectedGraph original, Cloner cloner)
56      : base(original, cloner) {
57      vertices = new HashSet<IVertex>(original.vertices.Select(cloner.Clone));
58      arcs = new HashSet<IArc>(original.arcs.Select(cloner.Clone));
59    }
60
61    public override IDeepCloneable Clone(Cloner cloner) {
62      return new DirectedGraph(this, cloner);
63    }
64
[10897]65    [StorableConstructor]
66    protected DirectedGraph(bool serializing)
67      : base(serializing) {
68    }
69
[10903]70    [StorableHook(HookType.AfterDeserialization)]
71    private void AfterDeserialization() {
[11229]72      foreach (var vertex in vertices) {
[11249]73        vertex.ArcAdded += Vertex_ArcAdded;
74        vertex.ArcRemoved += Vertex_ArcRemoved;
[10903]75      }
76
[11229]77      foreach (var arc in arcs) {
78        var source = arc.Source;
79        var target = arc.Target;
80        source.AddArc(arc);
81        target.AddArc(arc);
[10903]82      }
83    }
84
[11229]85    public virtual void Clear() {
86      vertices.Clear();
87      arcs.Clear();
[10267]88    }
89
[11229]90    public virtual void AddVertex(IVertex vertex) {
91      vertices.Add(vertex);
92      // register event handlers
[11249]93      vertex.ArcAdded += Vertex_ArcAdded;
94      vertex.ArcRemoved += Vertex_ArcRemoved;
95      OnVertedAdded(this, new EventArgs<IVertex>(vertex));
[10300]96    }
[10884]97
[11229]98    public virtual void RemoveVertex(IVertex vertex) {
99      vertices.Remove(vertex);
100      // remove connections to/from the removed vertex
[11238]101      var arcList = vertex.InArcs.Concat(vertex.OutArcs).ToList(); // avoid invalid operation exception: "collection was modified" below
[11234]102      foreach (var arc in arcList)
[11229]103        RemoveArc(arc);
104      // deregister event handlers
[11249]105      vertex.ArcAdded -= Vertex_ArcAdded;
106      vertex.ArcRemoved -= Vertex_ArcRemoved;
107      OnVertexRemoved(this, new EventArgs<IVertex>(vertex));
[10884]108    }
109
[11241]110    public virtual IArc AddArc(IVertex source, IVertex target) {
[11229]111      var arc = new Arc(source, target);
112      AddArc(arc);
113      return arc;
[10300]114    }
[10897]115
[11241]116    public virtual void AddArc(IArc arc) {
[11229]117      var source = (Vertex)arc.Source;
118      var target = (Vertex)arc.Target;
119      source.AddArc(arc);
120      target.AddArc(arc);
121      arcs.Add(arc);
[10267]122    }
[10897]123
[11241]124    public virtual void RemoveArc(IArc arc) {
[11229]125      arcs.Remove(arc);
126      var source = (Vertex)arc.Source;
127      var target = (Vertex)arc.Target;
128      source.RemoveArc(arc);
129      target.RemoveArc(arc);
[10267]130    }
[10897]131
[11249]132    protected virtual void Vertex_ArcAdded(object sender, EventArgs<IArc> args) {
133      // the ArcAdded event is fired by a vertex when an arc from/to another vertex is added to its list of connections
134      // because the arc is added in both directions by both the source and the target, this event will get fired twice here
[11229]135      var arc = args.Value;
[11249]136      if (arcs.Add(arc)) OnArcAdded(this, new EventArgs<IArc>(arc));
[10888]137    }
138
[11249]139    protected virtual void Vertex_ArcRemoved(object sender, EventArgs<IArc> args) {
[11229]140      var arc = args.Value;
[11249]141      if (arcs.Remove(arc)) OnArcRemoved(this, new EventArgs<IArc>(arc));
[10267]142    }
[10888]143
[11238]144    // events
[11250]145    public event EventHandler<EventArgs<IVertex> VertexAdded;
[11249]146    protected virtual void OnVertedAdded(object sender, EventArgs<IVertex> args) {
[11248]147      var added = VertexAdded;
148      if (added != null)
149        added(sender, args);
150    }
151
[11250]152    public event EventHandler<EventArgs<IVertex>> VertexRemoved;
[11249]153    protected virtual void OnVertexRemoved(object sender, EventArgs<IVertex> args) {
[11248]154      var removed = VertexRemoved;
155      if (removed != null)
156        removed(sender, args);
157    }
158
[11250]159    public event EventHandler<EventArgs<IArc>> ArcAdded;
[11249]160    protected virtual void OnArcAdded(object sender, EventArgs<IArc> args) {
[11248]161      var added = ArcAdded;
162      if (added != null)
163        added(sender, args);
164    }
165
[11250]166    public event EventHandler<EventArgs<IArc>> ArcRemoved;
[11249]167    protected virtual void OnArcRemoved(object sender, EventArgs<IArc> args) {
[11248]168      var removed = ArcRemoved;
169      if (removed != null)
170        removed(sender, args);
171    }
[10267]172  }
173}
Note: See TracBrowser for help on using the repository browser.