2 % (c) The University of Glasgow 2006
3 % (c) The AQUA Project, Glasgow University, 1994-1998
5 \section[UniqSet]{Specialised sets, for things with @Uniques@}
7 Based on @UniqFMs@ (as you would expect).
9 Basically, the things need to be in class @Uniquable@.
13 UniqSet, -- abstract type: NOT
15 mkUniqSet, uniqSetToList, emptyUniqSet, unitUniqSet,
16 addOneToUniqSet, addListToUniqSet,
17 delOneFromUniqSet, delListFromUniqSet,
18 unionUniqSets, unionManyUniqSets, minusUniqSet,
19 elementOfUniqSet, mapUniqSet, intersectUniqSets,
20 isEmptyUniqSet, filterUniqSet, sizeUniqSet, foldUniqSet,
21 elemUniqSet_Directly, lookupUniqSet, hashUniqSet
28 #if ! OMIT_NATIVE_CODEGEN
31 #define IF_NCG(a) {--}
35 %************************************************************************
37 \subsection{The @UniqSet@ type}
39 %************************************************************************
41 We use @UniqFM@, with a (@getUnique@-able) @Unique@ as ``key''
42 and the thing itself as the ``value'' (for later retrieval).
45 --data UniqSet a = MkUniqSet (FiniteMap Unique a) : NOT
47 type UniqSet a = UniqFM a
48 #define MkUniqSet {--}
50 emptyUniqSet :: UniqSet a
51 emptyUniqSet = MkUniqSet emptyUFM
53 unitUniqSet :: Uniquable a => a -> UniqSet a
54 unitUniqSet x = MkUniqSet (unitUFM x x)
56 uniqSetToList :: UniqSet a -> [a]
57 uniqSetToList (MkUniqSet set) = eltsUFM set
59 foldUniqSet :: (a -> b -> b) -> b -> UniqSet a -> b
60 foldUniqSet k z (MkUniqSet set) = foldUFM k z set
62 mkUniqSet :: Uniquable a => [a] -> UniqSet a
63 mkUniqSet xs = MkUniqSet (listToUFM [ (x, x) | x <- xs])
65 addOneToUniqSet :: Uniquable a => UniqSet a -> a -> UniqSet a
66 addOneToUniqSet (MkUniqSet set) x = MkUniqSet (addToUFM set x x)
68 delOneFromUniqSet :: Uniquable a => UniqSet a -> a -> UniqSet a
69 delOneFromUniqSet (MkUniqSet set) x = MkUniqSet (delFromUFM set x)
71 delListFromUniqSet :: Uniquable a => UniqSet a -> [a] -> UniqSet a
72 delListFromUniqSet (MkUniqSet set) xs = MkUniqSet (delListFromUFM set xs)
74 addListToUniqSet :: Uniquable a => UniqSet a -> [a] -> UniqSet a
75 addListToUniqSet (MkUniqSet set) xs = MkUniqSet (addListToUFM set [(x,x) | x<-xs])
77 unionUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
78 unionUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (plusUFM set1 set2)
80 unionManyUniqSets :: [UniqSet a] -> UniqSet a
81 -- = foldr unionUniqSets emptyUniqSet ss
82 unionManyUniqSets [] = emptyUniqSet
83 unionManyUniqSets [s] = s
84 unionManyUniqSets (s:ss) = s `unionUniqSets` unionManyUniqSets ss
86 minusUniqSet :: UniqSet a -> UniqSet a -> UniqSet a
87 minusUniqSet (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (minusUFM set1 set2)
89 filterUniqSet :: (a -> Bool) -> UniqSet a -> UniqSet a
90 filterUniqSet pred (MkUniqSet set) = MkUniqSet (filterUFM pred set)
92 intersectUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
93 intersectUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (intersectUFM set1 set2)
95 elementOfUniqSet :: Uniquable a => a -> UniqSet a -> Bool
96 elementOfUniqSet x (MkUniqSet set) = maybeToBool (lookupUFM set x)
98 lookupUniqSet :: Uniquable a => UniqSet a -> a -> Maybe a
99 lookupUniqSet (MkUniqSet set) x = lookupUFM set x
101 elemUniqSet_Directly :: Unique -> UniqSet a -> Bool
102 elemUniqSet_Directly x (MkUniqSet set) = maybeToBool (lookupUFM_Directly set x)
104 sizeUniqSet :: UniqSet a -> Int
105 sizeUniqSet (MkUniqSet set) = sizeUFM set
107 hashUniqSet :: UniqSet a -> Int
108 hashUniqSet (MkUniqSet set) = hashUFM set
110 isEmptyUniqSet :: UniqSet a -> Bool
111 isEmptyUniqSet (MkUniqSet set) = isNullUFM set {-SLOW: sizeUFM set == 0-}
113 mapUniqSet :: (a -> a) -> UniqSet a -> UniqSet a
114 -- VERY IMPORTANT: *assumes* that the function doesn't change the unique
115 mapUniqSet f (MkUniqSet set) = MkUniqSet (mapUFM f set)
119 #ifdef __GLASGOW_HASKELL__
121 addOneToUniqSet :: UniqSet Unique -> Unique -> UniqSet Unique
124 -- These next three specialisations disabled as importing Name creates a
125 -- loop, and getting the Uniquable Name instance in particular is tricky.
128 elementOfUniqSet :: Name -> UniqSet Name -> Bool
129 , Unique -> UniqSet Unique -> Bool
132 mkUniqSet :: [Name] -> UniqSet Name
136 unitUniqSet :: Name -> UniqSet Name
137 , Unique -> UniqSet Unique