# HG changeset patch
# User unC0Rr
# Date 1290088895 -10800
# Node ID 935de6cd5ea300d56578a73de6cb593300de9a20
# Parent  8867bc102f0540ed55fde423828991667fffba0b
New version of cycles searcher, doesn't work for some reason (shouldn't use O(n) lookup?)

diff -r 8867bc102f05 -r 935de6cd5ea3 tools/unitCycles.hs
--- a/tools/unitCycles.hs	Thu Nov 18 17:00:45 2010 +0300
+++ b/tools/unitCycles.hs	Thu Nov 18 17:01:35 2010 +0300
@@ -6,6 +6,7 @@
 import Data.Either
 import Data.List
 import Data.Graph
+import Data.Maybe
 
 unident :: Identificator -> String
 unident (Identificator s) = s
@@ -20,7 +21,26 @@
     showSCC (AcyclicSCC v) = v
     showSCC (CyclicSCC vs) = intercalate ", " vs
 
+myf :: [(String, [String])] -> String
+myf d = unlines . map (findCycle . fst) $ d
+    where
+    findCycle :: String -> String
+    findCycle searched = intercalate ", " $ fc searched [searched]
+        where
+        fc :: String -> [String] -> [String]
+        fc curSearch visited = let uses = curSearch `lookup` d in if isNothing uses then [] else concatMap t $ fromJust uses
+            where
+            t u =
+                if u == searched then
+                    [u]
+                    else
+                    if u `elem` visited then
+                        []
+                        else
+                        let chain = fc u (u:visited) in if null chain then [] else u:chain
+
+
 main = do
     fileNames <- getArgs
     files <- mapM readFile fileNames
-    putStrLn . f . map extractUnits . rights . map parsePascalUnit $ files
+    putStrLn . myf . map extractUnits . rights . map parsePascalUnit $ files