{-# LANGUAGE BangPatterns #-}
module Data.Vector.Compact.WordVec where
import Data.Bits
import Data.Word
import Data.Vector.Compact.Blob
newtype WordVec = WordVec Blob
data Shape = Shape
{ shapeLen :: !Int
, shapeBits :: !Int
}
deriving (Eq,Show)
vecShape :: WordVec -> Shape
vecShape = snd . vecShape'
vecShape' :: WordVec -> (Bool,Shape)
vecShape' (WordVec blob) = (isSmall,shape) where
!h = blobHead blob
!h2 = shiftR h 1
!isSmall = (h .&. 1) == 0
shape = if isSmall
then mkShape (shiftR h 3 .&. 31 ) (shiftL ((h2.&. 3)+1) 2)
else mkShape (shiftR h 5 .&. 0x07ffffff) (shiftL ((h2.&.15)+1) 2)
mkShape :: Word64 -> Word64 -> Shape
mkShape !x !y = Shape (fromIntegral x) (fromIntegral y)
vecIsSmall :: WordVec -> Bool
vecIsSmall (WordVec blob) = (blobHead blob .&. 1) == 0
vecLen, vecBits :: WordVec -> Int
vecLen = shapeLen . vecShape
vecBits = shapeBits . vecShape
instance Show WordVec where
showsPrec = showsPrecWordVec
showWordVec :: WordVec -> String
showWordVec dynvec = showsPrecWordVec 0 dynvec []
showsPrecWordVec :: Int -> WordVec -> ShowS
showsPrecWordVec prec dynvec
= showParen (prec > 10)
$ showString "fromList' "
. showsPrec 11 (vecShape dynvec)
. showChar ' '
. shows (toList dynvec)
instance Eq WordVec where
(==) x y = (vecLen x == vecLen y) && (toList x == toList y)
instance Ord WordVec where
compare x y = case compare (vecLen x) (vecLen y) of
LT -> LT
GT -> GT
EQ -> compare (toList x) (toList y)
empty :: WordVec
empty = fromList []
null :: WordVec -> Bool
null v = (vecLen v == 0)
unsafeIndex :: Int -> WordVec -> Word
unsafeIndex idx dynvec@(WordVec blob) =
case isSmall of
True -> extractSmallWord bits blob ( 8 + bits*idx)
False -> extractSmallWord bits blob (32 + bits*idx)
where
(isSmall, Shape _ bits) = vecShape' dynvec
safeIndex :: Int -> WordVec -> Maybe Word
safeIndex idx dynvec@(WordVec blob)
| idx < 0 = Nothing
| idx >= len = Nothing
| otherwise = Just $ case isSmall of
True -> extractSmallWord bits blob ( 8 + bits*idx)
False -> extractSmallWord bits blob (32 + bits*idx)
where
(isSmall, Shape len bits) = vecShape' dynvec
head :: WordVec -> Word
head dynvec@(WordVec blob) =
case vecIsSmall dynvec of
True -> extractSmallWord bits blob 8
False -> extractSmallWord bits blob 32
where
bits = vecBits dynvec
toList :: WordVec -> [Word]
toList dynvec@(WordVec blob) =
case isSmall of
True -> worker 8 len (shiftR header 8 : restOfWords)
False -> worker 32 len (shiftR header 32 : restOfWords)
where
isSmall = (header .&. 1) == 0
(header:restOfWords) = blobToWordList blob
Shape len bits = vecShape dynvec
the_mask = shiftL 1 bits - 1 :: Word64
mask :: Word64 -> Word
mask w = fromIntegral (w .&. the_mask)
worker !bitOfs !0 _ = []
worker !bitOfs !k [] = replicate k 0
worker !bitOfs !k (this:rest) =
let newOfs = bitOfs + bits
in case compare newOfs 64 of
LT -> (mask this) : worker newOfs (k-1) (shiftR this bits : rest)
EQ -> (mask this) : worker 0 (k-1) rest
GT -> case rest of
(that:rest') ->
let !newOfs' = newOfs - 64
!elem = mask (this .|. shiftL that (64-bitOfs))
in elem : worker newOfs' (k-1) (shiftR that newOfs' : rest')
[] -> error "WordVec/toList: FATAL ERROR! this should not happen"
toList_naive :: WordVec -> [Word]
toList_naive dynvec@(WordVec blob) =
case isSmall of
True -> [ extractSmallWord bits blob ( 8 + bits*i) | i<-[0..len-1] ]
False -> [ extractSmallWord bits blob (32 + bits*i) | i<-[0..len-1] ]
where
(isSmall, Shape len bits) = vecShape' dynvec
fromList :: [Word] -> WordVec
fromList [] = fromList' (Shape 0 4) []
fromList xs = fromList' (Shape l b) xs where
l = length xs
b = bitsNeededFor (maximum xs)
fromList' :: Shape -> [Word] -> WordVec
fromList' (Shape len bits0) words
| bits <= 16 && len <= 31 = WordVec $ mkBlob (mkHeader 0 2) 8 words
| otherwise = WordVec $ mkBlob (mkHeader 1 4) 32 words
where
!bits = max 4 $ min 64 $ (bits0 + 3) .&. 0xfc
!bitsEnc = shiftR bits 2 - 1 :: Int
!content = bits*len :: Int
!mask = shiftL 1 bits - 1 :: Word64
mkHeader :: Word64 -> Int -> Word64
mkHeader !isSmall !resoBits = isSmall + fromIntegral (shiftL (bitsEnc + shiftL len resoBits) 1)
mkBlob !header !ofs words = blobFromWordListN (shiftR (ofs+content+63) 6)
$ worker len header ofs words
worker :: Int -> Word64 -> Int -> [Word] -> [Word64]
worker 0 !current !bitOfs _ = if bitOfs == 0 then [] else [current]
worker !k !current !bitOfs [] = worker k current bitOfs [0]
worker !k !current !bitOfs (this0:rest) =
let !this = (fromIntegral this0) .&. mask
!newOfs = bitOfs + bits
!current' = (shiftL this bitOfs) .|. current
in case compare newOfs 64 of
LT -> worker (k-1) current' newOfs rest
EQ -> current' : worker (k-1) 0 0 rest
GT -> let !newOfs' = newOfs - 64
in current' : worker (k-1) (shiftR this (64-bitOfs)) newOfs' rest
naiveMap :: (Word -> Word) -> WordVec -> WordVec
naiveMap f u = fromList (map f $ toList u)
boundedMap :: Word -> (Word -> Word) -> WordVec -> WordVec
boundedMap bound f vec = fromList' (Shape l bits) (toList vec) where
l = vecLen vec
bits = bitsNeededFor bound
concat :: WordVec -> WordVec -> WordVec
concat u v = fromList' (Shape (lu+lv) (max bu bv)) (toList u ++ toList v) where
Shape lu bu = vecShape u
Shape lv bv = vecShape v
naiveZipWith :: (Word -> Word -> Word) -> WordVec -> WordVec -> WordVec
naiveZipWith f u v = fromList $ zipWith f (toList u) (toList v)
boundedZipWith :: Word -> (Word -> Word -> Word) -> WordVec -> WordVec -> WordVec
boundedZipWith bound f vec1 vec2 = fromList' (Shape l bits) $ zipWith f (toList vec1) (toList vec2) where
l = min (vecLen vec1) (vecLen vec2)
bits = bitsNeededFor bound
bitsNeededFor :: Word -> Int
bitsNeededFor bound = ceilingLog2 (bound + 1) where
ceilingLog2 :: Word -> Int
ceilingLog2 0 = 0
ceilingLog2 n = 1 + go (n-1) where
go 0 = -1
go k = 1 + go (shiftR k 1)