2 % (c) The AQUA Project, Glasgow University, 1994-1998
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@.
12 UniqSet, -- abstract type: NOT
14 mkUniqSet, uniqSetToList, emptyUniqSet, unitUniqSet,
15 addOneToUniqSet, addListToUniqSet, delOneFromUniqSet, delListFromUniqSet,
16 unionUniqSets, unionManyUniqSets, minusUniqSet,
17 elementOfUniqSet, mapUniqSet, intersectUniqSets,
18 isEmptyUniqSet, filterUniqSet, sizeUniqSet, foldUniqSet,
19 elemUniqSet_Directly, lookupUniqSet, hashUniqSet
22 #include "HsVersions.h"
24 import {-# SOURCE #-} Name ( Name )
26 import Maybes ( maybeToBool )
28 import Unique ( Unique, Uniquable(..) )
30 #if ! OMIT_NATIVE_CODEGEN
33 #define IF_NCG(a) {--}
37 %************************************************************************
39 \subsection{The @UniqSet@ type}
41 %************************************************************************
43 We use @UniqFM@, with a (@getUnique@-able) @Unique@ as ``key''
44 and the thing itself as the ``value'' (for later retrieval).
47 --data UniqSet a = MkUniqSet (FiniteMap Unique a) : NOT
49 type UniqSet a = UniqFM a
50 #define MkUniqSet {--}
52 emptyUniqSet :: UniqSet a
53 emptyUniqSet = MkUniqSet emptyUFM
55 unitUniqSet :: Uniquable a => a -> UniqSet a
56 unitUniqSet x = MkUniqSet (unitUFM x x)
58 uniqSetToList :: UniqSet a -> [a]
59 uniqSetToList (MkUniqSet set) = eltsUFM set
61 foldUniqSet :: (a -> b -> b) -> b -> UniqSet a -> b
62 foldUniqSet k z (MkUniqSet set) = foldUFM k z 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 delOneFromUniqSet :: Uniquable a => UniqSet a -> a -> UniqSet a
71 delOneFromUniqSet (MkUniqSet set) x = MkUniqSet (delFromUFM set x)
73 delListFromUniqSet :: Uniquable a => UniqSet a -> [a] -> UniqSet a
74 delListFromUniqSet (MkUniqSet set) xs = MkUniqSet (delListFromUFM set xs)
76 addListToUniqSet :: Uniquable a => UniqSet a -> [a] -> UniqSet a
77 addListToUniqSet (MkUniqSet set) xs = MkUniqSet (addListToUFM set [(x,x) | x<-xs])
79 unionUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
80 unionUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (plusUFM set1 set2)
82 unionManyUniqSets :: [UniqSet a] -> UniqSet a
83 -- = foldr unionUniqSets emptyUniqSet ss
84 unionManyUniqSets [] = emptyUniqSet
85 unionManyUniqSets [s] = s
86 unionManyUniqSets (s:ss) = s `unionUniqSets` unionManyUniqSets ss
88 minusUniqSet :: UniqSet a -> UniqSet a -> UniqSet a
89 minusUniqSet (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (minusUFM set1 set2)
91 filterUniqSet :: (a -> Bool) -> UniqSet a -> UniqSet a
92 filterUniqSet pred (MkUniqSet set) = MkUniqSet (filterUFM pred set)
94 intersectUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
95 intersectUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (intersectUFM set1 set2)
97 elementOfUniqSet :: Uniquable a => a -> UniqSet a -> Bool
98 elementOfUniqSet x (MkUniqSet set) = maybeToBool (lookupUFM set x)
100 lookupUniqSet :: Uniquable a => UniqSet a -> a -> Maybe a
101 lookupUniqSet (MkUniqSet set) x = lookupUFM set x
103 elemUniqSet_Directly :: Unique -> UniqSet a -> Bool
104 elemUniqSet_Directly x (MkUniqSet set) = maybeToBool (lookupUFM_Directly set x)
106 sizeUniqSet :: UniqSet a -> Int
107 sizeUniqSet (MkUniqSet set) = sizeUFM set
109 hashUniqSet :: UniqSet a -> Int
110 hashUniqSet (MkUniqSet set) = hashUFM set
112 isEmptyUniqSet :: UniqSet a -> Bool
113 isEmptyUniqSet (MkUniqSet set) = isNullUFM set {-SLOW: sizeUFM set == 0-}
115 mapUniqSet :: (a -> a) -> UniqSet a -> UniqSet a
116 -- VERY IMPORTANT: *assumes* that the function doesn't change the unique
117 mapUniqSet f (MkUniqSet set) = MkUniqSet (mapUFM f set)
121 #if __GLASGOW_HASKELL__
123 addOneToUniqSet :: UniqSet Unique -> Unique -> UniqSet Unique
126 elementOfUniqSet :: Name -> UniqSet Name -> Bool
127 , Unique -> UniqSet Unique -> Bool
130 mkUniqSet :: [Name] -> UniqSet Name
134 unitUniqSet :: Name -> UniqSet Name
135 , Unique -> UniqSet Unique