module Data.Vector.Fixed.Internal where
import Control.Applicative (Applicative)
import Control.Monad (liftM)
import Data.Monoid (Monoid(..))
import qualified Data.Foldable as T
import qualified Data.Traversable as T
import Foreign.Storable (Storable(..))
import Foreign.Ptr (Ptr,castPtr)
import Data.Vector.Fixed.Cont (Vector(..),Dim,S,Z,Arity,vector,Add)
import qualified Data.Vector.Fixed.Cont as C
import Data.Vector.Fixed.Cont (ContVec,Index)
import Prelude hiding ( replicate,map,zipWith,maximum,minimum,and,or,all,any
, foldl,foldr,foldl1,length,sum,reverse,scanl,scanl1
, head,tail,mapM,mapM_,sequence,sequence_,concat
)
mkN :: Make (S Z) a r => a -> r
mkN = unGo $ make id
class Make n a r where
make :: (ContVec Z a -> ContVec n a) -> r
instance (a'~a, Make (S n) a r) => Make n a' (a -> r) where
make f a = make (C.cons a . f)
instance Arity n => Make n a (ContVec n a) where
make f = C.reverse $ f C.empty
newtype Go r = Go { unGo :: r }
instance Make Z a r => Make Z a (Go r) where
make f = Go $ make f
(<|) :: a -> ContVec n a -> ContVec (S n) a
(<|) = C.cons
infixr 1 <|
mk0 :: (Vector v a, Dim v ~ C.Z) => v a
mk0 = vector $ C.empty
mk1 :: (Vector v a, Dim v ~ C.N1) => a -> v a
mk1 a1 = vector $ C.mk1 a1
mk2 :: (Vector v a, Dim v ~ C.N2) => a -> a -> v a
mk2 a1 a2 = vector $ C.mk2 a1 a2
mk3 :: (Vector v a, Dim v ~ C.N3) => a -> a -> a -> v a
mk3 a1 a2 a3 = vector $ C.mk3 a1 a2 a3
mk4 :: (Vector v a, Dim v ~ C.N4) => a -> a -> a -> a -> v a
mk4 a1 a2 a3 a4 = vector $ C.mk4 a1 a2 a3 a4
mk5 :: (Vector v a, Dim v ~ C.N5) => a -> a -> a -> a -> a -> v a
mk5 a1 a2 a3 a4 a5 = vector $ C.mk5 a1 a2 a3 a4 a5
replicate :: Vector v a => a -> v a
replicate
= vector . C.replicate
replicateM :: (Vector v a, Monad m) => m a -> m (v a)
replicateM
= liftM vector . C.replicateM
basis :: (Vector v a, Num a) => Int -> v a
basis = vector . C.basis
unfoldr :: (Vector v a) => (b -> (a,b)) -> b -> v a
unfoldr f = vector . C.unfoldr f
generate :: (Vector v a) => (Int -> a) -> v a
generate = vector . C.generate
generateM :: (Monad m, Vector v a) => (Int -> m a) -> m (v a)
generateM = liftM vector . C.generateM
head :: (Vector v a, Dim v ~ S n) => v a -> a
head = C.head . C.cvec
tail :: (Vector v a, Vector w a, Dim v ~ S (Dim w))
=> v a -> w a
tail = vector . C.tail . C.cvec
cons :: (Vector v a, Vector w a, S (Dim v) ~ Dim w)
=> a -> v a -> w a
cons a = vector . C.cons a . C.cvec
snoc :: (Vector v a, Vector w a, S (Dim v) ~ Dim w)
=> a -> v a -> w a
snoc a = vector . C.snoc a . C.cvec
concat :: (Vector v a, Vector u a, Vector w a, (Add (Dim v) (Dim u)) ~ Dim w)
=> v a -> u a -> w a
concat v u = vector $ C.concat (C.cvec v) (C.cvec u)
reverse :: Vector v a => v a -> v a
reverse = vector . C.reverse . C.cvec
(!) :: (Vector v a) => v a -> Int -> a
v ! n = runIndex n (C.cvec v)
runIndex :: Arity n => Int -> C.ContVec n r -> r
runIndex = C.index
index :: (Vector v a, C.Index k (Dim v)) => v a -> k -> a
index v k = C.runContVec (C.getF k)
$ C.cvec v
set :: (Vector v a, C.Index k (Dim v)) => k -> a -> v a -> v a
set k a v = inspect v
$ C.putF k a construct
element :: (Vector v a, Functor f) => Int -> (a -> f a) -> (v a -> f (v a))
element i f v = vector `fmap` C.element i f (C.cvec v)
elementTy :: (Vector v a, Index k (Dim v), Functor f)
=> k -> (a -> f a) -> (v a -> f (v a))
elementTy k f v = vector `fmap` C.elementTy k f (C.cvec v)
foldl :: Vector v a => (b -> a -> b) -> b -> v a -> b
foldl f x = C.foldl f x
. C.cvec
foldr :: Vector v a => (a -> b -> b) -> b -> v a -> b
foldr f x = C.foldr f x
. C.cvec
foldl1 :: (Vector v a, Dim v ~ S n) => (a -> a -> a) -> v a -> a
foldl1 f = C.foldl1 f
. C.cvec
fold :: (Vector v m, Monoid m) => v m -> m
fold = T.fold
. C.cvec
foldMap :: (Vector v a, Monoid m) => (a -> m) -> v a -> m
foldMap f = T.foldMap f
. C.cvec
ifoldr :: Vector v a => (Int -> a -> b -> b) -> b -> v a -> b
ifoldr f x = C.ifoldr f x
. C.cvec
ifoldl :: Vector v a => (b -> Int -> a -> b) -> b -> v a -> b
ifoldl f z = C.ifoldl f z
. C.cvec
foldM :: (Vector v a, Monad m) => (b -> a -> m b) -> b -> v a -> m b
foldM f x = C.foldM f x . C.cvec
ifoldM :: (Vector v a, Monad m) => (b -> Int -> a -> m b) -> b -> v a -> m b
ifoldM f x = C.ifoldM f x . C.cvec
sum :: (Vector v a, Num a) => v a -> a
sum = C.sum . C.cvec
maximum :: (Vector v a, Dim v ~ S n, Ord a) => v a -> a
maximum = C.maximum . C.cvec
minimum :: (Vector v a, Dim v ~ S n, Ord a) => v a -> a
minimum = C.minimum . C.cvec
and :: (Vector v Bool) => v Bool -> Bool
and = C.and . C.cvec
or :: (Vector v Bool) => v Bool -> Bool
or = C.or . C.cvec
all :: (Vector v a) => (a -> Bool) -> v a -> Bool
all f = (C.all f) . C.cvec
any :: (Vector v a) => (a -> Bool) -> v a -> Bool
any f = (C.any f) . C.cvec
find :: (Vector v a) => (a -> Bool) -> v a -> Maybe a
find f = (C.find f) . C.cvec
eq :: (Vector v a, Eq a) => v a -> v a -> Bool
eq v w = C.and
$ C.zipWith (==) (C.cvec v) (C.cvec w)
ord :: (Vector v a, Ord a) => v a -> v a -> Ordering
ord v w = C.foldl mappend mempty
$ C.zipWith compare (C.cvec v) (C.cvec w)
map :: (Vector v a, Vector v b) => (a -> b) -> v a -> v b
map f = vector
. C.map f
. C.cvec
sequence :: (Vector v a, Vector v (m a), Monad m) => v (m a) -> m (v a)
sequence = mapM id
sequence_ :: (Vector v (m a), Monad m) => v (m a) -> m ()
sequence_ = mapM_ id
mapM :: (Vector v a, Vector v b, Monad m) => (a -> m b) -> v a -> m (v b)
mapM f = liftM vector
. C.mapM f
. C.cvec
mapM_ :: (Vector v a, Monad m) => (a -> m b) -> v a -> m ()
mapM_ f = foldl (\m a -> m >> f a >> return ()) (return ())
imap :: (Vector v a, Vector v b) =>
(Int -> a -> b) -> v a -> v b
imap f = vector
. C.imap f
. C.cvec
imapM :: (Vector v a, Vector v b, Monad m)
=> (Int -> a -> m b) -> v a -> m (v b)
imapM f = liftM vector
. C.imapM f
. C.cvec
imapM_ :: (Vector v a, Monad m) => (Int -> a -> m b) -> v a -> m ()
imapM_ f = ifoldl (\m i a -> m >> f i a >> return ()) (return ())
scanl :: (Vector v a, Vector w b, Dim w ~ S (Dim v))
=> (b -> a -> b) -> b -> v a -> w b
scanl f x0 = vector . C.scanl f x0 . C.cvec
scanl1 :: (Vector v a)
=> (a -> a -> a) -> v a -> v a
scanl1 f = vector . C.scanl1 f . C.cvec
sequenceA :: (Vector v a, Vector v (f a), Applicative f)
=> v (f a) -> f (v a)
sequenceA = fmap vector . T.sequenceA . C.cvec
traverse :: (Vector v a, Vector v b, Applicative f)
=> (a -> f b) -> v a -> f (v b)
traverse f = fmap vector . T.traverse f . C.cvec
distribute :: (Vector v a, Vector v (f a), Functor f)
=> f (v a) -> v (f a)
distribute = vector . C.distribute . fmap C.cvec
collect :: (Vector v a, Vector v b, Vector v (f b), Functor f)
=> (a -> v b) -> f a -> v (f b)
collect f = vector . C.collect (C.cvec . f)
distributeM :: (Vector v a, Vector v (m a), Monad m)
=> m (v a) -> v (m a)
distributeM = vector . C.distributeM . liftM C.cvec
collectM :: (Vector v a, Vector v b, Vector v (m b), Monad m)
=> (a -> v b) -> m a -> v (m b)
collectM f = vector . C.collectM (C.cvec . f)
zipWith :: (Vector v a, Vector v b, Vector v c)
=> (a -> b -> c) -> v a -> v b -> v c
zipWith f v u = vector
$ C.zipWith f (C.cvec v) (C.cvec u)
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 v1 v2 v3
= vector
$ C.zipWith3 f (C.cvec v1) (C.cvec v2) (C.cvec v3)
zipWithM :: (Vector v a, Vector v b, Vector v c, Monad m)
=> (a -> b -> m c) -> v a -> v b -> m (v c)
zipWithM f v u = liftM vector
$ C.zipWithM f (C.cvec v) (C.cvec u)
zipWithM_
:: (Vector v a, Vector v b, Monad m)
=> (a -> b -> m c) -> v a -> v b -> m ()
zipWithM_ f xs ys = C.zipWithM_ f (C.cvec xs) (C.cvec ys)
izipWith :: (Vector v a, Vector v b, Vector v c)
=> (Int -> a -> b -> c) -> v a -> v b -> v c
izipWith f v u = vector
$ C.izipWith f (C.cvec v) (C.cvec u)
izipWith3
:: (Vector v a, Vector v b, Vector v c, Vector v d)
=> (Int -> a -> b -> c -> d)
-> v a -> v b -> v c
-> v d
izipWith3 f v1 v2 v3
= vector
$ C.izipWith3 f (C.cvec v1) (C.cvec v2) (C.cvec v3)
izipWithM :: (Vector v a, Vector v b, Vector v c, Monad m)
=> (Int -> a -> b -> m c) -> v a -> v b -> m (v c)
izipWithM f v u = liftM vector
$ C.izipWithM f (C.cvec v) (C.cvec u)
izipWithM_
:: (Vector v a, Vector v b, Vector v c, Monad m, Vector v (m c))
=> (Int -> a -> b -> m c) -> v a -> v b -> m ()
izipWithM_ f xs ys = C.izipWithM_ f (C.cvec xs) (C.cvec ys)
defaultAlignemnt :: forall a v. Storable a => v a -> Int
defaultAlignemnt _ = alignment (undefined :: a)
defaultSizeOf
:: forall a v. (Storable a, Vector v a)
=> v a -> Int
defaultSizeOf _ = sizeOf (undefined :: a) * C.arity (undefined :: Dim v)
defaultPeek :: (Storable a, Vector v a) => Ptr (v a) -> IO (v a)
defaultPeek ptr
= generateM (peekElemOff (castPtr ptr))
defaultPoke :: (Storable a, Vector v a) => Ptr (v a) -> v a -> IO ()
defaultPoke ptr
= imapM_ (pokeElemOff (castPtr ptr))
convert :: (Vector v a, Vector w a, Dim v ~ Dim w) => v a -> w a
convert = vector . C.cvec
toList :: (Vector v a) => v a -> [a]
toList = foldr (:) []
fromList :: (Vector v a) => [a] -> v a
fromList = vector . C.fromList
fromList' :: (Vector v a) => [a] -> v a
fromList' = vector . C.fromList'
fromListM :: (Vector v a) => [a] -> Maybe (v a)
fromListM = liftM vector . C.fromListM
fromFoldable :: (Vector v a, T.Foldable f) => f a -> Maybe (v a)
fromFoldable = fromListM . T.toList