From 80179ab3a1f11eafd94ff005316da78f9593dd40 Mon Sep 17 00:00:00 2001 From: Andreas Abel Date: Fri, 2 Feb 2024 12:08:59 +0100 Subject: [PATCH] Add `flattenSCC1 :: SCC a -> NonEmpty a` This gives a more precise type to the existing `flattenSCC :: SCC a -> [a]`. Closes #985. --- containers/src/Data/Graph.hs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/containers/src/Data/Graph.hs b/containers/src/Data/Graph.hs index 33983ffee..3ce605e89 100644 --- a/containers/src/Data/Graph.hs +++ b/containers/src/Data/Graph.hs @@ -246,9 +246,18 @@ flattenSCCs :: [SCC a] -> [a] flattenSCCs = concatMap flattenSCC -- | The vertices of a strongly connected component. +-- +-- @flattenSCC = 'Data.List.NonEmpty.toList' . 'flattenSCC1'@. +-- +-- This function is retained for backward compatibility, +-- 'flattenSCC1' has the more precise type. flattenSCC :: SCC vertex -> [vertex] -flattenSCC (AcyclicSCC v) = [v] -flattenSCC (NECyclicSCC vs) = NE.toList vs +flattenSCC = NE.toList . flattenSCC1 + +-- | The vertices of a strongly connected component. +flattenSCC1 :: SCC vertex -> NonEmpty vertex +flattenSCC1 (AcyclicSCC v) = NE.singleton v +flattenSCC1 (NECyclicSCC vs) = vs -- | \(O((V+E) \log V)\). The strongly connected components of a directed graph, -- reverse topologically sorted.