[project @ 2002-08-29 15:44:11 by simonmar]
[ghc-hetmet.git] / ghc / compiler / utils / BitSet.lhs
index fcd837d..a108136 100644 (file)
@@ -1,5 +1,5 @@
 %
-% (c) The GRASP Project, Glasgow University, 1994-1995
+% (c) The GRASP Project, Glasgow University, 1994-1998
 %
 \section[BitSet]{An implementation of very small sets}
 
@@ -18,14 +18,14 @@ Integer and get virtually unlimited sets.
 
 module BitSet (
        BitSet,         -- abstract type
-       mkBS, listBS, emptyBS, singletonBS,
-       unionBS, minusBS
-#if ! defined(COMPILING_GHC)
-       , elementBS, intersectBS, isEmptyBS
-#endif
+       mkBS, listBS, emptyBS, unitBS,
+       unionBS, minusBS, intBS
     ) where
 
+#include "HsVersions.h"
+
 #ifdef __GLASGOW_HASKELL__
+import GLAEXTS
 -- nothing to import
 #elif defined(__YALE_HASKELL__)
 {-hide import from mkdependHS-}
@@ -45,11 +45,15 @@ emptyBS :: BitSet
 emptyBS = MkBS (int2Word# 0#)
 
 mkBS :: [Int] -> BitSet
-mkBS xs = foldr (unionBS . singletonBS) emptyBS xs
+mkBS xs = foldr (unionBS . unitBS) emptyBS xs
 
-singletonBS :: Int -> BitSet
-singletonBS x = case x of
+unitBS :: Int -> BitSet
+unitBS x = case x of
+#if __GLASGOW_HASKELL__ >= 503
+    I# i# -> MkBS ((int2Word# 1#) `uncheckedShiftL#` i#)
+#else
     I# i# -> MkBS ((int2Word# 1#) `shiftL#` i#)
+#endif
 
 unionBS :: BitSet -> BitSet -> BitSet
 unionBS (MkBS x#) (MkBS y#) = MkBS (x# `or#` y#)
@@ -57,11 +61,11 @@ unionBS (MkBS x#) (MkBS y#) = MkBS (x# `or#` y#)
 minusBS :: BitSet -> BitSet -> BitSet
 minusBS (MkBS x#) (MkBS y#) = MkBS (x# `and#` (not# y#))
 
-#if ! defined(COMPILING_GHC)
+#if 0
 -- not used in GHC
 isEmptyBS :: BitSet -> Bool
-isEmptyBS (MkBS s#) =
-    case word2Int# s# of
+isEmptyBS (MkBS s#)
+  = case word2Int# s# of
        0# -> True
        _  -> False
 
@@ -85,7 +89,15 @@ listBS s = listify s 0
                      in case word2Int# (s# `and#` (int2Word# 1#)) of
                          0# -> more
                          _  -> n : more
+#if __GLASGOW_HASKELL__ >= 503
+         shiftr x y = uncheckedShiftRL# x y
+#else
          shiftr x y = shiftRL# x y
+#endif
+
+-- intBS is a bit naughty.
+intBS :: BitSet -> Int
+intBS (MkBS w#) = I# (word2Int# w#)
 
 #elif defined(__YALE_HASKELL__)
 
@@ -95,19 +107,19 @@ emptyBS :: BitSet
 emptyBS = MkBS 0
 
 mkBS :: [Int] -> BitSet
-mkBS xs = foldr (unionBS . singletonBS) emptyBS xs
+mkBS xs = foldr (unionBS . unitBS) emptyBS xs
 
-singletonBS :: Int -> BitSet
-singletonBS x = MkBS (1 `ashInt` x)
+unitBS :: Int -> BitSet
+unitBS x = MkBS (1 `ashInt` x)
 
 unionBS :: BitSet -> BitSet -> BitSet
 unionBS (MkBS x) (MkBS y) = MkBS (x `logiorInt` y)
 
-#if ! defined(COMPILING_GHC)
+#if 0
 -- not used in GHC
 isEmptyBS :: BitSet -> Bool
-isEmptyBS (MkBS s) =
-    case s of
+isEmptyBS (MkBS s)
+  = case s of
        0 -> True
        _ -> False
 
@@ -115,8 +127,8 @@ intersectBS :: BitSet -> BitSet -> BitSet
 intersectBS (MkBS x) (MkBS y) = MkBS (x `logandInt` y)
 
 elementBS :: Int -> BitSet -> Bool
-elementBS x (MkBS s) =
-    case logbitpInt x s of
+elementBS x (MkBS s)
+  = case logbitpInt x s of
        0 -> False
        _ -> True
 #endif
@@ -144,19 +156,19 @@ emptyBS :: BitSet
 emptyBS = MkBS 0
 
 mkBS :: [Int] -> BitSet
-mkBS xs = foldr (unionBS . singletonBS) emptyBS xs
+mkBS xs = foldr (unionBS . unitBS) emptyBS xs
 
-singletonBS :: Int -> BitSet
-singletonBS x = MkBS (1 `bitLsh` x)
+unitBS :: Int -> BitSet
+unitBS x = MkBS (1 `bitLsh` x)
 
 unionBS :: BitSet -> BitSet -> BitSet
 unionBS (MkBS x) (MkBS y) = MkBS (x `bitOr` y)
 
-#if ! defined(COMPILING_GHC)
+#if 0
 -- not used in GHC
 isEmptyBS :: BitSet -> Bool
-isEmptyBS (MkBS s) =
-    case s of
+isEmptyBS (MkBS s)
+  = case s of
        0 -> True
        _ -> False
 
@@ -164,8 +176,8 @@ intersectBS :: BitSet -> BitSet -> BitSet
 intersectBS (MkBS x) (MkBS y) = MkBS (x `bitAnd` y)
 
 elementBS :: Int -> BitSet -> Bool
-elementBS x (MkBS s) =
-    case (1 `bitLsh` x) `bitAnd` s of
+elementBS x (MkBS s)
+  = case (1 `bitLsh` x) `bitAnd` s of
        0 -> False
        _ -> True
 #endif