PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/compiler/utils/ListSetOps.lhs

https://bitbucket.org/carter/ghc
Haskell | 177 lines | 128 code | 36 blank | 13 comment | 6 complexity | 8c6736c2a774ac180eb39ffa5d1f8514 MD5 | raw file
  1. %
  2. % (c) The University of Glasgow 2006
  3. % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
  4. %
  5. \section[ListSetOps]{Set-like operations on lists}
  6. \begin{code}
  7. module ListSetOps (
  8. unionLists, minusList, insertList,
  9. -- Association lists
  10. Assoc, assoc, assocMaybe, assocUsing, assocDefault, assocDefaultUsing,
  11. -- Duplicate handling
  12. hasNoDups, runs, removeDups, findDupsEq,
  13. equivClasses, equivClassesByUniq,
  14. ) where
  15. #include "HsVersions.h"
  16. import Outputable
  17. import Unique
  18. import UniqFM
  19. import Util
  20. import Data.List
  21. \end{code}
  22. %************************************************************************
  23. %* *
  24. Treating lists as sets
  25. Assumes the lists contain no duplicates, but are unordered
  26. %* *
  27. %************************************************************************
  28. \begin{code}
  29. insertList :: Eq a => a -> [a] -> [a]
  30. -- Assumes the arg list contains no dups; guarantees the result has no dups
  31. insertList x xs | isIn "insert" x xs = xs
  32. | otherwise = x : xs
  33. unionLists :: (Outputable a, Eq a) => [a] -> [a] -> [a]
  34. -- Assumes that the arguments contain no duplicates
  35. unionLists xs ys
  36. = WARN(length xs > 100 || length ys > 100, ppr xs $$ ppr ys)
  37. [x | x <- xs, isn'tIn "unionLists" x ys] ++ ys
  38. minusList :: (Eq a) => [a] -> [a] -> [a]
  39. -- Everything in the first list that is not in the second list:
  40. minusList xs ys = [ x | x <- xs, isn'tIn "minusList" x ys]
  41. \end{code}
  42. %************************************************************************
  43. %* *
  44. \subsection[Utils-assoc]{Association lists}
  45. %* *
  46. %************************************************************************
  47. Inefficient finite maps based on association lists and equality.
  48. \begin{code}
  49. -- A finite mapping based on equality and association lists
  50. type Assoc a b = [(a,b)]
  51. assoc :: (Eq a) => String -> Assoc a b -> a -> b
  52. assocDefault :: (Eq a) => b -> Assoc a b -> a -> b
  53. assocUsing :: (a -> a -> Bool) -> String -> Assoc a b -> a -> b
  54. assocMaybe :: (Eq a) => Assoc a b -> a -> Maybe b
  55. assocDefaultUsing :: (a -> a -> Bool) -> b -> Assoc a b -> a -> b
  56. assocDefaultUsing _ deflt [] _ = deflt
  57. assocDefaultUsing eq deflt ((k,v) : rest) key
  58. | k `eq` key = v
  59. | otherwise = assocDefaultUsing eq deflt rest key
  60. assoc crash_msg list key = assocDefaultUsing (==) (panic ("Failed in assoc: " ++ crash_msg)) list key
  61. assocDefault deflt list key = assocDefaultUsing (==) deflt list key
  62. assocUsing eq crash_msg list key = assocDefaultUsing eq (panic ("Failed in assoc: " ++ crash_msg)) list key
  63. assocMaybe alist key
  64. = lookup alist
  65. where
  66. lookup [] = Nothing
  67. lookup ((tv,ty):rest) = if key == tv then Just ty else lookup rest
  68. \end{code}
  69. %************************************************************************
  70. %* *
  71. \subsection[Utils-dups]{Duplicate-handling}
  72. %* *
  73. %************************************************************************
  74. \begin{code}
  75. hasNoDups :: (Eq a) => [a] -> Bool
  76. hasNoDups xs = f [] xs
  77. where
  78. f _ [] = True
  79. f seen_so_far (x:xs) = if x `is_elem` seen_so_far
  80. then False
  81. else f (x:seen_so_far) xs
  82. is_elem = isIn "hasNoDups"
  83. \end{code}
  84. \begin{code}
  85. equivClasses :: (a -> a -> Ordering) -- Comparison
  86. -> [a]
  87. -> [[a]]
  88. equivClasses _ [] = []
  89. equivClasses _ stuff@[_] = [stuff]
  90. equivClasses cmp items = runs eq (sortBy cmp items)
  91. where
  92. eq a b = case cmp a b of { EQ -> True; _ -> False }
  93. \end{code}
  94. The first cases in @equivClasses@ above are just to cut to the point
  95. more quickly...
  96. @runs@ groups a list into a list of lists, each sublist being a run of
  97. identical elements of the input list. It is passed a predicate @p@ which
  98. tells when two elements are equal.
  99. \begin{code}
  100. runs :: (a -> a -> Bool) -- Equality
  101. -> [a]
  102. -> [[a]]
  103. runs _ [] = []
  104. runs p (x:xs) = case (span (p x) xs) of
  105. (first, rest) -> (x:first) : (runs p rest)
  106. \end{code}
  107. \begin{code}
  108. removeDups :: (a -> a -> Ordering) -- Comparison function
  109. -> [a]
  110. -> ([a], -- List with no duplicates
  111. [[a]]) -- List of duplicate groups. One representative from
  112. -- each group appears in the first result
  113. removeDups _ [] = ([], [])
  114. removeDups _ [x] = ([x],[])
  115. removeDups cmp xs
  116. = case (mapAccumR collect_dups [] (equivClasses cmp xs)) of { (dups, xs') ->
  117. (xs', dups) }
  118. where
  119. collect_dups _ [] = panic "ListSetOps: removeDups"
  120. collect_dups dups_so_far [x] = (dups_so_far, x)
  121. collect_dups dups_so_far dups@(x:_) = (dups:dups_so_far, x)
  122. findDupsEq :: (a->a->Bool) -> [a] -> [[a]]
  123. findDupsEq _ [] = []
  124. findDupsEq eq (x:xs) | null eq_xs = findDupsEq eq xs
  125. | otherwise = (x:eq_xs) : findDupsEq eq neq_xs
  126. where (eq_xs, neq_xs) = partition (eq x) xs
  127. \end{code}
  128. \begin{code}
  129. equivClassesByUniq :: (a -> Unique) -> [a] -> [[a]]
  130. -- NB: it's *very* important that if we have the input list [a,b,c],
  131. -- where a,b,c all have the same unique, then we get back the list
  132. -- [a,b,c]
  133. -- not
  134. -- [c,b,a]
  135. -- Hence the use of foldr, plus the reversed-args tack_on below
  136. equivClassesByUniq get_uniq xs
  137. = eltsUFM (foldr add emptyUFM xs)
  138. where
  139. add a ufm = addToUFM_C tack_on ufm (get_uniq a) [a]
  140. tack_on old new = new++old
  141. \end{code}