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, delOneFromUniqSet,
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(..) )
29 #if ! OMIT_NATIVE_CODEGEN
32 #define IF_NCG(a) {--}
36 %************************************************************************
38 \subsection{The @UniqSet@ type}
40 %************************************************************************
42 We use @UniqFM@, with a (@uniqueOf@-able) @Unique@ as ``key''
43 and the thing itself as the ``value'' (for later retrieval).
46 --data UniqSet a = MkUniqSet (FiniteMap Unique a) : NOT
48 type UniqSet a = UniqFM a
49 #define MkUniqSet {--}
51 emptyUniqSet :: UniqSet a
52 emptyUniqSet = MkUniqSet emptyUFM
54 unitUniqSet :: Uniquable a => a -> UniqSet a
55 unitUniqSet x = MkUniqSet (unitUFM x x)
57 uniqSetToList :: UniqSet a -> [a]
58 uniqSetToList (MkUniqSet set) = eltsUFM set
60 mkUniqSet :: Uniquable a => [a] -> UniqSet a
61 mkUniqSet xs = MkUniqSet (listToUFM [ (x, x) | x <- xs])
63 addOneToUniqSet :: Uniquable a => UniqSet a -> a -> UniqSet a
64 addOneToUniqSet (MkUniqSet set) x = MkUniqSet (addToUFM set x x)
66 delOneFromUniqSet :: Uniquable a => UniqSet a -> a -> UniqSet a
67 delOneFromUniqSet (MkUniqSet set) x = MkUniqSet (delFromUFM set x)
69 addListToUniqSet :: Uniquable a => UniqSet a -> [a] -> UniqSet a
70 addListToUniqSet (MkUniqSet set) xs = MkUniqSet (addListToUFM set [(x,x) | x<-xs])
72 unionUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
73 unionUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (plusUFM set1 set2)
75 unionManyUniqSets :: [UniqSet a] -> UniqSet a
76 -- = foldr unionUniqSets emptyUniqSet ss
77 unionManyUniqSets [] = emptyUniqSet
78 unionManyUniqSets [s] = s
79 unionManyUniqSets (s:ss) = s `unionUniqSets` unionManyUniqSets ss
81 minusUniqSet :: UniqSet a -> UniqSet a -> UniqSet a
82 minusUniqSet (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (minusUFM set1 set2)
84 filterUniqSet :: (a -> Bool) -> UniqSet a -> UniqSet a
85 filterUniqSet pred (MkUniqSet set) = MkUniqSet (filterUFM pred set)
87 intersectUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
88 intersectUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (intersectUFM set1 set2)
90 elementOfUniqSet :: Uniquable a => a -> UniqSet a -> Bool
91 elementOfUniqSet x (MkUniqSet set) = maybeToBool (lookupUFM set x)
93 sizeUniqSet :: UniqSet a -> Int
94 sizeUniqSet (MkUniqSet set) = sizeUFM set
96 isEmptyUniqSet :: UniqSet a -> Bool
97 isEmptyUniqSet (MkUniqSet set) = isNullUFM set {-SLOW: sizeUFM set == 0-}
99 mapUniqSet :: Uniquable b => (a -> b) -> UniqSet a -> UniqSet b
100 mapUniqSet f (MkUniqSet set)
101 = MkUniqSet (listToUFM [ let
102 mapped_thing = f thing
104 (mapped_thing, mapped_thing)
105 | thing <- eltsUFM set ])
109 #if __GLASGOW_HASKELL__
111 addOneToUniqSet :: UniqSet Unique -> Unique -> UniqSet Unique
114 elementOfUniqSet :: Name -> UniqSet Name -> Bool
115 , Unique -> UniqSet Unique -> Bool
118 mkUniqSet :: [Name] -> UniqSet Name
122 unitUniqSet :: Name -> UniqSet Name
123 , Unique -> UniqSet Unique