Changeset 11263


Ignore:
Timestamp:
08/04/14 14:58:47 (6 years ago)
Author:
bburlacu
Message:

#2223:

  • Vertex: remove cloning of arcs in the cloning constructor
  • DirectedGraph; Adjust cloning constructor to add arcs to cloned vertices
Location:
trunk/sources/HeuristicLab.Core/3.3/Collections/DirectedGraph
Files:
2 edited

Legend:

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

    r11251 r11263  
    5757      vertices = new HashSet<IVertex>(original.vertices.Select(cloner.Clone));
    5858      arcs = new HashSet<IArc>(original.arcs.Select(cloner.Clone));
     59
     60      // add the arcs to the newly cloned vertices
     61      foreach (var arc in arcs) {
     62        arc.Source.AddArc(arc);
     63        arc.Target.AddArc(arc);
     64      }
    5965    }
    6066
     
    8995
    9096    public virtual void AddVertex(IVertex vertex) {
    91       vertices.Add(vertex);
    92       // register event handlers
    93       vertex.ArcAdded += Vertex_ArcAdded;
    94       vertex.ArcRemoved += Vertex_ArcRemoved;
    95       OnVertedAdded(this, new EventArgs<IVertex>(vertex));
     97      if (!vertices.Contains(vertex) && vertex.Degree > 0)
     98        throw new ArgumentException("New vertices cannot have any arcs.");
     99
     100      if (vertices.Add(vertex)) {
     101        // register event handlers
     102        vertex.ArcAdded += Vertex_ArcAdded;
     103        vertex.ArcRemoved += Vertex_ArcRemoved;
     104        OnVertedAdded(this, new EventArgs<IVertex>(vertex));
     105      }
    96106    }
    97107
  • trunk/sources/HeuristicLab.Core/3.3/Collections/DirectedGraph/Vertex.cs

    r11248 r11263  
    9393      weight = original.Weight;
    9494
    95       inArcs = original.InArcs.Select(cloner.Clone).ToList();
    96       outArcs = original.OutArcs.Select(cloner.Clone).ToList();
     95      // we do not clone the arcs here (would cause too much recursion and ultimately a stack overflow)
    9796    }
    9897
     
    103102    public void AddArc(IArc arc) {
    104103      if (this != arc.Source && this != arc.Target)
    105         throw new InvalidOperationException("The current vertex must be either the arc source or the 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.");
    106108
    107109      if (this == arc.Source) {
     
    118120    public void RemoveArc(IArc arc) {
    119121      if (this != arc.Source && this != arc.Target)
    120         throw new InvalidOperationException("The current vertex must be either the arc source or the arc target.");
     122        throw new ArgumentException("The current vertex must be either the arc source or the arc target.");
    121123
    122124      if (this == arc.Source) {
     
    168170    }
    169171
     172    public Vertex(IDeepCloneable data)
     173      : base(data) {
     174    }
     175
    170176    public override IDeepCloneable Clone(Cloner cloner) {
    171177      return new Vertex<T>(this, cloner);
Note: See TracChangeset for help on using the changeset viewer.