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 import {-hide from mkdependHS-}
34 RnHsSyn ( RnName ) -- specialising only
36 #if ! OMIT_NATIVE_CODEGEN
39 #define IF_NCG(a) {--}
43 %************************************************************************
45 \subsection{The @UniqSet@ type}
47 %************************************************************************
49 We use @UniqFM@, with a (@uniqueOf@-able) @Unique@ as ``key''
50 and the thing itself as the ``value'' (for later retrieval).
53 --data UniqSet a = MkUniqSet (FiniteMap Unique a) : NOT
55 type UniqSet a = UniqFM a
56 #define MkUniqSet {--}
58 emptyUniqSet :: UniqSet a
59 emptyUniqSet = MkUniqSet emptyUFM
61 unitUniqSet :: Uniquable a => a -> UniqSet a
62 unitUniqSet x = MkUniqSet (unitUFM x x)
64 uniqSetToList :: UniqSet a -> [a]
65 uniqSetToList (MkUniqSet set) = eltsUFM set
67 mkUniqSet :: Uniquable a => [a] -> UniqSet a
68 mkUniqSet xs = MkUniqSet (listToUFM [ (x, x) | x <- xs])
70 addOneToUniqSet :: Uniquable a => UniqSet a -> a -> UniqSet a
71 addOneToUniqSet set x = set `unionUniqSets` unitUniqSet x
73 unionUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
74 unionUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (plusUFM set1 set2)
76 unionManyUniqSets :: [UniqSet a] -> UniqSet a
77 -- = foldr unionUniqSets emptyUniqSet ss
78 unionManyUniqSets [] = emptyUniqSet
79 unionManyUniqSets [s] = s
80 unionManyUniqSets (s:ss) = s `unionUniqSets` unionManyUniqSets ss
82 minusUniqSet :: UniqSet a -> UniqSet a -> UniqSet a
83 minusUniqSet (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (minusUFM set1 set2)
85 intersectUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
86 intersectUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (intersectUFM set1 set2)
88 elementOfUniqSet :: Uniquable a => a -> UniqSet a -> Bool
89 elementOfUniqSet x (MkUniqSet set) = maybeToBool (lookupUFM set x)
91 isEmptyUniqSet :: UniqSet a -> Bool
92 isEmptyUniqSet (MkUniqSet set) = isNullUFM set {-SLOW: sizeUFM set == 0-}
94 mapUniqSet :: Uniquable b => (a -> b) -> UniqSet a -> UniqSet b
95 mapUniqSet f (MkUniqSet set)
96 = MkUniqSet (listToUFM [ let
97 mapped_thing = f thing
99 (mapped_thing, mapped_thing)
100 | thing <- eltsUFM set ])
104 #if __GLASGOW_HASKELL__
106 addOneToUniqSet :: UniqSet Unique -> Unique -> UniqSet Unique
109 elementOfUniqSet :: RnName -> UniqSet RnName -> Bool
110 , Unique -> UniqSet Unique -> Bool
113 mkUniqSet :: [RnName] -> UniqSet RnName
117 unitUniqSet :: RnName -> UniqSet RnName
118 , Unique -> UniqSet Unique