Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/05/15 13:55:04 (9 years ago)
Author:
bburlacu
Message:

#2223: Moved Data member to the generic versions of Vertex and Arc (restricted to be an IDeepCloneable). Fixed AddVertices method and added complementary AddArcs and RemoveArcs methods.

Location:
trunk/sources/HeuristicLab.Core/3.3/Collections/DirectedGraph
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Core/3.3/Collections/DirectedGraph/Arc.cs

    r11879 r11912  
    5656    }
    5757
    58     [Storable]
    59     protected object data;
    60     public object Data {
    61       get { return data; }
    62       set {
    63         if (data == value) return;
    64         data = value;
    65         OnChanged(this, EventArgs.Empty);
    66       }
    67     }
    6858
    6959    [StorableConstructor]
     
    8171      label = original.Label;
    8272      weight = original.Weight;
    83       if (data is IDeepCloneable)
    84         data = cloner.Clone((IDeepCloneable)data);
    85       else data = original.Data;
    8673    }
    8774    public override IDeepCloneable Clone(Cloner cloner) { return new Arc(this, cloner); }
     
    9683
    9784  [StorableClass]
    98   public class Arc<T> : Arc, IArc<T> where T : class,IItem {
     85  public class Arc<T> : Arc, IArc<T> where T : class,IDeepCloneable {
     86    [Storable]
     87    protected T data;
     88    public T Data {
     89      get { return data; }
     90      set {
     91        if (data == value) return;
     92        data = value;
     93        OnChanged(this, EventArgs.Empty);
     94      }
     95    }
     96
     97    public override IDeepCloneable Clone(Cloner cloner) {
     98      return new Arc<T>(this, cloner);
     99    }
     100
     101    protected Arc(Arc<T> original, Cloner cloner)
     102      : base(original, cloner) {
     103      if (original.Data != null)
     104        Data = cloner.Clone(original.Data);
     105    }
     106
    99107    public Arc(bool deserializing)
    100108      : base(deserializing) {
     
    108116      : base(original, cloner) {
    109117    }
    110 
    111     new public IVertex<T> Source {
    112       get { return (IVertex<T>)base.Source; }
    113     }
    114     new public IVertex<T> Target {
    115       get { return (IVertex<T>)base.Target; }
    116     }
    117118  }
    118119}
  • trunk/sources/HeuristicLab.Core/3.3/Collections/DirectedGraph/DirectedGraph.cs

    r11444 r11912  
    102102        vertex.ArcAdded += Vertex_ArcAdded;
    103103        vertex.ArcRemoved += Vertex_ArcRemoved;
    104         OnVertedAdded(this, new EventArgs<IVertex>(vertex));
     104        OnVertexAdded(this, new EventArgs<IVertex>(vertex));
    105105      }
    106106    }
    107107
    108108    public virtual void AddVertices(IEnumerable<IVertex> vertexList) {
    109       var hash = new HashSet<IVertex>(vertexList);
    110       var arcList = vertexList.SelectMany(v => v.InArcs.Concat(v.OutArcs));
    111       if (arcList.Any(x => !hash.Contains(x.Source) || !hash.Contains(x.Target)))
    112         throw new ArgumentException("Vertex arcs are connected to vertices not in the graph.");
    113       // if everything is in order, add the vertices to the directed graph
    114       foreach (var v in vertexList)
    115         vertices.Add(v);
    116       foreach (var a in arcList)
    117         arcs.Add(a);
     109      foreach (var v in vertexList) { AddVertex(v); }
    118110    }
    119111
    120112    public virtual void RemoveVertices(IEnumerable<IVertex> vertexList) {
    121       foreach (var v in vertexList)
    122         RemoveVertex(v);
     113      foreach (var v in vertexList) { RemoveVertex(v); }
    123114    }
    124115
     
    156147    }
    157148
     149    public virtual void AddArcs(IEnumerable<IArc> arcList) {
     150      foreach (var a in arcList) { AddArc(a); }
     151    }
     152
    158153    public virtual void RemoveArc(IArc arc) {
    159154      arcs.Remove(arc);
     
    164159    }
    165160
     161    public virtual void RemoveArcs(IEnumerable<IArc> arcList) {
     162      foreach (var a in arcList) { RemoveArc(a); }
     163    }
     164
    166165    protected virtual void Vertex_ArcAdded(object sender, EventArgs<IArc> args) {
    167166      // the ArcAdded event is fired by a vertex when an arc from/to another vertex is added to its list of connections
     
    178177    // events
    179178    public event EventHandler<EventArgs<IVertex>> VertexAdded;
    180     protected virtual void OnVertedAdded(object sender, EventArgs<IVertex> args) {
     179    protected virtual void OnVertexAdded(object sender, EventArgs<IVertex> args) {
    181180      var added = VertexAdded;
    182181      if (added != null)
  • trunk/sources/HeuristicLab.Core/3.3/Collections/DirectedGraph/Vertex.cs

    r11263 r11912  
    5252    }
    5353
    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 
    6554    private readonly List<IArc> inArcs = new List<IArc>();
    6655    public IEnumerable<IArc> InArcs {
     
    8372    private void AfterDeserialization() { }
    8473
    85     public Vertex(IDeepCloneable data) {
    86       this.data = data;
    87     }
     74    public Vertex() { }
    8875
    8976    protected Vertex(Vertex original, Cloner cloner)
    9077      : base(original, cloner) {
    91       data = cloner.Clone(original.Data);
    9278      label = original.Label;
    9379      weight = original.Weight;
    94 
    9580      // we do not clone the arcs here (would cause too much recursion and ultimately a stack overflow)
    9681    }
     
    157142
    158143  [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; }
     144  public class Vertex<T> : Vertex, IVertex<T> where T : class,IDeepCloneable {
     145    [Storable]
     146    private T data;
     147    public T Data {
     148      get { return data; }
     149      set {
     150        if (data == value) return;
     151        data = value;
     152        OnChanged(this, EventArgs.Empty);
     153      }
    163154    }
    164155
     
    168159    protected Vertex(Vertex<T> original, Cloner cloner)
    169160      : base(original, cloner) {
     161      if (original.Data != null)
     162        Data = cloner.Clone(original.Data);
    170163    }
    171164
    172     public Vertex(IDeepCloneable data)
    173       : base(data) {
    174     }
     165    public Vertex() : base() { }
    175166
    176167    public override IDeepCloneable Clone(Cloner cloner) {
Note: See TracChangeset for help on using the changeset viewer.