Changeset 11324


Ignore:
Timestamp:
09/01/14 13:50:36 (8 years ago)
Author:
bburlacu
Message:

#2223: Introduced AddVertices method. Check if both source and target vertices are included in the graph when adding a new arc.

File:
1 edited

Legend:

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

    r11263 r11324  
    106106    }
    107107
     108    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);
     118    }
     119
    108120    public virtual void RemoveVertex(IVertex vertex) {
    109121      vertices.Remove(vertex);
     
    125137
    126138    public virtual void AddArc(IArc arc) {
    127       var source = (Vertex)arc.Source;
    128       var target = (Vertex)arc.Target;
     139      var source = arc.Source;
     140      var target = arc.Target;
     141
     142      if (!vertices.Contains(source) || !vertices.Contains(target))
     143        throw new InvalidOperationException("Cannot add arc connecting vertices that are not in the graph.");
     144
    129145      source.AddArc(arc);
    130146      target.AddArc(arc);
Note: See TracChangeset for help on using the changeset viewer.