{-# LANGUAGE CPP, BangPatterns, ForeignFunctionInterface #-}
module Data.Vector.Compact.WordVec
(
WordVec(..)
, Shape(..)
, vecShape , vecShape'
, vecLen , vecBits , vecIsSmall
, showWordVec , showsPrecWordVec
, null , empty
, singleton , isSingleton
, fromList , fromListN , fromList'
, toList , toRevList
, unsafeIndex , safeIndex
, head , tail , cons , uncons
, last , snoc
, concat
, sum , maximum
, eqStrict , eqExtZero
, cmpStrict , cmpExtZero
, lessOrEqual , partialSumsLessOrEqual
, add , subtract
, scale
, partialSums
, fold
, naiveMap , boundedMap
, naiveZipWith , boundedZipWith , listZipWith
, bitsNeededFor , bitsNeededFor'
, roundBits
)
where
import Prelude hiding ( head , tail , init , last , null , concat , subtract , sum , maximum )
import qualified Data.List as L
import Data.Bits
import Data.Word
import Foreign.C
import Data.Vector.Compact.Blob hiding ( head , tail , last )
import qualified Data.Vector.Compact.Blob as Blob
#ifdef x86_64_HOST_ARCH
#define MACHINE_WORD_BITS 64
#elif i386_HOST_ARCH
#define MACHINE_WORD_BITS 32
#elif i686_HOST_ARCH
#define MACHINE_WORD_BITS 32
#elif aarch64_HOST_ARCH
#define MACHINE_WORD_BITS 64
#else
#define MACHINE_WORD_BITS 32
#endif
newtype WordVec
= WordVec Blob
data Shape = Shape
{ Shape -> Int
shapeLen :: !Int
, Shape -> Int
shapeBits :: !Int
}
deriving (Shape -> Shape -> Bool
(Shape -> Shape -> Bool) -> (Shape -> Shape -> Bool) -> Eq Shape
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Shape -> Shape -> Bool
$c/= :: Shape -> Shape -> Bool
== :: Shape -> Shape -> Bool
$c== :: Shape -> Shape -> Bool
Eq,Int -> Shape -> ShowS
[Shape] -> ShowS
Shape -> String
(Int -> Shape -> ShowS)
-> (Shape -> String) -> ([Shape] -> ShowS) -> Show Shape
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Shape] -> ShowS
$cshowList :: [Shape] -> ShowS
show :: Shape -> String
$cshow :: Shape -> String
showsPrec :: Int -> Shape -> ShowS
$cshowsPrec :: Int -> Shape -> ShowS
Show)
vecShape :: WordVec -> Shape
vecShape :: WordVec -> Shape
vecShape = (Bool, Shape) -> Shape
forall a b. (a, b) -> b
snd ((Bool, Shape) -> Shape)
-> (WordVec -> (Bool, Shape)) -> WordVec -> Shape
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WordVec -> (Bool, Shape)
vecShape'
vecShape' :: WordVec -> (Bool,Shape)
vecShape' :: WordVec -> (Bool, Shape)
vecShape' (WordVec Blob
blob) = (Bool
isSmall,Shape
shape) where
!h :: Word64
h = Blob -> Word64
Blob.head Blob
blob
!h2 :: Word64
h2 = Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
shiftR Word64
h Int
1
!isSmall :: Bool
isSmall = (Word64
h Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
1) Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
0
shape :: Shape
shape = if Bool
isSmall
then Word64 -> Word64 -> Shape
mkShape (Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
shiftR Word64
h Int
3 Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
31 ) (Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
shiftL ((Word64
h2Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
3)Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+Word64
1) Int
2)
else Word64 -> Word64 -> Shape
mkShape (Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
shiftR Word64
h Int
5 Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
0x07ffffff) (Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
shiftL ((Word64
h2Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&.Word64
15)Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+Word64
1) Int
2)
mkShape :: Word64 -> Word64 -> Shape
mkShape :: Word64 -> Word64 -> Shape
mkShape !Word64
x !Word64
y = Int -> Int -> Shape
Shape (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
x) (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
y)
vecIsSmall :: WordVec -> Bool
vecIsSmall :: WordVec -> Bool
vecIsSmall (WordVec !Blob
blob) = (Blob -> Word64
Blob.head Blob
blob Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
1) Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
0
vecLen :: WordVec -> Int
vecLen :: WordVec -> Int
vecLen = Shape -> Int
shapeLen (Shape -> Int) -> (WordVec -> Shape) -> WordVec -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WordVec -> Shape
vecShape
vecBits :: WordVec -> Int
vecBits :: WordVec -> Int
vecBits = Shape -> Int
shapeBits (Shape -> Int) -> (WordVec -> Shape) -> WordVec -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WordVec -> Shape
vecShape
instance Show WordVec where
showsPrec :: Int -> WordVec -> ShowS
showsPrec = Int -> WordVec -> ShowS
showsPrecWordVec
showWordVec :: WordVec -> String
showWordVec :: WordVec -> String
showWordVec WordVec
dynvec = Int -> WordVec -> ShowS
showsPrecWordVec Int
0 WordVec
dynvec []
showsPrecWordVec :: Int -> WordVec -> ShowS
showsPrecWordVec :: Int -> WordVec -> ShowS
showsPrecWordVec Int
prec WordVec
dynvec
= Bool -> ShowS -> ShowS
showParen (Int
prec Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10)
(ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ String -> ShowS
showString String
"fromList' "
ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Shape -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 (WordVec -> Shape
vecShape WordVec
dynvec)
ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
' '
ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Word] -> ShowS
forall a. Show a => a -> ShowS
shows (WordVec -> [Word]
toList WordVec
dynvec)
instance Eq WordVec where
== :: WordVec -> WordVec -> Bool
(==) WordVec
x WordVec
y = WordVec -> WordVec -> Bool
eqStrict WordVec
x WordVec
y
instance Ord WordVec where
compare :: WordVec -> WordVec -> Ordering
compare WordVec
x WordVec
y = WordVec -> WordVec -> Ordering
cmpStrict WordVec
x WordVec
y
empty :: WordVec
empty :: WordVec
empty = [Word] -> WordVec
fromList []
null :: WordVec -> Bool
null :: WordVec -> Bool
null (WordVec !Blob
blob) =
let !h :: Word64
h = Blob -> Word64
Blob.head Blob
blob
in (Word64
h Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
0xf9 Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
0) Bool -> Bool -> Bool
|| (Word64
h Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
0xffffffe1 Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
1)
singleton :: Word -> WordVec
singleton :: Word -> WordVec
singleton !Word
x = Int -> Word -> [Word] -> WordVec
fromListN Int
1 Word
x [Word
x] where
isSingleton :: WordVec -> Maybe Word
isSingleton :: WordVec -> Maybe Word
isSingleton !WordVec
v = case (WordVec -> Int
vecLen WordVec
v) of
Int
1 -> Word -> Maybe Word
forall a. a -> Maybe a
Just (WordVec -> Word
head WordVec
v)
Int
_ -> Maybe Word
forall a. Maybe a
Nothing
unsafeIndex :: Int -> WordVec -> Word
unsafeIndex :: Int -> WordVec -> Word
unsafeIndex Int
idx dynvec :: WordVec
dynvec@(WordVec Blob
blob) =
case Bool
isSmall of
Bool
True -> Int -> Blob -> Int -> Word
forall a. Integral a => Int -> Blob -> Int -> a
extractSmallWord Int
bits Blob
blob ( Int
8 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
bitsInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
idx)
Bool
False -> Int -> Blob -> Int -> Word
forall a. Integral a => Int -> Blob -> Int -> a
extractSmallWord Int
bits Blob
blob (Int
32 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
bitsInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
idx)
where
(Bool
isSmall, Shape Int
_ Int
bits) = WordVec -> (Bool, Shape)
vecShape' WordVec
dynvec
safeIndex :: Int -> WordVec -> Maybe Word
safeIndex :: Int -> WordVec -> Maybe Word
safeIndex Int
idx dynvec :: WordVec
dynvec@(WordVec Blob
blob)
| Int
idx Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = Maybe Word
forall a. Maybe a
Nothing
| Int
idx Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
len = Maybe Word
forall a. Maybe a
Nothing
| Bool
otherwise = Word -> Maybe Word
forall a. a -> Maybe a
Just (Word -> Maybe Word) -> Word -> Maybe Word
forall a b. (a -> b) -> a -> b
$ case Bool
isSmall of
Bool
True -> Int -> Blob -> Int -> Word
forall a. Integral a => Int -> Blob -> Int -> a
extractSmallWord Int
bits Blob
blob ( Int
8 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
bitsInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
idx)
Bool
False -> Int -> Blob -> Int -> Word
forall a. Integral a => Int -> Blob -> Int -> a
extractSmallWord Int
bits Blob
blob (Int
32 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
bitsInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
idx)
where
(Bool
isSmall, Shape Int
len Int
bits) = WordVec -> (Bool, Shape)
vecShape' WordVec
dynvec
head :: WordVec -> Word
head :: WordVec -> Word
head dynvec :: WordVec
dynvec@(WordVec Blob
blob)
| WordVec -> Bool
null WordVec
dynvec = Word
0
| Bool
otherwise = case WordVec -> Bool
vecIsSmall WordVec
dynvec of
Bool
True -> Int -> Blob -> Int -> Word
forall a. Integral a => Int -> Blob -> Int -> a
extractSmallWord Int
bits Blob
blob Int
8
Bool
False -> Int -> Blob -> Int -> Word
forall a. Integral a => Int -> Blob -> Int -> a
extractSmallWord Int
bits Blob
blob Int
32
where
bits :: Int
bits = WordVec -> Int
vecBits WordVec
dynvec
last :: WordVec -> Word
last :: WordVec -> Word
last dynvec :: WordVec
dynvec@(WordVec Blob
blob)
| Int
len Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = Word
0
| Bool
otherwise = case Bool
isSmall of
Bool
True -> Int -> Blob -> Int -> Word
forall a. Integral a => Int -> Blob -> Int -> a
extractSmallWord Int
bits Blob
blob ( Int
8 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
bitsInt -> Int -> Int
forall a. Num a => a -> a -> a
*(Int
lenInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1))
Bool
False -> Int -> Blob -> Int -> Word
forall a. Integral a => Int -> Blob -> Int -> a
extractSmallWord Int
bits Blob
blob (Int
32 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
bitsInt -> Int -> Int
forall a. Num a => a -> a -> a
*(Int
lenInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1))
where
(Bool
isSmall, Shape Int
len Int
bits) = WordVec -> (Bool, Shape)
vecShape' WordVec
dynvec
tail :: WordVec -> WordVec
tail :: WordVec -> WordVec
tail = WordVec -> WordVec
tail_v2
cons :: Word -> WordVec -> WordVec
cons :: Word -> WordVec -> WordVec
cons = Word -> WordVec -> WordVec
cons_v2
snoc :: WordVec -> Word -> WordVec
snoc :: WordVec -> Word -> WordVec
snoc = WordVec -> Word -> WordVec
snoc_v2
uncons :: WordVec -> Maybe (Word, WordVec)
uncons :: WordVec -> Maybe (Word, WordVec)
uncons = WordVec -> Maybe (Word, WordVec)
uncons_v2
concat :: WordVec -> WordVec -> WordVec
concat :: WordVec -> WordVec -> WordVec
concat WordVec
u WordVec
v = Shape -> [Word] -> WordVec
fromList' (Int -> Int -> Shape
Shape (Int
luInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lv) (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
bu Int
bv)) (WordVec -> [Word]
toList WordVec
u [Word] -> [Word] -> [Word]
forall a. [a] -> [a] -> [a]
++ WordVec -> [Word]
toList WordVec
v) where
Shape Int
lu Int
bu = WordVec -> Shape
vecShape WordVec
u
Shape Int
lv Int
bv = WordVec -> Shape
vecShape WordVec
v
foreign import ccall unsafe "vec_identity" c_vec_identity :: CFun11_
foreign import ccall unsafe "vec_tail" c_vec_tail :: CFun11_
foreign import ccall unsafe "vec_head_tail" c_vec_head_tail :: CFun11 Word64
foreign import ccall unsafe "vec_cons" c_vec_cons :: Word64 -> CFun11_
foreign import ccall unsafe "vec_snoc" c_vec_snoc :: Word64 -> CFun11_
tail_v2 :: WordVec -> WordVec
tail_v2 :: WordVec -> WordVec
tail_v2 (WordVec Blob
blob) = Blob -> WordVec
WordVec (Blob -> WordVec) -> Blob -> WordVec
forall a b. (a -> b) -> a -> b
$ CFun11_ -> (Int -> Int) -> Blob -> Blob
wrapCFun11_ CFun11_
c_vec_tail Int -> Int
forall a. a -> a
id Blob
blob
cons_v2 :: Word -> WordVec -> WordVec
cons_v2 :: Word -> WordVec -> WordVec
cons_v2 Word
y vec :: WordVec
vec@(WordVec Blob
blob) = Blob -> WordVec
WordVec (Blob -> WordVec) -> Blob -> WordVec
forall a b. (a -> b) -> a -> b
$ CFun11_ -> (Int -> Int) -> Blob -> Blob
wrapCFun11_ (Word64 -> CFun11_
c_vec_cons (Word -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
y)) Int -> Int
f Blob
blob where
f :: Int -> Int
f !Int
n = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
2) Int
worstcase
len :: Int
len = WordVec -> Int
vecLen WordVec
vec
worstcase :: Int
worstcase = Int -> Int -> Int
forall a. Bits a => a -> Int -> a
shiftR (Int
32 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Word -> Int
bitsNeededFor Word
y Int -> Int -> Int
forall a. Num a => a -> a -> a
* (Int
lenInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
63) Int
6
snoc_v2 :: WordVec -> Word -> WordVec
snoc_v2 :: WordVec -> Word -> WordVec
snoc_v2 vec :: WordVec
vec@(WordVec Blob
blob) Word
y = Blob -> WordVec
WordVec (Blob -> WordVec) -> Blob -> WordVec
forall a b. (a -> b) -> a -> b
$ CFun11_ -> (Int -> Int) -> Blob -> Blob
wrapCFun11_ (Word64 -> CFun11_
c_vec_snoc (Word -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
y)) Int -> Int
f Blob
blob where
f :: Int -> Int
f !Int
n = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
2) Int
worstcase
len :: Int
len = WordVec -> Int
vecLen WordVec
vec
worstcase :: Int
worstcase = Int -> Int -> Int
forall a. Bits a => a -> Int -> a
shiftR (Int
32 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Word -> Int
bitsNeededFor Word
y Int -> Int -> Int
forall a. Num a => a -> a -> a
* (Int
lenInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
63) Int
6
uncons_v2 :: WordVec -> Maybe (Word,WordVec)
uncons_v2 :: WordVec -> Maybe (Word, WordVec)
uncons_v2 vec :: WordVec
vec@(WordVec Blob
blob) = if WordVec -> Bool
null WordVec
vec
then Maybe (Word, WordVec)
forall a. Maybe a
Nothing
else let (Word64
hd,Blob
tl) = CFun11 Word64 -> (Int -> Int) -> Blob -> (Word64, Blob)
forall a. CFun11 a -> (Int -> Int) -> Blob -> (a, Blob)
wrapCFun11 CFun11 Word64
c_vec_head_tail Int -> Int
forall a. a -> a
id Blob
blob
in (Word, WordVec) -> Maybe (Word, WordVec)
forall a. a -> Maybe a
Just (Word64 -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
hd , Blob -> WordVec
WordVec Blob
tl)
toList :: WordVec -> [Word]
toList :: WordVec -> [Word]
toList dynvec :: WordVec
dynvec@(WordVec Blob
blob) =
case Bool
isSmall of
Bool
True -> Int -> Int -> [Word64] -> [Word]
worker Int
8 Int
len (Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
shiftR Word64
header Int
8 Word64 -> [Word64] -> [Word64]
forall a. a -> [a] -> [a]
: [Word64]
restOfWords)
Bool
False -> Int -> Int -> [Word64] -> [Word]
worker Int
32 Int
len (Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
shiftR Word64
header Int
32 Word64 -> [Word64] -> [Word64]
forall a. a -> [a] -> [a]
: [Word64]
restOfWords)
where
isSmall :: Bool
isSmall = (Word64
header Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
1) Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
0
(Word64
header:[Word64]
restOfWords) = Blob -> [Word64]
blobToWordList Blob
blob
Shape Int
len Int
bits = WordVec -> Shape
vecShape WordVec
dynvec
the_mask :: Word64
the_mask = Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
shiftL Word64
1 Int
bits Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
- Word64
1 :: Word64
mask :: Word64 -> Word
mask :: Word64 -> Word
mask Word64
w = Word64 -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64
w Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
the_mask)
worker :: Int -> Int -> [Word64] -> [Word]
worker !Int
bitOfs !Int
0 [Word64]
_ = []
worker !Int
bitOfs !Int
k [] = Int -> Word -> [Word]
forall a. Int -> a -> [a]
replicate Int
k Word
0
worker !Int
bitOfs !Int
k (Word64
this:[Word64]
rest) =
let newOfs :: Int
newOfs = Int
bitOfs Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
bits
in case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
newOfs Int
64 of
Ordering
LT -> (Word64 -> Word
mask Word64
this) Word -> [Word] -> [Word]
forall a. a -> [a] -> [a]
: Int -> Int -> [Word64] -> [Word]
worker Int
newOfs (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) (Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
shiftR Word64
this Int
bits Word64 -> [Word64] -> [Word64]
forall a. a -> [a] -> [a]
: [Word64]
rest)
Ordering
EQ -> (Word64 -> Word
mask Word64
this) Word -> [Word] -> [Word]
forall a. a -> [a] -> [a]
: Int -> Int -> [Word64] -> [Word]
worker Int
0 (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) [Word64]
rest
Ordering
GT -> case [Word64]
rest of
(Word64
that:[Word64]
rest') ->
let !newOfs' :: Int
newOfs' = Int
newOfs Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
64
!elem :: Word
elem = Word64 -> Word
mask (Word64
this Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
shiftL Word64
that (Int
64Int -> Int -> Int
forall a. Num a => a -> a -> a
-Int
bitOfs))
in Word
elem Word -> [Word] -> [Word]
forall a. a -> [a] -> [a]
: Int -> Int -> [Word64] -> [Word]
worker Int
newOfs' (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) (Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
shiftR Word64
that Int
newOfs' Word64 -> [Word64] -> [Word64]
forall a. a -> [a] -> [a]
: [Word64]
rest')
[] -> String -> [Word]
forall a. HasCallStack => String -> a
error String
"WordVec/toList: FATAL ERROR! this should not happen"
toRevList :: WordVec -> [Word]
toRevList :: WordVec -> [Word]
toRevList dynvec :: WordVec
dynvec@(WordVec Blob
blob) =
case Bool
isSmall of
Bool
True -> [ Int -> Blob -> Int -> Word
forall a. Integral a => Int -> Blob -> Int -> a
extractSmallWord Int
bits Blob
blob ( Int
8 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
bitsInt -> Int -> Int
forall a. Num a => a -> a -> a
*(Int
lenInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
i)) | Int
i<-[Int
1..Int
len] ]
Bool
False -> [ Int -> Blob -> Int -> Word
forall a. Integral a => Int -> Blob -> Int -> a
extractSmallWord Int
bits Blob
blob (Int
32 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
bitsInt -> Int -> Int
forall a. Num a => a -> a -> a
*(Int
lenInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
i)) | Int
i<-[Int
1..Int
len] ]
where
(Bool
isSmall, Shape Int
len Int
bits) = WordVec -> (Bool, Shape)
vecShape' WordVec
dynvec
fromList :: [Word] -> WordVec
fromList :: [Word] -> WordVec
fromList [] = Shape -> [Word] -> WordVec
fromList' (Int -> Int -> Shape
Shape Int
0 Int
4) []
fromList [Word]
xs = Shape -> [Word] -> WordVec
fromList' (Int -> Int -> Shape
Shape Int
l Int
b) [Word]
xs where
l :: Int
l = [Word] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Word]
xs
b :: Int
b = Word -> Int
bitsNeededFor ([Word] -> Word
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
L.maximum [Word]
xs)
fromListN
:: Int
-> Word
-> [Word]
-> WordVec
fromListN :: Int -> Word -> [Word] -> WordVec
fromListN Int
len Word
max = Shape -> [Word] -> WordVec
fromList' (Int -> Int -> Shape
Shape Int
len (Word -> Int
bitsNeededFor Word
max))
fromList' :: Shape -> [Word] -> WordVec
fromList' :: Shape -> [Word] -> WordVec
fromList' (Shape Int
len Int
bits0) [Word]
words
| Int
bits Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
16 Bool -> Bool -> Bool
&& Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
31 = Blob -> WordVec
WordVec (Blob -> WordVec) -> Blob -> WordVec
forall a b. (a -> b) -> a -> b
$ Word64 -> Int -> [Word] -> Blob
mkBlob (Word64 -> Int -> Word64
mkHeader Word64
0 Int
2) Int
8 [Word]
words
| Bool
otherwise = Blob -> WordVec
WordVec (Blob -> WordVec) -> Blob -> WordVec
forall a b. (a -> b) -> a -> b
$ Word64 -> Int -> [Word] -> Blob
mkBlob (Word64 -> Int -> Word64
mkHeader Word64
1 Int
4) Int
32 [Word]
words
where
!bits :: Int
bits = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
4 (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
64 (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ (Int
bits0 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3) Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0xfc
!bitsEnc :: Int
bitsEnc = Int -> Int -> Int
forall a. Bits a => a -> Int -> a
shiftR Int
bits Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 :: Int
!content :: Int
content = Int
bitsInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
len :: Int
!mask :: Word64
mask = Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
shiftL Word64
1 Int
bits Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
- Word64
1 :: Word64
mkHeader :: Word64 -> Int -> Word64
mkHeader :: Word64 -> Int -> Word64
mkHeader !Word64
isSmall !Int
resoBits = Word64
isSmall Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Int -> Int
forall a. Bits a => a -> Int -> a
shiftL (Int
bitsEnc Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int -> Int -> Int
forall a. Bits a => a -> Int -> a
shiftL Int
len Int
resoBits) Int
1)
mkBlob :: Word64 -> Int -> [Word] -> Blob
mkBlob !Word64
header !Int
ofs [Word]
words = Int -> [Word64] -> Blob
blobFromWordListN (Int -> Int -> Int
forall a. Bits a => a -> Int -> a
shiftR (Int
ofsInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
contentInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
63) Int
6)
([Word64] -> Blob) -> [Word64] -> Blob
forall a b. (a -> b) -> a -> b
$ Int -> Word64 -> Int -> [Word] -> [Word64]
worker Int
len Word64
header Int
ofs [Word]
words
worker :: Int -> Word64 -> Int -> [Word] -> [Word64]
worker :: Int -> Word64 -> Int -> [Word] -> [Word64]
worker Int
0 !Word64
current !Int
bitOfs [Word]
_ = if Int
bitOfs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 then [] else [Word64
current]
worker !Int
k !Word64
current !Int
bitOfs [] = Int -> Word64 -> Int -> [Word] -> [Word64]
worker Int
k Word64
current Int
bitOfs [Word
0]
worker !Int
k !Word64
current !Int
bitOfs (Word
this0:[Word]
rest) =
let !this :: Word64
this = (Word -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
this0) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
mask
!newOfs :: Int
newOfs = Int
bitOfs Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
bits
!current' :: Word64
current' = (Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
shiftL Word64
this Int
bitOfs) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. Word64
current
in case Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
newOfs Int
64 of
Ordering
LT -> Int -> Word64 -> Int -> [Word] -> [Word64]
worker (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) Word64
current' Int
newOfs [Word]
rest
Ordering
EQ -> Word64
current' Word64 -> [Word64] -> [Word64]
forall a. a -> [a] -> [a]
: Int -> Word64 -> Int -> [Word] -> [Word64]
worker (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) Word64
0 Int
0 [Word]
rest
Ordering
GT -> let !newOfs' :: Int
newOfs' = Int
newOfs Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
64
in Word64
current' Word64 -> [Word64] -> [Word64]
forall a. a -> [a] -> [a]
: Int -> Word64 -> Int -> [Word] -> [Word64]
worker (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) (Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
shiftR Word64
this (Int
64Int -> Int -> Int
forall a. Num a => a -> a -> a
-Int
bitOfs)) Int
newOfs' [Word]
rest
sum :: WordVec -> Word
sum :: WordVec -> Word
sum (WordVec Blob
blob) = Word64 -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Word) -> Word64 -> Word
forall a b. (a -> b) -> a -> b
$ CFun10 Word64 -> Blob -> Word64
forall a. CFun10 a -> Blob -> a
wrapCFun10 CFun10 Word64
c_vec_sum Blob
blob
maximum :: WordVec -> Word
maximum :: WordVec -> Word
maximum (WordVec Blob
blob) = Word64 -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Word) -> Word64 -> Word
forall a b. (a -> b) -> a -> b
$ CFun10 Word64 -> Blob -> Word64
forall a. CFun10 a -> Blob -> a
wrapCFun10 CFun10 Word64
c_vec_max Blob
blob
foreign import ccall unsafe "vec_sum" c_vec_sum :: CFun10 Word64
foreign import ccall unsafe "vec_max" c_vec_max :: CFun10 Word64
foreign import ccall unsafe "vec_equal_strict" c_equal_strict :: CFun20 CInt
foreign import ccall unsafe "vec_equal_extzero" c_equal_extzero :: CFun20 CInt
foreign import ccall unsafe "vec_compare_strict" c_compare_strict :: CFun20 CInt
foreign import ccall unsafe "vec_compare_extzero" c_compare_extzero :: CFun20 CInt
foreign import ccall unsafe "vec_less_or_equal" c_less_or_equal :: CFun20 CInt
foreign import ccall unsafe "vec_partial_sums_less_or_equal" c_partial_sums_less_or_equal :: CFun20 CInt
eqStrict :: WordVec -> WordVec -> Bool
eqStrict :: WordVec -> WordVec -> Bool
eqStrict (WordVec Blob
blob1) (WordVec Blob
blob2) = (CInt
0 CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/= CFun20 CInt -> Blob -> Blob -> CInt
forall a. CFun20 a -> Blob -> Blob -> a
wrapCFun20 CFun20 CInt
c_equal_strict Blob
blob1 Blob
blob2)
eqExtZero :: WordVec -> WordVec -> Bool
eqExtZero :: WordVec -> WordVec -> Bool
eqExtZero (WordVec Blob
blob1) (WordVec Blob
blob2) = (CInt
0 CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/= CFun20 CInt -> Blob -> Blob -> CInt
forall a. CFun20 a -> Blob -> Blob -> a
wrapCFun20 CFun20 CInt
c_equal_extzero Blob
blob1 Blob
blob2)
cintToOrdering :: CInt -> Ordering
cintToOrdering :: CInt -> Ordering
cintToOrdering !CInt
k
| CInt
k CInt -> CInt -> Bool
forall a. Ord a => a -> a -> Bool
< CInt
0 = Ordering
LT
| CInt
k CInt -> CInt -> Bool
forall a. Ord a => a -> a -> Bool
> CInt
0 = Ordering
GT
| Bool
otherwise = Ordering
EQ
cmpStrict :: WordVec -> WordVec -> Ordering
cmpStrict :: WordVec -> WordVec -> Ordering
cmpStrict (WordVec Blob
blob1) (WordVec Blob
blob2) = CInt -> Ordering
cintToOrdering (CInt -> Ordering) -> CInt -> Ordering
forall a b. (a -> b) -> a -> b
$ CFun20 CInt -> Blob -> Blob -> CInt
forall a. CFun20 a -> Blob -> Blob -> a
wrapCFun20 CFun20 CInt
c_compare_strict Blob
blob1 Blob
blob2
cmpExtZero :: WordVec -> WordVec -> Ordering
cmpExtZero :: WordVec -> WordVec -> Ordering
cmpExtZero (WordVec Blob
blob1) (WordVec Blob
blob2) = CInt -> Ordering
cintToOrdering (CInt -> Ordering) -> CInt -> Ordering
forall a b. (a -> b) -> a -> b
$ CFun20 CInt -> Blob -> Blob -> CInt
forall a. CFun20 a -> Blob -> Blob -> a
wrapCFun20 CFun20 CInt
c_compare_extzero Blob
blob1 Blob
blob2
lessOrEqual :: WordVec -> WordVec -> Bool
lessOrEqual :: WordVec -> WordVec -> Bool
lessOrEqual (WordVec Blob
blob1) (WordVec Blob
blob2) = (CInt
0 CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/= CFun20 CInt -> Blob -> Blob -> CInt
forall a. CFun20 a -> Blob -> Blob -> a
wrapCFun20 CFun20 CInt
c_less_or_equal Blob
blob1 Blob
blob2)
partialSumsLessOrEqual :: WordVec -> WordVec -> Bool
partialSumsLessOrEqual :: WordVec -> WordVec -> Bool
partialSumsLessOrEqual (WordVec Blob
blob1) (WordVec Blob
blob2) =
(CInt
0 CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/= CFun20 CInt -> Blob -> Blob -> CInt
forall a. CFun20 a -> Blob -> Blob -> a
wrapCFun20 CFun20 CInt
c_partial_sums_less_or_equal Blob
blob1 Blob
blob2)
foreign import ccall unsafe "vec_add" c_vec_add :: CFun21_
foreign import ccall unsafe "vec_sub_overflow" c_vec_sub_overflow :: CFun21 CInt
add :: WordVec -> WordVec -> WordVec
add :: WordVec -> WordVec -> WordVec
add vec1 :: WordVec
vec1@(WordVec Blob
blob1) vec2 :: WordVec
vec2@(WordVec Blob
blob2) = Blob -> WordVec
WordVec (Blob -> WordVec) -> Blob -> WordVec
forall a b. (a -> b) -> a -> b
$ CFun21_ -> (Int -> Int -> Int) -> Blob -> Blob -> Blob
wrapCFun21_ CFun21_
c_vec_add Int -> Int -> Int
forall p p. p -> p -> Int
f Blob
blob1 Blob
blob2 where
f :: p -> p -> Int
f p
_ p
_ = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int -> Int -> Int
forall a. Bits a => a -> Int -> a
shiftR ( (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
b1 Int
b2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4)Int -> Int -> Int
forall a. Num a => a -> a -> a
*(Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
l1 Int
l2) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
63 ) Int
6
Shape !Int
l1 !Int
b1 = WordVec -> Shape
vecShape WordVec
vec1
Shape !Int
l2 !Int
b2 = WordVec -> Shape
vecShape WordVec
vec2
subtract :: WordVec -> WordVec -> Maybe WordVec
subtract :: WordVec -> WordVec -> Maybe WordVec
subtract vec1 :: WordVec
vec1@(WordVec Blob
blob1) vec2 :: WordVec
vec2@(WordVec Blob
blob2) =
case (CFun21 CInt -> (Int -> Int -> Int) -> Blob -> Blob -> (CInt, Blob)
forall a.
CFun21 a -> (Int -> Int -> Int) -> Blob -> Blob -> (a, Blob)
wrapCFun21 CFun21 CInt
c_vec_sub_overflow Int -> Int -> Int
forall p p. p -> p -> Int
f Blob
blob1 Blob
blob2) of
(CInt
0 , Blob
blob3) -> WordVec -> Maybe WordVec
forall a. a -> Maybe a
Just (Blob -> WordVec
WordVec Blob
blob3)
(CInt
_ , Blob
_ ) -> Maybe WordVec
forall a. Maybe a
Nothing
where
f :: p -> p -> Int
f p
_ p
_ = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int -> Int -> Int
forall a. Bits a => a -> Int -> a
shiftR ( (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
b1 Int
b2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4)Int -> Int -> Int
forall a. Num a => a -> a -> a
*(Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
l1 Int
l2) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
63 ) Int
6
Shape !Int
l1 !Int
b1 = WordVec -> Shape
vecShape WordVec
vec1
Shape !Int
l2 !Int
b2 = WordVec -> Shape
vecShape WordVec
vec2
foreign import ccall unsafe "vec_scale" c_vec_scale :: Word64 -> CFun11_
scale :: Word -> WordVec -> WordVec
scale :: Word -> WordVec -> WordVec
scale Word
s vec :: WordVec
vec@(WordVec Blob
blob) = Blob -> WordVec
WordVec (Blob -> WordVec) -> Blob -> WordVec
forall a b. (a -> b) -> a -> b
$ CFun11_ -> (Int -> Int) -> Blob -> Blob
wrapCFun11_ (Word64 -> CFun11_
c_vec_scale (Word -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
s)) Int -> Int
forall p. p -> Int
f Blob
blob where
f :: p -> Int
f p
_ = Int -> Int -> Int
forall a. Bits a => a -> Int -> a
shiftR (Int
32 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
lenInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
newbits Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
63) Int
6
Shape !Int
len !Int
bits = WordVec -> Shape
vecShape WordVec
vec
bound :: Word
bound = if Word
s Word -> Word -> Bool
forall a. Ord a => a -> a -> Bool
<= Word -> Int -> Word
forall a. Bits a => a -> Int -> a
shiftL Word
1 (Int
64Int -> Int -> Int
forall a. Num a => a -> a -> a
-Int
bits)
then (Word
2Word -> Int -> Word
forall a b. (Num a, Integral b) => a -> b -> a
^Int
bits Word -> Word -> Word
forall a. Num a => a -> a -> a
- Word
1) Word -> Word -> Word
forall a. Num a => a -> a -> a
* Word
s
else (Word
2Word -> Integer -> Word
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
64 Word -> Word -> Word
forall a. Num a => a -> a -> a
- Word
1)
newbits :: Int
newbits = Word -> Int
bitsNeededFor Word
bound
foreign import ccall unsafe "vec_partial_sums" c_vec_partial_sums :: CFun11 Word64
partialSums :: WordVec -> WordVec
partialSums :: WordVec -> WordVec
partialSums vec :: WordVec
vec@(WordVec Blob
blob) = Blob -> WordVec
WordVec (Blob -> WordVec) -> Blob -> WordVec
forall a b. (a -> b) -> a -> b
$ (Word64, Blob) -> Blob
forall a b. (a, b) -> b
snd ((Word64, Blob) -> Blob) -> (Word64, Blob) -> Blob
forall a b. (a -> b) -> a -> b
$ CFun11 Word64 -> (Int -> Int) -> Blob -> (Word64, Blob)
forall a. CFun11 a -> (Int -> Int) -> Blob -> (a, Blob)
wrapCFun11 CFun11 Word64
c_vec_partial_sums Int -> Int
forall p. p -> Int
f Blob
blob where
f :: p -> Int
f p
_ = Int -> Int -> Int
forall a. Bits a => a -> Int -> a
shiftR (Int
32 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
lenInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
newbits Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
63) Int
6
Shape !Int
len !Int
bits = WordVec -> Shape
vecShape WordVec
vec
bound :: Word
bound = if Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int -> Int -> Int
forall a. Bits a => a -> Int -> a
shiftL Int
1 (Int
64Int -> Int -> Int
forall a. Num a => a -> a -> a
-Int
bits)
then (Word
2Word -> Int -> Word
forall a b. (Num a, Integral b) => a -> b -> a
^Int
bits Word -> Word -> Word
forall a. Num a => a -> a -> a
- Word
1) Word -> Word -> Word
forall a. Num a => a -> a -> a
* (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len :: Word)
else (Word
2Word -> Integer -> Word
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
64 Word -> Word -> Word
forall a. Num a => a -> a -> a
- Word
1)
newbits :: Int
newbits = Word -> Int
bitsNeededFor Word
bound
fold :: (a -> Word -> a) -> a -> WordVec -> a
fold :: (a -> Word -> a) -> a -> WordVec -> a
fold a -> Word -> a
f a
x WordVec
v = (a -> Word -> a) -> a -> [Word] -> a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
L.foldl' a -> Word -> a
f a
x (WordVec -> [Word]
toList WordVec
v)
naiveMap :: (Word -> Word) -> WordVec -> WordVec
naiveMap :: (Word -> Word) -> WordVec -> WordVec
naiveMap Word -> Word
f WordVec
u = [Word] -> WordVec
fromList ((Word -> Word) -> [Word] -> [Word]
forall a b. (a -> b) -> [a] -> [b]
map Word -> Word
f ([Word] -> [Word]) -> [Word] -> [Word]
forall a b. (a -> b) -> a -> b
$ WordVec -> [Word]
toList WordVec
u)
boundedMap :: Word -> (Word -> Word) -> WordVec -> WordVec
boundedMap :: Word -> (Word -> Word) -> WordVec -> WordVec
boundedMap Word
bound Word -> Word
f WordVec
vec = Shape -> [Word] -> WordVec
fromList' (Int -> Int -> Shape
Shape Int
l Int
bits) (WordVec -> [Word]
toList WordVec
vec) where
l :: Int
l = WordVec -> Int
vecLen WordVec
vec
bits :: Int
bits = Word -> Int
bitsNeededFor Word
bound
naiveZipWith :: (Word -> Word -> Word) -> WordVec -> WordVec -> WordVec
naiveZipWith :: (Word -> Word -> Word) -> WordVec -> WordVec -> WordVec
naiveZipWith Word -> Word -> Word
f WordVec
u WordVec
v = [Word] -> WordVec
fromList ([Word] -> WordVec) -> [Word] -> WordVec
forall a b. (a -> b) -> a -> b
$ (Word -> Word -> Word) -> [Word] -> [Word] -> [Word]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
L.zipWith Word -> Word -> Word
f (WordVec -> [Word]
toList WordVec
u) (WordVec -> [Word]
toList WordVec
v)
boundedZipWith :: Word -> (Word -> Word -> Word) -> WordVec -> WordVec -> WordVec
boundedZipWith :: Word -> (Word -> Word -> Word) -> WordVec -> WordVec -> WordVec
boundedZipWith Word
bound Word -> Word -> Word
f WordVec
vec1 WordVec
vec2 = Shape -> [Word] -> WordVec
fromList' (Int -> Int -> Shape
Shape Int
l Int
bits) ([Word] -> WordVec) -> [Word] -> WordVec
forall a b. (a -> b) -> a -> b
$ (Word -> Word -> Word) -> [Word] -> [Word] -> [Word]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
L.zipWith Word -> Word -> Word
f (WordVec -> [Word]
toList WordVec
vec1) (WordVec -> [Word]
toList WordVec
vec2) where
l :: Int
l = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min (WordVec -> Int
vecLen WordVec
vec1) (WordVec -> Int
vecLen WordVec
vec2)
bits :: Int
bits = Word -> Int
bitsNeededFor Word
bound
listZipWith :: (Word -> Word -> a) -> WordVec -> WordVec -> [a]
listZipWith :: (Word -> Word -> a) -> WordVec -> WordVec -> [a]
listZipWith Word -> Word -> a
f WordVec
u WordVec
v = (Word -> Word -> a) -> [Word] -> [Word] -> [a]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
L.zipWith Word -> Word -> a
f (WordVec -> [Word]
toList WordVec
u) (WordVec -> [Word]
toList WordVec
v)
bitsNeededFor :: Word -> Int
bitsNeededFor :: Word -> Int
bitsNeededFor = Word -> Int
bitsNeededForHs
bitsNeededFor' :: Word -> Int
bitsNeededFor' :: Word -> Int
bitsNeededFor' = Word -> Int
bitsNeededForHs'
bitsNeededForHs :: Word -> Int
bitsNeededForHs :: Word -> Int
bitsNeededForHs = Int -> Int
roundBits (Int -> Int) -> (Word -> Int) -> Word -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Int
bitsNeededForHs'
bitsNeededForHs' :: Word -> Int
bitsNeededForHs' :: Word -> Int
bitsNeededForHs' Word
bound
| Word
bound Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
== Word
0 = Int
1
| Word
boundWord -> Word -> Word
forall a. Num a => a -> a -> a
+Word
1 Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
== Word
0 = MACHINE_WORD_BITS
| Bool
otherwise = Word -> Int
ceilingLog2 (Word
bound Word -> Word -> Word
forall a. Num a => a -> a -> a
+ Word
1)
where
ceilingLog2 :: Word -> Int
ceilingLog2 :: Word -> Int
ceilingLog2 Word
0 = Int
0
ceilingLog2 Word
n = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Word -> Int
forall t p. (Num t, Num p, Bits t) => t -> p
go (Word
nWord -> Word -> Word
forall a. Num a => a -> a -> a
-Word
1) where
go :: t -> p
go t
0 = -p
1
go t
k = p
1 p -> p -> p
forall a. Num a => a -> a -> a
+ t -> p
go (t -> Int -> t
forall a. Bits a => a -> Int -> a
shiftR t
k Int
1)
roundBits :: Int -> Int
roundBits :: Int -> Int
roundBits Int
0 = Int
4
roundBits Int
k = Int -> Int -> Int
forall a. Bits a => a -> Int -> a
shiftL (Int -> Int -> Int
forall a. Bits a => a -> Int -> a
shiftR (Int
kInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
3) Int
2) Int
2