@@ -205,7 +205,7 @@ import Utils.Containers.Internal.Prelude hiding
205205 (filter , foldr , foldl , foldl' , foldMap , null , map )
206206import Prelude ()
207207
208- import Utils.Containers.Internal.BitUtil
208+ import Utils.Containers.Internal.BitUtil ( iShiftRL , shiftLL , shiftRL )
209209import Utils.Containers.Internal.StrictPair
210210import Data.IntSet.Internal.IntTreeCommons
211211 ( Key
@@ -217,6 +217,7 @@ import Data.IntSet.Internal.IntTreeCommons
217217 , branchMask
218218 , TreeTreeBranch (.. )
219219 , treeTreeBranch
220+ , i2w
220221 )
221222
222223#if __GLASGOW_HASKELL__
@@ -240,17 +241,6 @@ import Data.Functor.Identity (Identity(..))
240241
241242infixl 9 \\ {- This comment teaches CPP correct behaviour -}
242243
243- -- A "Nat" is a natural machine word (an unsigned Int)
244- type Nat = Word
245-
246- natFromInt :: Int -> Nat
247- natFromInt i = fromIntegral i
248- {-# INLINE natFromInt #-}
249-
250- intFromNat :: Nat -> Int
251- intFromNat w = fromIntegral w
252- {-# INLINE intFromNat #-}
253-
254244{- -------------------------------------------------------------------
255245 Operators
256246--------------------------------------------------------------------}
@@ -1388,10 +1378,10 @@ fromRange (lx,rx)
13881378 | m < suffixBitMask = Tip p (complement 0 )
13891379 | otherwise = Bin (Prefix (p .|. m)) (goFull p (shr1 m)) (goFull (p .|. m) (shr1 m))
13901380 lbm :: Int -> Int
1391- lbm p = intFromNat (lowestBitMask (natFromInt p))
1381+ lbm p = p .&. negate p -- lowest bit mask
13921382 {-# INLINE lbm #-}
13931383 shr1 :: Int -> Int
1394- shr1 m = intFromNat (natFromInt m `shiftRL ` 1 )
1384+ shr1 m = m `iShiftRL ` 1
13951385 {-# INLINE shr1 #-}
13961386
13971387-- | \(O(n)\). Build a set from an ascending list of elements.
@@ -1621,7 +1611,7 @@ link k1 t1 k2 t2 = linkWithMask (branchMask k1 k2) k1 t1 k2 t2
16211611-- `linkWithMask` is useful when the `branchMask` has already been computed
16221612linkWithMask :: Int -> Key -> IntSet -> Key -> IntSet -> IntSet
16231613linkWithMask m k1 t1 k2 t2
1624- | natFromInt k1 < natFromInt k2 = Bin p t1 t2
1614+ | i2w k1 < i2w k2 = Bin p t1 t2
16251615 | otherwise = Bin p t2 t1
16261616 where
16271617 p = Prefix (mask k1 m .|. m)
@@ -1685,18 +1675,18 @@ bitmapOf x = bitmapOfSuffix (suffixOf x)
16851675 The signatures of methods in question are placed after this comment.
16861676----------------------------------------------------------------------}
16871677
1688- lowestBitSet :: Nat -> Int
1689- highestBitSet :: Nat -> Int
1690- foldlBits :: Int -> (a -> Int -> a ) -> a -> Nat -> a
1691- foldl'Bits :: Int -> (a -> Int -> a ) -> a -> Nat -> a
1692- foldrBits :: Int -> (Int -> a -> a ) -> a -> Nat -> a
1693- foldr'Bits :: Int -> (Int -> a -> a ) -> a -> Nat -> a
1678+ lowestBitSet :: Word -> Int
1679+ highestBitSet :: Word -> Int
1680+ foldlBits :: Int -> (a -> Int -> a ) -> a -> Word -> a
1681+ foldl'Bits :: Int -> (a -> Int -> a ) -> a -> Word -> a
1682+ foldrBits :: Int -> (Int -> a -> a ) -> a -> Word -> a
1683+ foldr'Bits :: Int -> (Int -> a -> a ) -> a -> Word -> a
16941684#if MIN_VERSION_base(4,11,0)
1695- foldMapBits :: Semigroup a => Int -> (Int -> a ) -> Nat -> a
1685+ foldMapBits :: Semigroup a => Int -> (Int -> a ) -> Word -> a
16961686#else
1697- foldMapBits :: Monoid a => Int -> (Int -> a ) -> Nat -> a
1687+ foldMapBits :: Monoid a => Int -> (Int -> a ) -> Word -> a
16981688#endif
1699- takeWhileAntitoneBits :: Int -> (Int -> Bool ) -> Nat -> Nat
1689+ takeWhileAntitoneBits :: Int -> (Int -> Bool ) -> Word -> Word
17001690
17011691{-# INLINE lowestBitSet #-}
17021692{-# INLINE highestBitSet #-}
@@ -1707,26 +1697,26 @@ takeWhileAntitoneBits :: Int -> (Int -> Bool) -> Nat -> Nat
17071697{-# INLINE foldMapBits #-}
17081698{-# INLINE takeWhileAntitoneBits #-}
17091699
1710- lowestBitMask :: Nat -> Nat
1700+ #if defined(__GLASGOW_HASKELL__)
1701+
1702+ lowestBitMask :: Word -> Word
17111703lowestBitMask x = x .&. negate x
17121704{-# INLINE lowestBitMask #-}
17131705
1714- #if defined(__GLASGOW_HASKELL__)
1715-
17161706lowestBitSet x = countTrailingZeros x
17171707
17181708highestBitSet x = WORD_SIZE_IN_BITS - 1 - countLeadingZeros x
17191709
1720- -- Reverse the order of bits in the Nat .
1721- revNat :: Nat -> Nat
1710+ -- Reverse the order of bits in the Word .
1711+ revWord :: Word -> Word
17221712#if WORD_SIZE_IN_BITS==32
1723- revNat x1 = case ((x1 `shiftRL` 1 ) .&. 0x55555555 ) .|. ((x1 .&. 0x55555555 ) `shiftLL` 1 ) of
1713+ revWord x1 = case ((x1 `shiftRL` 1 ) .&. 0x55555555 ) .|. ((x1 .&. 0x55555555 ) `shiftLL` 1 ) of
17241714 x2 -> case ((x2 `shiftRL` 2 ) .&. 0x33333333 ) .|. ((x2 .&. 0x33333333 ) `shiftLL` 2 ) of
17251715 x3 -> case ((x3 `shiftRL` 4 ) .&. 0x0F0F0F0F ) .|. ((x3 .&. 0x0F0F0F0F ) `shiftLL` 4 ) of
17261716 x4 -> case ((x4 `shiftRL` 8 ) .&. 0x00FF00FF ) .|. ((x4 .&. 0x00FF00FF ) `shiftLL` 8 ) of
17271717 x5 -> ( x5 `shiftRL` 16 ) .|. ( x5 `shiftLL` 16 );
17281718#else
1729- revNat x1 = case ((x1 `shiftRL` 1 ) .&. 0x5555555555555555 ) .|. ((x1 .&. 0x5555555555555555 ) `shiftLL` 1 ) of
1719+ revWord x1 = case ((x1 `shiftRL` 1 ) .&. 0x5555555555555555 ) .|. ((x1 .&. 0x5555555555555555 ) `shiftLL` 1 ) of
17301720 x2 -> case ((x2 `shiftRL` 2 ) .&. 0x3333333333333333 ) .|. ((x2 .&. 0x3333333333333333 ) `shiftLL` 2 ) of
17311721 x3 -> case ((x3 `shiftRL` 4 ) .&. 0x0F0F0F0F0F0F0F0F ) .|. ((x3 .&. 0x0F0F0F0F0F0F0F0F ) `shiftLL` 4 ) of
17321722 x4 -> case ((x4 `shiftRL` 8 ) .&. 0x00FF00FF00FF00FF ) .|. ((x4 .&. 0x00FF00FF00FF00FF ) `shiftLL` 8 ) of
@@ -1747,14 +1737,14 @@ foldl'Bits prefix f z bitmap = go bitmap z
17471737 where ! bitmask = lowestBitMask bm
17481738 ! bi = countTrailingZeros bitmask
17491739
1750- foldrBits prefix f z bitmap = go (revNat bitmap) z
1740+ foldrBits prefix f z bitmap = go (revWord bitmap) z
17511741 where go 0 acc = acc
17521742 go bm acc = go (bm `xor` bitmask) ((f $! (prefix+ (WORD_SIZE_IN_BITS - 1 )- bi)) acc)
17531743 where ! bitmask = lowestBitMask bm
17541744 ! bi = countTrailingZeros bitmask
17551745
17561746
1757- foldr'Bits prefix f z bitmap = go (revNat bitmap) z
1747+ foldr'Bits prefix f z bitmap = go (revWord bitmap) z
17581748 where go 0 acc = acc
17591749 go bm ! acc = go (bm `xor` bitmask) ((f $! (prefix+ (WORD_SIZE_IN_BITS - 1 )- bi)) acc)
17601750 where ! bitmask = lowestBitMask bm
0 commit comments