Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2223: Copied directed graph implented in bottom up tree distance branch into the trunk.

File size: 5.4 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  [StorableClass]
30  public class Vertex : Item, IVertex {
31    [Storable]
32    private string label;
33    public string Label {
34      get { return label; }
35      set {
36        if (label.Equals(value)) return;
37        label = value;
38        OnChanged(this, EventArgs.Empty);
39      }
40    }
41
42    [Storable]
43    private double weight;
44    public double Weight {
45      get { return weight; }
46      set {
47        if (weight.Equals(value)) return;
48        weight = value;
49        OnChanged(this, EventArgs.Empty);
50      }
51    }
52
53    [Storable]
54    private IDeepCloneable data;
55    public IDeepCloneable Data {
56      get { return data; }
57      set {
58        if (data == value) return;
59        data = value;
60        OnChanged(this, EventArgs.Empty);
61      }
62    }
63
64    private readonly List<IArc> inArcs = new List<IArc>();
65    public IEnumerable<IArc> InArcs {
66      get { return inArcs; }
67    }
68
69    private readonly List<IArc> outArcs = new List<IArc>();
70    public IEnumerable<IArc> OutArcs {
71      get { return outArcs; }
72    }
73
74    public int InDegree { get { return InArcs.Count(); } }
75    public int OutDegree { get { return OutArcs.Count(); } }
76    public int Degree { get { return InDegree + OutDegree; } }
77
78    [StorableConstructor]
79    public Vertex(bool deserializing) : base(deserializing) { }
80
81    [StorableHook(HookType.AfterDeserialization)]
82    private void AfterDeserialization() { }
83
84    public Vertex(IDeepCloneable data) {
85      this.data = data;
86    }
87
88    protected Vertex(Vertex original, Cloner cloner)
89      : base(original, cloner) {
90      data = cloner.Clone(original.Data);
91      label = original.Label;
92      weight = original.Weight;
93
94      inArcs = original.InArcs.Select(cloner.Clone).ToList();
95      outArcs = original.OutArcs.Select(cloner.Clone).ToList();
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 InvalidOperationException("The current vertex must be either the arc source or the arc target.");
105
106      if (this == arc.Source) {
107        if (outArcs.Contains(arc)) throw new InvalidOperationException("Arc already added.");
108        outArcs.Add(arc);
109      }
110      if (this == arc.Target) {
111        if (inArcs.Contains(arc)) throw new InvalidOperationException("Arc already added.");
112        inArcs.Add(arc);
113      }
114      OnArcAdded(this, new EventArgs<IArc>(arc));
115    }
116
117    public void RemoveArc(IArc arc) {
118      if (this != arc.Source && this != arc.Target)
119        throw new InvalidOperationException("The current vertex must be either the arc source or the arc target.");
120
121      if (this == arc.Source) {
122        if (!outArcs.Remove(arc)) throw new InvalidOperationException("Arc is not present in this vertex' outgoing arcs.");
123      }
124      if (this == arc.Target) {
125        if (!inArcs.Remove(arc)) throw new InvalidOperationException("Arc is not present in this vertex' incoming arcs.");
126      }
127      OnArcRemoved(this, new EventArgs<IArc>(arc));
128    }
129
130    #region events
131    // use the same event to signal a change in the content, weight or label
132    public event EventHandler Changed;
133    protected virtual void OnChanged(object sender, EventArgs args) {
134      var changed = Changed;
135      if (changed != null)
136        changed(sender, args);
137    }
138
139    public event EventHandler<EventArgs<IArc>> ArcAdded;
140    protected virtual void OnArcAdded(object sender, EventArgs<IArc> args) {
141      var added = ArcAdded;
142      if (added != null)
143        added(sender, args);
144    }
145
146    public event EventHandler<EventArgs<IArc>> ArcRemoved;
147    protected virtual void OnArcRemoved(object sender, EventArgs<IArc> args) {
148      var removed = ArcRemoved;
149      if (removed != null)
150        removed(sender, args);
151    }
152    #endregion
153  }
154
155  [StorableClass]
156  public class Vertex<T> : Vertex, IVertex<T> where T : class,IItem {
157    public new T Data {
158      get { return (T)base.Data; }
159      set { base.Data = value; }
160    }
161
162    [StorableConstructor]
163    protected Vertex(bool deserializing) : base(deserializing) { }
164
165    protected Vertex(Vertex<T> original, Cloner cloner)
166      : base(original, cloner) {
167    }
168
169    public override IDeepCloneable Clone(Cloner cloner) {
170      return new Vertex<T>(this, cloner);
171    }
172  }
173}
Note: See TracBrowser for help on using the repository browser.