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@.
11 #include "HsVersions.h"
14 SYN_IE(UniqSet), -- abstract type: NOT
16 mkUniqSet, uniqSetToList, emptyUniqSet, unitUniqSet,
18 unionUniqSets, unionManyUniqSets, minusUniqSet,
19 elementOfUniqSet, mapUniqSet, intersectUniqSets,
25 import Maybes ( maybeToBool )
27 import Unique ( Unique )
28 import SrcLoc ( SrcLoc )
29 import Pretty ( SYN_IE(Pretty), PrettyRep )
30 import PprStyle ( PprStyle )
31 import Util ( Ord3(..) )
33 #if ! OMIT_NATIVE_CODEGEN
36 #define IF_NCG(a) {--}
40 %************************************************************************
42 \subsection{The @UniqSet@ type}
44 %************************************************************************
46 We use @UniqFM@, with a (@uniqueOf@-able) @Unique@ as ``key''
47 and the thing itself as the ``value'' (for later retrieval).
50 --data UniqSet a = MkUniqSet (FiniteMap Unique a) : NOT
52 type UniqSet a = UniqFM a
53 #define MkUniqSet {--}
55 emptyUniqSet :: UniqSet a
56 emptyUniqSet = MkUniqSet emptyUFM
58 unitUniqSet :: Uniquable a => a -> UniqSet a
59 unitUniqSet x = MkUniqSet (unitUFM x x)
61 uniqSetToList :: UniqSet a -> [a]
62 uniqSetToList (MkUniqSet set) = eltsUFM 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 set x = set `unionUniqSets` unitUniqSet x
70 unionUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
71 unionUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (plusUFM set1 set2)
73 unionManyUniqSets :: [UniqSet a] -> UniqSet a
74 -- = foldr unionUniqSets emptyUniqSet ss
75 unionManyUniqSets [] = emptyUniqSet
76 unionManyUniqSets [s] = s
77 unionManyUniqSets (s:ss) = s `unionUniqSets` unionManyUniqSets ss
79 minusUniqSet :: UniqSet a -> UniqSet a -> UniqSet a
80 minusUniqSet (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (minusUFM set1 set2)
82 intersectUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
83 intersectUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (intersectUFM set1 set2)
85 elementOfUniqSet :: Uniquable a => a -> UniqSet a -> Bool
86 elementOfUniqSet x (MkUniqSet set) = maybeToBool (lookupUFM set x)
88 isEmptyUniqSet :: UniqSet a -> Bool
89 isEmptyUniqSet (MkUniqSet set) = isNullUFM set {-SLOW: sizeUFM set == 0-}
91 mapUniqSet :: Uniquable b => (a -> b) -> UniqSet a -> UniqSet b
92 mapUniqSet f (MkUniqSet set)
93 = MkUniqSet (listToUFM [ let
94 mapped_thing = f thing
96 (mapped_thing, mapped_thing)
97 | thing <- eltsUFM set ])
101 #if __GLASGOW_HASKELL__
103 addOneToUniqSet :: UniqSet Unique -> Unique -> UniqSet Unique
106 elementOfUniqSet :: RnName -> UniqSet RnName -> Bool
107 , Unique -> UniqSet Unique -> Bool
110 mkUniqSet :: [RnName] -> UniqSet RnName
114 unitUniqSet :: RnName -> UniqSet RnName
115 , Unique -> UniqSet Unique