Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11444 was 11263, checked in by bburlacu, 10 years ago

#2223:

  • Vertex: remove cloning of arcs in the cloning constructor
  • DirectedGraph; Adjust cloning constructor to add arcs to cloned vertices
File size: 5.7 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Core {
29  [Item("Vertex", "An object representing a vertex in the graph. It can have a text label, a weight, and an additional data object.")]
30  [StorableClass]
31  public class Vertex : Item, IVertex {
32    [Storable]
33    private string label;
34    public string Label {
35      get { return label; }
36      set {
37        if (label.Equals(value)) return;
38        label = value;
39        OnChanged(this, EventArgs.Empty);
40      }
41    }
42
43    [Storable]
44    private double weight;
45    public double Weight {
46      get { return weight; }
47      set {
48        if (weight.Equals(value)) return;
49        weight = value;
50        OnChanged(this, EventArgs.Empty);
51      }
52    }
53
54    [Storable]
55    private IDeepCloneable data;
56    public IDeepCloneable Data {
57      get { return data; }
58      set {
59        if (data == value) return;
60        data = value;
61        OnChanged(this, EventArgs.Empty);
62      }
63    }
64
65    private readonly List<IArc> inArcs = new List<IArc>();
66    public IEnumerable<IArc> InArcs {
67      get { return inArcs; }
68    }
69
70    private readonly List<IArc> outArcs = new List<IArc>();
71    public IEnumerable<IArc> OutArcs {
72      get { return outArcs; }
73    }
74
75    public int InDegree { get { return InArcs.Count(); } }
76    public int OutDegree { get { return OutArcs.Count(); } }
77    public int Degree { get { return InDegree + OutDegree; } }
78
79    [StorableConstructor]
80    public Vertex(bool deserializing) : base(deserializing) { }
81
82    [StorableHook(HookType.AfterDeserialization)]
83    private void AfterDeserialization() { }
84
85    public Vertex(IDeepCloneable data) {
86      this.data = data;
87    }
88
89    protected Vertex(Vertex original, Cloner cloner)
90      : base(original, cloner) {
91      data = cloner.Clone(original.Data);
92      label = original.Label;
93      weight = original.Weight;
94
95      // we do not clone the arcs here (would cause too much recursion and ultimately a stack overflow)
96    }
97
98    public override IDeepCloneable Clone(Cloner cloner) {
99      return new Vertex(this, cloner);
100    }
101
102    public void AddArc(IArc arc) {
103      if (this != arc.Source && this != arc.Target)
104        throw new ArgumentException("The current vertex must be either the arc source or the arc target.");
105
106      if (arc.Source == arc.Target)
107        throw new ArgumentException("Arc source and target must be different.");
108
109      if (this == arc.Source) {
110        if (outArcs.Contains(arc)) throw new InvalidOperationException("Arc already added.");
111        outArcs.Add(arc);
112      }
113      if (this == arc.Target) {
114        if (inArcs.Contains(arc)) throw new InvalidOperationException("Arc already added.");
115        inArcs.Add(arc);
116      }
117      OnArcAdded(this, new EventArgs<IArc>(arc));
118    }
119
120    public void RemoveArc(IArc arc) {
121      if (this != arc.Source && this != arc.Target)
122        throw new ArgumentException("The current vertex must be either the arc source or the arc target.");
123
124      if (this == arc.Source) {
125        if (!outArcs.Remove(arc)) throw new InvalidOperationException("Arc is not present in this vertex' outgoing arcs.");
126      }
127      if (this == arc.Target) {
128        if (!inArcs.Remove(arc)) throw new InvalidOperationException("Arc is not present in this vertex' incoming arcs.");
129      }
130      OnArcRemoved(this, new EventArgs<IArc>(arc));
131    }
132
133    #region events
134    // use the same event to signal a change in the content, weight or label
135    public event EventHandler Changed;
136    protected virtual void OnChanged(object sender, EventArgs args) {
137      var changed = Changed;
138      if (changed != null)
139        changed(sender, args);
140    }
141
142    public event EventHandler<EventArgs<IArc>> ArcAdded;
143    protected virtual void OnArcAdded(object sender, EventArgs<IArc> args) {
144      var added = ArcAdded;
145      if (added != null)
146        added(sender, args);
147    }
148
149    public event EventHandler<EventArgs<IArc>> ArcRemoved;
150    protected virtual void OnArcRemoved(object sender, EventArgs<IArc> args) {
151      var removed = ArcRemoved;
152      if (removed != null)
153        removed(sender, args);
154    }
155    #endregion
156  }
157
158  [StorableClass]
159  public class Vertex<T> : Vertex, IVertex<T> where T : class,IItem {
160    public new T Data {
161      get { return (T)base.Data; }
162      set { base.Data = value; }
163    }
164
165    [StorableConstructor]
166    protected Vertex(bool deserializing) : base(deserializing) { }
167
168    protected Vertex(Vertex<T> original, Cloner cloner)
169      : base(original, cloner) {
170    }
171
172    public Vertex(IDeepCloneable data)
173      : base(data) {
174    }
175
176    public override IDeepCloneable Clone(Cloner cloner) {
177      return new Vertex<T>(this, cloner);
178    }
179  }
180}
Note: See TracBrowser for help on using the repository browser.