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,
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 addListToUniqSet :: Uniquable a => UniqSet a -> [a] -> UniqSet a
74 addListToUniqSet (MkUniqSet set) xs = MkUniqSet (addListToUFM set [(x,x) | x<-xs])
76 unionUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
77 unionUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (plusUFM set1 set2)
79 unionManyUniqSets :: [UniqSet a] -> UniqSet a
80 -- = foldr unionUniqSets emptyUniqSet ss
81 unionManyUniqSets [] = emptyUniqSet
82 unionManyUniqSets [s] = s
83 unionManyUniqSets (s:ss) = s `unionUniqSets` unionManyUniqSets ss
85 minusUniqSet :: UniqSet a -> UniqSet a -> UniqSet a
86 minusUniqSet (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (minusUFM set1 set2)
88 filterUniqSet :: (a -> Bool) -> UniqSet a -> UniqSet a
89 filterUniqSet pred (MkUniqSet set) = MkUniqSet (filterUFM pred set)
91 intersectUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
92 intersectUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (intersectUFM set1 set2)
94 elementOfUniqSet :: Uniquable a => a -> UniqSet a -> Bool
95 elementOfUniqSet x (MkUniqSet set) = maybeToBool (lookupUFM set x)
97 lookupUniqSet :: Uniquable a => UniqSet a -> a -> Maybe a
98 lookupUniqSet (MkUniqSet set) x = lookupUFM set x
100 elemUniqSet_Directly :: Unique -> UniqSet a -> Bool
101 elemUniqSet_Directly x (MkUniqSet set) = maybeToBool (lookupUFM_Directly set x)
103 sizeUniqSet :: UniqSet a -> Int
104 sizeUniqSet (MkUniqSet set) = sizeUFM set
106 hashUniqSet :: UniqSet a -> Int
107 hashUniqSet (MkUniqSet set) = hashUFM set
109 isEmptyUniqSet :: UniqSet a -> Bool
110 isEmptyUniqSet (MkUniqSet set) = isNullUFM set {-SLOW: sizeUFM set == 0-}
112 mapUniqSet :: (a -> a) -> UniqSet a -> UniqSet a
113 -- VERY IMPORTANT: *assumes* that the function doesn't change the unique
114 mapUniqSet f (MkUniqSet set) = MkUniqSet (mapUFM f set)
118 #if __GLASGOW_HASKELL__
120 addOneToUniqSet :: UniqSet Unique -> Unique -> UniqSet Unique
123 elementOfUniqSet :: Name -> UniqSet Name -> Bool
124 , Unique -> UniqSet Unique -> Bool
127 mkUniqSet :: [Name] -> UniqSet Name
131 unitUniqSet :: Name -> UniqSet Name
132 , Unique -> UniqSet Unique