-
Notifications
You must be signed in to change notification settings - Fork 2
/
Recommend.hs
52 lines (42 loc) · 2.02 KB
/
Recommend.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
-- | Module for recommending a data structure based on operations used
module Recommend (printRecommendationFromAnalysis, recommendAllDs, recommendDS) where
import Defs.Structures
import AllStructures
import Analyzer
import Defs.Util
import Defs.Common
import Data.List
import System.Random
-- | Recommends a data structure which is best for given operations @opns@. If there's more than one optimal structure, it chooses one at random
recommendDS :: [OperationName] -> IO Structure
recommendDS opns = do
let sorted = reverse $ sortBy (\x y-> compareDS x y opns) allStructures
let bestStructures = head $ groupBy (\x y -> compareDS x y opns == EQ) sorted
ridx <- randomRIO (0, length bestStructures - 1)
return $ bestStructures !! ridx
-- | Recommends all optimal data structures for given operations @opns@
recommendAllDs :: [OperationName] -> [Structure]
recommendAllDs opns = recommendAllDsFromList opns allStructures
-- | Recommends the best possible data structure from @structs@ for given operations @opns@
recommendAllDsFromList :: [OperationName] -> [Structure] -> [Structure]
recommendAllDsFromList opns structs = let
sorted = reverse $ sortBy (\x y-> compareDS x y opns) structs
in head $ groupBy (\x y -> compareDS x y opns == EQ) sorted
-- | Pretty print single 'DSInfo'
printDSI :: (String -> IO ()) -> DSInfo -> IO ()
printDSI output dsi = do
output "The recommended structure for:\n"
printDSINames $ getDSINames dsi
output "is:\n"
cyanColor
recommendedDS >>= output . show
output "\n"
resetColor where
recommendedDS = do
let opns = map getDSUName $ getDSIDSU dsi
recommendDS opns
printDSINames [] = return ()
printDSINames ((F fn,V vn):ns) = greenColor >> output vn >> resetColor >> output " in " >> greenColor >> output fn >> resetColor >> output "\n"
-- | Pretty printer for the analyzer effects
printRecommendationFromAnalysis :: (String -> IO ()) -> [DSInfo] -> IO()
printRecommendationFromAnalysis output = mapM_ (printDSI output)