2 % (c) The AQUA Project, Glasgow University, 1994-1996
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@.
11 #include "HsVersions.h"
14 UniqSet(..), -- abstract type: NOT
16 mkUniqSet, uniqSetToList, emptyUniqSet, singletonUniqSet,
17 unionUniqSets, unionManyUniqSets, minusUniqSet,
18 elementOfUniqSet, mapUniqSet, intersectUniqSets,
22 CHK_Ubiq() -- debugging consistency check
24 import Maybes ( maybeToBool, Maybe )
26 import Unique ( Unique )
27 import Outputable ( Outputable(..), NamedThing(..), ExportFlag )
28 import SrcLoc ( SrcLoc )
29 import Pretty ( Pretty(..), PrettyRep )
30 import PprStyle ( PprStyle )
31 import Util ( Ord3(..) )
33 #if ! OMIT_NATIVE_CODEGEN
36 #define IF_NCG(a) {--}
40 %************************************************************************
42 \subsection{The @UniqSet@ type}
44 %************************************************************************
46 We use @UniqFM@, with a (@getItsUnique@-able) @Unique@ as ``key''
47 and the thing itself as the ``value'' (for later retrieval).
50 --data UniqSet a = MkUniqSet (FiniteMap Unique a) : NOT
52 type UniqSet a = UniqFM a
53 #define MkUniqSet {--}
55 emptyUniqSet :: UniqSet a
56 emptyUniqSet = MkUniqSet emptyUFM
58 singletonUniqSet :: NamedThing a => a -> UniqSet a
59 singletonUniqSet x = MkUniqSet (singletonUFM x x)
61 uniqSetToList :: UniqSet a -> [a]
62 uniqSetToList (MkUniqSet set) = eltsUFM set
64 mkUniqSet :: NamedThing a => [a] -> UniqSet a
65 mkUniqSet xs = MkUniqSet (listToUFM [ (x, x) | x <- xs])
67 unionUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
68 unionUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (plusUFM set1 set2)
70 unionManyUniqSets :: [UniqSet a] -> UniqSet a
71 -- = foldr unionUniqSets emptyUniqSet ss
72 unionManyUniqSets [] = emptyUniqSet
73 unionManyUniqSets [s] = s
74 unionManyUniqSets (s:ss) = s `unionUniqSets` unionManyUniqSets ss
76 minusUniqSet :: UniqSet a -> UniqSet a -> UniqSet a
77 minusUniqSet (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (minusUFM set1 set2)
79 intersectUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
80 intersectUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (intersectUFM set1 set2)
82 elementOfUniqSet :: NamedThing a => a -> UniqSet a -> Bool
83 elementOfUniqSet x (MkUniqSet set) = maybeToBool (lookupUFM set x)
85 isEmptyUniqSet :: UniqSet a -> Bool
86 isEmptyUniqSet (MkUniqSet set) = isNullUFM set {-SLOW: sizeUFM set == 0-}
88 mapUniqSet :: NamedThing b => (a -> b) -> UniqSet a -> UniqSet b
89 mapUniqSet f (MkUniqSet set)
90 = MkUniqSet (listToUFM [ let
91 mapped_thing = f thing
93 (mapped_thing, mapped_thing)
94 | thing <- eltsUFM set ])
97 %************************************************************************
99 \subsection{The @IdSet@ and @TyVarSet@ specialisations for sets of Ids/TyVars}
101 %************************************************************************
103 @IdSet@ is a specialised version, optimised for sets of Ids.
106 --type NameSet = UniqSet Name
107 --type GenTyVarSet flexi = UniqSet (GenTyVar flexi)
108 --type GenIdSet ty = UniqSet (GenId ty)
110 #if ! OMIT_NATIVE_CODEGEN
111 --type RegSet = UniqSet Reg
115 #if __GLASGOW_HASKELL__
117 singletonUniqSet :: GenId ty -> GenIdSet ty,
118 GenTyVar flexi -> GenTyVarSet flexi,
120 IF_NCG(COMMA Reg -> RegSet)
124 mkUniqSet :: [GenId ty] -> GenIdSet ty,
125 [GenTyVar flexi] -> GenTyVarSet flexi,
127 IF_NCG(COMMA [Reg] -> RegSet)
131 elementOfUniqSet :: GenId ty -> GenIdSet ty -> Bool,
132 GenTyVar flexi -> GenTyVarSet flexi -> Bool,
133 Name -> NameSet -> Bool
134 IF_NCG(COMMA Reg -> RegSet -> Bool)
138 mapUniqSet :: (GenId ty -> GenId ty) -> GenIdSet ty -> GenIdSet ty,
139 (GenTyVar flexi -> GenTyVar flexi) -> GenTyVarSet flexi -> GenTyVarSet flexi,
140 (Name -> Name) -> NameSet -> NameSet
141 IF_NCG(COMMA (Reg -> Reg) -> RegSet -> RegSet)