#include "phases.h"
module Data.Vector.Generic (
Vector(..),
length, null,
empty, singleton, cons, snoc, replicate, (++), copy,
(!), head, last, indexM, headM, lastM,
slice, init, tail, take, drop,
accum, (//), update, backpermute, reverse,
map, concatMap,
zipWith, zipWith3, zip, zip3, unzip, unzip3,
eq, cmp,
filter, takeWhile, dropWhile,
elem, notElem, find, findIndex,
foldl, foldl1, foldl', foldl1', foldr, foldr1,
and, or, sum, product, maximum, minimum,
unfoldr,
prescanl, prescanl',
postscanl, postscanl',
scanl, scanl', scanl1, scanl1',
enumFromTo, enumFromThenTo,
toList, fromList,
stream, unstream,
new
) where
import Data.Vector.Generic.Mutable ( MVector )
import qualified Data.Vector.Generic.New as New
import Data.Vector.Generic.New ( New )
import qualified Data.Vector.Fusion.Stream as Stream
import Data.Vector.Fusion.Stream ( Stream, MStream, inplace, inplace' )
import qualified Data.Vector.Fusion.Stream.Monadic as MStream
import Data.Vector.Fusion.Stream.Size
import Data.Vector.Fusion.Util
import Control.Exception ( assert )
import Prelude hiding ( length, null,
replicate, (++),
head, last,
init, tail, take, drop, reverse,
map, concatMap,
zipWith, zipWith3, zip, zip3, unzip, unzip3,
filter, takeWhile, dropWhile,
elem, notElem,
foldl, foldl1, foldr, foldr1,
and, or, sum, product, maximum, minimum,
scanl, scanl1,
enumFromTo, enumFromThenTo )
class Vector v a where
vnew :: (forall mv m. MVector mv m a => m (mv a)) -> v a
vlength :: v a -> Int
unsafeSlice :: v a -> Int -> Int -> v a
unsafeIndexM :: Monad m => v a -> Int -> m a
new :: Vector v a => New a -> v a
new m = new' undefined m
new' :: Vector v a => v a -> New a -> v a
new' _ m = vnew (New.run m)
stream :: Vector v a => v a -> Stream a
stream v = v `seq` (Stream.unfoldr get 0 `Stream.sized` Exact n)
where
n = length v
get i | i < n = case unsafeIndexM v i of Box x -> Just (x, i+1)
| otherwise = Nothing
unstream :: Vector v a => Stream a -> v a
unstream s = new (New.unstream s)
length :: Vector v a => v a -> Int
length v = vlength v
null :: Vector v a => v a -> Bool
null v = vlength v == 0
empty :: Vector v a => v a
empty = unstream Stream.empty
singleton :: Vector v a => a -> v a
singleton x = unstream (Stream.singleton x)
replicate :: Vector v a => Int -> a -> v a
replicate n = unstream . Stream.replicate n
cons :: Vector v a => a -> v a -> v a
cons x = unstream . Stream.cons x . stream
snoc :: Vector v a => v a -> a -> v a
snoc v = unstream . Stream.snoc (stream v)
infixr 5 ++
(++) :: Vector v a => v a -> v a -> v a
v ++ w = unstream (stream v Stream.++ stream w)
copy :: Vector v a => v a -> v a
copy = unstream . stream
(!) :: Vector v a => v a -> Int -> a
v ! i = assert (i >= 0 && i < length v)
$ unId (unsafeIndexM v i)
head :: Vector v a => v a -> a
head v = v ! 0
last :: Vector v a => v a -> a
last v = v ! (length v 1)
indexM :: (Vector v a, Monad m) => v a -> Int -> m a
indexM v i = assert (i >= 0 && i < length v)
$ unsafeIndexM v i
headM :: (Vector v a, Monad m) => v a -> m a
headM v = indexM v 0
lastM :: (Vector v a, Monad m) => v a -> m a
lastM v = indexM v (length v 1)
slice :: Vector v a => v a -> Int
-> Int
-> v a
slice v i n = assert (i >= 0 && n >= 0 && i+n <= length v)
$ unsafeSlice v i n
init :: Vector v a => v a -> v a
init v = slice v 0 (length v 1)
tail :: Vector v a => v a -> v a
tail v = slice v 1 (length v 1)
take :: Vector v a => Int -> v a -> v a
take n v = slice v 0 (min n' (length v))
where n' = max n 0
drop :: Vector v a => Int -> v a -> v a
drop n v = slice v (min n' len) (max 0 (len n'))
where n' = max n 0
len = length v
accum :: Vector v a => (a -> b -> a) -> v a -> [(Int,b)] -> v a
accum f v us = new (New.accum f (New.unstream (stream v))
(Stream.fromList us))
(//) :: Vector v a => v a -> [(Int, a)] -> v a
v // us = new (New.update (New.unstream (stream v))
(Stream.fromList us))
update :: (Vector v a, Vector v (Int, a)) => v a -> v (Int, a) -> v a
update v w = new (New.update (New.unstream (stream v)) (stream w))
backpermute :: (Vector v a, Vector v Int) => v a -> v Int -> v a
backpermute v is = seq v
$ unstream
$ MStream.trans (Id . unBox)
$ MStream.mapM (indexM v)
$ MStream.trans (Box . unId)
$ stream is
reverse :: (Vector v a) => v a -> v a
reverse = new . New.reverse . New.unstream . stream
map :: (Vector v a, Vector v b) => (a -> b) -> v a -> v b
map f = unstream . inplace' (MStream.map f) . stream
concatMap :: (Vector v a, Vector v b) => (a -> v b) -> v a -> v b
concatMap f = unstream . Stream.concatMap (stream . f) . stream
zipWith :: (Vector v a, Vector v b, Vector v c) => (a -> b -> c) -> v a -> v b -> v c
zipWith f xs ys = unstream (Stream.zipWith f (stream xs) (stream ys))
zipWith3 :: (Vector v a, Vector v b, Vector v c, Vector v d) => (a -> b -> c -> d) -> v a -> v b -> v c -> v d
zipWith3 f xs ys zs = unstream (Stream.zipWith3 f (stream xs) (stream ys) (stream zs))
zip :: (Vector v a, Vector v b, Vector v (a,b)) => v a -> v b -> v (a, b)
zip = zipWith (,)
zip3 :: (Vector v a, Vector v b, Vector v c, Vector v (a, b, c)) => v a -> v b -> v c -> v (a, b, c)
zip3 = zipWith3 (,,)
unzip :: (Vector v a, Vector v b, Vector v (a,b)) => v (a, b) -> (v a, v b)
unzip xs = (map fst xs, map snd xs)
unzip3 :: (Vector v a, Vector v b, Vector v c, Vector v (a, b, c)) => v (a, b, c) -> (v a, v b, v c)
unzip3 xs = (map (\(a, b, c) -> a) xs, map (\(a, b, c) -> b) xs, map (\(a, b, c) -> c) xs)
eq :: (Vector v a, Eq a) => v a -> v a -> Bool
xs `eq` ys = stream xs == stream ys
cmp :: (Vector v a, Ord a) => v a -> v a -> Ordering
cmp xs ys = compare (stream xs) (stream ys)
filter :: Vector v a => (a -> Bool) -> v a -> v a
filter f = unstream . inplace (MStream.filter f) . stream
takeWhile :: Vector v a => (a -> Bool) -> v a -> v a
takeWhile f = unstream . Stream.takeWhile f . stream
dropWhile :: Vector v a => (a -> Bool) -> v a -> v a
dropWhile f = unstream . Stream.dropWhile f . stream
infix 4 `elem`
elem :: (Vector v a, Eq a) => a -> v a -> Bool
elem x = Stream.elem x . stream
infix 4 `notElem`
notElem :: (Vector v a, Eq a) => a -> v a -> Bool
notElem x = Stream.notElem x . stream
find :: Vector v a => (a -> Bool) -> v a -> Maybe a
find f = Stream.find f . stream
findIndex :: Vector v a => (a -> Bool) -> v a -> Maybe Int
findIndex f = Stream.findIndex f . stream
foldl :: Vector v b => (a -> b -> a) -> a -> v b -> a
foldl f z = Stream.foldl f z . stream
foldl1 :: Vector v a => (a -> a -> a) -> v a -> a
foldl1 f = Stream.foldl1 f . stream
foldl' :: Vector v b => (a -> b -> a) -> a -> v b -> a
foldl' f z = Stream.foldl' f z . stream
foldl1' :: Vector v a => (a -> a -> a) -> v a -> a
foldl1' f = Stream.foldl1' f . stream
foldr :: Vector v a => (a -> b -> b) -> b -> v a -> b
foldr f z = Stream.foldr f z . stream
foldr1 :: Vector v a => (a -> a -> a) -> v a -> a
foldr1 f = Stream.foldr1 f . stream
and :: Vector v Bool => v Bool -> Bool
and = Stream.and . stream
or :: Vector v Bool => v Bool -> Bool
or = Stream.or . stream
sum :: (Vector v a, Num a) => v a -> a
sum = Stream.foldl' (+) 0 . stream
product :: (Vector v a, Num a) => v a -> a
product = Stream.foldl' (*) 1 . stream
maximum :: (Vector v a, Ord a) => v a -> a
maximum = Stream.foldl1' max . stream
minimum :: (Vector v a, Ord a) => v a -> a
minimum = Stream.foldl1' min . stream
unfoldr :: Vector v a => (b -> Maybe (a, b)) -> b -> v a
unfoldr f = unstream . Stream.unfoldr f
prescanl :: (Vector v a, Vector v b) => (a -> b -> a) -> a -> v b -> v a
prescanl f z = unstream . inplace' (MStream.prescanl f z) . stream
prescanl' :: (Vector v a, Vector v b) => (a -> b -> a) -> a -> v b -> v a
prescanl' f z = unstream . inplace' (MStream.prescanl' f z) . stream
postscanl :: (Vector v a, Vector v b) => (a -> b -> a) -> a -> v b -> v a
postscanl f z = unstream . inplace' (MStream.postscanl f z) . stream
postscanl' :: (Vector v a, Vector v b) => (a -> b -> a) -> a -> v b -> v a
postscanl' f z = unstream . inplace' (MStream.postscanl' f z) . stream
scanl :: (Vector v a, Vector v b) => (a -> b -> a) -> a -> v b -> v a
scanl f z = unstream . Stream.scanl f z . stream
scanl' :: (Vector v a, Vector v b) => (a -> b -> a) -> a -> v b -> v a
scanl' f z = unstream . Stream.scanl' f z . stream
scanl1 :: Vector v a => (a -> a -> a) -> v a -> v a
scanl1 f = unstream . inplace (MStream.scanl1 f) . stream
scanl1' :: Vector v a => (a -> a -> a) -> v a -> v a
scanl1' f = unstream . inplace (MStream.scanl1' f) . stream
enumFromTo :: (Vector v a, Enum a) => a -> a -> v a
enumFromTo from to = fromList [from .. to]
enumFromThenTo :: (Vector v a, Enum a) => a -> a -> a -> v a
enumFromThenTo from next to = fromList [from, next .. to]
toList :: Vector v a => v a -> [a]
toList = Stream.toList . stream
fromList :: Vector v a => [a] -> v a
fromList = unstream . Stream.fromList