From ef23c64e800b9179af7cdd75895a7f1d067c1e8b Mon Sep 17 00:00:00 2001 From: apalala Date: Sun, 10 Dec 2023 09:17:32 -0400 Subject: [PATCH] [util][misc] document topological_sort --- tatsu/util/misc.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tatsu/util/misc.py b/tatsu/util/misc.py index 7f6d67a5..2d73739c 100644 --- a/tatsu/util/misc.py +++ b/tatsu/util/misc.py @@ -1,5 +1,5 @@ from __future__ import annotations -from typing import TypeVar +from typing import Iterable, TypeVar import re @@ -85,7 +85,7 @@ def findfirst( pattern, string, pos=None, endpos=None, flags=0, default=_undefin ) -def topological_sort(nodes: list[_T], order: set[tuple[_T, _T]]) -> list[_T]: +def topological_sort(nodes: Iterable[_T], order: Iterable[tuple[_T, _T]]) -> list[_T]: # https://en.wikipedia.org/wiki/Topological_sorting order = set(order) @@ -98,11 +98,13 @@ def topological_sort(nodes: list[_T], order: set[tuple[_T, _T]]) -> list[_T]: while pending: n = pending.pop() result.insert(0, n) + outgoing = {m for (x, m) in order if x == n} + # node m with an edge e from n to m for m in outgoing: order.remove((n, m)) if not any(x for x, y in order if y == m): - # m has no other incoming edges then + # m has no other incoming edges pending.append(m) if order: