Throw error if added edges are invalid

This is the case if both the origin and the target of the edge point to
the same node or if an edge already exists between two nodes.
This commit is contained in:
Robert Machmer 2015-12-21 12:36:38 +01:00
parent 805278d67a
commit 89b6c16974

View File

@ -31,6 +31,13 @@ function Graph.new()
end
function self:addEdge( origin, target )
for _, edge in pairs(edges) do
if edge.origin == origin and edge.target == target then
error "Trying to connect nodes which are already connected.";
end
end
assert(origin ~= target, "Tried to connect a node with itself.");
edges[edgeIDs] = Edge.new( edgeIDs, origin, target );
edgeIDs = edgeIDs + 1;
end