[project @ 1996-12-19 09:10:02 by simonpj]
[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       ( Outputable(..) )
30 import Pretty           ( SYN_IE(Pretty), PrettyRep )
31 import PprStyle         ( PprStyle )
32 import Util             ( Ord3(..) )
33
34 #if ! OMIT_NATIVE_CODEGEN
35 #define IF_NCG(a) a
36 #else
37 #define IF_NCG(a) {--}
38 #endif
39 \end{code}
40
41 %************************************************************************
42 %*                                                                      *
43 \subsection{The @UniqSet@ type}
44 %*                                                                      *
45 %************************************************************************
46
47 We use @UniqFM@, with a (@uniqueOf@-able) @Unique@ as ``key''
48 and the thing itself as the ``value'' (for later retrieval).
49
50 \begin{code}
51 --data UniqSet a = MkUniqSet (FiniteMap Unique a) : NOT
52
53 type UniqSet a = UniqFM a
54 #define MkUniqSet {--}
55
56 emptyUniqSet :: UniqSet a
57 emptyUniqSet = MkUniqSet emptyUFM
58
59 unitUniqSet :: Uniquable a => a -> UniqSet a
60 unitUniqSet x = MkUniqSet (unitUFM x x)
61
62 uniqSetToList :: UniqSet a -> [a]
63 uniqSetToList (MkUniqSet set) = eltsUFM 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 addListToUniqSet :: Uniquable a => UniqSet a -> [a] -> UniqSet a
72 addListToUniqSet (MkUniqSet set) xs = MkUniqSet (addListToUFM set [(x,x) | x<-xs])
73
74 unionUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
75 unionUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (plusUFM set1 set2)
76
77 unionManyUniqSets :: [UniqSet a] -> UniqSet a
78         -- = foldr unionUniqSets emptyUniqSet ss
79 unionManyUniqSets []     = emptyUniqSet
80 unionManyUniqSets [s]    = s
81 unionManyUniqSets (s:ss) = s `unionUniqSets` unionManyUniqSets ss
82
83 minusUniqSet  :: UniqSet a -> UniqSet a -> UniqSet a
84 minusUniqSet (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (minusUFM set1 set2)
85
86 filterUniqSet :: (a -> Bool) -> UniqSet a -> UniqSet a
87 filterUniqSet pred (MkUniqSet set) = MkUniqSet (filterUFM pred set)
88
89 intersectUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
90 intersectUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (intersectUFM set1 set2)
91
92 elementOfUniqSet :: Uniquable a => a -> UniqSet a -> Bool
93 elementOfUniqSet x (MkUniqSet set) = maybeToBool (lookupUFM set x)
94
95 sizeUniqSet :: UniqSet a -> Int
96 sizeUniqSet (MkUniqSet set) = sizeUFM set
97
98 isEmptyUniqSet :: UniqSet a -> Bool
99 isEmptyUniqSet (MkUniqSet set) = isNullUFM set {-SLOW: sizeUFM set == 0-}
100
101 mapUniqSet :: Uniquable b => (a -> b) -> UniqSet a -> UniqSet b
102 mapUniqSet f (MkUniqSet set)
103   = MkUniqSet (listToUFM [ let
104                              mapped_thing = f thing
105                           in
106                           (mapped_thing, mapped_thing)
107                         | thing <- eltsUFM set ])
108 \end{code}
109
110 \begin{code}
111 #if __GLASGOW_HASKELL__
112 {-# SPECIALIZE
113     addOneToUniqSet :: UniqSet Unique -> Unique -> UniqSet Unique
114     #-}
115 {-# SPECIALIZE
116     elementOfUniqSet :: Name -> UniqSet Name -> Bool
117                       , Unique -> UniqSet Unique -> Bool
118     #-}
119 {-# SPECIALIZE
120     mkUniqSet :: [Name] -> UniqSet Name
121     #-}
122
123 {-# SPECIALIZE
124     unitUniqSet :: Name -> UniqSet Name
125                  , Unique -> UniqSet Unique
126     #-}
127 #endif
128 \end{code}