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, delOneFromUniqSet, delListFromUniqSet,
17 unionUniqSets, unionManyUniqSets, minusUniqSet,
18 elementOfUniqSet, mapUniqSet, intersectUniqSets,
19 isEmptyUniqSet, filterUniqSet, sizeUniqSet, foldUniqSet,
20 elemUniqSet_Directly, lookupUniqSet, hashUniqSet
23 #include "HsVersions.h"
25 import {-# SOURCE #-} Name ( Name )
27 import Maybes ( maybeToBool )
29 import Unique ( Unique, Uniquable(..) )
31 #if ! OMIT_NATIVE_CODEGEN
34 #define IF_NCG(a) {--}
38 %************************************************************************
40 \subsection{The @UniqSet@ type}
42 %************************************************************************
44 We use @UniqFM@, with a (@getUnique@-able) @Unique@ as ``key''
45 and the thing itself as the ``value'' (for later retrieval).
48 --data UniqSet a = MkUniqSet (FiniteMap Unique a) : NOT
50 type UniqSet a = UniqFM a
51 #define MkUniqSet {--}
53 emptyUniqSet :: UniqSet a
54 emptyUniqSet = MkUniqSet emptyUFM
56 unitUniqSet :: Uniquable a => a -> UniqSet a
57 unitUniqSet x = MkUniqSet (unitUFM x x)
59 uniqSetToList :: UniqSet a -> [a]
60 uniqSetToList (MkUniqSet set) = eltsUFM set
62 foldUniqSet :: (a -> b -> b) -> b -> UniqSet a -> b
63 foldUniqSet k z (MkUniqSet set) = foldUFM k z 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 delOneFromUniqSet :: Uniquable a => UniqSet a -> a -> UniqSet a
72 delOneFromUniqSet (MkUniqSet set) x = MkUniqSet (delFromUFM set x)
74 delListFromUniqSet :: Uniquable a => UniqSet a -> [a] -> UniqSet a
75 delListFromUniqSet (MkUniqSet set) xs = MkUniqSet (delListFromUFM set xs)
77 addListToUniqSet :: Uniquable a => UniqSet a -> [a] -> UniqSet a
78 addListToUniqSet (MkUniqSet set) xs = MkUniqSet (addListToUFM set [(x,x) | x<-xs])
80 unionUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
81 unionUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (plusUFM set1 set2)
83 unionManyUniqSets :: [UniqSet a] -> UniqSet a
84 -- = foldr unionUniqSets emptyUniqSet ss
85 unionManyUniqSets [] = emptyUniqSet
86 unionManyUniqSets [s] = s
87 unionManyUniqSets (s:ss) = s `unionUniqSets` unionManyUniqSets ss
89 minusUniqSet :: UniqSet a -> UniqSet a -> UniqSet a
90 minusUniqSet (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (minusUFM set1 set2)
92 filterUniqSet :: (a -> Bool) -> UniqSet a -> UniqSet a
93 filterUniqSet pred (MkUniqSet set) = MkUniqSet (filterUFM pred set)
95 intersectUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
96 intersectUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (intersectUFM set1 set2)
98 elementOfUniqSet :: Uniquable a => a -> UniqSet a -> Bool
99 elementOfUniqSet x (MkUniqSet set) = maybeToBool (lookupUFM set x)
101 lookupUniqSet :: Uniquable a => UniqSet a -> a -> Maybe a
102 lookupUniqSet (MkUniqSet set) x = lookupUFM set x
104 elemUniqSet_Directly :: Unique -> UniqSet a -> Bool
105 elemUniqSet_Directly x (MkUniqSet set) = maybeToBool (lookupUFM_Directly set x)
107 sizeUniqSet :: UniqSet a -> Int
108 sizeUniqSet (MkUniqSet set) = sizeUFM set
110 hashUniqSet :: UniqSet a -> Int
111 hashUniqSet (MkUniqSet set) = hashUFM set
113 isEmptyUniqSet :: UniqSet a -> Bool
114 isEmptyUniqSet (MkUniqSet set) = isNullUFM set {-SLOW: sizeUFM set == 0-}
116 mapUniqSet :: (a -> a) -> UniqSet a -> UniqSet a
117 -- VERY IMPORTANT: *assumes* that the function doesn't change the unique
118 mapUniqSet f (MkUniqSet set) = MkUniqSet (mapUFM f set)
122 #if __GLASGOW_HASKELL__
124 addOneToUniqSet :: UniqSet Unique -> Unique -> UniqSet Unique
127 elementOfUniqSet :: Name -> UniqSet Name -> Bool
128 , Unique -> UniqSet Unique -> Bool
131 mkUniqSet :: [Name] -> UniqSet Name
135 unitUniqSet :: Name -> UniqSet Name
136 , Unique -> UniqSet Unique