2 % (c) The AQUA Project, Glasgow University, 1994-1995
4 \section[UniqSet]{Specialised sets, for things with @Uniques@}
6 Based on @UniqFMs@ (as you would expect).
8 Basically, the things need to be in class @NamedThing@.
10 We also export specialisations for @Ids@ and @TyVars@.
13 #include "HsVersions.h"
16 UniqSet(..), -- abstract type: NOT
18 mkUniqSet, uniqSetToList, emptyUniqSet, singletonUniqSet,
19 unionUniqSets, unionManyUniqSets, minusUniqSet,
20 elementOfUniqSet, mapUniqSet,
21 intersectUniqSets, isEmptyUniqSet,
23 -- specalised for Ids:
26 -- specalised for TyVars:
29 -- specalised for Names:
32 -- to make the interface self-sufficient
37 -- and to be pragma friendly
38 #ifdef USE_ATTACK_PRAGMAS
39 , emptyUFM, intersectUFM, isNullUFM, minusUFM, singletonUFM,
46 import Id -- for specialisation to Ids
48 import Maybes ( maybeToBool, Maybe(..) )
51 import AbsUniType -- for specialisation to TyVars
53 #if ! OMIT_NATIVE_CODEGEN
54 import AsmRegAlloc ( Reg )
57 #define IF_NCG(a) {--}
61 %************************************************************************
63 \subsection{The @UniqSet@ type}
65 %************************************************************************
67 We use @UniqFM@, with a (@getTheUnique@-able) @Unique@ as ``key''
68 and the thing itself as the ``value'' (for later retrieval).
71 --data UniqSet a = MkUniqSet (FiniteMap Unique a) : NOT
73 type UniqSet a = UniqFM a
74 #define MkUniqSet {--}
76 emptyUniqSet :: UniqSet a
77 emptyUniqSet = MkUniqSet emptyUFM
79 singletonUniqSet :: NamedThing a => a -> UniqSet a
80 singletonUniqSet x = MkUniqSet (singletonUFM x x)
82 uniqSetToList :: UniqSet a -> [a]
83 uniqSetToList (MkUniqSet set) = BSCC("uniqSetToList") eltsUFM set ESCC
85 mkUniqSet :: NamedThing a => [a] -> UniqSet a
86 mkUniqSet xs = MkUniqSet (listToUFM [ (x, x) | x <- xs])
88 unionUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
89 unionUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (plusUFM set1 set2)
91 unionManyUniqSets :: [UniqSet a] -> UniqSet a
92 -- = foldr unionUniqSets emptyUniqSet ss
93 unionManyUniqSets [] = emptyUniqSet
94 unionManyUniqSets [s] = s
95 unionManyUniqSets (s:ss) = s `unionUniqSets` unionManyUniqSets ss
97 minusUniqSet :: UniqSet a -> UniqSet a -> UniqSet a
98 minusUniqSet (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (minusUFM set1 set2)
100 intersectUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
101 intersectUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (intersectUFM set1 set2)
103 elementOfUniqSet :: NamedThing a => a -> UniqSet a -> Bool
104 elementOfUniqSet x (MkUniqSet set) = maybeToBool (lookupUFM set x)
106 isEmptyUniqSet :: UniqSet a -> Bool
107 isEmptyUniqSet (MkUniqSet set) = isNullUFM set {-SLOW: sizeUFM set == 0-}
109 mapUniqSet :: NamedThing b => (a -> b) -> UniqSet a -> UniqSet b
110 mapUniqSet f (MkUniqSet set)
111 = MkUniqSet (listToUFM [ let
112 mapped_thing = f thing
114 (mapped_thing, mapped_thing)
115 | thing <- eltsUFM set ])
118 %************************************************************************
120 \subsection{The @IdSet@ and @TyVarSet@ specialisations for sets of Ids/TyVars}
122 %************************************************************************
124 @IdSet@ is a specialised version, optimised for sets of Ids.
127 type IdSet = UniqSet Id
128 type TyVarSet = UniqSet TyVar
129 type NameSet = UniqSet Name
130 #if ! OMIT_NATIVE_CODEGEN
131 type RegSet = UniqSet Reg
134 #if __GLASGOW_HASKELL__
135 -- avoid hbc bug (0.999.7)
137 singletonUniqSet :: Id -> IdSet,
140 IF_NCG(COMMA Reg -> RegSet)
144 mkUniqSet :: [Id] -> IdSet,
147 IF_NCG(COMMA [Reg] -> RegSet)
151 elementOfUniqSet :: Id -> IdSet -> Bool,
152 TyVar -> TyVarSet -> Bool,
153 Name -> NameSet -> Bool
154 IF_NCG(COMMA Reg -> RegSet -> Bool)
158 mapUniqSet :: (Id -> Id) -> IdSet -> IdSet,
159 (TyVar -> TyVar) -> TyVarSet -> TyVarSet,
160 (Name -> Name) -> NameSet -> NameSet
161 IF_NCG(COMMA (Reg -> Reg) -> RegSet -> RegSet)