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 ( PprStyle, Outputable(..) )
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 (@uniqueOf@-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 unitUniqSet :: Uniquable a => a -> UniqSet a
59 unitUniqSet x = MkUniqSet (unitUFM x x)
61 uniqSetToList :: UniqSet a -> [a]
62 uniqSetToList (MkUniqSet set) = eltsUFM set
64 mkUniqSet :: Uniquable a => [a] -> UniqSet a
65 mkUniqSet xs = MkUniqSet (listToUFM [ (x, x) | x <- xs])
67 addOneToUniqSet :: Uniquable a => UniqSet a -> a -> UniqSet a
68 addOneToUniqSet (MkUniqSet set) x = MkUniqSet (addToUFM set x x)
70 addListToUniqSet :: Uniquable a => UniqSet a -> [a] -> UniqSet a
71 addListToUniqSet (MkUniqSet set) xs = MkUniqSet (addListToUFM set [(x,x) | x<-xs])
73 unionUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
74 unionUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (plusUFM set1 set2)
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
82 minusUniqSet :: UniqSet a -> UniqSet a -> UniqSet a
83 minusUniqSet (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (minusUFM set1 set2)
85 filterUniqSet :: (a -> Bool) -> UniqSet a -> UniqSet a
86 filterUniqSet pred (MkUniqSet set) = MkUniqSet (filterUFM pred set)
88 intersectUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
89 intersectUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (intersectUFM set1 set2)
91 elementOfUniqSet :: Uniquable a => a -> UniqSet a -> Bool
92 elementOfUniqSet x (MkUniqSet set) = maybeToBool (lookupUFM set x)
94 sizeUniqSet :: UniqSet a -> Int
95 sizeUniqSet (MkUniqSet set) = sizeUFM set
97 isEmptyUniqSet :: UniqSet a -> Bool
98 isEmptyUniqSet (MkUniqSet set) = isNullUFM set {-SLOW: sizeUFM set == 0-}
100 mapUniqSet :: Uniquable b => (a -> b) -> UniqSet a -> UniqSet b
101 mapUniqSet f (MkUniqSet set)
102 = MkUniqSet (listToUFM [ let
103 mapped_thing = f thing
105 (mapped_thing, mapped_thing)
106 | thing <- eltsUFM set ])
110 #if __GLASGOW_HASKELL__
112 addOneToUniqSet :: UniqSet Unique -> Unique -> UniqSet Unique
115 elementOfUniqSet :: Name -> UniqSet Name -> Bool
116 , Unique -> UniqSet Unique -> Bool
119 mkUniqSet :: [Name] -> UniqSet Name
123 unitUniqSet :: Name -> UniqSet Name
124 , Unique -> UniqSet Unique