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 @Uniquable@.
11 #include "HsVersions.h"
14 SYN_IE(UniqSet), -- abstract type: NOT
16 mkUniqSet, uniqSetToList, emptyUniqSet, unitUniqSet,
17 addOneToUniqSet, addListToUniqSet,
18 unionUniqSets, unionManyUniqSets, minusUniqSet,
19 elementOfUniqSet, mapUniqSet, intersectUniqSets,
20 isEmptyUniqSet, filterUniqSet, sizeUniqSet
23 IMPORT_DELOOPER( SpecLoop )
25 import Maybes ( maybeToBool )
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(..) )
34 #if ! OMIT_NATIVE_CODEGEN
37 #define IF_NCG(a) {--}
41 %************************************************************************
43 \subsection{The @UniqSet@ type}
45 %************************************************************************
47 We use @UniqFM@, with a (@uniqueOf@-able) @Unique@ as ``key''
48 and the thing itself as the ``value'' (for later retrieval).
51 --data UniqSet a = MkUniqSet (FiniteMap Unique a) : NOT
53 type UniqSet a = UniqFM a
54 #define MkUniqSet {--}
56 emptyUniqSet :: UniqSet a
57 emptyUniqSet = MkUniqSet emptyUFM
59 unitUniqSet :: Uniquable a => a -> UniqSet a
60 unitUniqSet x = MkUniqSet (unitUFM x x)
62 uniqSetToList :: UniqSet a -> [a]
63 uniqSetToList (MkUniqSet set) = eltsUFM set
65 mkUniqSet :: Uniquable a => [a] -> UniqSet a
66 mkUniqSet xs = MkUniqSet (listToUFM [ (x, x) | x <- xs])
68 addOneToUniqSet :: Uniquable a => UniqSet a -> a -> UniqSet a
69 addOneToUniqSet (MkUniqSet set) x = MkUniqSet (addToUFM set x x)
71 addListToUniqSet :: Uniquable a => UniqSet a -> [a] -> UniqSet a
72 addListToUniqSet (MkUniqSet set) xs = MkUniqSet (addListToUFM set [(x,x) | x<-xs])
74 unionUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
75 unionUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (plusUFM set1 set2)
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
83 minusUniqSet :: UniqSet a -> UniqSet a -> UniqSet a
84 minusUniqSet (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (minusUFM set1 set2)
86 filterUniqSet :: (a -> Bool) -> UniqSet a -> UniqSet a
87 filterUniqSet pred (MkUniqSet set) = MkUniqSet (filterUFM pred set)
89 intersectUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
90 intersectUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (intersectUFM set1 set2)
92 elementOfUniqSet :: Uniquable a => a -> UniqSet a -> Bool
93 elementOfUniqSet x (MkUniqSet set) = maybeToBool (lookupUFM set x)
95 sizeUniqSet :: UniqSet a -> Int
96 sizeUniqSet (MkUniqSet set) = sizeUFM set
98 isEmptyUniqSet :: UniqSet a -> Bool
99 isEmptyUniqSet (MkUniqSet set) = isNullUFM set {-SLOW: sizeUFM set == 0-}
101 mapUniqSet :: Uniquable b => (a -> b) -> UniqSet a -> UniqSet b
102 mapUniqSet f (MkUniqSet set)
103 = MkUniqSet (listToUFM [ let
104 mapped_thing = f thing
106 (mapped_thing, mapped_thing)
107 | thing <- eltsUFM set ])
111 #if __GLASGOW_HASKELL__
113 addOneToUniqSet :: UniqSet Unique -> Unique -> UniqSet Unique
116 elementOfUniqSet :: Name -> UniqSet Name -> Bool
117 , Unique -> UniqSet Unique -> Bool
120 mkUniqSet :: [Name] -> UniqSet Name
124 unitUniqSet :: Name -> UniqSet Name
125 , Unique -> UniqSet Unique