{-# OPTIONS_GHC -fno-warn-orphans #-}
module Data.StorableVector (
Vector,
empty,
singleton,
pack,
unpack,
packN,
packWith,
unpackWith,
cons,
snoc,
append,
head,
last,
tail,
init,
null,
length,
viewL,
viewR,
switchL,
switchR,
map,
mapIndexed,
reverse,
intersperse,
transpose,
foldl,
foldl',
foldl1,
foldl1',
foldr,
foldr1,
concat,
concatMap,
foldMap,
monoidConcatMap,
any,
all,
maximum,
minimum,
scanl,
scanl1,
scanr,
scanr1,
mapAccumL,
mapAccumR,
crochetL,
crochetLResult,
replicate,
iterateN,
unfoldr,
unfoldrN,
unfoldrResultN,
sample,
take,
drop,
splitAt,
takeWhile,
dropWhile,
span,
spanEnd,
break,
breakEnd,
group,
groupBy,
inits,
tails,
split,
splitWith,
tokens,
sliceVertical,
join,
isPrefixOf,
isSuffixOf,
elem,
notElem,
find,
filter,
index,
elemIndex,
elemIndices,
elemIndexEnd,
findIndex,
findIndices,
count,
findIndexOrEnd,
zip,
zipWith,
zipWith3,
zipWith4,
unzip,
copy,
sieve,
deinterleave,
interleave,
poke,
peek,
hGet,
hPut,
readFile,
writeFile,
appendFile,
) where
import Data.StorableVector.Base
import qualified System.Unsafe as Unsafe
import Control.Exception (assert, bracket, )
import System.IO (IO, FilePath, Handle, IOMode(..),
openBinaryFile, hClose, hFileSize,
hGetBuf, hPutBuf, )
import qualified Foreign.Storable as St
import Foreign.ForeignPtr (ForeignPtr, withForeignPtr, )
import Foreign.Marshal.Array (advancePtr, copyArray, withArray, )
import Foreign.Ptr (Ptr, minusPtr, )
import Foreign.Storable (Storable(sizeOf, alignment,
pokeElemOff, peekElemOff), )
import qualified Test.QuickCheck as QC
import qualified Control.Monad.Trans.Cont as MC
import Control.Monad (mplus, guard, when, liftM2, liftM3, liftM4,
mapM, sequence_, return, (=<<), (>>=), (>>), )
import Data.Functor (fmap, )
import Data.Monoid (Monoid, mempty, mappend, mconcat, )
import Data.Semigroup (Semigroup, (<>), )
import qualified Data.List as List
import qualified Data.List.HT as ListHT
import qualified Data.Strictness.HT as Strict
import Text.Show (show, )
import Data.Function (flip, id, const, ($), (.), )
import Data.List (and, (++), )
import Data.Tuple.HT (mapSnd, )
import Data.Tuple (uncurry, curry, fst, snd, )
import Data.Either (Either(Left, Right), )
import Data.Maybe.HT (toMaybe, )
import Data.Maybe (Maybe(Just, Nothing), maybe, fromMaybe, isJust, )
import Data.Bool (Bool(False, True), not, otherwise, (&&), (||), )
import Data.Ord (Ord, min, max, (<), (<=), (>), (>=), )
import Data.Eq (Eq, (==), (/=), )
import qualified Prelude as P
import Prelude
(String, Int, (*), (-), (+), div, mod,
fromIntegral, error, undefined, )
instance (Storable a, Eq a) => Eq (Vector a) where
== :: Vector a -> Vector a -> Bool
(==) = forall a. (Storable a, Eq a) => Vector a -> Vector a -> Bool
equal
instance (Storable a) => Semigroup (Vector a) where
<> :: Vector a -> Vector a -> Vector a
(<>) = forall a. Storable a => Vector a -> Vector a -> Vector a
append
instance (Storable a) => Monoid (Vector a) where
mempty :: Vector a
mempty = forall a. Storable a => Vector a
empty
mappend :: Vector a -> Vector a -> Vector a
mappend = forall a. Storable a => Vector a -> Vector a -> Vector a
append
mconcat :: [Vector a] -> Vector a
mconcat = forall a. Storable a => [Vector a] -> Vector a
concat
instance (Storable a, QC.Arbitrary a) => QC.Arbitrary (Vector a) where
arbitrary :: Gen (Vector a)
arbitrary = forall a. Storable a => [a] -> Vector a
pack forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` forall a. Arbitrary a => Gen a
QC.arbitrary
equal :: (Storable a, Eq a) => Vector a -> Vector a -> Bool
equal :: forall a. (Storable a, Eq a) => Vector a -> Vector a -> Bool
equal Vector a
a Vector a
b =
forall a. IO a -> a
Unsafe.performIO forall a b. (a -> b) -> a -> b
$
forall a b.
Storable a =>
Vector a -> (Ptr a -> Int -> IO b) -> IO b
withStartPtr Vector a
a forall a b. (a -> b) -> a -> b
$ \Ptr a
paf Int
la ->
forall a b.
Storable a =>
Vector a -> (Ptr a -> Int -> IO b) -> IO b
withStartPtr Vector a
b forall a b. (a -> b) -> a -> b
$ \Ptr a
pbf Int
lb ->
if Int
la forall a. Eq a => a -> a -> Bool
/= Int
lb
then
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
else
if Ptr a
paf forall a. Eq a => a -> a -> Bool
== Ptr a
pbf
then forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
else
let go :: Ptr a -> Ptr a -> Int -> IO Bool
go = forall a b c x. (a -> b -> c -> x) -> a -> b -> c -> x
Strict.arguments3 forall a b. (a -> b) -> a -> b
$ \Ptr a
p Ptr a
q Int
l ->
if Int
lforall a. Eq a => a -> a -> Bool
==Int
0
then forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
else
do a
x <- forall a. Storable a => Ptr a -> IO a
St.peek Ptr a
p
a
y <- forall a. Storable a => Ptr a -> IO a
St.peek Ptr a
q
if a
xforall a. Eq a => a -> a -> Bool
==a
y
then Ptr a -> Ptr a -> Int -> IO Bool
go (forall a. Storable a => Ptr a -> Ptr a
incPtr Ptr a
p) (forall a. Storable a => Ptr a -> Ptr a
incPtr Ptr a
q) (Int
lforall a. Num a => a -> a -> a
-Int
1)
else forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
in Ptr a -> Ptr a -> Int -> IO Bool
go Ptr a
paf Ptr a
pbf Int
la
{-# INLINE equal #-}
empty :: (Storable a) => Vector a
empty :: forall a. Storable a => Vector a
empty = forall a. Storable a => Int -> (Ptr a -> IO ()) -> Vector a
unsafeCreate Int
0 forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return ()
{-# NOINLINE empty #-}
singleton :: (Storable a) => a -> Vector a
singleton :: forall a. Storable a => a -> Vector a
singleton a
c = forall a. Storable a => Int -> (Ptr a -> IO ()) -> Vector a
unsafeCreate Int
1 forall a b. (a -> b) -> a -> b
$ \Ptr a
p -> forall a. Storable a => Ptr a -> a -> IO ()
St.poke Ptr a
p a
c
{-# INLINE singleton #-}
pack :: (Storable a) => [a] -> Vector a
pack :: forall a. Storable a => [a] -> Vector a
pack [a]
str = forall a. Storable a => Int -> (Ptr a -> IO ()) -> Vector a
unsafeCreate (forall (t :: * -> *) a. Foldable t => t a -> Int
P.length [a]
str) forall a b. (a -> b) -> a -> b
$ \Ptr a
p -> Ptr a -> [a] -> IO ()
go Ptr a
p [a]
str
where
go :: Ptr a -> [a] -> IO ()
go = forall a b x. (a -> b -> x) -> a -> b -> x
Strict.arguments2 forall a b. (a -> b) -> a -> b
$ \Ptr a
p ->
forall b a. b -> (a -> [a] -> b) -> [a] -> b
ListHT.switchL
(forall (m :: * -> *) a. Monad m => a -> m a
return ())
(\a
x [a]
xs -> forall a. Storable a => Ptr a -> a -> IO ()
St.poke Ptr a
p a
x forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Ptr a -> [a] -> IO ()
go (forall a. Storable a => Ptr a -> Ptr a
incPtr Ptr a
p) [a]
xs)
packN :: (Storable a) => Int -> [a] -> (Vector a, [a])
packN :: forall a. Storable a => Int -> [a] -> (Vector a, [a])
packN Int
n =
forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd (forall a. a -> Maybe a -> a
fromMaybe []) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a.
Storable b =>
Int -> (a -> Maybe (b, a)) -> a -> (Vector b, Maybe a)
unfoldrN Int
n forall a. [a] -> Maybe (a, [a])
ListHT.viewL
unpack :: (Storable a) => Vector a -> [a]
unpack :: forall a. Storable a => Vector a -> [a]
unpack = forall a b. Storable a => (a -> b -> b) -> b -> Vector a -> b
foldr (:) []
{-# INLINE unpack #-}
packWith :: (Storable b) => (a -> b) -> [a] -> Vector b
packWith :: forall b a. Storable b => (a -> b) -> [a] -> Vector b
packWith a -> b
k [a]
str = forall a. Storable a => Int -> (Ptr a -> IO ()) -> Vector a
unsafeCreate (forall (t :: * -> *) a. Foldable t => t a -> Int
P.length [a]
str) forall a b. (a -> b) -> a -> b
$ \Ptr b
p -> Ptr b -> [a] -> IO ()
go Ptr b
p [a]
str
where
go :: Ptr b -> [a] -> IO ()
go = forall a b x. (a -> b -> x) -> a -> b -> x
Strict.arguments2 forall a b. (a -> b) -> a -> b
$ \Ptr b
p ->
forall b a. b -> (a -> [a] -> b) -> [a] -> b
ListHT.switchL
(forall (m :: * -> *) a. Monad m => a -> m a
return ())
(\a
x [a]
xs -> forall a. Storable a => Ptr a -> a -> IO ()
St.poke Ptr b
p (a -> b
k a
x) forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Ptr b -> [a] -> IO ()
go (forall a. Storable a => Ptr a -> Ptr a
incPtr Ptr b
p) [a]
xs)
{-# INLINE packWith #-}
unpackWith :: (Storable a) => (a -> b) -> Vector a -> [b]
unpackWith :: forall a b. Storable a => (a -> b) -> Vector a -> [b]
unpackWith a -> b
f = forall a b. Storable a => (a -> b -> b) -> b -> Vector a -> b
foldr ((:) forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f) []
{-# INLINE unpackWith #-}
null :: Vector a -> Bool
null :: forall a. Vector a -> Bool
null (SV ForeignPtr a
_ Int
_ Int
l) = forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
l forall a. Ord a => a -> a -> Bool
>= Int
0) forall a b. (a -> b) -> a -> b
$ Int
l forall a. Ord a => a -> a -> Bool
<= Int
0
{-# INLINE null #-}
length :: Vector a -> Int
length :: forall a. Vector a -> Int
length (SV ForeignPtr a
_ Int
_ Int
l) = forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
l forall a. Ord a => a -> a -> Bool
>= Int
0) forall a b. (a -> b) -> a -> b
$ Int
l
{-# INLINE [1] length #-}
cons :: (Storable a) => a -> Vector a -> Vector a
cons :: forall a. Storable a => a -> Vector a -> Vector a
cons a
c Vector a
v =
forall a b. Storable a => Vector a -> (Ptr a -> Int -> IO b) -> b
unsafeWithStartPtr Vector a
v forall a b. (a -> b) -> a -> b
$ \Ptr a
f Int
l ->
forall a. Storable a => Int -> (Ptr a -> IO ()) -> IO (Vector a)
create (Int
l forall a. Num a => a -> a -> a
+ Int
1) forall a b. (a -> b) -> a -> b
$ \Ptr a
p -> do
forall a. Storable a => Ptr a -> a -> IO ()
St.poke Ptr a
p a
c
forall a. Storable a => Ptr a -> Ptr a -> Int -> IO ()
copyArray (forall a. Storable a => Ptr a -> Ptr a
incPtr Ptr a
p) Ptr a
f (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
l)
{-# INLINE cons #-}
snoc :: (Storable a) => Vector a -> a -> Vector a
snoc :: forall a. Storable a => Vector a -> a -> Vector a
snoc Vector a
v a
c =
forall a b. Storable a => Vector a -> (Ptr a -> Int -> IO b) -> b
unsafeWithStartPtr Vector a
v forall a b. (a -> b) -> a -> b
$ \Ptr a
f Int
l ->
forall a. Storable a => Int -> (Ptr a -> IO ()) -> IO (Vector a)
create (Int
l forall a. Num a => a -> a -> a
+ Int
1) forall a b. (a -> b) -> a -> b
$ \Ptr a
p -> do
forall a. Storable a => Ptr a -> Ptr a -> Int -> IO ()
copyArray Ptr a
p Ptr a
f Int
l
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr a
p Int
l a
c
{-# INLINE snoc #-}
head :: (Storable a) => Vector a -> a
head :: forall a. Storable a => Vector a -> a
head =
forall a b.
String -> (ForeignPtr a -> Int -> Int -> b) -> Vector a -> b
withNonEmptyVector String
"head" forall a b. (a -> b) -> a -> b
$ \ ForeignPtr a
p Int
s Int
_l -> forall a. Storable a => ForeignPtr a -> Int -> a
foreignPeek ForeignPtr a
p Int
s
{-# INLINE head #-}
tail :: (Storable a) => Vector a -> Vector a
tail :: forall a. Storable a => Vector a -> Vector a
tail =
forall a b.
String -> (ForeignPtr a -> Int -> Int -> b) -> Vector a -> b
withNonEmptyVector String
"tail" forall a b. (a -> b) -> a -> b
$ \ ForeignPtr a
p Int
s Int
l -> forall a. ForeignPtr a -> Int -> Int -> Vector a
SV ForeignPtr a
p (Int
sforall a. Num a => a -> a -> a
+Int
1) (Int
lforall a. Num a => a -> a -> a
-Int
1)
{-# INLINE tail #-}
laxTail :: (Storable a) => Vector a -> Vector a
laxTail :: forall a. Storable a => Vector a -> Vector a
laxTail v :: Vector a
v@(SV ForeignPtr a
fp Int
s Int
l) =
if Int
lforall a. Ord a => a -> a -> Bool
<=Int
0
then Vector a
v
else forall a. ForeignPtr a -> Int -> Int -> Vector a
SV ForeignPtr a
fp (Int
sforall a. Num a => a -> a -> a
+Int
1) (Int
lforall a. Num a => a -> a -> a
-Int
1)
{-# INLINE laxTail #-}
last :: (Storable a) => Vector a -> a
last :: forall a. Storable a => Vector a -> a
last =
forall a b.
String -> (ForeignPtr a -> Int -> Int -> b) -> Vector a -> b
withNonEmptyVector String
"last" forall a b. (a -> b) -> a -> b
$ \ ForeignPtr a
p Int
s Int
l -> forall a. Storable a => ForeignPtr a -> Int -> a
foreignPeek ForeignPtr a
p (Int
sforall a. Num a => a -> a -> a
+Int
lforall a. Num a => a -> a -> a
-Int
1)
{-# INLINE last #-}
init :: Vector a -> Vector a
init :: forall a. Vector a -> Vector a
init =
forall a b.
String -> (ForeignPtr a -> Int -> Int -> b) -> Vector a -> b
withNonEmptyVector String
"init" forall a b. (a -> b) -> a -> b
$ \ ForeignPtr a
p Int
s Int
l -> forall a. ForeignPtr a -> Int -> Int -> Vector a
SV ForeignPtr a
p Int
s (Int
lforall a. Num a => a -> a -> a
-Int
1)
{-# INLINE init #-}
append :: (Storable a) => Vector a -> Vector a -> Vector a
append :: forall a. Storable a => Vector a -> Vector a -> Vector a
append Vector a
xs Vector a
ys = forall a. Storable a => [Vector a] -> Vector a
concat [Vector a
xs,Vector a
ys]
{-# INLINE append #-}
map :: (Storable a, Storable b) => (a -> b) -> Vector a -> Vector b
map :: forall a b.
(Storable a, Storable b) =>
(a -> b) -> Vector a -> Vector b
map a -> b
f Vector a
v =
forall a b. Storable a => Vector a -> (Ptr a -> Int -> IO b) -> b
unsafeWithStartPtr Vector a
v forall a b. (a -> b) -> a -> b
$ \Ptr a
a Int
len ->
forall a. Storable a => Int -> (Ptr a -> IO ()) -> IO (Vector a)
create Int
len forall a b. (a -> b) -> a -> b
$ \Ptr b
p ->
let go :: Int -> Ptr a -> Ptr b -> IO ()
go = forall a b c x. (a -> b -> c -> x) -> a -> b -> c -> x
Strict.arguments3 forall a b. (a -> b) -> a -> b
$
\ Int
n Ptr a
p1 Ptr b
p2 ->
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
nforall a. Ord a => a -> a -> Bool
>Int
0) forall a b. (a -> b) -> a -> b
$
do forall a. Storable a => Ptr a -> a -> IO ()
St.poke Ptr b
p2 forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall a. Storable a => Ptr a -> IO a
St.peek Ptr a
p1
Int -> Ptr a -> Ptr b -> IO ()
go (Int
nforall a. Num a => a -> a -> a
-Int
1) (forall a. Storable a => Ptr a -> Ptr a
incPtr Ptr a
p1) (forall a. Storable a => Ptr a -> Ptr a
incPtr Ptr b
p2)
in Int -> Ptr a -> Ptr b -> IO ()
go Int
len Ptr a
a Ptr b
p
{-# INLINE map #-}
reverse :: (Storable a) => Vector a -> Vector a
reverse :: forall a. Storable a => Vector a -> Vector a
reverse Vector a
v =
forall a b. Storable a => Vector a -> (Ptr a -> Int -> IO b) -> b
unsafeWithStartPtr Vector a
v forall a b. (a -> b) -> a -> b
$ \Ptr a
f Int
l ->
forall a. Storable a => Int -> (Ptr a -> IO ()) -> IO (Vector a)
create Int
l forall a b. (a -> b) -> a -> b
$ \Ptr a
p ->
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_ [forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr a
f Int
i forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr a
p (Int
l forall a. Num a => a -> a -> a
- Int
i forall a. Num a => a -> a -> a
- Int
1)
| Int
i <- [Int
0 .. Int
l forall a. Num a => a -> a -> a
- Int
1]]
intersperse :: (Storable a) => a -> Vector a -> Vector a
intersperse :: forall a. Storable a => a -> Vector a -> Vector a
intersperse a
c = forall a. Storable a => [a] -> Vector a
pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> [a] -> [a]
List.intersperse a
c forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Storable a => Vector a -> [a]
unpack
transpose :: (Storable a) => [Vector a] -> [Vector a]
transpose :: forall a. Storable a => [Vector a] -> [Vector a]
transpose [Vector a]
ps = forall a b. (a -> b) -> [a] -> [b]
P.map forall a. Storable a => [a] -> Vector a
pack (forall a. [[a]] -> [[a]]
List.transpose (forall a b. (a -> b) -> [a] -> [b]
P.map forall a. Storable a => Vector a -> [a]
unpack [Vector a]
ps))
foldl :: (Storable a) => (b -> a -> b) -> b -> Vector a -> b
foldl :: forall a b. Storable a => (b -> a -> b) -> b -> Vector a -> b
foldl b -> a -> b
f b
v Vector a
xs =
forall a b. Storable a => (a -> b -> b) -> b -> Vector a -> b
foldr (\a
x b -> b
k b
acc -> b -> b
k (b -> a -> b
f b
acc a
x)) forall a. a -> a
id Vector a
xs b
v
{-# INLINE foldl #-}
foldl' :: (Storable a) => (b -> a -> b) -> b -> Vector a -> b
foldl' :: forall a b. Storable a => (b -> a -> b) -> b -> Vector a -> b
foldl' b -> a -> b
f b
b Vector a
v =
forall a. IO a -> a
Unsafe.performIO forall a b. (a -> b) -> a -> b
$ forall a b.
Storable a =>
Vector a -> (Ptr a -> Int -> IO b) -> IO b
withStartPtr Vector a
v forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr Int
l ->
let q :: Ptr a
q = Ptr a
ptr forall a. Storable a => Ptr a -> Int -> Ptr a
`advancePtr` Int
l
go :: Ptr a -> b -> IO b
go = forall a b x. (a -> b -> x) -> a -> b -> x
Strict.arguments2 forall a b. (a -> b) -> a -> b
$ \Ptr a
p b
z ->
if Ptr a
p forall a. Eq a => a -> a -> Bool
== Ptr a
q
then forall (m :: * -> *) a. Monad m => a -> m a
return b
z
else Ptr a -> b -> IO b
go (forall a. Storable a => Ptr a -> Ptr a
incPtr Ptr a
p) forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> a -> b
f b
z forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall a. Storable a => Ptr a -> IO a
St.peek Ptr a
p
in Ptr a -> b -> IO b
go Ptr a
ptr b
b
{-# INLINE foldl' #-}
foldr :: (Storable a) => (a -> b -> b) -> b -> Vector a -> b
foldr :: forall a b. Storable a => (a -> b -> b) -> b -> Vector a -> b
foldr = forall a b. Storable a => (a -> b -> b) -> b -> Vector a -> b
foldrByLoop
{-# INLINE foldr #-}
foldrByLoop :: (Storable a) => (a -> b -> b) -> b -> Vector a -> b
foldrByLoop :: forall a b. Storable a => (a -> b -> b) -> b -> Vector a -> b
foldrByLoop a -> b -> b
f b
z (SV ForeignPtr a
fp Int
s Int
l) =
let end :: Int
end = Int
sforall a. Num a => a -> a -> a
+Int
l
go :: Int -> b
go = forall a x. (a -> x) -> a -> x
Strict.arguments1 forall a b. (a -> b) -> a -> b
$ \Int
k ->
if Int
kforall a. Ord a => a -> a -> Bool
<Int
end
then a -> b -> b
f (forall a. Storable a => ForeignPtr a -> Int -> a
foreignPeek ForeignPtr a
fp Int
k) (Int -> b
go (Int -> Int
succ Int
k))
else b
z
in Int -> b
go Int
s
{-# INLINE foldrByLoop #-}
foldl1 :: (Storable a) => (a -> a -> a) -> Vector a -> a
foldl1 :: forall a. Storable a => (a -> a -> a) -> Vector a -> a
foldl1 a -> a -> a
f =
forall a b.
Storable a =>
b -> (a -> Vector a -> b) -> Vector a -> b
switchL
(forall a. String -> a
errorEmpty String
"foldl1")
(forall a b. Storable a => (b -> a -> b) -> b -> Vector a -> b
foldl a -> a -> a
f)
{-# INLINE foldl1 #-}
foldl1' :: (Storable a) => (a -> a -> a) -> Vector a -> a
foldl1' :: forall a. Storable a => (a -> a -> a) -> Vector a -> a
foldl1' a -> a -> a
f =
forall a b.
Storable a =>
b -> (a -> Vector a -> b) -> Vector a -> b
switchL
(forall a. String -> a
errorEmpty String
"foldl1'")
(forall a b. Storable a => (b -> a -> b) -> b -> Vector a -> b
foldl' a -> a -> a
f)
{-# INLINE foldl1' #-}
foldr1 :: (Storable a) => (a -> a -> a) -> Vector a -> a
foldr1 :: forall a. Storable a => (a -> a -> a) -> Vector a -> a
foldr1 a -> a -> a
f =
forall a b.
Storable a =>
b -> (Vector a -> a -> b) -> Vector a -> b
switchR
(forall a. String -> a
errorEmpty String
"foldr1")
(forall a b c. (a -> b -> c) -> b -> a -> c
flip (forall a b. Storable a => (a -> b -> b) -> b -> Vector a -> b
foldr a -> a -> a
f))
{-# INLINE foldr1 #-}
concat :: (Storable a) => [Vector a] -> Vector a
concat :: forall a. Storable a => [Vector a] -> Vector a
concat = forall a. Storable a => [Vector a] -> Vector a
concatCore forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
List.filter (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Vector a -> Bool
null)
concatCore :: (Storable a) => [Vector a] -> Vector a
concatCore :: forall a. Storable a => [Vector a] -> Vector a
concatCore [] = forall a. Storable a => Vector a
empty
concatCore [Vector a
ps] = Vector a
ps
concatCore [Vector a]
xs = forall a. Storable a => Int -> (Ptr a -> IO ()) -> Vector a
unsafeCreate Int
len forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr -> Ptr a -> [Vector a] -> IO ()
go Ptr a
ptr [Vector a]
xs
where len :: Int
len = forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
P.sum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
P.map forall a. Vector a -> Int
length forall a b. (a -> b) -> a -> b
$ [Vector a]
xs
go :: Ptr a -> [Vector a] -> IO ()
go =
forall a b x. (a -> b -> x) -> a -> b -> x
Strict.arguments2 forall a b. (a -> b) -> a -> b
$ \Ptr a
ptr ->
forall b a. b -> (a -> [a] -> b) -> [a] -> b
ListHT.switchL
(forall (m :: * -> *) a. Monad m => a -> m a
return ())
(\Vector a
v [Vector a]
ps -> do
forall a b.
Storable a =>
Vector a -> (Ptr a -> Int -> IO b) -> IO b
withStartPtr Vector a
v forall a b. (a -> b) -> a -> b
$ forall a. Storable a => Ptr a -> Ptr a -> Int -> IO ()
copyArray Ptr a
ptr
Ptr a -> [Vector a] -> IO ()
go (Ptr a
ptr forall a. Storable a => Ptr a -> Int -> Ptr a
`advancePtr` forall a. Vector a -> Int
length Vector a
v) [Vector a]
ps)
concatMap :: (Storable a, Storable b) => (a -> Vector b) -> Vector a -> Vector b
concatMap :: forall a b.
(Storable a, Storable b) =>
(a -> Vector b) -> Vector a -> Vector b
concatMap a -> Vector b
f = forall a. Storable a => [Vector a] -> Vector a
concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. Storable a => (a -> b) -> Vector a -> [b]
unpackWith a -> Vector b
f
{-# INLINE concatMap #-}
foldMap :: (Storable a, Monoid m) => (a -> m) -> Vector a -> m
foldMap :: forall a m. (Storable a, Monoid m) => (a -> m) -> Vector a -> m
foldMap a -> m
f = forall a b. Storable a => (a -> b -> b) -> b -> Vector a -> b
foldr (forall a. Monoid a => a -> a -> a
mappend forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> m
f) forall a. Monoid a => a
mempty
{-# INLINE foldMap #-}
{-# DEPRECATED monoidConcatMap "Use foldMap instead." #-}
monoidConcatMap :: (Storable a, Monoid m) => (a -> m) -> Vector a -> m
monoidConcatMap :: forall a m. (Storable a, Monoid m) => (a -> m) -> Vector a -> m
monoidConcatMap = forall a m. (Storable a, Monoid m) => (a -> m) -> Vector a -> m
foldMap
{-# INLINE monoidConcatMap #-}
any :: (Storable a) => (a -> Bool) -> Vector a -> Bool
any :: forall a. Storable a => (a -> Bool) -> Vector a -> Bool
any a -> Bool
f = forall a b. Storable a => (a -> b -> b) -> b -> Vector a -> b
foldr (Bool -> Bool -> Bool
(||) forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f) Bool
False
{-# INLINE any #-}
all :: (Storable a) => (a -> Bool) -> Vector a -> Bool
all :: forall a. Storable a => (a -> Bool) -> Vector a -> Bool
all a -> Bool
f = forall a b. Storable a => (a -> b -> b) -> b -> Vector a -> b
foldr (Bool -> Bool -> Bool
(&&) forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f) Bool
True
{-# INLINE all #-}
maximum :: (Storable a, Ord a) => Vector a -> a
maximum :: forall a. (Storable a, Ord a) => Vector a -> a
maximum = forall a. Storable a => (a -> a -> a) -> Vector a -> a
foldl1' forall a. Ord a => a -> a -> a
max
minimum :: (Storable a, Ord a) => Vector a -> a
minimum :: forall a. (Storable a, Ord a) => Vector a -> a
minimum = forall a. Storable a => (a -> a -> a) -> Vector a -> a
foldl1' forall a. Ord a => a -> a -> a
min
switchL :: Storable a => b -> (a -> Vector a -> b) -> Vector a -> b
switchL :: forall a b.
Storable a =>
b -> (a -> Vector a -> b) -> Vector a -> b
switchL b
n a -> Vector a -> b
j Vector a
x =
if forall a. Vector a -> Bool
null Vector a
x
then b
n
else a -> Vector a -> b
j (forall a. Storable a => Vector a -> a
unsafeHead Vector a
x) (forall a. Storable a => Vector a -> Vector a
unsafeTail Vector a
x)
{-# INLINE switchL #-}
switchR :: Storable a => b -> (Vector a -> a -> b) -> Vector a -> b
switchR :: forall a b.
Storable a =>
b -> (Vector a -> a -> b) -> Vector a -> b
switchR b
n Vector a -> a -> b
j Vector a
x =
if forall a. Vector a -> Bool
null Vector a
x
then b
n
else Vector a -> a -> b
j (forall a. Storable a => Vector a -> Vector a
unsafeInit Vector a
x) (forall a. Storable a => Vector a -> a
unsafeLast Vector a
x)
{-# INLINE switchR #-}
viewL :: Storable a => Vector a -> Maybe (a, Vector a)
viewL :: forall a. Storable a => Vector a -> Maybe (a, Vector a)
viewL = forall a b.
Storable a =>
b -> (a -> Vector a -> b) -> Vector a -> b
switchL forall a. Maybe a
Nothing (forall a b c. ((a, b) -> c) -> a -> b -> c
curry forall a. a -> Maybe a
Just)
{-# INLINE viewL #-}
viewR :: Storable a => Vector a -> Maybe (Vector a, a)
viewR :: forall a. Storable a => Vector a -> Maybe (Vector a, a)
viewR = forall a b.
Storable a =>
b -> (Vector a -> a -> b) -> Vector a -> b
switchR forall a. Maybe a
Nothing (forall a b c. ((a, b) -> c) -> a -> b -> c
curry forall a. a -> Maybe a
Just)
{-# INLINE viewR #-}
mapAccumL :: (Storable a, Storable b) => (acc -> a -> (acc, b)) -> acc -> Vector a -> (acc, Vector b)
mapAccumL :: forall a b acc.
(Storable a, Storable b) =>
(acc -> a -> (acc, b)) -> acc -> Vector a -> (acc, Vector b)
mapAccumL acc -> a -> (acc, b)
f acc
acc0 Vector a
as0 =
let (Vector b
bs, Just (acc
acc2, Vector a
_)) =
forall b a.
Storable b =>
Int -> (a -> Maybe (b, a)) -> a -> (Vector b, Maybe a)
unfoldrN (forall a. Vector a -> Int
length Vector a
as0)
(\(acc
acc,Vector a
as) ->
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
(\(a
asHead,Vector a
asTail) ->
let (acc
acc1,b
b) = acc -> a -> (acc, b)
f acc
acc a
asHead
in (b
b, (acc
acc1, Vector a
asTail)))
(forall a. Storable a => Vector a -> Maybe (a, Vector a)
viewL Vector a
as))
(acc
acc0,Vector a
as0)
in (acc
acc2, Vector b
bs)
{-# INLINE mapAccumL #-}
mapAccumR :: (Storable a, Storable b) => (acc -> a -> (acc, b)) -> acc -> Vector a -> (acc, Vector b)
mapAccumR :: forall a b acc.
(Storable a, Storable b) =>
(acc -> a -> (acc, b)) -> acc -> Vector a -> (acc, Vector b)
mapAccumR acc -> a -> (acc, b)
f acc
acc0 Vector a
as0 =
let (Vector b
bs, Just (acc
acc2, Vector a
_)) =
forall b a.
Storable b =>
Int -> (a -> Maybe (b, a)) -> a -> (Vector b, Maybe a)
unfoldlN (forall a. Vector a -> Int
length Vector a
as0)
(\(acc
acc,Vector a
as) ->
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
(\(Vector a
asInit,a
asLast) ->
let (acc
acc1,b
b) = acc -> a -> (acc, b)
f acc
acc a
asLast
in (b
b, (acc
acc1, Vector a
asInit)))
(forall a. Storable a => Vector a -> Maybe (Vector a, a)
viewR Vector a
as))
(acc
acc0,Vector a
as0)
in (acc
acc2, Vector b
bs)
{-# INLINE mapAccumR #-}
crochetLResult ::
(Storable x, Storable y) =>
(x -> acc -> Maybe (y, acc))
-> acc
-> Vector x
-> (Vector y, Maybe acc)
crochetLResult :: forall x y acc.
(Storable x, Storable y) =>
(x -> acc -> Maybe (y, acc))
-> acc -> Vector x -> (Vector y, Maybe acc)
crochetLResult x -> acc -> Maybe (y, acc)
f acc
acc0 Vector x
x0 =
forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> a
fst) forall a b. (a -> b) -> a -> b
$
forall b a.
Storable b =>
Int -> (a -> Maybe (b, a)) -> a -> (Vector b, Maybe a)
unfoldrN
(forall a. Vector a -> Int
length Vector x
x0)
(\(acc
acc,Vector x
xt) ->
do (x
x,Vector x
xs) <- forall a. Storable a => Vector a -> Maybe (a, Vector a)
viewL Vector x
xt
(y
y,acc
acc') <- x -> acc -> Maybe (y, acc)
f x
x acc
acc
forall (m :: * -> *) a. Monad m => a -> m a
return (y
y, (acc
acc',Vector x
xs)))
(acc
acc0, Vector x
x0)
{-# INLINE crochetLResult #-}
crochetL ::
(Storable x, Storable y) =>
(x -> acc -> Maybe (y, acc))
-> acc
-> Vector x
-> Vector y
crochetL :: forall x y acc.
(Storable x, Storable y) =>
(x -> acc -> Maybe (y, acc)) -> acc -> Vector x -> Vector y
crochetL x -> acc -> Maybe (y, acc)
f acc
acc = forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall x y acc.
(Storable x, Storable y) =>
(x -> acc -> Maybe (y, acc))
-> acc -> Vector x -> (Vector y, Maybe acc)
crochetLResult x -> acc -> Maybe (y, acc)
f acc
acc
{-# INLINE crochetL #-}
mapIndexed :: (Storable a, Storable b) => (Int -> a -> b) -> Vector a -> Vector b
mapIndexed :: forall a b.
(Storable a, Storable b) =>
(Int -> a -> b) -> Vector a -> Vector b
mapIndexed Int -> a -> b
f = forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b acc.
(Storable a, Storable b) =>
(acc -> a -> (acc, b)) -> acc -> Vector a -> (acc, Vector b)
mapAccumL (\Int
i a
e -> (Int
i forall a. Num a => a -> a -> a
+ Int
1, Int -> a -> b
f Int
i a
e)) Int
0
{-# INLINE mapIndexed #-}
scanl :: (Storable a, Storable b) => (a -> b -> a) -> a -> Vector b -> Vector a
scanl :: forall a b.
(Storable a, Storable b) =>
(a -> b -> a) -> a -> Vector b -> Vector a
scanl a -> b -> a
f a
acc0 Vector b
as0 =
forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$
forall b a.
Storable b =>
Int -> (a -> Maybe (b, a)) -> a -> (Vector b, Maybe a)
unfoldrN (Int -> Int
succ (forall a. Vector a -> Int
length Vector b
as0))
(forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a -> b) -> a -> b
$ \(a
acc,Vector b
as) ->
(a
acc,
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
(\(b
asHead,Vector b
asTail) ->
(a -> b -> a
f a
acc b
asHead, Vector b
asTail))
(forall a. Storable a => Vector a -> Maybe (a, Vector a)
viewL Vector b
as)))
(forall a. a -> Maybe a
Just (a
acc0, Vector b
as0))
{-# INLINE scanl #-}
scanl1 :: (Storable a) => (a -> a -> a) -> Vector a -> Vector a
scanl1 :: forall a. Storable a => (a -> a -> a) -> Vector a -> Vector a
scanl1 a -> a -> a
f = forall a b.
Storable a =>
b -> (a -> Vector a -> b) -> Vector a -> b
switchL forall a. Storable a => Vector a
empty (forall a b.
(Storable a, Storable b) =>
(a -> b -> a) -> a -> Vector b -> Vector a
scanl a -> a -> a
f)
{-# INLINE scanl1 #-}
scanr :: (Storable a, Storable b) => (a -> b -> b) -> b -> Vector a -> Vector b
scanr :: forall a b.
(Storable a, Storable b) =>
(a -> b -> b) -> b -> Vector a -> Vector b
scanr a -> b -> b
f b
acc0 Vector a
as0 =
forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$
forall b a.
Storable b =>
Int -> (a -> Maybe (b, a)) -> a -> (Vector b, Maybe a)
unfoldlN (Int -> Int
succ (forall a. Vector a -> Int
length Vector a
as0))
(forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a -> b) -> a -> b
$ \(b
acc,Vector a
as) ->
(b
acc,
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap
(\(Vector a
asInit,a
asLast) ->
(a -> b -> b
f a
asLast b
acc, Vector a
asInit))
(forall a. Storable a => Vector a -> Maybe (Vector a, a)
viewR Vector a
as)))
(forall a. a -> Maybe a
Just (b
acc0, Vector a
as0))
{-# INLINE scanr #-}
scanr1 :: (Storable a) => (a -> a -> a) -> Vector a -> Vector a
scanr1 :: forall a. Storable a => (a -> a -> a) -> Vector a -> Vector a
scanr1 a -> a -> a
f = forall a b.
Storable a =>
b -> (Vector a -> a -> b) -> Vector a -> b
switchR forall a. Storable a => Vector a
empty (forall a b c. (a -> b -> c) -> b -> a -> c
flip (forall a b.
(Storable a, Storable b) =>
(a -> b -> a) -> a -> Vector b -> Vector a
scanl a -> a -> a
f))
{-# INLINE scanr1 #-}
replicate :: (Storable a) => Int -> a -> Vector a
replicate :: forall a. Storable a => Int -> a -> Vector a
replicate Int
n a
c =
if Int
n forall a. Ord a => a -> a -> Bool
<= Int
0
then forall a. Storable a => Vector a
empty
else forall a. Storable a => Int -> (Ptr a -> IO ()) -> Vector a
unsafeCreate Int
n forall a b. (a -> b) -> a -> b
$
let go :: Int -> Ptr a -> IO ()
go = forall a b x. (a -> b -> x) -> a -> b -> x
Strict.arguments2 forall a b. (a -> b) -> a -> b
$ \Int
i Ptr a
p ->
if Int
i forall a. Eq a => a -> a -> Bool
== Int
0
then forall (m :: * -> *) a. Monad m => a -> m a
return ()
else forall a. Storable a => Ptr a -> a -> IO ()
St.poke Ptr a
p a
c forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Int -> Ptr a -> IO ()
go (Int -> Int
pred Int
i) (forall a. Storable a => Ptr a -> Ptr a
incPtr Ptr a
p)
in Int -> Ptr a -> IO ()
go Int
n
{-# INLINE replicate #-}
iterateN :: (Storable a) => Int -> (a -> a) -> a -> Vector a
iterateN :: forall a. Storable a => Int -> (a -> a) -> a -> Vector a
iterateN Int
n a -> a
f =
forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a.
Storable b =>
Int -> (a -> Maybe (b, a)) -> a -> (Vector b, Maybe a)
unfoldrN Int
n (\a
a -> forall a. a -> Maybe a
Just (a
a, a -> a
f a
a))
{-# INLINE iterateN #-}
unfoldr :: (Storable b) => (a -> Maybe (b, a)) -> a -> Vector b
unfoldr :: forall b a. Storable b => (a -> Maybe (b, a)) -> a -> Vector b
unfoldr a -> Maybe (b, a)
f = forall a. Storable a => [Vector a] -> Vector a
concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> a -> [Vector b]
unfoldChunk Int
32 Int
64
where unfoldChunk :: Int -> Int -> a -> [Vector b]
unfoldChunk Int
n Int
n' a
x =
case forall b a.
Storable b =>
Int -> (a -> Maybe (b, a)) -> a -> (Vector b, Maybe a)
unfoldrN Int
n a -> Maybe (b, a)
f a
x of
(Vector b
s, Maybe a
mx) -> Vector b
s forall a. a -> [a] -> [a]
: forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (Int -> Int -> a -> [Vector b]
unfoldChunk Int
n' (Int
nforall a. Num a => a -> a -> a
+Int
n')) Maybe a
mx
{-# INLINE unfoldr #-}
unfoldrN :: (Storable b) => Int -> (a -> Maybe (b, a)) -> a -> (Vector b, Maybe a)
unfoldrN :: forall b a.
Storable b =>
Int -> (a -> Maybe (b, a)) -> a -> (Vector b, Maybe a)
unfoldrN Int
n a -> Maybe (b, a)
f a
x0 =
if Int
n forall a. Ord a => a -> a -> Bool
<= Int
0
then (forall a. Storable a => Vector a
empty, forall a. a -> Maybe a
Just a
x0)
else forall a. IO a -> a
Unsafe.performIO forall a b. (a -> b) -> a -> b
$ forall a b.
Storable a =>
Int -> (Ptr a -> IO (Int, Int, b)) -> IO (Vector a, b)
createAndTrim' Int
n forall a b. (a -> b) -> a -> b
$ \Ptr b
p -> Ptr b -> Int -> a -> IO (Int, Int, Maybe a)
go Ptr b
p Int
n a
x0
where
go :: Ptr b -> Int -> a -> IO (Int, Int, Maybe a)
go = forall a b x. (a -> b -> x) -> a -> b -> x
Strict.arguments2 forall a b. (a -> b) -> a -> b
$ \Ptr b
p Int
i -> \a
x ->
if Int
i forall a. Eq a => a -> a -> Bool
== Int
0
then forall (m :: * -> *) a. Monad m => a -> m a
return (Int
0, Int
nforall a. Num a => a -> a -> a
-Int
i, forall a. a -> Maybe a
Just a
x)
else
case a -> Maybe (b, a)
f a
x of
Maybe (b, a)
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return (Int
0, Int
nforall a. Num a => a -> a -> a
-Int
i, forall a. Maybe a
Nothing)
Just (b
w,a
x') -> do forall a. Storable a => Ptr a -> a -> IO ()
St.poke Ptr b
p b
w
Ptr b -> Int -> a -> IO (Int, Int, Maybe a)
go (forall a. Storable a => Ptr a -> Ptr a
incPtr Ptr b
p) (Int
iforall a. Num a => a -> a -> a
-Int
1) a
x'
{-# INLINE unfoldrN #-}
unfoldrResultN :: (Storable b) => Int -> (a -> c) -> (a -> Either c (b, a)) -> a -> (Vector b, c)
unfoldrResultN :: forall b a c.
Storable b =>
Int -> (a -> c) -> (a -> Either c (b, a)) -> a -> (Vector b, c)
unfoldrResultN Int
i a -> c
g a -> Either c (b, a)
f a
x0 =
if Int
i forall a. Ord a => a -> a -> Bool
<= Int
0
then (forall a. Storable a => Vector a
empty, a -> c
g a
x0)
else forall a. IO a -> a
Unsafe.performIO forall a b. (a -> b) -> a -> b
$ forall a b.
Storable a =>
Int -> (Ptr a -> IO (Int, Int, b)) -> IO (Vector a, b)
createAndTrim' Int
i forall a b. (a -> b) -> a -> b
$ \Ptr b
p -> Ptr b -> Int -> a -> IO (Int, Int, c)
go Ptr b
p Int
0 a
x0
where
go :: Ptr b -> Int -> a -> IO (Int, Int, c)
go = forall a b x. (a -> b -> x) -> a -> b -> x
Strict.arguments2 forall a b. (a -> b) -> a -> b
$ \Ptr b
p Int
n -> \a
a0 ->
if Int
n forall a. Eq a => a -> a -> Bool
== Int
i
then forall (m :: * -> *) a. Monad m => a -> m a
return (Int
0, Int
n, a -> c
g a
a0)
else
case a -> Either c (b, a)
f a
a0 of
Left c
c -> forall (m :: * -> *) a. Monad m => a -> m a
return (Int
0, Int
n, c
c)
Right (b
b,a
a1) -> do forall a. Storable a => Ptr a -> a -> IO ()
St.poke Ptr b
p b
b
Ptr b -> Int -> a -> IO (Int, Int, c)
go (forall a. Storable a => Ptr a -> Ptr a
incPtr Ptr b
p) (Int
nforall a. Num a => a -> a -> a
+Int
1) a
a1
{-# INLINE unfoldrResultN #-}
unfoldlN :: (Storable b) => Int -> (a -> Maybe (b, a)) -> a -> (Vector b, Maybe a)
unfoldlN :: forall b a.
Storable b =>
Int -> (a -> Maybe (b, a)) -> a -> (Vector b, Maybe a)
unfoldlN Int
i a -> Maybe (b, a)
f a
x0
| Int
i forall a. Ord a => a -> a -> Bool
< Int
0 = (forall a. Storable a => Vector a
empty, forall a. a -> Maybe a
Just a
x0)
| Bool
otherwise = forall a. IO a -> a
Unsafe.performIO forall a b. (a -> b) -> a -> b
$ forall a b.
Storable a =>
Int -> (Ptr a -> IO (Int, Int, b)) -> IO (Vector a, b)
createAndTrim' Int
i forall a b. (a -> b) -> a -> b
$ \Ptr b
p -> Ptr b -> Int -> a -> IO (Int, Int, Maybe a)
go (Ptr b
p forall a. Storable a => Ptr a -> Int -> Ptr a
`advancePtr` Int
i) Int
i a
x0
where go :: Ptr b -> Int -> a -> IO (Int, Int, Maybe a)
go = forall a b x. (a -> b -> x) -> a -> b -> x
Strict.arguments2 forall a b. (a -> b) -> a -> b
$ \Ptr b
p Int
n -> \a
x ->
if Int
n forall a. Eq a => a -> a -> Bool
== Int
0
then forall (m :: * -> *) a. Monad m => a -> m a
return (Int
n, Int
i, forall a. a -> Maybe a
Just a
x)
else
case a -> Maybe (b, a)
f a
x of
Maybe (b, a)
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return (Int
n, Int
i, forall a. Maybe a
Nothing)
Just (b
w,a
x') ->
let p' :: Ptr b
p' = Ptr b
p forall a. Storable a => Ptr a -> Int -> Ptr a
`advancePtr` (-Int
1)
in do forall a. Storable a => Ptr a -> a -> IO ()
St.poke Ptr b
p' b
w
Ptr b -> Int -> a -> IO (Int, Int, Maybe a)
go Ptr b
p' (Int
nforall a. Num a => a -> a -> a
-Int
1) a
x'
{-# INLINE unfoldlN #-}
sample :: (Storable a) => Int -> (Int -> a) -> Vector a
sample :: forall a. Storable a => Int -> (Int -> a) -> Vector a
sample Int
n Int -> a
f =
forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ forall b a.
Storable b =>
Int -> (a -> Maybe (b, a)) -> a -> (Vector b, Maybe a)
unfoldrN Int
n (\Int
i -> forall a. a -> Maybe a
Just (Int -> a
f Int
i, Int -> Int
succ Int
i)) Int
0
{-# INLINE sample #-}
take :: (Storable a) => Int -> Vector a -> Vector a
take :: forall a. Storable a => Int -> Vector a -> Vector a
take Int
n ps :: Vector a
ps@(SV ForeignPtr a
x Int
s Int
l)
| Int
n forall a. Ord a => a -> a -> Bool
<= Int
0 = forall a. Storable a => Vector a
empty
| Int
n forall a. Ord a => a -> a -> Bool
>= Int
l = Vector a
ps
| Bool
otherwise = forall a. ForeignPtr a -> Int -> Int -> Vector a
SV ForeignPtr a
x Int
s Int
n
{-# INLINE take #-}
drop :: (Storable a) => Int -> Vector a -> Vector a
drop :: forall a. Storable a => Int -> Vector a -> Vector a
drop Int
n ps :: Vector a
ps@(SV ForeignPtr a
x Int
s Int
l)
| Int
n forall a. Ord a => a -> a -> Bool
<= Int
0 = Vector a
ps
| Int
n forall a. Ord a => a -> a -> Bool
>= Int
l = forall a. Storable a => Vector a
empty
| Bool
otherwise = forall a. ForeignPtr a -> Int -> Int -> Vector a
SV ForeignPtr a
x (Int
sforall a. Num a => a -> a -> a
+Int
n) (Int
lforall a. Num a => a -> a -> a
-Int
n)
{-# INLINE drop #-}
splitAt :: (Storable a) => Int -> Vector a -> (Vector a, Vector a)
splitAt :: forall a. Storable a => Int -> Vector a -> (Vector a, Vector a)
splitAt Int
n ps :: Vector a
ps@(SV ForeignPtr a
x Int
s Int
l)
| Int
n forall a. Ord a => a -> a -> Bool
<= Int
0 = (forall a. Storable a => Vector a
empty, Vector a
ps)
| Int
n forall a. Ord a => a -> a -> Bool
>= Int
l = (Vector a
ps, forall a. Storable a => Vector a
empty)
| Bool
otherwise = (forall a. ForeignPtr a -> Int -> Int -> Vector a
SV ForeignPtr a
x Int
s Int
n, forall a. ForeignPtr a -> Int -> Int -> Vector a
SV ForeignPtr a
x (Int
sforall a. Num a => a -> a -> a
+Int
n) (Int
lforall a. Num a => a -> a -> a
-Int
n))
{-# INLINE splitAt #-}
sliceVertical :: (Storable a) => Int -> Vector a -> [Vector a]
sliceVertical :: forall a. Storable a => Int -> Vector a -> [Vector a]
sliceVertical Int
n =
forall b a. (b -> Maybe (a, b)) -> b -> [a]
List.unfoldr (\Vector a
x -> forall a. Bool -> a -> Maybe a
toMaybe (Bool -> Bool
not (forall a. Vector a -> Bool
null Vector a
x)) (forall a. Storable a => Int -> Vector a -> (Vector a, Vector a)
splitAt Int
n Vector a
x))
{-# INLINE sliceVertical #-}
_sliceVertical :: (Storable a) => Int -> Vector a -> [Vector a]
_sliceVertical :: forall a. Storable a => Int -> Vector a -> [Vector a]
_sliceVertical Int
n Vector a
xs =
forall a b. (a -> b) -> [a] -> [b]
List.map (forall a. Storable a => Int -> Vector a -> Vector a
take Int
n forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. Storable a => Int -> Vector a -> Vector a
drop Vector a
xs) forall a b. (a -> b) -> a -> b
$
forall a. (a -> Bool) -> [a] -> [a]
List.takeWhile (forall a. Ord a => a -> a -> Bool
< forall a. Vector a -> Int
length Vector a
xs) forall a b. (a -> b) -> a -> b
$ forall a. (a -> a) -> a -> [a]
List.iterate (Int
nforall a. Num a => a -> a -> a
+) Int
0
takeWhile :: (Storable a) => (a -> Bool) -> Vector a -> Vector a
takeWhile :: forall a. Storable a => (a -> Bool) -> Vector a -> Vector a
takeWhile a -> Bool
f Vector a
ps = forall a. Storable a => Int -> Vector a -> Vector a
unsafeTake (forall a. Storable a => (a -> Bool) -> Vector a -> Int
findIndexOrEnd (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f) Vector a
ps) Vector a
ps
{-# INLINE takeWhile #-}
dropWhile :: (Storable a) => (a -> Bool) -> Vector a -> Vector a
dropWhile :: forall a. Storable a => (a -> Bool) -> Vector a -> Vector a
dropWhile a -> Bool
f Vector a
ps = forall a. Storable a => Int -> Vector a -> Vector a
unsafeDrop (forall a. Storable a => (a -> Bool) -> Vector a -> Int
findIndexOrEnd (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
f) Vector a
ps) Vector a
ps
{-# INLINE dropWhile #-}
break :: (Storable a) => (a -> Bool) -> Vector a -> (Vector a, Vector a)
break :: forall a.
Storable a =>
(a -> Bool) -> Vector a -> (Vector a, Vector a)
break a -> Bool
p Vector a
ps = case forall a. Storable a => (a -> Bool) -> Vector a -> Int
findIndexOrEnd a -> Bool
p Vector a
ps of Int
n -> (forall a. Storable a => Int -> Vector a -> Vector a
unsafeTake Int
n Vector a
ps, forall a. Storable a => Int -> Vector a -> Vector a
unsafeDrop Int
n Vector a
ps)
{-# INLINE break #-}
breakEnd :: (Storable a) => (a -> Bool) -> Vector a -> (Vector a, Vector a)
breakEnd :: forall a.
Storable a =>
(a -> Bool) -> Vector a -> (Vector a, Vector a)
breakEnd a -> Bool
p Vector a
ps = forall a. Storable a => Int -> Vector a -> (Vector a, Vector a)
splitAt (forall a. Storable a => (a -> Bool) -> Vector a -> Int
findFromEndUntil a -> Bool
p Vector a
ps) Vector a
ps
span :: (Storable a) => (a -> Bool) -> Vector a -> (Vector a, Vector a)
span :: forall a.
Storable a =>
(a -> Bool) -> Vector a -> (Vector a, Vector a)
span a -> Bool
p Vector a
ps = forall a.
Storable a =>
(a -> Bool) -> Vector a -> (Vector a, Vector a)
break (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
p) Vector a
ps
{-# INLINE span #-}
spanEnd :: (Storable a) => (a -> Bool) -> Vector a -> (Vector a, Vector a)
spanEnd :: forall a.
Storable a =>
(a -> Bool) -> Vector a -> (Vector a, Vector a)
spanEnd a -> Bool
p Vector a
ps = forall a. Storable a => Int -> Vector a -> (Vector a, Vector a)
splitAt (forall a. Storable a => (a -> Bool) -> Vector a -> Int
findFromEndUntil (Bool -> Bool
notforall b c a. (b -> c) -> (a -> b) -> a -> c
.a -> Bool
p) Vector a
ps) Vector a
ps
splitWith :: (Storable a) => (a -> Bool) -> Vector a -> [Vector a]
splitWith :: forall a. Storable a => (a -> Bool) -> Vector a -> [Vector a]
splitWith a -> Bool
_ (SV ForeignPtr a
_ Int
_ Int
0) = []
splitWith a -> Bool
p Vector a
ps = Vector a -> [Vector a]
loop Vector a
ps
where
loop :: Vector a -> [Vector a]
loop =
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (:) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall b c a. (b -> c) -> (a, b) -> (a, c)
mapSnd (forall a b.
Storable a =>
b -> (a -> Vector a -> b) -> Vector a -> b
switchL [] (\ a
_ Vector a
t -> Vector a -> [Vector a]
loop Vector a
t)) forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall a.
Storable a =>
(a -> Bool) -> Vector a -> (Vector a, Vector a)
break a -> Bool
p
{-# INLINE splitWith #-}
split :: (Storable a, Eq a) => a -> Vector a -> [Vector a]
split :: forall a. (Storable a, Eq a) => a -> Vector a -> [Vector a]
split a
w Vector a
v = forall a. Storable a => (a -> Bool) -> Vector a -> [Vector a]
splitWith (a
wforall a. Eq a => a -> a -> Bool
==) Vector a
v
{-# INLINE split #-}
tokens :: (Storable a) => (a -> Bool) -> Vector a -> [Vector a]
tokens :: forall a. Storable a => (a -> Bool) -> Vector a -> [Vector a]
tokens a -> Bool
f = forall a. (a -> Bool) -> [a] -> [a]
P.filter (Bool -> Bool
notforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall a. Vector a -> Bool
null) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Storable a => (a -> Bool) -> Vector a -> [Vector a]
splitWith a -> Bool
f
{-# INLINE tokens #-}
group :: (Storable a, Eq a) => Vector a -> [Vector a]
group :: forall a. (Storable a, Eq a) => Vector a -> [Vector a]
group Vector a
xs =
forall a b.
Storable a =>
b -> (a -> Vector a -> b) -> Vector a -> b
switchL []
(\ a
h Vector a
_ ->
let (Vector a
ys, Vector a
zs) = forall a.
Storable a =>
(a -> Bool) -> Vector a -> (Vector a, Vector a)
span (forall a. Eq a => a -> a -> Bool
== a
h) Vector a
xs
in Vector a
ys forall a. a -> [a] -> [a]
: forall a. (Storable a, Eq a) => Vector a -> [Vector a]
group Vector a
zs)
Vector a
xs
groupBy :: (Storable a) => (a -> a -> Bool) -> Vector a -> [Vector a]
groupBy :: forall a. Storable a => (a -> a -> Bool) -> Vector a -> [Vector a]
groupBy a -> a -> Bool
k Vector a
xs =
forall a b.
Storable a =>
b -> (a -> Vector a -> b) -> Vector a -> b
switchL []
(\ a
h Vector a
t ->
let n :: Int
n = Int
1 forall a. Num a => a -> a -> a
+ forall a. Storable a => (a -> Bool) -> Vector a -> Int
findIndexOrEnd (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a -> Bool
k a
h) Vector a
t
in forall a. Storable a => Int -> Vector a -> Vector a
unsafeTake Int
n Vector a
xs forall a. a -> [a] -> [a]
: forall a. Storable a => (a -> a -> Bool) -> Vector a -> [Vector a]
groupBy a -> a -> Bool
k (forall a. Storable a => Int -> Vector a -> Vector a
unsafeDrop Int
n Vector a
xs))
Vector a
xs
{-# INLINE groupBy #-}
join :: (Storable a) => Vector a -> [Vector a] -> Vector a
join :: forall a. Storable a => Vector a -> [Vector a] -> Vector a
join Vector a
s = forall a. Storable a => [Vector a] -> Vector a
concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> [a] -> [a]
List.intersperse Vector a
s
{-# INLINE join #-}
index :: (Storable a) => Vector a -> Int -> a
index :: forall a. Storable a => Vector a -> Int -> a
index Vector a
ps Int
n
| Int
n forall a. Ord a => a -> a -> Bool
< Int
0 = forall a. String -> String -> a
moduleError String
"index" (String
"negative index: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
n)
| Int
n forall a. Ord a => a -> a -> Bool
>= forall a. Vector a -> Int
length Vector a
ps = forall a. String -> String -> a
moduleError String
"index" (String
"index too large: " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
n
forall a. [a] -> [a] -> [a]
++ String
", length = " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall a. Vector a -> Int
length Vector a
ps))
| Bool
otherwise = Vector a
ps forall a. Storable a => Vector a -> Int -> a
`unsafeIndex` Int
n
{-# INLINE index #-}
elemIndex :: (Storable a, Eq a) => a -> Vector a -> Maybe Int
elemIndex :: forall a. (Storable a, Eq a) => a -> Vector a -> Maybe Int
elemIndex a
c = forall a. Storable a => (a -> Bool) -> Vector a -> Maybe Int
findIndex (a
cforall a. Eq a => a -> a -> Bool
==)
{-# INLINE elemIndex #-}
elemIndexEnd :: (Storable a, Eq a) => a -> Vector a -> Maybe Int
elemIndexEnd :: forall a. (Storable a, Eq a) => a -> Vector a -> Maybe Int
elemIndexEnd a
c =
forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall a b. Storable a => (b -> a -> b) -> b -> Vector a -> b
foldl
(\(Maybe Int
ri,Int
i) a
x -> (if a
cforall a. Eq a => a -> a -> Bool
==a
x then forall a. a -> Maybe a
Just Int
i else Maybe Int
ri, Int -> Int
succ Int
i))
(forall a. Maybe a
Nothing,Int
0)
{-# INLINE elemIndexEnd #-}
elemIndices :: (Storable a, Eq a) => a -> Vector a -> [Int]
elemIndices :: forall a. (Storable a, Eq a) => a -> Vector a -> [Int]
elemIndices a
c = forall a. Storable a => (a -> Bool) -> Vector a -> [Int]
findIndices (a
cforall a. Eq a => a -> a -> Bool
==)
{-# INLINE elemIndices #-}
count :: (Storable a, Eq a) => a -> Vector a -> Int
count :: forall a. (Storable a, Eq a) => a -> Vector a -> Int
count a
w =
forall a b. Storable a => (b -> a -> b) -> b -> Vector a -> b
foldl (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a b. (a -> b) -> a -> b
$ \a
c -> if a
cforall a. Eq a => a -> a -> Bool
==a
w then Int -> Int
succ else forall a. a -> a
id) Int
0
{-# INLINE count #-}
findIndex :: (Storable a) => (a -> Bool) -> Vector a -> Maybe Int
findIndex :: forall a. Storable a => (a -> Bool) -> Vector a -> Maybe Int
findIndex a -> Bool
p Vector a
xs =
forall a b. Storable a => (a -> b -> b) -> b -> Vector a -> b
foldr
(\a
x Int -> Maybe Int
k Int
n ->
forall a. Bool -> a -> Maybe a
toMaybe (a -> Bool
p a
x) Int
n forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` Int -> Maybe Int
k (Int -> Int
succ Int
n))
(forall a b. a -> b -> a
const forall a. Maybe a
Nothing) Vector a
xs Int
0
{-# INLINE findIndex #-}
findIndices :: (Storable a) => (a -> Bool) -> Vector a -> [Int]
findIndices :: forall a. Storable a => (a -> Bool) -> Vector a -> [Int]
findIndices a -> Bool
p Vector a
xs =
forall a b. Storable a => (a -> b -> b) -> b -> Vector a -> b
foldr
(\a
x Int -> [Int]
k Int
n ->
(if a -> Bool
p a
x then (Int
nforall a. a -> [a] -> [a]
:) else forall a. a -> a
id)
(Int -> [Int]
k (Int -> Int
succ Int
n)))
(forall a b. a -> b -> a
const []) Vector a
xs Int
0
{-# INLINE findIndices #-}
findIndexOrEnd :: (Storable a) => (a -> Bool) -> Vector a -> Int
findIndexOrEnd :: forall a. Storable a => (a -> Bool) -> Vector a -> Int
findIndexOrEnd a -> Bool
p Vector a
xs =
forall a b. Storable a => (a -> b -> b) -> b -> Vector a -> b
foldr
(\a
x Int -> Int
k Int
n ->
if a -> Bool
p a
x then Int
n else Int -> Int
k (Int -> Int
succ Int
n))
forall a. a -> a
id Vector a
xs Int
0
{-# INLINE findIndexOrEnd #-}
elem :: (Storable a, Eq a) => a -> Vector a -> Bool
elem :: forall a. (Storable a, Eq a) => a -> Vector a -> Bool
elem a
c Vector a
ps = forall a. Maybe a -> Bool
isJust forall a b. (a -> b) -> a -> b
$ forall a. (Storable a, Eq a) => a -> Vector a -> Maybe Int
elemIndex a
c Vector a
ps
{-# INLINE elem #-}
notElem :: (Storable a, Eq a) => a -> Vector a -> Bool
notElem :: forall a. (Storable a, Eq a) => a -> Vector a -> Bool
notElem a
c Vector a
ps = Bool -> Bool
not (forall a. (Storable a, Eq a) => a -> Vector a -> Bool
elem a
c Vector a
ps)
{-# INLINE notElem #-}
filter :: (Storable a) => (a -> Bool) -> Vector a -> Vector a
filter :: forall a. Storable a => (a -> Bool) -> Vector a -> Vector a
filter a -> Bool
p (SV ForeignPtr a
fp Int
s Int
l) =
let end :: Int
end = Int
sforall a. Num a => a -> a -> a
+Int
l
in forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$
forall b a.
Storable b =>
Int -> (a -> Maybe (b, a)) -> a -> (Vector b, Maybe a)
unfoldrN Int
l
(let go :: Int -> Maybe (a, Int)
go = forall a x. (a -> x) -> a -> x
Strict.arguments1 forall a b. (a -> b) -> a -> b
$ \Int
k0 ->
do forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Int
k0forall a. Ord a => a -> a -> Bool
<Int
end)
let x :: a
x = forall a. Storable a => ForeignPtr a -> Int -> a
foreignPeek ForeignPtr a
fp Int
k0
k1 :: Int
k1 = Int -> Int
succ Int
k0
if a -> Bool
p a
x
then forall a. a -> Maybe a
Just (a
x,Int
k1)
else Int -> Maybe (a, Int)
go Int
k1
in Int -> Maybe (a, Int)
go)
Int
s
{-# INLINE filter #-}
find :: (Storable a) => (a -> Bool) -> Vector a -> Maybe a
find :: forall a. Storable a => (a -> Bool) -> Vector a -> Maybe a
find a -> Bool
f Vector a
p = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. Storable a => Vector a -> Int -> a
unsafeIndex Vector a
p) (forall a. Storable a => (a -> Bool) -> Vector a -> Maybe Int
findIndex a -> Bool
f Vector a
p)
{-# INLINE find #-}
isPrefixOf :: (Storable a, Eq a) => Vector a -> Vector a -> Bool
isPrefixOf :: forall a. (Storable a, Eq a) => Vector a -> Vector a -> Bool
isPrefixOf x :: Vector a
x@(SV ForeignPtr a
_ Int
_ Int
l1) y :: Vector a
y@(SV ForeignPtr a
_ Int
_ Int
l2) =
Int
l1 forall a. Ord a => a -> a -> Bool
<= Int
l2 Bool -> Bool -> Bool
&& Vector a
x forall a. Eq a => a -> a -> Bool
== forall a. Storable a => Int -> Vector a -> Vector a
unsafeTake Int
l1 Vector a
y
isSuffixOf :: (Storable a, Eq a) => Vector a -> Vector a -> Bool
isSuffixOf :: forall a. (Storable a, Eq a) => Vector a -> Vector a -> Bool
isSuffixOf x :: Vector a
x@(SV ForeignPtr a
_ Int
_ Int
l1) y :: Vector a
y@(SV ForeignPtr a
_ Int
_ Int
l2) =
Int
l1 forall a. Ord a => a -> a -> Bool
<= Int
l2 Bool -> Bool -> Bool
&& Vector a
x forall a. Eq a => a -> a -> Bool
== forall a. Storable a => Int -> Vector a -> Vector a
unsafeDrop (Int
l2 forall a. Num a => a -> a -> a
- Int
l1) Vector a
y
zip :: (Storable a, Storable b) => Vector a -> Vector b -> [(a, b)]
zip :: forall a b.
(Storable a, Storable b) =>
Vector a -> Vector b -> [(a, b)]
zip Vector a
ps Vector b
qs =
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] forall a. a -> a
id forall a b. (a -> b) -> a -> b
$
do (a
ph,Vector a
pt) <- forall a. Storable a => Vector a -> Maybe (a, Vector a)
viewL Vector a
ps
(b
qh,Vector b
qt) <- forall a. Storable a => Vector a -> Maybe (a, Vector a)
viewL Vector b
qs
forall (m :: * -> *) a. Monad m => a -> m a
return ((a
ph,b
qh) forall a. a -> [a] -> [a]
: forall a b.
(Storable a, Storable b) =>
Vector a -> Vector b -> [(a, b)]
zip Vector a
pt Vector b
qt)
zipWith :: (Storable a, Storable b, Storable c) =>
(a -> b -> c) -> Vector a -> Vector b -> Vector c
zipWith :: forall a b c.
(Storable a, Storable b, Storable c) =>
(a -> b -> c) -> Vector a -> Vector b -> Vector c
zipWith a -> b -> c
f Vector a
as Vector b
bs =
forall a b. Storable a => Vector a -> (Ptr a -> Int -> IO b) -> b
unsafeWithStartPtr Vector a
as forall a b. (a -> b) -> a -> b
$ \Ptr a
pa0 Int
la ->
forall a b.
Storable a =>
Vector a -> (Ptr a -> Int -> IO b) -> IO b
withStartPtr Vector b
bs forall a b. (a -> b) -> a -> b
$ \Ptr b
pb0 Int
lb ->
let len :: Int
len = forall a. Ord a => a -> a -> a
min Int
la Int
lb
in forall a. Storable a => Int -> (Ptr a -> IO ()) -> IO (Vector a)
create Int
len forall a b. (a -> b) -> a -> b
$ \Ptr c
p0 ->
let go :: Int -> Ptr c -> Ptr a -> Ptr b -> IO ()
go = forall a b c d x. (a -> b -> c -> d -> x) -> a -> b -> c -> d -> x
Strict.arguments4 forall a b. (a -> b) -> a -> b
$ \Int
n Ptr c
p Ptr a
pa Ptr b
pb ->
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
nforall a. Ord a => a -> a -> Bool
>Int
0) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 a -> b -> c
f (forall a. Storable a => Ptr a -> IO a
St.peek Ptr a
pa) (forall a. Storable a => Ptr a -> IO a
St.peek Ptr b
pb) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. Storable a => Ptr a -> a -> IO ()
St.poke Ptr c
p forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
Int -> Ptr c -> Ptr a -> Ptr b -> IO ()
go (Int -> Int
pred Int
n) (forall a. Storable a => Ptr a -> Ptr a
incPtr Ptr c
p) (forall a. Storable a => Ptr a -> Ptr a
incPtr Ptr a
pa) (forall a. Storable a => Ptr a -> Ptr a
incPtr Ptr b
pb)
in Int -> Ptr c -> Ptr a -> Ptr b -> IO ()
go Int
len Ptr c
p0 Ptr a
pa0 Ptr b
pb0
{-# INLINE zipWith #-}
zipWith3 :: (Storable a, Storable b, Storable c, Storable d) =>
(a -> b -> c -> d) -> Vector a -> Vector b -> Vector c -> Vector d
zipWith3 :: forall a b c d.
(Storable a, Storable b, Storable c, Storable d) =>
(a -> b -> c -> d) -> Vector a -> Vector b -> Vector c -> Vector d
zipWith3 a -> b -> c -> d
f Vector a
as Vector b
bs Vector c
cs =
forall a b. Storable a => Vector a -> (Ptr a -> Int -> IO b) -> b
unsafeWithStartPtr Vector a
as forall a b. (a -> b) -> a -> b
$ \Ptr a
pa0 Int
la ->
forall a b.
Storable a =>
Vector a -> (Ptr a -> Int -> IO b) -> IO b
withStartPtr Vector b
bs forall a b. (a -> b) -> a -> b
$ \Ptr b
pb0 Int
lb ->
forall a b.
Storable a =>
Vector a -> (Ptr a -> Int -> IO b) -> IO b
withStartPtr Vector c
cs forall a b. (a -> b) -> a -> b
$ \Ptr c
pc0 Int
lc ->
let len :: Int
len = Int
la forall a. Ord a => a -> a -> a
`min` Int
lb forall a. Ord a => a -> a -> a
`min` Int
lc
in forall a. Storable a => Int -> (Ptr a -> IO ()) -> IO (Vector a)
create Int
len forall a b. (a -> b) -> a -> b
$ \Ptr d
p0 ->
let go :: Int -> Ptr d -> Ptr a -> Ptr b -> Ptr c -> IO ()
go = forall a b c d e x.
(a -> b -> c -> d -> e -> x) -> a -> b -> c -> d -> e -> x
Strict.arguments5 forall a b. (a -> b) -> a -> b
$ \Int
n Ptr d
p Ptr a
pa Ptr b
pb Ptr c
pc ->
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
nforall a. Ord a => a -> a -> Bool
>Int
0) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a1 a2 a3 r.
Monad m =>
(a1 -> a2 -> a3 -> r) -> m a1 -> m a2 -> m a3 -> m r
liftM3 a -> b -> c -> d
f (forall a. Storable a => Ptr a -> IO a
St.peek Ptr a
pa) (forall a. Storable a => Ptr a -> IO a
St.peek Ptr b
pb) (forall a. Storable a => Ptr a -> IO a
St.peek Ptr c
pc) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. Storable a => Ptr a -> a -> IO ()
St.poke Ptr d
p forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
Int -> Ptr d -> Ptr a -> Ptr b -> Ptr c -> IO ()
go (Int -> Int
pred Int
n) (forall a. Storable a => Ptr a -> Ptr a
incPtr Ptr d
p) (forall a. Storable a => Ptr a -> Ptr a
incPtr Ptr a
pa) (forall a. Storable a => Ptr a -> Ptr a
incPtr Ptr b
pb) (forall a. Storable a => Ptr a -> Ptr a
incPtr Ptr c
pc)
in Int -> Ptr d -> Ptr a -> Ptr b -> Ptr c -> IO ()
go Int
len Ptr d
p0 Ptr a
pa0 Ptr b
pb0 Ptr c
pc0
{-# INLINE zipWith3 #-}
zipWith4 :: (Storable a, Storable b, Storable c, Storable d, Storable e) =>
(a -> b -> c -> d -> e) -> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
zipWith4 :: forall a b c d e.
(Storable a, Storable b, Storable c, Storable d, Storable e) =>
(a -> b -> c -> d -> e)
-> Vector a -> Vector b -> Vector c -> Vector d -> Vector e
zipWith4 a -> b -> c -> d -> e
f Vector a
as Vector b
bs Vector c
cs Vector d
ds =
forall a b. Storable a => Vector a -> (Ptr a -> Int -> IO b) -> b
unsafeWithStartPtr Vector a
as forall a b. (a -> b) -> a -> b
$ \Ptr a
pa0 Int
la ->
forall a b.
Storable a =>
Vector a -> (Ptr a -> Int -> IO b) -> IO b
withStartPtr Vector b
bs forall a b. (a -> b) -> a -> b
$ \Ptr b
pb0 Int
lb ->
forall a b.
Storable a =>
Vector a -> (Ptr a -> Int -> IO b) -> IO b
withStartPtr Vector c
cs forall a b. (a -> b) -> a -> b
$ \Ptr c
pc0 Int
lc ->
forall a b.
Storable a =>
Vector a -> (Ptr a -> Int -> IO b) -> IO b
withStartPtr Vector d
ds forall a b. (a -> b) -> a -> b
$ \Ptr d
pd0 Int
ld ->
let len :: Int
len = Int
la forall a. Ord a => a -> a -> a
`min` Int
lb forall a. Ord a => a -> a -> a
`min` Int
lc forall a. Ord a => a -> a -> a
`min` Int
ld
in forall a. Storable a => Int -> (Ptr a -> IO ()) -> IO (Vector a)
create Int
len forall a b. (a -> b) -> a -> b
$ \Ptr e
p0 ->
let go :: Int -> Ptr e -> Ptr a -> Ptr b -> Ptr c -> Ptr d -> IO ()
go =
forall a b x. (a -> b -> x) -> a -> b -> x
Strict.arguments2 forall a b. (a -> b) -> a -> b
$ \Int
n Ptr e
p ->
forall a b c d x. (a -> b -> c -> d -> x) -> a -> b -> c -> d -> x
Strict.arguments4 forall a b. (a -> b) -> a -> b
$ \Ptr a
pa Ptr b
pb Ptr c
pc Ptr d
pd ->
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
nforall a. Ord a => a -> a -> Bool
>Int
0) forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a1 a2 a3 a4 r.
Monad m =>
(a1 -> a2 -> a3 -> a4 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m r
liftM4 a -> b -> c -> d -> e
f (forall a. Storable a => Ptr a -> IO a
St.peek Ptr a
pa) (forall a. Storable a => Ptr a -> IO a
St.peek Ptr b
pb) (forall a. Storable a => Ptr a -> IO a
St.peek Ptr c
pc) (forall a. Storable a => Ptr a -> IO a
St.peek Ptr d
pd) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. Storable a => Ptr a -> a -> IO ()
St.poke Ptr e
p forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
Int -> Ptr e -> Ptr a -> Ptr b -> Ptr c -> Ptr d -> IO ()
go (Int -> Int
pred Int
n) (forall a. Storable a => Ptr a -> Ptr a
incPtr Ptr e
p) (forall a. Storable a => Ptr a -> Ptr a
incPtr Ptr a
pa) (forall a. Storable a => Ptr a -> Ptr a
incPtr Ptr b
pb) (forall a. Storable a => Ptr a -> Ptr a
incPtr Ptr c
pc) (forall a. Storable a => Ptr a -> Ptr a
incPtr Ptr d
pd)
in Int -> Ptr e -> Ptr a -> Ptr b -> Ptr c -> Ptr d -> IO ()
go Int
len Ptr e
p0 Ptr a
pa0 Ptr b
pb0 Ptr c
pc0 Ptr d
pd0
{-# INLINE zipWith4 #-}
unzip :: (Storable a, Storable b) => [(a, b)] -> (Vector a, Vector b)
unzip :: forall a b.
(Storable a, Storable b) =>
[(a, b)] -> (Vector a, Vector b)
unzip [(a, b)]
ls = (forall a. Storable a => [a] -> Vector a
pack (forall a b. (a -> b) -> [a] -> [b]
P.map forall a b. (a, b) -> a
fst [(a, b)]
ls), forall a. Storable a => [a] -> Vector a
pack (forall a b. (a -> b) -> [a] -> [b]
P.map forall a b. (a, b) -> b
snd [(a, b)]
ls))
{-# INLINE unzip #-}
sieve :: (Storable a) => Int -> Vector a -> Vector a
sieve :: forall a. Storable a => Int -> Vector a -> Vector a
sieve Int
n Vector a
xs =
case forall a. Ord a => a -> a -> Ordering
P.compare Int
n Int
1 of
Ordering
P.LT -> forall a. (?callStack::CallStack) => String -> a
error String
"sieve: non-positive step size"
Ordering
P.EQ -> Vector a
xs
Ordering
P.GT -> forall a. Storable a => Int -> Vector a -> Vector a
sieveCore Int
n Vector a
xs
sieveCore :: (Storable a) => Int -> Vector a -> Vector a
sieveCore :: forall a. Storable a => Int -> Vector a -> Vector a
sieveCore Int
n (SV ForeignPtr a
fp Int
s Int
l) =
let end :: Int
end = Int
sforall a. Num a => a -> a -> a
+Int
l
in forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$
forall b a.
Storable b =>
Int -> (a -> Maybe (b, a)) -> a -> (Vector b, Maybe a)
unfoldrN (- forall a. Integral a => a -> a -> a
div (-Int
l) Int
n)
(forall a x. (a -> x) -> a -> x
Strict.arguments1 forall a b. (a -> b) -> a -> b
$ \Int
k0 ->
do forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Int
k0forall a. Ord a => a -> a -> Bool
<Int
end)
forall a. a -> Maybe a
Just (forall a. Storable a => ForeignPtr a -> Int -> a
foreignPeek ForeignPtr a
fp Int
k0, Int
k0 forall a. Num a => a -> a -> a
+ Int
n))
Int
s
{-# INLINE sieve #-}
deinterleave :: (Storable a) => Int -> Vector a -> [Vector a]
deinterleave :: forall a. Storable a => Int -> Vector a -> [Vector a]
deinterleave Int
n =
forall a b. (a -> b) -> [a] -> [b]
P.map (forall a. Storable a => Int -> Vector a -> Vector a
sieve Int
n) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> [a] -> [a]
P.take Int
n forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> a) -> a -> [a]
P.iterate forall a. Storable a => Vector a -> Vector a
laxTail
interleave :: (Storable a) => [Vector a] -> Vector a
interleave :: forall a. Storable a => [Vector a] -> Vector a
interleave [] = forall a. Storable a => Vector a
empty
interleave [Vector a
xs] = Vector a
xs
interleave [Vector a]
vs =
forall a. IO a -> a
Unsafe.performIO forall a b. (a -> b) -> a -> b
$
forall {k} (r :: k) (m :: k -> *) a.
ContT r m a -> (a -> m r) -> m r
MC.runContT
(do
[(Ptr a, Int)]
pls <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (\Vector a
v -> forall {k} (r :: k) (m :: k -> *) a.
((a -> m r) -> m r) -> ContT r m a
MC.ContT (forall a b.
Storable a =>
Vector a -> (Ptr a -> Int -> IO b) -> IO b
withStartPtr Vector a
v forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. ((a, b) -> c) -> a -> b -> c
curry)) [Vector a]
vs
let ([Ptr a]
ps,[Int]
ls) = forall a b. [(a, b)] -> ([a], [b])
P.unzip [(Ptr a, Int)]
pls
Ptr (Ptr a)
ptrs <- forall {k} (r :: k) (m :: k -> *) a.
((a -> m r) -> m r) -> ContT r m a
MC.ContT (forall a b. Storable a => [a] -> (Ptr a -> IO b) -> IO b
withArray [Ptr a]
ps)
if forall (t :: * -> *). Foldable t => t Bool -> Bool
and (forall a b. (a -> a -> b) -> [a] -> [b]
ListHT.mapAdjacent forall a. Eq a => a -> a -> Bool
(==) [Int]
ls)
then forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr (Ptr a)
ptrs, forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
P.sum [Int]
ls)
else forall a. String -> String -> a
moduleError String
"interleave" String
"all input vectors must have the same length")
(\(Ptr (Ptr a)
ptrs, Int
totalLength) -> forall a. Storable a => Int -> (Ptr a -> IO ()) -> IO (Vector a)
create Int
totalLength forall a b. (a -> b) -> a -> b
$ \Ptr a
p ->
let len :: Int
len = forall (t :: * -> *) a. Foldable t => t a -> Int
P.length [Vector a]
vs
go :: Int -> IO ()
go = forall a x. (a -> x) -> a -> x
Strict.arguments1 forall a b. (a -> b) -> a -> b
$ \Int
m ->
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
m forall a. Ord a => a -> a -> Bool
< Int
totalLength) forall a b. (a -> b) -> a -> b
$ do
let (Int
j,Int
k) = forall a. Integral a => a -> a -> (a, a)
P.quotRem Int
m Int
len
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr a
p Int
m forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Int
j forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr (Ptr a)
ptrs Int
k
Int -> IO ()
go forall a b. (a -> b) -> a -> b
$ Int -> Int
succ Int
m
in Int -> IO ()
go Int
0)
{-# INLINE interleave #-}
inits :: (Storable a) => Vector a -> [Vector a]
inits :: forall a. Storable a => Vector a -> [Vector a]
inits (SV ForeignPtr a
x Int
s Int
l) = forall a b. (a -> b) -> [a] -> [b]
List.map (forall a. ForeignPtr a -> Int -> Int -> Vector a
SV ForeignPtr a
x Int
s) [Int
0..Int
l]
tails :: (Storable a) => Vector a -> [Vector a]
tails :: forall a. Storable a => Vector a -> [Vector a]
tails Vector a
p =
forall a b.
Storable a =>
b -> (a -> Vector a -> b) -> Vector a -> b
switchL [forall a. Storable a => Vector a
empty] (\ a
_ Vector a
t -> Vector a
p forall a. a -> [a] -> [a]
: forall a. Storable a => Vector a -> [Vector a]
tails Vector a
t) Vector a
p
copy :: (Storable a) => Vector a -> Vector a
copy :: forall a. Storable a => Vector a -> Vector a
copy Vector a
v =
forall a b. Storable a => Vector a -> (Ptr a -> Int -> IO b) -> b
unsafeWithStartPtr Vector a
v forall a b. (a -> b) -> a -> b
$ \Ptr a
f Int
l ->
forall a. Storable a => Int -> (Ptr a -> IO ()) -> IO (Vector a)
create Int
l forall a b. (a -> b) -> a -> b
$ \Ptr a
p ->
forall a. Storable a => Ptr a -> Ptr a -> Int -> IO ()
copyArray Ptr a
p Ptr a
f (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
l)
poke :: (Storable a) => Ptr a -> Vector a -> IO ()
poke :: forall a. Storable a => Ptr a -> Vector a -> IO ()
poke Ptr a
dst Vector a
v =
forall a b.
Storable a =>
Vector a -> (Ptr a -> Int -> IO b) -> IO b
withStartPtr Vector a
v forall a b. (a -> b) -> a -> b
$ \Ptr a
src Int
len -> forall a. Storable a => Ptr a -> Ptr a -> Int -> IO ()
copyArray Ptr a
dst Ptr a
src Int
len
peek :: (Storable a) => Int -> Ptr a -> IO (Vector a)
peek :: forall a. Storable a => Int -> Ptr a -> IO (Vector a)
peek Int
len Ptr a
src =
forall a. Storable a => Int -> (Ptr a -> IO ()) -> IO (Vector a)
create Int
len forall a b. (a -> b) -> a -> b
$ \Ptr a
dst -> forall a. Storable a => Ptr a -> Ptr a -> Int -> IO ()
copyArray Ptr a
dst Ptr a
src Int
len
hPut :: (Storable a) => Handle -> Vector a -> IO ()
hPut :: forall a. Storable a => Handle -> Vector a -> IO ()
hPut Handle
h Vector a
v =
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not (forall a. Vector a -> Bool
null Vector a
v)) forall a b. (a -> b) -> a -> b
$
forall a b.
Storable a =>
Vector a -> (Ptr a -> Int -> IO b) -> IO b
withStartPtr Vector a
v forall a b. (a -> b) -> a -> b
$ \ Ptr a
ptrS Int
l ->
let ptrE :: Ptr a
ptrE = forall a. Storable a => Ptr a -> Int -> Ptr a
advancePtr Ptr a
ptrS Int
l
in forall a. Handle -> Ptr a -> Int -> IO ()
hPutBuf Handle
h Ptr a
ptrS (forall a b. Ptr a -> Ptr b -> Int
minusPtr Ptr a
ptrE Ptr a
ptrS)
hGet :: (Storable a) => Handle -> Int -> IO (Vector a)
hGet :: forall a. Storable a => Handle -> Int -> IO (Vector a)
hGet Handle
_ Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Storable a => Vector a
empty
hGet Handle
h Int
l =
forall a. Storable a => Int -> (Ptr a -> IO Int) -> IO (Vector a)
createAndTrim Int
l forall a b. (a -> b) -> a -> b
$ \Ptr a
p ->
let elemType :: Ptr a -> a
elemType :: forall a. Ptr a -> a
elemType Ptr a
_ = forall a. (?callStack::CallStack) => a
undefined
roundUp :: a -> a -> a
roundUp a
m a
n = a
n forall a. Num a => a -> a -> a
+ forall a. Integral a => a -> a -> a
mod (-a
n) a
m
sizeOfElem :: Int
sizeOfElem =
forall a. Integral a => a -> a -> a
roundUp
(forall a. Storable a => a -> Int
alignment (forall a. Ptr a -> a
elemType Ptr a
p))
(forall a. Storable a => a -> Int
sizeOf (forall a. Ptr a -> a
elemType Ptr a
p))
in forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. Integral a => a -> a -> a
div Int
sizeOfElem) forall a b. (a -> b) -> a -> b
$
forall a. Handle -> Ptr a -> Int -> IO Int
hGetBuf Handle
h Ptr a
p (Int
l forall a. Num a => a -> a -> a
* Int
sizeOfElem)
readFile :: (Storable a) => FilePath -> IO (Vector a)
readFile :: forall a. Storable a => String -> IO (Vector a)
readFile String
f =
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket (String -> IOMode -> IO Handle
openBinaryFile String
f IOMode
ReadMode) Handle -> IO ()
hClose
(\Handle
h -> forall a. Storable a => Handle -> Int -> IO (Vector a)
hGet Handle
h forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Handle -> IO Integer
hFileSize Handle
h)
writeFile :: (Storable a) => FilePath -> Vector a -> IO ()
writeFile :: forall a. Storable a => String -> Vector a -> IO ()
writeFile String
f Vector a
txt =
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket (String -> IOMode -> IO Handle
openBinaryFile String
f IOMode
WriteMode) Handle -> IO ()
hClose
(\Handle
h -> forall a. Storable a => Handle -> Vector a -> IO ()
hPut Handle
h Vector a
txt)
appendFile :: (Storable a) => FilePath -> Vector a -> IO ()
appendFile :: forall a. Storable a => String -> Vector a -> IO ()
appendFile String
f Vector a
txt =
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket (String -> IOMode -> IO Handle
openBinaryFile String
f IOMode
AppendMode) Handle -> IO ()
hClose
(\Handle
h -> forall a. Storable a => Handle -> Vector a -> IO ()
hPut Handle
h Vector a
txt)
succ :: Int -> Int
succ :: Int -> Int
succ Int
n = Int
nforall a. Num a => a -> a -> a
+Int
1
{-# INLINE succ #-}
pred :: Int -> Int
pred :: Int -> Int
pred Int
n = Int
nforall a. Num a => a -> a -> a
-Int
1
{-# INLINE pred #-}
unsafeWithStartPtr :: Storable a => Vector a -> (Ptr a -> Int -> IO b) -> b
unsafeWithStartPtr :: forall a b. Storable a => Vector a -> (Ptr a -> Int -> IO b) -> b
unsafeWithStartPtr Vector a
v Ptr a -> Int -> IO b
f =
forall a. IO a -> a
Unsafe.performIO (forall a b.
Storable a =>
Vector a -> (Ptr a -> Int -> IO b) -> IO b
withStartPtr Vector a
v Ptr a -> Int -> IO b
f)
{-# INLINE unsafeWithStartPtr #-}
foreignPeek :: Storable a => ForeignPtr a -> Int -> a
foreignPeek :: forall a. Storable a => ForeignPtr a -> Int -> a
foreignPeek ForeignPtr a
fp Int
k =
forall a. IO a -> a
inlinePerformIO forall a b. (a -> b) -> a -> b
$ forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr a
fp forall a b. (a -> b) -> a -> b
$ forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Int
k
{-# INLINE foreignPeek #-}
withNonEmptyVector ::
String -> (ForeignPtr a -> Int -> Int -> b) -> Vector a -> b
withNonEmptyVector :: forall a b.
String -> (ForeignPtr a -> Int -> Int -> b) -> Vector a -> b
withNonEmptyVector String
fun ForeignPtr a -> Int -> Int -> b
f (SV ForeignPtr a
x Int
s Int
l) =
if Int
l forall a. Ord a => a -> a -> Bool
<= Int
0
then forall a. String -> a
errorEmpty String
fun
else ForeignPtr a -> Int -> Int -> b
f ForeignPtr a
x Int
s Int
l
{-# INLINE withNonEmptyVector #-}
errorEmpty :: String -> a
errorEmpty :: forall a. String -> a
errorEmpty String
fun = forall a. String -> String -> a
moduleError String
fun String
"empty Vector"
{-# NOINLINE errorEmpty #-}
moduleError :: String -> String -> a
moduleError :: forall a. String -> String -> a
moduleError String
fun String
msg = forall a. (?callStack::CallStack) => String -> a
error (String
"Data.StorableVector." forall a. [a] -> [a] -> [a]
++ String
fun forall a. [a] -> [a] -> [a]
++ Char
':'forall a. a -> [a] -> [a]
:Char
' 'forall a. a -> [a] -> [a]
:String
msg)
{-# NOINLINE moduleError #-}
findFromEndUntil :: (Storable a) => (a -> Bool) -> Vector a -> Int
findFromEndUntil :: forall a. Storable a => (a -> Bool) -> Vector a -> Int
findFromEndUntil = forall a b x. (a -> b -> x) -> a -> b -> x
Strict.arguments2 forall a b. (a -> b) -> a -> b
$ \a -> Bool
f ps :: Vector a
ps@(SV ForeignPtr a
x Int
s Int
l) ->
if forall a. Vector a -> Bool
null Vector a
ps then Int
0
else if a -> Bool
f (forall a. Storable a => Vector a -> a
last Vector a
ps) then Int
l
else forall a. Storable a => (a -> Bool) -> Vector a -> Int
findFromEndUntil a -> Bool
f (forall a. ForeignPtr a -> Int -> Int -> Vector a
SV ForeignPtr a
x Int
s (Int
lforall a. Num a => a -> a -> a
-Int
1))