[project @ 1998-02-03 17:11:28 by simonm]
[ghc-hetmet.git] / ghc / compiler / utils / UniqSet.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1994-1996
3 %
4 \section[UniqSet]{Specialised sets, for things with @Uniques@}
5
6 Based on @UniqFMs@ (as you would expect).
7
8 Basically, the things need to be in class @Uniquable@.
9
10 \begin{code}
11 module UniqSet (
12         UniqSet,    -- abstract type: NOT
13
14         mkUniqSet, uniqSetToList, emptyUniqSet, unitUniqSet,
15         addOneToUniqSet, addListToUniqSet,
16         unionUniqSets, unionManyUniqSets, minusUniqSet,
17         elementOfUniqSet, mapUniqSet, intersectUniqSets,
18         isEmptyUniqSet, filterUniqSet, sizeUniqSet
19     ) where
20
21 #include "HsVersions.h"
22
23 import {-# SOURCE #-} Name
24
25 import Maybes           ( maybeToBool )
26 import UniqFM
27 import Unique           ( Unique, Uniquable(..) )
28 import SrcLoc           ( SrcLoc )
29 import Outputable       ( Outputable(..) )
30
31 #if ! OMIT_NATIVE_CODEGEN
32 #define IF_NCG(a) a
33 #else
34 #define IF_NCG(a) {--}
35 #endif
36 \end{code}
37
38 %************************************************************************
39 %*                                                                      *
40 \subsection{The @UniqSet@ type}
41 %*                                                                      *
42 %************************************************************************
43
44 We use @UniqFM@, with a (@uniqueOf@-able) @Unique@ as ``key''
45 and the thing itself as the ``value'' (for later retrieval).
46
47 \begin{code}
48 --data UniqSet a = MkUniqSet (FiniteMap Unique a) : NOT
49
50 type UniqSet a = UniqFM a
51 #define MkUniqSet {--}
52
53 emptyUniqSet :: UniqSet a
54 emptyUniqSet = MkUniqSet emptyUFM
55
56 unitUniqSet :: Uniquable a => a -> UniqSet a
57 unitUniqSet x = MkUniqSet (unitUFM x x)
58
59 uniqSetToList :: UniqSet a -> [a]
60 uniqSetToList (MkUniqSet set) = eltsUFM set
61
62 mkUniqSet :: Uniquable a => [a]  -> UniqSet a
63 mkUniqSet xs = MkUniqSet (listToUFM [ (x, x) | x <- xs])
64
65 addOneToUniqSet :: Uniquable a => UniqSet a -> a -> UniqSet a
66 addOneToUniqSet (MkUniqSet set) x = MkUniqSet (addToUFM set x x)
67
68 addListToUniqSet :: Uniquable a => UniqSet a -> [a] -> UniqSet a
69 addListToUniqSet (MkUniqSet set) xs = MkUniqSet (addListToUFM set [(x,x) | x<-xs])
70
71 unionUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
72 unionUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (plusUFM set1 set2)
73
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
79
80 minusUniqSet  :: UniqSet a -> UniqSet a -> UniqSet a
81 minusUniqSet (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (minusUFM set1 set2)
82
83 filterUniqSet :: (a -> Bool) -> UniqSet a -> UniqSet a
84 filterUniqSet pred (MkUniqSet set) = MkUniqSet (filterUFM pred set)
85
86 intersectUniqSets :: UniqSet a -> UniqSet a -> UniqSet a
87 intersectUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (intersectUFM set1 set2)
88
89 elementOfUniqSet :: Uniquable a => a -> UniqSet a -> Bool
90 elementOfUniqSet x (MkUniqSet set) = maybeToBool (lookupUFM set x)
91
92 sizeUniqSet :: UniqSet a -> Int
93 sizeUniqSet (MkUniqSet set) = sizeUFM set
94
95 isEmptyUniqSet :: UniqSet a -> Bool
96 isEmptyUniqSet (MkUniqSet set) = isNullUFM set {-SLOW: sizeUFM set == 0-}
97
98 mapUniqSet :: Uniquable b => (a -> b) -> UniqSet a -> UniqSet b
99 mapUniqSet f (MkUniqSet set)
100   = MkUniqSet (listToUFM [ let
101                              mapped_thing = f thing
102                           in
103                           (mapped_thing, mapped_thing)
104                         | thing <- eltsUFM set ])
105 \end{code}
106
107 \begin{code}
108 #if __GLASGOW_HASKELL__
109 {-# SPECIALIZE
110     addOneToUniqSet :: UniqSet Unique -> Unique -> UniqSet Unique
111     #-}
112 {-# SPECIALIZE
113     elementOfUniqSet :: Name -> UniqSet Name -> Bool
114                       , Unique -> UniqSet Unique -> Bool
115     #-}
116 {-# SPECIALIZE
117     mkUniqSet :: [Name] -> UniqSet Name
118     #-}
119
120 {-# SPECIALIZE
121     unitUniqSet :: Name -> UniqSet Name
122                  , Unique -> UniqSet Unique
123     #-}
124 #endif
125 \end{code}