{-# LANGUAGE CPP #-}
{-# LANGUAGE BangPatterns #-}
#include "containers.h"
module Data.Strict.ContainersUtils.Autogen.BitQueue
( BitQueue
, BitQueueB
, emptyQB
, snocQB
, buildQ
, unconsQ
, toListQ
) where
import Data.Strict.ContainersUtils.Autogen.BitUtil (shiftLL, shiftRL, wordSize)
import Data.Bits ((.|.), (.&.), testBit)
import Data.Bits (countTrailingZeros)
data BitQueueB = BQB {-# UNPACK #-} !Word
{-# UNPACK #-} !Word
newtype BitQueue = BQ BitQueueB deriving Int -> BitQueue -> ShowS
[BitQueue] -> ShowS
BitQueue -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BitQueue] -> ShowS
$cshowList :: [BitQueue] -> ShowS
show :: BitQueue -> String
$cshow :: BitQueue -> String
showsPrec :: Int -> BitQueue -> ShowS
$cshowsPrec :: Int -> BitQueue -> ShowS
Show
instance Show BitQueueB where
show :: BitQueueB -> String
show (BQB Word
hi Word
lo) = String
"BQ"forall a. [a] -> [a] -> [a]
++
forall a. Show a => a -> String
show (forall a b. (a -> b) -> [a] -> [b]
map (forall a. Bits a => a -> Int -> Bool
testBit Word
hi) [(Int
wordSize forall a. Num a => a -> a -> a
- Int
1),(Int
wordSize forall a. Num a => a -> a -> a
- Int
2)..Int
0]
forall a. [a] -> [a] -> [a]
++ forall a b. (a -> b) -> [a] -> [b]
map (forall a. Bits a => a -> Int -> Bool
testBit Word
lo) [(Int
wordSize forall a. Num a => a -> a -> a
- Int
1),(Int
wordSize forall a. Num a => a -> a -> a
- Int
2)..Int
0])
emptyQB :: BitQueueB
emptyQB :: BitQueueB
emptyQB = Word -> Word -> BitQueueB
BQB (Word
1 Word -> Int -> Word
`shiftLL` (Int
wordSize forall a. Num a => a -> a -> a
- Int
1)) Word
0
{-# INLINE emptyQB #-}
shiftQBR1 :: BitQueueB -> BitQueueB
shiftQBR1 :: BitQueueB -> BitQueueB
shiftQBR1 (BQB Word
hi Word
lo) = Word -> Word -> BitQueueB
BQB Word
hi' Word
lo' where
lo' :: Word
lo' = (Word
lo Word -> Int -> Word
`shiftRL` Int
1) forall a. Bits a => a -> a -> a
.|. (Word
hi Word -> Int -> Word
`shiftLL` (Int
wordSize forall a. Num a => a -> a -> a
- Int
1))
hi' :: Word
hi' = Word
hi Word -> Int -> Word
`shiftRL` Int
1
{-# INLINE shiftQBR1 #-}
{-# INLINE snocQB #-}
snocQB :: BitQueueB -> Bool -> BitQueueB
snocQB :: BitQueueB -> Bool -> BitQueueB
snocQB BitQueueB
bq Bool
b = case BitQueueB -> BitQueueB
shiftQBR1 BitQueueB
bq of
BQB Word
hi Word
lo -> Word -> Word -> BitQueueB
BQB (Word
hi forall a. Bits a => a -> a -> a
.|. (forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. Enum a => a -> Int
fromEnum Bool
b) Word -> Int -> Word
`shiftLL` (Int
wordSize forall a. Num a => a -> a -> a
- Int
1))) Word
lo
{-# INLINE buildQ #-}
buildQ :: BitQueueB -> BitQueue
buildQ :: BitQueueB -> BitQueue
buildQ (BQB Word
hi Word
0) = BitQueueB -> BitQueue
BQ (Word -> Word -> BitQueueB
BQB Word
0 Word
lo') where
zeros :: Int
zeros = forall b. FiniteBits b => b -> Int
countTrailingZeros Word
hi
lo' :: Word
lo' = ((Word
hi Word -> Int -> Word
`shiftRL` Int
1) forall a. Bits a => a -> a -> a
.|. (Word
1 Word -> Int -> Word
`shiftLL` (Int
wordSize forall a. Num a => a -> a -> a
- Int
1))) Word -> Int -> Word
`shiftRL` Int
zeros
buildQ (BQB Word
hi Word
lo) = BitQueueB -> BitQueue
BQ (Word -> Word -> BitQueueB
BQB Word
hi' Word
lo') where
zeros :: Int
zeros = forall b. FiniteBits b => b -> Int
countTrailingZeros Word
lo
lo1 :: Word
lo1 = (Word
lo Word -> Int -> Word
`shiftRL` Int
1) forall a. Bits a => a -> a -> a
.|. (Word
hi Word -> Int -> Word
`shiftLL` (Int
wordSize forall a. Num a => a -> a -> a
- Int
1))
hi1 :: Word
hi1 = (Word
hi Word -> Int -> Word
`shiftRL` Int
1) forall a. Bits a => a -> a -> a
.|. (Word
1 Word -> Int -> Word
`shiftLL` (Int
wordSize forall a. Num a => a -> a -> a
- Int
1))
lo' :: Word
lo' = (Word
lo1 Word -> Int -> Word
`shiftRL` Int
zeros) forall a. Bits a => a -> a -> a
.|. (Word
hi1 Word -> Int -> Word
`shiftLL` (Int
wordSize forall a. Num a => a -> a -> a
- Int
zeros))
hi' :: Word
hi' = Word
hi1 Word -> Int -> Word
`shiftRL` Int
zeros
nullQ :: BitQueue -> Bool
nullQ :: BitQueue -> Bool
nullQ (BQ (BQB Word
0 Word
1)) = Bool
True
nullQ BitQueue
_ = Bool
False
{-# INLINE nullQ #-}
unconsQ :: BitQueue -> Maybe (Bool, BitQueue)
unconsQ :: BitQueue -> Maybe (Bool, BitQueue)
unconsQ BitQueue
q | BitQueue -> Bool
nullQ BitQueue
q = forall a. Maybe a
Nothing
unconsQ (BQ bq :: BitQueueB
bq@(BQB Word
_ Word
lo)) = forall a. a -> Maybe a
Just (Bool
hd, BitQueueB -> BitQueue
BQ BitQueueB
tl)
where
!hd :: Bool
hd = (Word
lo forall a. Bits a => a -> a -> a
.&. Word
1) forall a. Eq a => a -> a -> Bool
/= Word
0
!tl :: BitQueueB
tl = BitQueueB -> BitQueueB
shiftQBR1 BitQueueB
bq
{-# INLINE unconsQ #-}
toListQ :: BitQueue -> [Bool]
toListQ :: BitQueue -> [Bool]
toListQ BitQueue
bq = case BitQueue -> Maybe (Bool, BitQueue)
unconsQ BitQueue
bq of
Maybe (Bool, BitQueue)
Nothing -> []
Just (Bool
hd, BitQueue
tl) -> Bool
hd forall a. a -> [a] -> [a]
: BitQueue -> [Bool]
toListQ BitQueue
tl