5d892fb2e848483fb2e388337b2d75786edb6db1
[ghc-hetmet.git] / ghc / compiler / utils / UniqSet.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1994-1996
3 %
4 \section[UniqSet]{Specialised sets, for things with @Uniques@}
5
6 Based on @UniqFMs@ (as you would expect).
7
8 Basically, the things need to be in class @Uniquable@.
9
10 \begin{code}
11 #include "HsVersions.h"
12
13 module UniqSet (
14         SYN_IE(UniqSet),    -- abstract type: NOT
15
16         mkUniqSet, uniqSetToList, emptyUniqSet, unitUniqSet,
17         addOneToUniqSet,
18         unionUniqSets, unionManyUniqSets, minusUniqSet,
19         elementOfUniqSet, mapUniqSet, intersectUniqSets,
20         isEmptyUniqSet
21     ) where
22
23 IMP_Ubiq(){-uitous-}
24
25 import Maybes           ( maybeToBool )
26 import UniqFM
27 import Unique           ( Unique )
28 import SrcLoc           ( SrcLoc )
29 import Pretty           ( SYN_IE(Pretty), PrettyRep )
30 import PprStyle         ( PprStyle )
31 import Util             ( Ord3(..) )
32
33 #if ! OMIT_NATIVE_CODEGEN
34 #define IF_NCG(a) a
35 #else
36 #define IF_NCG(a) {--}
37 #endif
38 \end{code}
39
40 %************************************************************************
41 %*                                                                      *
42 \subsection{The @UniqSet@ type}
43 %*                                                                      *
44 %************************************************************************
45
46 We use @UniqFM@, with a (@uniqueOf@-able) @Unique@ as ``key''
47 and the thing itself as the ``value'' (for later retrieval).
48
49 \begin{code}
50 --data UniqSet a = MkUniqSet (FiniteMap Unique a) : NOT
51
52 type UniqSet a = UniqFM a
53 #define MkUniqSet {--}
54
55 emptyUniqSet :: UniqSet a
56 emptyUniqSet = MkUniqSet emptyUFM
57
58 unitUniqSet :: Uniquable a => a -> UniqSet a
59 unitUniqSet x = MkUniqSet (unitUFM x x)
60
61 uniqSetToList :: UniqSet a -> [a]
62 uniqSetToList (MkUniqSet set) = eltsUFM set
63
64 mkUniqSet :: Uniquable a => [a]  -> UniqSet a
65 mkUniqSet xs = MkUniqSet (listToUFM [ (x, x) | x <- xs])
66
67 addOneToUniqSet :: Uniquable a => UniqSet a -> a -> UniqSet a
68 addOneToUniqSet set x = set `unionUniqSets` unitUniqSet x
69
70 unionUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
71 unionUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (plusUFM set1 set2)
72
73 unionManyUniqSets :: [UniqSet a] -> UniqSet a
74         -- = foldr unionUniqSets emptyUniqSet ss
75 unionManyUniqSets []     = emptyUniqSet
76 unionManyUniqSets [s]    = s
77 unionManyUniqSets (s:ss) = s `unionUniqSets` unionManyUniqSets ss
78
79 minusUniqSet  :: UniqSet a -> UniqSet a -> UniqSet a
80 minusUniqSet (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (minusUFM set1 set2)
81
82 intersectUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
83 intersectUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (intersectUFM set1 set2)
84
85 elementOfUniqSet :: Uniquable a => a -> UniqSet a -> Bool
86 elementOfUniqSet x (MkUniqSet set) = maybeToBool (lookupUFM set x)
87
88 isEmptyUniqSet :: UniqSet a -> Bool
89 isEmptyUniqSet (MkUniqSet set) = isNullUFM set {-SLOW: sizeUFM set == 0-}
90
91 mapUniqSet :: Uniquable b => (a -> b) -> UniqSet a -> UniqSet b
92 mapUniqSet f (MkUniqSet set)
93   = MkUniqSet (listToUFM [ let
94                              mapped_thing = f thing
95                           in
96                           (mapped_thing, mapped_thing)
97                         | thing <- eltsUFM set ])
98 \end{code}
99
100 \begin{code}
101 #if __GLASGOW_HASKELL__
102 {-# SPECIALIZE
103     addOneToUniqSet :: UniqSet Unique -> Unique -> UniqSet Unique
104     #-}
105 {-# SPECIALIZE
106     elementOfUniqSet :: RnName -> UniqSet RnName -> Bool
107                       , Unique -> UniqSet Unique -> Bool
108     #-}
109 {-# SPECIALIZE
110     mkUniqSet :: [RnName] -> UniqSet RnName
111     #-}
112
113 {-# SPECIALIZE
114     unitUniqSet :: RnName -> UniqSet RnName
115                  , Unique -> UniqSet Unique
116     #-}
117 #endif
118 \end{code}