PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/compiler/utils/UniqSet.hs

http://github.com/ghc/ghc
Haskell | 122 lines | 67 code | 21 blank | 34 comment | 0 complexity | ba42b1d7a3be6aefdf0cb1ea08bab48d MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, GPL-3.0
  1. {-
  2. (c) The University of Glasgow 2006
  3. (c) The AQUA Project, Glasgow University, 1994-1998
  4. \section[UniqSet]{Specialised sets, for things with @Uniques@}
  5. Based on @UniqFMs@ (as you would expect).
  6. Basically, the things need to be in class @Uniquable@.
  7. -}
  8. module UniqSet (
  9. -- * Unique set type
  10. UniqSet, -- type synonym for UniqFM a
  11. -- ** Manipulating these sets
  12. emptyUniqSet,
  13. unitUniqSet,
  14. mkUniqSet,
  15. addOneToUniqSet, addOneToUniqSet_C, addListToUniqSet,
  16. delOneFromUniqSet, delOneFromUniqSet_Directly, delListFromUniqSet,
  17. unionUniqSets, unionManyUniqSets,
  18. minusUniqSet,
  19. intersectUniqSets,
  20. uniqSetAny, uniqSetAll,
  21. elementOfUniqSet,
  22. elemUniqSet_Directly,
  23. filterUniqSet,
  24. sizeUniqSet,
  25. isEmptyUniqSet,
  26. lookupUniqSet,
  27. partitionUniqSet
  28. ) where
  29. import UniqFM
  30. import Unique
  31. {-
  32. ************************************************************************
  33. * *
  34. \subsection{The signature of the module}
  35. * *
  36. ************************************************************************
  37. -}
  38. emptyUniqSet :: UniqSet a
  39. unitUniqSet :: Uniquable a => a -> UniqSet a
  40. mkUniqSet :: Uniquable a => [a] -> UniqSet a
  41. addOneToUniqSet :: Uniquable a => UniqSet a -> a -> UniqSet a
  42. addOneToUniqSet_C :: Uniquable a => (a -> a -> a) -> UniqSet a -> a -> UniqSet a
  43. addListToUniqSet :: Uniquable a => UniqSet a -> [a] -> UniqSet a
  44. delOneFromUniqSet :: Uniquable a => UniqSet a -> a -> UniqSet a
  45. delOneFromUniqSet_Directly :: UniqSet a -> Unique -> UniqSet a
  46. delListFromUniqSet :: Uniquable a => UniqSet a -> [a] -> UniqSet a
  47. unionUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
  48. unionManyUniqSets :: [UniqSet a] -> UniqSet a
  49. minusUniqSet :: UniqSet a -> UniqSet a -> UniqSet a
  50. intersectUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
  51. elementOfUniqSet :: Uniquable a => a -> UniqSet a -> Bool
  52. elemUniqSet_Directly :: Unique -> UniqSet a -> Bool
  53. filterUniqSet :: (a -> Bool) -> UniqSet a -> UniqSet a
  54. partitionUniqSet :: (a -> Bool) -> UniqSet a -> (UniqSet a, UniqSet a)
  55. sizeUniqSet :: UniqSet a -> Int
  56. isEmptyUniqSet :: UniqSet a -> Bool
  57. lookupUniqSet :: Uniquable a => UniqSet b -> a -> Maybe b
  58. {-
  59. ************************************************************************
  60. * *
  61. \subsection{Implementation using ``UniqFM''}
  62. * *
  63. ************************************************************************
  64. -}
  65. -- Note [Unsound mapUniqSet]
  66. -- ~~~~~~~~~~~~~~~~~~~~~~~~~
  67. -- UniqSet has the following invariant:
  68. -- The keys in the map are the uniques of the values
  69. -- It means that to implement mapUniqSet you'd have to update
  70. -- both the keys and the values. There used to be an implementation
  71. -- that only updated the values and it's been removed, because it broke
  72. -- the invariant.
  73. type UniqSet a = UniqFM a
  74. emptyUniqSet = emptyUFM
  75. unitUniqSet x = unitUFM x x
  76. mkUniqSet = foldl addOneToUniqSet emptyUniqSet
  77. addOneToUniqSet set x = addToUFM set x x
  78. addOneToUniqSet_C f set x = addToUFM_C f set x x
  79. addListToUniqSet = foldl addOneToUniqSet
  80. delOneFromUniqSet = delFromUFM
  81. delOneFromUniqSet_Directly = delFromUFM_Directly
  82. delListFromUniqSet = delListFromUFM
  83. unionUniqSets = plusUFM
  84. unionManyUniqSets [] = emptyUniqSet
  85. unionManyUniqSets sets = foldr1 unionUniqSets sets
  86. minusUniqSet = minusUFM
  87. intersectUniqSets = intersectUFM
  88. elementOfUniqSet = elemUFM
  89. elemUniqSet_Directly = elemUFM_Directly
  90. filterUniqSet = filterUFM
  91. partitionUniqSet = partitionUFM
  92. sizeUniqSet = sizeUFM
  93. isEmptyUniqSet = isNullUFM
  94. lookupUniqSet = lookupUFM
  95. uniqSetAny :: (a -> Bool) -> UniqSet a -> Bool
  96. uniqSetAny = anyUFM
  97. uniqSetAll :: (a -> Bool) -> UniqSet a -> Bool
  98. uniqSetAll = allUFM