{-# OPTIONS_HADDOCK hide #-}
module ForSyDe.Atom.Skel.FastVector.Lib where
import ForSyDe.Atom
import Data.Maybe
import Control.Applicative
import Data.List.Split
import qualified Data.List as L
import Prelude hiding (take, drop, length, zip, unzip)
newtype Vector a = Vector { Vector a -> [a]
fromVector :: [a] } deriving (Vector a -> Vector a -> Bool
(Vector a -> Vector a -> Bool)
-> (Vector a -> Vector a -> Bool) -> Eq (Vector a)
forall a. Eq a => Vector a -> Vector a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Vector a -> Vector a -> Bool
$c/= :: forall a. Eq a => Vector a -> Vector a -> Bool
== :: Vector a -> Vector a -> Bool
$c== :: forall a. Eq a => Vector a -> Vector a -> Bool
Eq)
vector :: [a] -> Vector a
vector = [a] -> Vector a
forall a. [a] -> Vector a
Vector
instance Functor Vector where
fmap :: (a -> b) -> Vector a -> Vector b
fmap f :: a -> b
f (Vector a :: [a]
a) = [b] -> Vector b
forall a. [a] -> Vector a
Vector ((a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f [a]
a)
instance Applicative Vector where
pure :: a -> Vector a
pure a :: a
a = [a] -> Vector a
forall a. [a] -> Vector a
Vector [a
a]
(Vector fs :: [a -> b]
fs) <*> :: Vector (a -> b) -> Vector a -> Vector b
<*> (Vector as :: [a]
as) = [b] -> Vector b
forall a. [a] -> Vector a
Vector ([b] -> Vector b) -> [b] -> Vector b
forall a b. (a -> b) -> a -> b
$ ZipList b -> [b]
forall a. ZipList a -> [a]
getZipList ([a -> b] -> ZipList (a -> b)
forall a. [a] -> ZipList a
ZipList [a -> b]
fs ZipList (a -> b) -> ZipList a -> ZipList b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [a] -> ZipList a
forall a. [a] -> ZipList a
ZipList [a]
as)
instance (Show a) => Show (Vector a) where
showsPrec :: Int -> Vector a -> ShowS
showsPrec p :: Int
p (Vector []) = Bool -> ShowS -> ShowS
showParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 9) (String -> ShowS
showString "<>")
showsPrec p :: Int
p (Vector xs :: [a]
xs) = Bool -> ShowS -> ShowS
showParen (Int
p Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 9) (Char -> ShowS
showChar '<' ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> ShowS
forall a. Show a => [a] -> ShowS
showVector1 [a]
xs)
where
showVector1 :: [a] -> ShowS
showVector1 [] = Char -> ShowS
showChar '>'
showVector1 (y :: a
y:[]) = a -> ShowS
forall a. Show a => a -> ShowS
shows a
y ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar '>'
showVector1 (y :: a
y:ys :: [a]
ys) = a -> ShowS
forall a. Show a => a -> ShowS
shows a
y ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar ','
ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> ShowS
showVector1 [a]
ys
instance Foldable Vector where
foldr :: (a -> b -> b) -> b -> Vector a -> b
foldr f :: a -> b -> b
f x :: b
x (Vector a :: [a]
a) = (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> b -> b
f b
x [a]
a
farm11 :: (a -> b) -> f a -> f b
farm11 f :: a -> b
f = (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f
farm21 :: (a -> a -> b) -> f a -> f a -> f b
farm21 f :: a -> a -> b
f a :: f a
a b :: f a
b = a -> a -> b
f (a -> a -> b) -> f a -> f (a -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
a f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f a
b
farm31 :: (a -> a -> a -> b) -> f a -> f a -> f a -> f b
farm31 f :: a -> a -> a -> b
f a :: f a
a b :: f a
b c :: f a
c = a -> a -> a -> b
f (a -> a -> a -> b) -> f a -> f (a -> a -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
a f (a -> a -> b) -> f a -> f (a -> b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f a
b f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f a
c
farm41 :: (a -> a -> a -> a -> b) -> f a -> f a -> f a -> f a -> f b
farm41 f :: a -> a -> a -> a -> b
f a :: f a
a b :: f a
b c :: f a
c d :: f a
d = a -> a -> a -> a -> b
f (a -> a -> a -> a -> b) -> f a -> f (a -> a -> a -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
a f (a -> a -> a -> b) -> f a -> f (a -> a -> b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f a
b f (a -> a -> b) -> f a -> f (a -> b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f a
c f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f a
d
farm51 :: (a -> a -> a -> a -> a -> b)
-> f a -> f a -> f a -> f a -> f a -> f b
farm51 f :: a -> a -> a -> a -> a -> b
f a :: f a
a b :: f a
b c :: f a
c d :: f a
d e :: f a
e = a -> a -> a -> a -> a -> b
f (a -> a -> a -> a -> a -> b) -> f a -> f (a -> a -> a -> a -> b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
a f (a -> a -> a -> a -> b) -> f a -> f (a -> a -> a -> b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f a
b f (a -> a -> a -> b) -> f a -> f (a -> a -> b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f a
c f (a -> a -> b) -> f a -> f (a -> b)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f a
d f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> f a
e
farm12 :: (a -> (a1, b)) -> f a -> (f a1, f b)
farm12 f :: a -> (a1, b)
f = f (a1, b) -> (f a1, f b)
forall (f :: * -> *) a1 b. Functor f => f (a1, b) -> (f a1, f b)
(|<) (f (a1, b) -> (f a1, f b))
-> (f a -> f (a1, b)) -> f a -> (f a1, f b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> (a1, b)) -> f a -> f (a1, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> (a1, b)
f
farm22 :: (a -> a -> (a1, b)) -> f a -> f a -> (f a1, f b)
farm22 f :: a -> a -> (a1, b)
f a :: f a
a = f (a1, b) -> (f a1, f b)
forall (f :: * -> *) a1 b. Functor f => f (a1, b) -> (f a1, f b)
(|<) (f (a1, b) -> (f a1, f b))
-> (f a -> f (a1, b)) -> f a -> (f a1, f b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> (a1, b)) -> f a -> f a -> f (a1, b)
forall (f :: * -> *) a a b.
Applicative f =>
(a -> a -> b) -> f a -> f a -> f b
farm21 a -> a -> (a1, b)
f f a
a
infixr 5 <++>
(Vector a :: [a]
a) <++> :: Vector a -> Vector a -> Vector a
<++> (Vector b :: [a]
b) = [a] -> Vector a
forall a. [a] -> Vector a
Vector ([a]
a [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a]
b)
unsafeApply :: ([a] -> t) -> Vector a -> t
unsafeApply f :: [a] -> t
f (Vector a :: [a]
a) = [a] -> t
f [a]
a
unsafeLift :: ([a] -> [a]) -> Vector a -> Vector a
unsafeLift f :: [a] -> [a]
f (Vector a :: [a]
a) = [a] -> Vector a
forall a. [a] -> Vector a
Vector ([a] -> [a]
f [a]
a)
reduce :: (t -> t -> t) -> Vector t -> t
reduce f :: t -> t -> t
f = ([t] -> t) -> Vector t -> t
forall a t. ([a] -> t) -> Vector a -> t
unsafeApply ((t -> t -> t) -> [t] -> t
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
L.foldr1 t -> t -> t
f)
length :: Vector a -> Int
length = ([a] -> Int) -> Vector a -> Int
forall a t. ([a] -> t) -> Vector a -> t
unsafeApply ([a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
L.length)
drop :: Int -> Vector a -> Vector a
drop n :: Int
n = ([a] -> [a]) -> Vector a -> Vector a
forall a a. ([a] -> [a]) -> Vector a -> Vector a
unsafeLift (Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
L.drop Int
n)
take :: Int -> Vector a -> Vector a
take n :: Int
n = ([a] -> [a]) -> Vector a -> Vector a
forall a a. ([a] -> [a]) -> Vector a -> Vector a
unsafeLift (Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
L.take Int
n)
first :: Vector t -> t
first = ([t] -> t) -> Vector t -> t
forall a t. ([a] -> t) -> Vector a -> t
unsafeApply [t] -> t
forall a. [a] -> a
L.head
group :: Int -> Vector a -> Vector (Vector a)
group :: Int -> Vector a -> Vector (Vector a)
group n :: Int
n (Vector a :: [a]
a) = [Vector a] -> Vector (Vector a)
forall a. [a] -> Vector a
vector ([Vector a] -> Vector (Vector a))
-> [Vector a] -> Vector (Vector a)
forall a b. (a -> b) -> a -> b
$ ([a] -> Vector a) -> [[a]] -> [Vector a]
forall a b. (a -> b) -> [a] -> [b]
map [a] -> Vector a
forall a. [a] -> Vector a
vector ([[a]] -> [Vector a]) -> [[a]] -> [Vector a]
forall a b. (a -> b) -> a -> b
$ Int -> [a] -> [[a]]
forall e. Int -> [e] -> [[e]]
chunksOf Int
n [a]
a
fanout :: a -> Vector a
fanout = [a] -> Vector a
forall a. [a] -> Vector a
vector ([a] -> Vector a) -> (a -> [a]) -> a -> Vector a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
forall a. a -> [a]
L.repeat
fanoutn :: Int -> a -> Vector a
fanoutn n :: Int
n = [a] -> Vector a
forall a. [a] -> Vector a
vector ([a] -> Vector a) -> (a -> [a]) -> a -> Vector a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a -> [a]
forall a. Int -> a -> [a]
L.replicate Int
n
stencil :: Int -> Vector a -> Vector (Vector a)
stencil n :: Int
n v :: Vector a
v = (Vector a -> Vector a) -> Vector (Vector a) -> Vector (Vector a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
farm11 (Int -> Vector a -> Vector a
forall a. Int -> Vector a -> Vector a
take Int
n) (Vector (Vector a) -> Vector (Vector a))
-> Vector (Vector a) -> Vector (Vector a)
forall a b. (a -> b) -> a -> b
$ Int -> Vector (Vector a) -> Vector (Vector a)
forall a. Int -> Vector a -> Vector a
dropFromEnd Int
n (Vector (Vector a) -> Vector (Vector a))
-> Vector (Vector a) -> Vector (Vector a)
forall a b. (a -> b) -> a -> b
$ Vector a -> Vector (Vector a)
forall a. Vector a -> Vector (Vector a)
tails Vector a
v
where dropFromEnd :: Int -> Vector a -> Vector a
dropFromEnd n :: Int
n = Int -> Vector a -> Vector a
forall a. Int -> Vector a -> Vector a
take (Vector a -> Int
forall a. Vector a -> Int
length Vector a
v Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 1)
tails :: Vector a -> Vector (Vector a)
tails = ([a] -> [Vector a]) -> Vector a -> Vector (Vector a)
forall a a. ([a] -> [a]) -> Vector a -> Vector a
unsafeLift ([Vector a] -> [Vector a]
forall a. [a] -> [a]
L.init ([Vector a] -> [Vector a])
-> ([a] -> [Vector a]) -> [a] -> [Vector a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([a] -> Vector a) -> [[a]] -> [Vector a]
forall a b. (a -> b) -> [a] -> [b]
map [a] -> Vector a
forall a. [a] -> Vector a
vector ([[a]] -> [Vector a]) -> ([a] -> [[a]]) -> [a] -> [Vector a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> [[a]]
forall a. [a] -> [[a]]
L.tails)
concat :: Vector (Vector a) -> Vector a
concat = ([Vector a] -> [a]) -> Vector (Vector a) -> Vector a
forall a a. ([a] -> [a]) -> Vector a -> Vector a
unsafeLift ([[a]] -> [a]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
L.concat ([[a]] -> [a]) -> ([Vector a] -> [[a]]) -> [Vector a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Vector a -> [a]) -> [Vector a] -> [[a]]
forall a b. (a -> b) -> [a] -> [b]
map Vector a -> [a]
forall a. Vector a -> [a]
fromVector)
iterate :: Int -> (a -> a) -> a -> Vector a
iterate n :: Int
n f :: a -> a
f i :: a
i = [a] -> Vector a
forall a. [a] -> Vector a
vector ([a] -> Vector a) -> [a] -> Vector a
forall a b. (a -> b) -> a -> b
$ Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
L.take Int
n ([a] -> [a]) -> [a] -> [a]
forall a b. (a -> b) -> a -> b
$ (a -> a) -> a -> [a]
forall a. (a -> a) -> a -> [a]
L.iterate a -> a
f a
i
pipe :: Vector (a -> a) -> a -> a
pipe (Vector []) i :: a
i = a
i
pipe v :: Vector (a -> a)
v i :: a
i = ([a -> a] -> a -> a) -> Vector (a -> a) -> a -> a
forall a t. ([a] -> t) -> Vector a -> t
unsafeApply (((a -> a) -> (a -> a) -> a -> a) -> [a -> a] -> a -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
L.foldr1 (a -> a) -> (a -> a) -> a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
(.)) Vector (a -> a)
v a
i
pipe1 :: (a -> t -> t) -> Vector a -> t -> t
pipe1 f :: a -> t -> t
f v :: Vector a
v i :: t
i = ([a] -> t) -> Vector a -> t
forall a t. ([a] -> t) -> Vector a -> t
unsafeApply ((a -> t -> t) -> t -> [a] -> t
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
L.foldr a -> t -> t
f t
i) Vector a
v
reverse :: Vector a -> Vector a
reverse = ([a] -> [a]) -> Vector a -> Vector a
forall a a. ([a] -> [a]) -> Vector a -> Vector a
unsafeLift [a] -> [a]
forall a. [a] -> [a]
L.reverse
recuri :: Vector (b -> b) -> b -> Vector b
recuri ps :: Vector (b -> b)
ps s :: b
s = (Vector (b -> b) -> b) -> Vector (Vector (b -> b)) -> Vector b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
farm11 (Vector (b -> b) -> b -> b
forall a. Vector (a -> a) -> a -> a
`pipe` b
s) (([b -> b] -> [Vector (b -> b)])
-> Vector (b -> b) -> Vector (Vector (b -> b))
forall a a. ([a] -> [a]) -> Vector a -> Vector a
unsafeLift (([b -> b] -> Vector (b -> b)) -> [[b -> b]] -> [Vector (b -> b)]
forall a b. (a -> b) -> [a] -> [b]
L.map [b -> b] -> Vector (b -> b)
forall a. [a] -> Vector a
vector ([[b -> b]] -> [Vector (b -> b)])
-> ([b -> b] -> [[b -> b]]) -> [b -> b] -> [Vector (b -> b)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [b -> b] -> [[b -> b]]
forall a. [a] -> [[a]]
L.tails) Vector (b -> b)
ps)
get :: Int -> Vector a -> Maybe a
get :: Int -> Vector a -> Maybe a
get _ (Vector []) = Maybe a
forall a. Maybe a
Nothing
get n :: Int
n v :: Vector a
v | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Vector a -> Int
forall a. Vector a -> Int
length Vector a
v = Maybe a
forall a. Maybe a
Nothing
| Bool
otherwise = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ Vector a -> [a]
forall a. Vector a -> [a]
fromVector Vector a
v [a] -> Int -> a
forall a. [a] -> Int -> a
!! Int
n
evensF :: [a] -> [a]
evensF [] = []
evensF [x :: a
x] = [a
x]
evensF (x :: a
x:_:xs :: [a]
xs) = a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a] -> [a]
evensF [a]
xs
oddsF :: [a] -> [a]
oddsF [] = []
oddsF [_] = []
oddsF (_:y :: a
y:xs :: [a]
xs) = a
ya -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a] -> [a]
oddsF [a]
xs
evens :: Vector a -> Vector a
evens = ([a] -> [a]) -> Vector a -> Vector a
forall a a. ([a] -> [a]) -> Vector a -> Vector a
unsafeLift [a] -> [a]
forall a. [a] -> [a]
evensF
odds :: Vector a -> Vector a
odds = ([a] -> [a]) -> Vector a -> Vector a
forall a a. ([a] -> [a]) -> Vector a -> Vector a
unsafeLift [a] -> [a]
forall a. [a] -> [a]
oddsF