@@ -21,6 +21,7 @@ class RelationshipMatchCollector:
2121 def __init__ (self ) -> None :
2222 self ._matches : List [RelationshipMatchRecord ] = []
2323 self ._node_ids : List [str ] = []
24+ self ._node_id_set : set [str ] = set ()
2425
2526 def push (self , relationship : 'Relationship' , traversal_id : str = "" ) -> RelationshipMatchRecord :
2627 """Push a new match onto the collector."""
@@ -42,6 +43,7 @@ def push(self, relationship: 'Relationship', traversal_id: str = "") -> Relation
4243 }
4344 self ._matches .append (match )
4445 self ._node_ids .append (traversal_id )
46+ self ._node_id_set .add (traversal_id )
4547 return match
4648
4749 @property
@@ -61,7 +63,8 @@ def end_node(self, node: 'Node') -> None:
6163 def pop (self ) -> Optional [RelationshipMatchRecord ]:
6264 """Pop the last match from the collector."""
6365 if self ._node_ids :
64- self ._node_ids .pop ()
66+ removed_id = self ._node_ids .pop ()
67+ self ._node_id_set .discard (removed_id )
6568 if self ._matches :
6669 return self ._matches .pop ()
6770 return None
@@ -82,4 +85,4 @@ def matches(self) -> List[RelationshipMatchRecord]:
8285
8386 def is_circular (self , next_id : str = "" ) -> bool :
8487 """Check if traversing to the given node id would form a cycle."""
85- return next_id in self ._node_ids
88+ return next_id in self ._node_id_set
0 commit comments