@@ -205,7 +205,7 @@ import Utils.Containers.Internal.Prelude hiding
205
205
(filter , foldr , foldl , foldl' , foldMap , null , map )
206
206
import Prelude ()
207
207
208
- import Utils.Containers.Internal.BitUtil
208
+ import Utils.Containers.Internal.BitUtil ( iShiftRL , shiftLL , shiftRL )
209
209
import Utils.Containers.Internal.StrictPair
210
210
import Data.IntSet.Internal.IntTreeCommons
211
211
( Key
@@ -217,6 +217,7 @@ import Data.IntSet.Internal.IntTreeCommons
217
217
, branchMask
218
218
, TreeTreeBranch (.. )
219
219
, treeTreeBranch
220
+ , i2w
220
221
)
221
222
222
223
#if __GLASGOW_HASKELL__
@@ -240,17 +241,6 @@ import Data.Functor.Identity (Identity(..))
240
241
241
242
infixl 9 \\ {- This comment teaches CPP correct behaviour -}
242
243
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
-
254
244
{- -------------------------------------------------------------------
255
245
Operators
256
246
--------------------------------------------------------------------}
@@ -1388,10 +1378,10 @@ fromRange (lx,rx)
1388
1378
| m < suffixBitMask = Tip p (complement 0 )
1389
1379
| otherwise = Bin (Prefix (p .|. m)) (goFull p (shr1 m)) (goFull (p .|. m) (shr1 m))
1390
1380
lbm :: Int -> Int
1391
- lbm p = intFromNat (lowestBitMask (natFromInt p))
1381
+ lbm p = p .&. negate p -- lowest bit mask
1392
1382
{-# INLINE lbm #-}
1393
1383
shr1 :: Int -> Int
1394
- shr1 m = intFromNat (natFromInt m `shiftRL ` 1 )
1384
+ shr1 m = m `iShiftRL ` 1
1395
1385
{-# INLINE shr1 #-}
1396
1386
1397
1387
-- | \(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
1621
1611
-- `linkWithMask` is useful when the `branchMask` has already been computed
1622
1612
linkWithMask :: Int -> Key -> IntSet -> Key -> IntSet -> IntSet
1623
1613
linkWithMask m k1 t1 k2 t2
1624
- | natFromInt k1 < natFromInt k2 = Bin p t1 t2
1614
+ | i2w k1 < i2w k2 = Bin p t1 t2
1625
1615
| otherwise = Bin p t2 t1
1626
1616
where
1627
1617
p = Prefix (mask k1 m .|. m)
@@ -1685,18 +1675,18 @@ bitmapOf x = bitmapOfSuffix (suffixOf x)
1685
1675
The signatures of methods in question are placed after this comment.
1686
1676
----------------------------------------------------------------------}
1687
1677
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
1694
1684
#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
1696
1686
#else
1697
- foldMapBits :: Monoid a => Int -> (Int -> a ) -> Nat -> a
1687
+ foldMapBits :: Monoid a => Int -> (Int -> a ) -> Word -> a
1698
1688
#endif
1699
- takeWhileAntitoneBits :: Int -> (Int -> Bool ) -> Nat -> Nat
1689
+ takeWhileAntitoneBits :: Int -> (Int -> Bool ) -> Word -> Word
1700
1690
1701
1691
{-# INLINE lowestBitSet #-}
1702
1692
{-# INLINE highestBitSet #-}
@@ -1707,26 +1697,26 @@ takeWhileAntitoneBits :: Int -> (Int -> Bool) -> Nat -> Nat
1707
1697
{-# INLINE foldMapBits #-}
1708
1698
{-# INLINE takeWhileAntitoneBits #-}
1709
1699
1710
- lowestBitMask :: Nat -> Nat
1700
+ #if defined(__GLASGOW_HASKELL__)
1701
+
1702
+ lowestBitMask :: Word -> Word
1711
1703
lowestBitMask x = x .&. negate x
1712
1704
{-# INLINE lowestBitMask #-}
1713
1705
1714
- #if defined(__GLASGOW_HASKELL__)
1715
-
1716
1706
lowestBitSet x = countTrailingZeros x
1717
1707
1718
1708
highestBitSet x = WORD_SIZE_IN_BITS - 1 - countLeadingZeros x
1719
1709
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
1722
1712
#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
1724
1714
x2 -> case ((x2 `shiftRL` 2 ) .&. 0x33333333 ) .|. ((x2 .&. 0x33333333 ) `shiftLL` 2 ) of
1725
1715
x3 -> case ((x3 `shiftRL` 4 ) .&. 0x0F0F0F0F ) .|. ((x3 .&. 0x0F0F0F0F ) `shiftLL` 4 ) of
1726
1716
x4 -> case ((x4 `shiftRL` 8 ) .&. 0x00FF00FF ) .|. ((x4 .&. 0x00FF00FF ) `shiftLL` 8 ) of
1727
1717
x5 -> ( x5 `shiftRL` 16 ) .|. ( x5 `shiftLL` 16 );
1728
1718
#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
1730
1720
x2 -> case ((x2 `shiftRL` 2 ) .&. 0x3333333333333333 ) .|. ((x2 .&. 0x3333333333333333 ) `shiftLL` 2 ) of
1731
1721
x3 -> case ((x3 `shiftRL` 4 ) .&. 0x0F0F0F0F0F0F0F0F ) .|. ((x3 .&. 0x0F0F0F0F0F0F0F0F ) `shiftLL` 4 ) of
1732
1722
x4 -> case ((x4 `shiftRL` 8 ) .&. 0x00FF00FF00FF00FF ) .|. ((x4 .&. 0x00FF00FF00FF00FF ) `shiftLL` 8 ) of
@@ -1747,14 +1737,14 @@ foldl'Bits prefix f z bitmap = go bitmap z
1747
1737
where ! bitmask = lowestBitMask bm
1748
1738
! bi = countTrailingZeros bitmask
1749
1739
1750
- foldrBits prefix f z bitmap = go (revNat bitmap) z
1740
+ foldrBits prefix f z bitmap = go (revWord bitmap) z
1751
1741
where go 0 acc = acc
1752
1742
go bm acc = go (bm `xor` bitmask) ((f $! (prefix+ (WORD_SIZE_IN_BITS - 1 )- bi)) acc)
1753
1743
where ! bitmask = lowestBitMask bm
1754
1744
! bi = countTrailingZeros bitmask
1755
1745
1756
1746
1757
- foldr'Bits prefix f z bitmap = go (revNat bitmap) z
1747
+ foldr'Bits prefix f z bitmap = go (revWord bitmap) z
1758
1748
where go 0 acc = acc
1759
1749
go bm ! acc = go (bm `xor` bitmask) ((f $! (prefix+ (WORD_SIZE_IN_BITS - 1 )- bi)) acc)
1760
1750
where ! bitmask = lowestBitMask bm
0 commit comments