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@.
12 UniqSet, -- abstract type: NOT
14 mkUniqSet, uniqSetToList, emptyUniqSet, unitUniqSet,
15 addOneToUniqSet, addListToUniqSet,
16 unionUniqSets, unionManyUniqSets, minusUniqSet,
17 elementOfUniqSet, mapUniqSet, intersectUniqSets,
18 isEmptyUniqSet, filterUniqSet, sizeUniqSet
21 #include "HsVersions.h"
23 import {-# SOURCE #-} Name
25 import Maybes ( maybeToBool )
27 import Unique ( Unique, Uniquable(..) )
28 import SrcLoc ( SrcLoc )
29 import Outputable ( Outputable(..) )
31 #if ! OMIT_NATIVE_CODEGEN
34 #define IF_NCG(a) {--}
38 %************************************************************************
40 \subsection{The @UniqSet@ type}
42 %************************************************************************
44 We use @UniqFM@, with a (@uniqueOf@-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 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 addListToUniqSet :: Uniquable a => UniqSet a -> [a] -> UniqSet a
69 addListToUniqSet (MkUniqSet set) xs = MkUniqSet (addListToUFM set [(x,x) | x<-xs])
71 unionUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
72 unionUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (plusUFM set1 set2)
74 unionManyUniqSets :: [UniqSet a] -> UniqSet a
75 -- = foldr unionUniqSets emptyUniqSet ss
76 unionManyUniqSets [] = emptyUniqSet
77 unionManyUniqSets [s] = s
78 unionManyUniqSets (s:ss) = s `unionUniqSets` unionManyUniqSets ss
80 minusUniqSet :: UniqSet a -> UniqSet a -> UniqSet a
81 minusUniqSet (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (minusUFM set1 set2)
83 filterUniqSet :: (a -> Bool) -> UniqSet a -> UniqSet a
84 filterUniqSet pred (MkUniqSet set) = MkUniqSet (filterUFM pred set)
86 intersectUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
87 intersectUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (intersectUFM set1 set2)
89 elementOfUniqSet :: Uniquable a => a -> UniqSet a -> Bool
90 elementOfUniqSet x (MkUniqSet set) = maybeToBool (lookupUFM set x)
92 sizeUniqSet :: UniqSet a -> Int
93 sizeUniqSet (MkUniqSet set) = sizeUFM set
95 isEmptyUniqSet :: UniqSet a -> Bool
96 isEmptyUniqSet (MkUniqSet set) = isNullUFM set {-SLOW: sizeUFM set == 0-}
98 mapUniqSet :: Uniquable b => (a -> b) -> UniqSet a -> UniqSet b
99 mapUniqSet f (MkUniqSet set)
100 = MkUniqSet (listToUFM [ let
101 mapped_thing = f thing
103 (mapped_thing, mapped_thing)
104 | thing <- eltsUFM set ])
108 #if __GLASGOW_HASKELL__
110 addOneToUniqSet :: UniqSet Unique -> Unique -> UniqSet Unique
113 elementOfUniqSet :: Name -> UniqSet Name -> Bool
114 , Unique -> UniqSet Unique -> Bool
117 mkUniqSet :: [Name] -> UniqSet Name
121 unitUniqSet :: Name -> UniqSet Name
122 , Unique -> UniqSet Unique