Forbid duplicate edges in the Graph
Created by: gkirgizov
Resolves #604 (closed) & #580 (closed)
I was thinking about 2 ways to resolve this:
- Maintain the new invariant in the Graph (actually, in GraphOperator) on each operation (add,remove,update etc.)
- Maintain the new invariant in the GraphNode
I chose the second alternative and substituted implementation of nodes_from
from standard list
to thin subclass UniqueList
that forbids duplicate elements. Btw: list is used instead of something like ordered set to preserve standard list interface, and for small lists such as nodes_from
it's no less efficient than a set.
Motivation for the second approach:
- This for sure prevents us from future bugs related to duplicate edges. otherwise on each change to Graph or its subclasses we must remember about its invariants. It's difficult to enforce.
- Trying to maintain the new invariant on Graph level requires a lot of scattered changes, that complicates core logic of graph methods. GraphNode is essential part of Graph, so that's natural that it bears responsibility for Graph invariants. Anyway, I see no sense to allow storing duplicate elements in
node.nodes_from
. - It also somewhat hides implementation details of GraphNode (that we use a list underneath)