[project @ 1996-06-26 10:26:00 by partain]
[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 import {-hide from mkdependHS-}
34         RnHsSyn ( RnName ) -- specialising only
35
36 #if ! OMIT_NATIVE_CODEGEN
37 #define IF_NCG(a) a
38 #else
39 #define IF_NCG(a) {--}
40 #endif
41 \end{code}
42
43 %************************************************************************
44 %*                                                                      *
45 \subsection{The @UniqSet@ type}
46 %*                                                                      *
47 %************************************************************************
48
49 We use @UniqFM@, with a (@uniqueOf@-able) @Unique@ as ``key''
50 and the thing itself as the ``value'' (for later retrieval).
51
52 \begin{code}
53 --data UniqSet a = MkUniqSet (FiniteMap Unique a) : NOT
54
55 type UniqSet a = UniqFM a
56 #define MkUniqSet {--}
57
58 emptyUniqSet :: UniqSet a
59 emptyUniqSet = MkUniqSet emptyUFM
60
61 unitUniqSet :: Uniquable a => a -> UniqSet a
62 unitUniqSet x = MkUniqSet (unitUFM x x)
63
64 uniqSetToList :: UniqSet a -> [a]
65 uniqSetToList (MkUniqSet set) = eltsUFM set
66
67 mkUniqSet :: Uniquable a => [a]  -> UniqSet a
68 mkUniqSet xs = MkUniqSet (listToUFM [ (x, x) | x <- xs])
69
70 addOneToUniqSet :: Uniquable a => UniqSet a -> a -> UniqSet a
71 addOneToUniqSet set x = set `unionUniqSets` unitUniqSet x
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 intersectUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
86 intersectUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (intersectUFM set1 set2)
87
88 elementOfUniqSet :: Uniquable a => a -> UniqSet a -> Bool
89 elementOfUniqSet x (MkUniqSet set) = maybeToBool (lookupUFM set x)
90
91 isEmptyUniqSet :: UniqSet a -> Bool
92 isEmptyUniqSet (MkUniqSet set) = isNullUFM set {-SLOW: sizeUFM set == 0-}
93
94 mapUniqSet :: Uniquable b => (a -> b) -> UniqSet a -> UniqSet b
95 mapUniqSet f (MkUniqSet set)
96   = MkUniqSet (listToUFM [ let
97                              mapped_thing = f thing
98                           in
99                           (mapped_thing, mapped_thing)
100                         | thing <- eltsUFM set ])
101 \end{code}
102
103 \begin{code}
104 #if __GLASGOW_HASKELL__
105 {-# SPECIALIZE
106     addOneToUniqSet :: UniqSet Unique -> Unique -> UniqSet Unique
107     #-}
108 {-# SPECIALIZE
109     elementOfUniqSet :: RnName -> UniqSet RnName -> Bool
110                       , Unique -> UniqSet Unique -> Bool
111     #-}
112 {-# SPECIALIZE
113     mkUniqSet :: [RnName] -> UniqSet RnName
114     #-}
115
116 {-# SPECIALIZE
117     unitUniqSet :: RnName -> UniqSet RnName
118                  , Unique -> UniqSet Unique
119     #-}
120 #endif
121 \end{code}