X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fcompiler%2Futils%2FUniqSet.lhs;h=3123c7c2153afb80b4c505451977fb82587eeb4d;hb=c67bf292d43066b96cdcb1e71e122b9bdcaeca58;hp=9df9fc852a115eb8de72b6b7724c5e1c41517222;hpb=7b0181919416d8f04324575b7e17031ca692f5b0;p=ghc-hetmet.git diff --git a/ghc/compiler/utils/UniqSet.lhs b/ghc/compiler/utils/UniqSet.lhs index 9df9fc8..3123c7c 100644 --- a/ghc/compiler/utils/UniqSet.lhs +++ b/ghc/compiler/utils/UniqSet.lhs @@ -1,5 +1,5 @@ % -% (c) The AQUA Project, Glasgow University, 1994-1996 +% (c) The AQUA Project, Glasgow University, 1994-1998 % \section[UniqSet]{Specialised sets, for things with @Uniques@} @@ -8,28 +8,24 @@ Based on @UniqFMs@ (as you would expect). Basically, the things need to be in class @Uniquable@. \begin{code} -#include "HsVersions.h" - module UniqSet ( - UniqSet(..), -- abstract type: NOT + UniqSet, -- abstract type: NOT mkUniqSet, uniqSetToList, emptyUniqSet, unitUniqSet, - addOneToUniqSet, + addOneToUniqSet, addListToUniqSet, delOneFromUniqSet, unionUniqSets, unionManyUniqSets, minusUniqSet, elementOfUniqSet, mapUniqSet, intersectUniqSets, - isEmptyUniqSet + isEmptyUniqSet, filterUniqSet, sizeUniqSet, foldUniqSet, + elemUniqSet_Directly, lookupUniqSet, hashUniqSet ) where -import Ubiq{-uitous-} +#include "HsVersions.h" + +import {-# SOURCE #-} Name ( Name ) -import Maybes ( maybeToBool, Maybe ) +import Maybes ( maybeToBool ) import UniqFM -import Unique ( Unique ) ---import Outputable ( Outputable(..), ExportFlag ) -import SrcLoc ( SrcLoc ) -import Pretty ( Pretty(..), PrettyRep ) -import PprStyle ( PprStyle ) -import Util ( Ord3(..) ) +import Unique ( Unique, Uniquable(..) ) #if ! OMIT_NATIVE_CODEGEN #define IF_NCG(a) a @@ -44,7 +40,7 @@ import Util ( Ord3(..) ) %* * %************************************************************************ -We use @UniqFM@, with a (@uniqueOf@-able) @Unique@ as ``key'' +We use @UniqFM@, with a (@getUnique@-able) @Unique@ as ``key'' and the thing itself as the ``value'' (for later retrieval). \begin{code} @@ -62,11 +58,20 @@ unitUniqSet x = MkUniqSet (unitUFM x x) uniqSetToList :: UniqSet a -> [a] uniqSetToList (MkUniqSet set) = eltsUFM set +foldUniqSet :: (a -> b -> b) -> b -> UniqSet a -> b +foldUniqSet k z (MkUniqSet set) = foldUFM k z set + mkUniqSet :: Uniquable a => [a] -> UniqSet a mkUniqSet xs = MkUniqSet (listToUFM [ (x, x) | x <- xs]) addOneToUniqSet :: Uniquable a => UniqSet a -> a -> UniqSet a -addOneToUniqSet set x = set `unionUniqSets` unitUniqSet x +addOneToUniqSet (MkUniqSet set) x = MkUniqSet (addToUFM set x x) + +delOneFromUniqSet :: Uniquable a => UniqSet a -> a -> UniqSet a +delOneFromUniqSet (MkUniqSet set) x = MkUniqSet (delFromUFM set x) + +addListToUniqSet :: Uniquable a => UniqSet a -> [a] -> UniqSet a +addListToUniqSet (MkUniqSet set) xs = MkUniqSet (addListToUFM set [(x,x) | x<-xs]) unionUniqSets :: UniqSet a -> UniqSet a -> UniqSet a unionUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (plusUFM set1 set2) @@ -80,70 +85,51 @@ unionManyUniqSets (s:ss) = s `unionUniqSets` unionManyUniqSets ss minusUniqSet :: UniqSet a -> UniqSet a -> UniqSet a minusUniqSet (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (minusUFM set1 set2) +filterUniqSet :: (a -> Bool) -> UniqSet a -> UniqSet a +filterUniqSet pred (MkUniqSet set) = MkUniqSet (filterUFM pred set) + intersectUniqSets :: UniqSet a -> UniqSet a -> UniqSet a intersectUniqSets (MkUniqSet set1) (MkUniqSet set2) = MkUniqSet (intersectUFM set1 set2) elementOfUniqSet :: Uniquable a => a -> UniqSet a -> Bool elementOfUniqSet x (MkUniqSet set) = maybeToBool (lookupUFM set x) -isEmptyUniqSet :: UniqSet a -> Bool -isEmptyUniqSet (MkUniqSet set) = isNullUFM set {-SLOW: sizeUFM set == 0-} +lookupUniqSet :: Uniquable a => UniqSet a -> a -> Maybe a +lookupUniqSet (MkUniqSet set) x = lookupUFM set x -mapUniqSet :: Uniquable b => (a -> b) -> UniqSet a -> UniqSet b -mapUniqSet f (MkUniqSet set) - = MkUniqSet (listToUFM [ let - mapped_thing = f thing - in - (mapped_thing, mapped_thing) - | thing <- eltsUFM set ]) -\end{code} +elemUniqSet_Directly :: Unique -> UniqSet a -> Bool +elemUniqSet_Directly x (MkUniqSet set) = maybeToBool (lookupUFM_Directly set x) -%************************************************************************ -%* * -\subsection{The @IdSet@ and @TyVarSet@ specialisations for sets of Ids/TyVars} -%* * -%************************************************************************ +sizeUniqSet :: UniqSet a -> Int +sizeUniqSet (MkUniqSet set) = sizeUFM set -@IdSet@ is a specialised version, optimised for sets of Ids. +hashUniqSet :: UniqSet a -> Int +hashUniqSet (MkUniqSet set) = hashUFM set -\begin{code} ---type NameSet = UniqSet Name ---type GenTyVarSet flexi = UniqSet (GenTyVar flexi) ---type GenIdSet ty = UniqSet (GenId ty) +isEmptyUniqSet :: UniqSet a -> Bool +isEmptyUniqSet (MkUniqSet set) = isNullUFM set {-SLOW: sizeUFM set == 0-} -#if ! OMIT_NATIVE_CODEGEN ---type RegSet = UniqSet Reg -#endif +mapUniqSet :: (a -> a) -> UniqSet a -> UniqSet a + -- VERY IMPORTANT: *assumes* that the function doesn't change the unique +mapUniqSet f (MkUniqSet set) = MkUniqSet (mapUFM f set) +\end{code} -#if 0 +\begin{code} #if __GLASGOW_HASKELL__ {-# SPECIALIZE - unitUniqSet :: GenId ty -> GenIdSet ty, - GenTyVar flexi -> GenTyVarSet flexi, - Name -> NameSet - IF_NCG(COMMA Reg -> RegSet) - #-} - -{-# SPECIALIZE - mkUniqSet :: [GenId ty] -> GenIdSet ty, - [GenTyVar flexi] -> GenTyVarSet flexi, - [Name] -> NameSet - IF_NCG(COMMA [Reg] -> RegSet) - #-} - -{-# SPECIALIZE - elementOfUniqSet :: GenId ty -> GenIdSet ty -> Bool, - GenTyVar flexi -> GenTyVarSet flexi -> Bool, - Name -> NameSet -> Bool - IF_NCG(COMMA Reg -> RegSet -> Bool) + addOneToUniqSet :: UniqSet Unique -> Unique -> UniqSet Unique #-} - -{-# SPECIALIZE - mapUniqSet :: (GenId ty -> GenId ty) -> GenIdSet ty -> GenIdSet ty, - (GenTyVar flexi -> GenTyVar flexi) -> GenTyVarSet flexi -> GenTyVarSet flexi, - (Name -> Name) -> NameSet -> NameSet - IF_NCG(COMMA (Reg -> Reg) -> RegSet -> RegSet) - #-} -#endif +{- SPECIALIZE + elementOfUniqSet :: Name -> UniqSet Name -> Bool + , Unique -> UniqSet Unique -> Bool + -} +{- SPECIALIZE + mkUniqSet :: [Name] -> UniqSet Name + -} + +{- SPECIALIZE + unitUniqSet :: Name -> UniqSet Name + , Unique -> UniqSet Unique + -} #endif \end{code}