3dc014e012ef4175da2721678e200ef0a71efc27
[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, addListToUniqSet,
18         unionUniqSets, unionManyUniqSets, minusUniqSet,
19         elementOfUniqSet, mapUniqSet, intersectUniqSets,
20         isEmptyUniqSet, filterUniqSet, sizeUniqSet
21     ) where
22
23 IMPORT_DELOOPER( SpecLoop )
24
25 import Maybes           ( maybeToBool )
26 import UniqFM
27 import Unique           ( Unique )
28 import SrcLoc           ( SrcLoc )
29 import Outputable       ( PprStyle, Outputable(..) )
30 import Pretty           ( Doc )
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 (MkUniqSet set) x = MkUniqSet (addToUFM set x x)
69
70 addListToUniqSet :: Uniquable a => UniqSet a -> [a] -> UniqSet a
71 addListToUniqSet (MkUniqSet set) xs = MkUniqSet (addListToUFM set [(x,x) | x<-xs])
72
73 unionUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
74 unionUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (plusUFM set1 set2)
75
76 unionManyUniqSets :: [UniqSet a] -> UniqSet a
77         -- = foldr unionUniqSets emptyUniqSet ss
78 unionManyUniqSets []     = emptyUniqSet
79 unionManyUniqSets [s]    = s
80 unionManyUniqSets (s:ss) = s `unionUniqSets` unionManyUniqSets ss
81
82 minusUniqSet  :: UniqSet a -> UniqSet a -> UniqSet a
83 minusUniqSet (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (minusUFM set1 set2)
84
85 filterUniqSet :: (a -> Bool) -> UniqSet a -> UniqSet a
86 filterUniqSet pred (MkUniqSet set) = MkUniqSet (filterUFM pred set)
87
88 intersectUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
89 intersectUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (intersectUFM set1 set2)
90
91 elementOfUniqSet :: Uniquable a => a -> UniqSet a -> Bool
92 elementOfUniqSet x (MkUniqSet set) = maybeToBool (lookupUFM set x)
93
94 sizeUniqSet :: UniqSet a -> Int
95 sizeUniqSet (MkUniqSet set) = sizeUFM set
96
97 isEmptyUniqSet :: UniqSet a -> Bool
98 isEmptyUniqSet (MkUniqSet set) = isNullUFM set {-SLOW: sizeUFM set == 0-}
99
100 mapUniqSet :: Uniquable b => (a -> b) -> UniqSet a -> UniqSet b
101 mapUniqSet f (MkUniqSet set)
102   = MkUniqSet (listToUFM [ let
103                              mapped_thing = f thing
104                           in
105                           (mapped_thing, mapped_thing)
106                         | thing <- eltsUFM set ])
107 \end{code}
108
109 \begin{code}
110 #if __GLASGOW_HASKELL__
111 {-# SPECIALIZE
112     addOneToUniqSet :: UniqSet Unique -> Unique -> UniqSet Unique
113     #-}
114 {-# SPECIALIZE
115     elementOfUniqSet :: Name -> UniqSet Name -> Bool
116                       , Unique -> UniqSet Unique -> Bool
117     #-}
118 {-# SPECIALIZE
119     mkUniqSet :: [Name] -> UniqSet Name
120     #-}
121
122 {-# SPECIALIZE
123     unitUniqSet :: Name -> UniqSet Name
124                  , Unique -> UniqSet Unique
125     #-}
126 #endif
127 \end{code}