Module header tidyup #2
[ghc-hetmet.git] / compiler / utils / UniqSet.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The AQUA Project, Glasgow University, 1994-1998
4 %
5 \section[UniqSet]{Specialised sets, for things with @Uniques@}
6
7 Based on @UniqFMs@ (as you would expect).
8
9 Basically, the things need to be in class @Uniquable@.
10
11 \begin{code}
12 module UniqSet (
13         UniqSet,    -- abstract type: NOT
14
15         mkUniqSet, uniqSetToList, emptyUniqSet, unitUniqSet,
16         addOneToUniqSet, addListToUniqSet, delOneFromUniqSet, delListFromUniqSet,
17         unionUniqSets, unionManyUniqSets, minusUniqSet,
18         elementOfUniqSet, mapUniqSet, intersectUniqSets,
19         isEmptyUniqSet, filterUniqSet, sizeUniqSet, foldUniqSet,
20         elemUniqSet_Directly, lookupUniqSet, hashUniqSet
21     ) where
22
23 #include "HsVersions.h"
24
25 import {-# SOURCE #-} Name ( Name )
26
27 import Maybes           ( maybeToBool )
28 import UniqFM
29 import Unique           ( Unique, Uniquable(..) )
30
31 #if ! OMIT_NATIVE_CODEGEN
32 #define IF_NCG(a) a
33 #else
34 #define IF_NCG(a) {--}
35 #endif
36 \end{code}
37
38 %************************************************************************
39 %*                                                                      *
40 \subsection{The @UniqSet@ type}
41 %*                                                                      *
42 %************************************************************************
43
44 We use @UniqFM@, with a (@getUnique@-able) @Unique@ as ``key''
45 and the thing itself as the ``value'' (for later retrieval).
46
47 \begin{code}
48 --data UniqSet a = MkUniqSet (FiniteMap Unique a) : NOT
49
50 type UniqSet a = UniqFM a
51 #define MkUniqSet {--}
52
53 emptyUniqSet :: UniqSet a
54 emptyUniqSet = MkUniqSet emptyUFM
55
56 unitUniqSet :: Uniquable a => a -> UniqSet a
57 unitUniqSet x = MkUniqSet (unitUFM x x)
58
59 uniqSetToList :: UniqSet a -> [a]
60 uniqSetToList (MkUniqSet set) = eltsUFM set
61
62 foldUniqSet :: (a -> b -> b) -> b -> UniqSet a -> b
63 foldUniqSet k z (MkUniqSet set) = foldUFM k z set 
64
65 mkUniqSet :: Uniquable a => [a]  -> UniqSet a
66 mkUniqSet xs = MkUniqSet (listToUFM [ (x, x) | x <- xs])
67
68 addOneToUniqSet :: Uniquable a => UniqSet a -> a -> UniqSet a
69 addOneToUniqSet (MkUniqSet set) x = MkUniqSet (addToUFM set x x)
70
71 delOneFromUniqSet :: Uniquable a => UniqSet a -> a -> UniqSet a
72 delOneFromUniqSet (MkUniqSet set) x = MkUniqSet (delFromUFM set x)
73
74 delListFromUniqSet :: Uniquable a => UniqSet a -> [a] -> UniqSet a
75 delListFromUniqSet (MkUniqSet set) xs = MkUniqSet (delListFromUFM set xs)
76
77 addListToUniqSet :: Uniquable a => UniqSet a -> [a] -> UniqSet a
78 addListToUniqSet (MkUniqSet set) xs = MkUniqSet (addListToUFM set [(x,x) | x<-xs])
79
80 unionUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
81 unionUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (plusUFM set1 set2)
82
83 unionManyUniqSets :: [UniqSet a] -> UniqSet a
84         -- = foldr unionUniqSets emptyUniqSet ss
85 unionManyUniqSets []     = emptyUniqSet
86 unionManyUniqSets [s]    = s
87 unionManyUniqSets (s:ss) = s `unionUniqSets` unionManyUniqSets ss
88
89 minusUniqSet  :: UniqSet a -> UniqSet a -> UniqSet a
90 minusUniqSet (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (minusUFM set1 set2)
91
92 filterUniqSet :: (a -> Bool) -> UniqSet a -> UniqSet a
93 filterUniqSet pred (MkUniqSet set) = MkUniqSet (filterUFM pred set)
94
95 intersectUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
96 intersectUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (intersectUFM set1 set2)
97
98 elementOfUniqSet :: Uniquable a => a -> UniqSet a -> Bool
99 elementOfUniqSet x (MkUniqSet set) = maybeToBool (lookupUFM set x)
100
101 lookupUniqSet :: Uniquable a => UniqSet a -> a -> Maybe a
102 lookupUniqSet (MkUniqSet set) x = lookupUFM set x
103
104 elemUniqSet_Directly :: Unique -> UniqSet a -> Bool
105 elemUniqSet_Directly x (MkUniqSet set) = maybeToBool (lookupUFM_Directly set x)
106
107 sizeUniqSet :: UniqSet a -> Int
108 sizeUniqSet (MkUniqSet set) = sizeUFM set
109
110 hashUniqSet :: UniqSet a -> Int
111 hashUniqSet (MkUniqSet set) = hashUFM set
112
113 isEmptyUniqSet :: UniqSet a -> Bool
114 isEmptyUniqSet (MkUniqSet set) = isNullUFM set {-SLOW: sizeUFM set == 0-}
115
116 mapUniqSet :: (a -> a) -> UniqSet a -> UniqSet a
117   -- VERY IMPORTANT: *assumes* that the function doesn't change the unique
118 mapUniqSet f (MkUniqSet set) = MkUniqSet (mapUFM f set)
119 \end{code}
120
121 \begin{code}
122 #if __GLASGOW_HASKELL__
123 {-# SPECIALIZE
124     addOneToUniqSet :: UniqSet Unique -> Unique -> UniqSet Unique
125     #-}
126 {- SPECIALIZE
127     elementOfUniqSet :: Name -> UniqSet Name -> Bool
128                       , Unique -> UniqSet Unique -> Bool
129     -}
130 {- SPECIALIZE
131     mkUniqSet :: [Name] -> UniqSet Name
132     -}
133
134 {- SPECIALIZE
135     unitUniqSet :: Name -> UniqSet Name
136                  , Unique -> UniqSet Unique
137     -}
138 #endif
139 \end{code}