Skip to content

Commit

Permalink
Disconnect nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellri committed Oct 9, 2019
1 parent dcec19c commit 8c0a065
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Dijkstra.NET/Graph/Graph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ public bool Connect(uint from, uint to, int cost, TEdgeCustom custom)
return true;
}

/// <summary>
/// Disconnect node to from node from
/// </summary>
/// <param name="from">First node</param>
/// <param name="to">Second node</param>
/// <returns>Returns true if nodes disconnected</returns>
public bool Disconnect(uint from, uint to)
{
if (!_nodes.ContainsKey(from) || !_nodes.ContainsKey(to))
return false;

Node<T,TEdgeCustom> nodeFrom = _nodes[from];
Node<T, TEdgeCustom> nodeTo = _nodes[to];

return nodeTo.RemoveParent(nodeFrom) & nodeFrom.RemoveEdge(to);
}

public IEnumerator<INode<T, TEdgeCustom>> GetEnumerator() => _nodes.Select(x => x.Value).GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
Expand Down
19 changes: 19 additions & 0 deletions src/Dijkstra.NET/Graph/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,30 @@ internal void AddEdge(in Edge<T, TEdgeCustom> edge)
EdgesCount++;
}

internal bool RemoveEdge(in uint to)
{
for (int i = 0; i < EdgesCount; i++)
{
if (_edges[i].Node.Key == to)
{
EdgesCount--;
_edges[i] = _edges[EdgesCount];
return true;
}
}
return true;
}

internal void AddParent(Node<T, TEdgeCustom> parent)
{
_parents.Add(parent);
}

internal bool RemoveParent(Node<T, TEdgeCustom> parent)
{
return _parents.Remove(parent);
}

public override int GetHashCode()
{
return Key.GetHashCode();
Expand Down

0 comments on commit 8c0a065

Please sign in to comment.