From 4df758227d31b2d0344ab28d5e47b6f550dafcea Mon Sep 17 00:00:00 2001 From: Michael Mikonos <127171689+mknos@users.noreply.github.com> Date: Wed, 16 Oct 2024 08:42:30 +0800 Subject: [PATCH] tsort: allow independent nodes * When a node refers to itself in a pair it means the node exists but does not depend on anything * Previously, this input would result in a "cycle" error * In test file, allow node 'd' to be added to npred-hash with a predecessor count of zero, so it can appear in list-list * The inner "foreach" loop does not execute for node 'd' because it has no dependency; it has no need for an entry in succ-hash %perl cat in.tsort2 a b b c d d %perl tsort.old in.tsort2 a b c tsort.old: cycle detected %perl tsort in.tsort2 a b c d %perl cat in.tsort3 a a %perl tsort in.tsort3 a --- bin/tsort | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/tsort b/bin/tsort index 4a018dfd..c56400ab 100755 --- a/bin/tsort +++ b/bin/tsort @@ -68,6 +68,7 @@ while (@input) { next if defined $pairs{$l}{$r}; $pairs{$l}{$r}++; $npred{$l} += 0; + next if $l eq $r; ++$npred{$r}; push @{$succ{$l}}, $r; }