private bool CanAddNeighbour(Node node) { if (node == null) return false; //Check if the same graph, ie. same reference //Allow adding only if edge was added by Graph beforehand return Parent.Equals(node.Parent) && Parent.ContainsNode(node.Key) && Parent.ContainsEdge(this, node); }
private bool CanConnect(Node from, Node to) { if (from == null || to == null) return false; if (!Equals(from.Parent, to.Parent)) return false; if (!from.Parent.ContainsNode(from.Key) || !to.Parent.ContainsNode(to.Key)) return false; return true; }
public Edge(Node from, Node to) { if (from == null) throw new ArgumentNullException(nameof(from)); if (to == null) throw new ArgumentNullException(nameof(to)); if (!CanConnect(from, to)) throw new ArgumentException("Nodes cannot be connected due to mismatched criterias!"); From = from; To = to; }
/// <summary> /// Removes neighbouring node /// </summary> /// <param name="node">Neighbour to remove</param> public void RemoveNeighbour(Node node) { if (node == null) throw new ArgumentNullException(nameof(node)); var match = Neighbours.SingleOrDefault(n => node.Equals(n)); //Prevent endless circular removing if (match == null) return; if (!CanRemoveNeighbour(node)) throw new InvalidOperationException("Neighbour cannot be removed!"); Neighbours.Remove(match); match.RemoveNeighbour(this); }
/// <summary> /// Adds neighbouring node. Will fail if not added via <see cref="T:Localwire.Graphinder.Core.Graph.Graph"/>. /// </summary> /// <param name="node">New neighbour to add</param> public void AddNeighbour(Node node) { if (node == null) throw new ArgumentNullException(nameof(node)); //Prevent endless circular adding if (Neighbours.Contains(node)) return; if (node.Equals(this)) throw new InvalidOperationException("Attempt to add self as neighbour!"); if (!CanAddNeighbour(node)) throw new InvalidOperationException("Neighbour cannot be added!"); Neighbours.Add(node); node.AddNeighbour(this); }
private bool CanRemoveNeighbour(Node node) { if (node == null) return false; return Parent.ContainsNode(node.Key) && !Parent.ContainsEdge(this, node); }
/// <summary> /// Checks if graph contains edge connecting given nodes. /// </summary> /// <param name="from">First vertex of an edge.</param> /// <param name="to">Second vertex of an edge.</param> /// <returns></returns> public bool ContainsEdge(Node from, Node to) { return _edges.Contains(new Edge(from, to)); }
/// <summary> /// Adds node to the graph. /// </summary> /// <param name="key">Key representing node to be added</param> public Node AddNode(string key, Guid? id = null) { if (!CanAdd()) throw new DataStructureLockedException("Adding node to graph", GetType()); if (ContainsNode(key)) throw new InvalidOperationException("Attempt to add duplicate node!"); var node = new Node(key, this); _nodes.Add(node.Key, node); _nodesList.Add(node); return node; }