module ForSyDe.Atom.Skel.FastVector.Cube where

import ForSyDe.Atom.Skel.FastVector (Vector(..), vector, fromVector, (<++>))
import ForSyDe.Atom.Skel.FastVector.Matrix (Matrix, matrix, fromMatrix)

import qualified ForSyDe.Atom.Skel.FastVector as V
import qualified ForSyDe.Atom.Skel.FastVector.Matrix as M

-- | 'Cube' is simply a type synonym for vector of vectors. This
-- means that /any/ function on 'Vector' works also on 'Cube'.
type Cube a = Vector (Vector (Vector a))

-- | Prints out to the terminal a cube in a readable format, where
-- all elements are right-aligned and separated by a custom separator.
--
-- >>> let m = cube 2 2 2 [1,2,3,3,100,4,12,32]
-- >>> pretty "|" m
-- --------
-- 1|2
-- 3|3
-- --------
-- 100| 4
--  12|32
-- --------
pretty :: Show a
       => String   -- ^ separator string
       -> Cube a -- ^ input cube
       -> IO ()
pretty :: String -> Cube a -> IO ()
pretty sep :: String
sep mat :: Cube a
mat = (Matrix a -> IO ()) -> Cube a -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (\m :: Matrix a
m -> String -> IO ()
putStrLn "--------" IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> String -> Matrix a -> IO ()
forall a. Show a => String -> Matrix a -> IO ()
M.pretty String
sep Matrix a
m) Cube a
mat IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> String -> IO ()
putStrLn "--------"

-- | See 'ForSyDe.Atom.Skel.Vector.Cube.isNull'.
isNull :: Matrix a -> Bool
isNull :: Matrix a -> Bool
isNull (Vector []) = Bool
True
isNull (Vector [Vector []]) = Bool
True
isNull _ = Bool
False

-- | See 'ForSyDe.Atom.Skel.Vector.Cube.size'.
size :: Cube a -> (Int,Int,Int)
size :: Cube a -> (Int, Int, Int)
size m :: Cube a
m = (Int
x,Int
y,Int
z)
  where
    z :: Int
z = Cube a -> Int
forall a. Vector a -> Int
V.length (Cube a -> Cube a
forall a. Cube a -> Cube a
wellFormed Cube a
m)
    y :: Int
y = (Vector (Vector a) -> Int
forall a. Vector a -> Int
V.length (Vector (Vector a) -> Int)
-> (Cube a -> Vector (Vector a)) -> Cube a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Cube a -> Vector (Vector a)
forall t. Vector t -> t
V.first) Cube a
m
    x :: Int
x = (Vector a -> Int
forall a. Vector a -> Int
V.length (Vector a -> Int) -> (Cube a -> Vector a) -> Cube a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector (Vector a) -> Vector a
forall t. Vector t -> t
V.first (Vector (Vector a) -> Vector a)
-> (Cube a -> Vector (Vector a)) -> Cube a -> Vector a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Cube a -> Vector (Vector a)
forall t. Vector t -> t
V.first) Cube a
m

-- | See 'ForSyDe.Atom.Skel.Vector.Cube.wellFormed'.
wellFormed :: Cube a -> Cube a
wellFormed :: Cube a -> Cube a
wellFormed (Vector []) = [Vector (Vector a)] -> Cube a
forall a. [a] -> Vector a
Vector []
wellFormed (Vector (x :: Vector (Vector a)
x:xs :: [Vector (Vector a)]
xs)) = [Vector (Vector a)] -> Cube a
forall a. [a] -> Vector a
Vector ([Vector (Vector a)] -> Cube a) -> [Vector (Vector a)] -> Cube a
forall a b. (a -> b) -> a -> b
$ Vector (Vector a) -> Vector (Vector a)
forall a. Matrix a -> Matrix a
M.wellFormed Vector (Vector a)
x Vector (Vector a) -> [Vector (Vector a)] -> [Vector (Vector a)]
forall a. a -> [a] -> [a]
:
                             (Cube a -> [Vector (Vector a)]
forall a. Vector a -> [a]
fromVector (Cube a -> [Vector (Vector a)]) -> Cube a -> [Vector (Vector a)]
forall a b. (a -> b) -> a -> b
$ Cube a -> Cube a
forall a. Cube a -> Cube a
wellFormed (Cube a -> Cube a) -> Cube a -> Cube a
forall a b. (a -> b) -> a -> b
$ [Vector (Vector a)] -> Cube a
forall a. [a] -> Vector a
Vector [Vector (Vector a)]
xs)

-- | See 'ForSyDe.Atom.Skel.Vector.Cube.cube'.
cube :: Int      -- ^ number of columns (X dimension) @= x@
     -> Int      -- ^ number of rows (Y dimension) @= y@
     -> Int      -- ^ depth (Z dimension) @= z@
     -> [a]      -- ^ list of values; /length/ = @x * y * z@
     -> Cube a -- ^ 'Cube' of values; /size/ = @(x,y,z)@
cube :: Int -> Int -> Int -> [a] -> Cube a
cube x :: Int
x y :: Int
y z :: Int
z = [Matrix a] -> Cube a
forall a. [a] -> Vector a
vector ([Matrix a] -> Cube a) -> ([a] -> [Matrix a]) -> [a] -> Cube a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([a] -> Matrix a) -> [[a]] -> [Matrix a]
forall a b. (a -> b) -> [a] -> [b]
Prelude.map (Int -> Int -> [a] -> Matrix a
forall a. Int -> Int -> [a] -> Matrix a
matrix Int
x Int
y) ([[a]] -> [Matrix a]) -> ([a] -> [[a]]) -> [a] -> [Matrix a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [a] -> [[a]]
forall a. Int -> [a] -> [[a]]
M.groupEvery (Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
y) ([a] -> [[a]]) -> ([a] -> [a]) -> [a] -> [[a]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> [a]
forall (t :: * -> *) a. Foldable t => t a -> t a
check
  where
    check :: t a -> t a
check l :: t a
l | t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length t a
l Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
y Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
z = t a
l
            | Bool
otherwise
      = String -> t a
forall a. HasCallStack => String -> a
error (String -> t a) -> String -> t a
forall a b. (a -> b) -> a -> b
$ "cannot form cube (" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
x String -> String -> String
forall a. [a] -> [a] -> [a]
++ ","
              String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
y String -> String -> String
forall a. [a] -> [a] -> [a]
++ "," String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
z String -> String -> String
forall a. [a] -> [a] -> [a]
++ ") from a list with "
              String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show (t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length t a
l) String -> String -> String
forall a. [a] -> [a] -> [a]
++ " elements"

-- | See 'ForSyDe.Atom.Skel.Vector.Cube.fromCube'.
fromCube :: Cube a -- ^ /size/ = @(x,y)@
         -> [a]    -- ^ /length/ = @x * y@
fromCube :: Cube a -> [a]
fromCube = (Vector a -> [a]) -> [Vector a] -> [a]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Vector a -> [a]
forall a. Vector a -> [a]
fromVector ([Vector a] -> [a]) -> (Cube a -> [Vector a]) -> Cube a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Cube a -> [Vector a]
forall a. Matrix a -> [a]
fromMatrix

-- | See 'ForSyDe.Atom.Skel.Vector.Cube.unit'.
unit :: a -> Cube a -- ^ /size/ = @(1,1)@
unit :: a -> Cube a
unit a :: a
a = [Vector (Vector a)] -> Cube a
forall a. [a] -> Vector a
Vector [[Vector a] -> Vector (Vector a)
forall a. [a] -> Vector a
Vector [[a] -> Vector a
forall a. [a] -> Vector a
Vector [a
a]]]

-- | See 'ForSyDe.Atom.Skel.Vector.Cube.fanout'.
fanout :: a -> Cube a
fanout :: a -> Cube a
fanout n :: a
n = Vector (Vector a) -> Cube a
forall a. a -> Vector a
V.fanout (Vector (Vector a) -> Cube a) -> Vector (Vector a) -> Cube a
forall a b. (a -> b) -> a -> b
$ Vector a -> Vector (Vector a)
forall a. a -> Vector a
V.fanout (Vector a -> Vector (Vector a)) -> Vector a -> Vector (Vector a)
forall a b. (a -> b) -> a -> b
$ a -> Vector a
forall a. a -> Vector a
V.fanout a
n

-- | See 'ForSyDe.Atom.Skel.Vector.Cube.indexes'.
indexes :: Cube (Int, Int, Int)
indexes :: Cube (Int, Int, Int)
indexes = (Int -> Int -> Int -> (Int, Int, Int))
-> Cube Int -> Cube Int -> Cube Int -> Cube (Int, Int, Int)
forall a b c d.
(a -> b -> c -> d) -> Cube a -> Cube b -> Cube c -> Cube d
farm31 (,,) Cube Int
colix Cube Int
rowix Cube Int
depthix
  where
    colix :: Cube Int
colix = [Vector (Vector Int)] -> Cube Int
forall a. [a] -> Vector a
vector ([Vector (Vector Int)] -> Cube Int)
-> [Vector (Vector Int)] -> Cube Int
forall a b. (a -> b) -> a -> b
$ Vector (Vector Int) -> [Vector (Vector Int)]
forall a. a -> [a]
repeat (Vector (Vector Int) -> [Vector (Vector Int)])
-> Vector (Vector Int) -> [Vector (Vector Int)]
forall a b. (a -> b) -> a -> b
$ [Vector Int] -> Vector (Vector Int)
forall a. [a] -> Vector a
vector ([Vector Int] -> Vector (Vector Int))
-> [Vector Int] -> Vector (Vector Int)
forall a b. (a -> b) -> a -> b
$ Vector Int -> [Vector Int]
forall a. a -> [a]
repeat (Vector Int -> [Vector Int]) -> Vector Int -> [Vector Int]
forall a b. (a -> b) -> a -> b
$ [Int] -> Vector Int
forall a. [a] -> Vector a
vector [0..]
    rowix :: Cube Int
rowix = (Vector (Vector Int) -> Vector (Vector Int))
-> Cube Int -> Cube Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
V.farm11 Vector (Vector Int) -> Vector (Vector Int)
forall a. Matrix a -> Matrix a
M.transpose Cube Int
colix
    depthix :: Cube Int
depthix =  Cube Int -> Cube Int
forall a. Matrix a -> Matrix a
M.transpose (Cube Int -> Cube Int) -> Cube Int -> Cube Int
forall a b. (a -> b) -> a -> b
$ (Vector (Vector Int) -> Vector (Vector Int))
-> Cube Int -> Cube Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
V.farm11 Vector (Vector Int) -> Vector (Vector Int)
forall a. Matrix a -> Matrix a
M.transpose Cube Int
colix
    
-- | See 'ForSyDe.Atom.Skel.Vector.Cube.transpose'.
transpose :: Cube a -- ^ dimensions @(Z,Y,X)@
           -> Cube a -- ^ dimensions @(Y,X,Z)@
transpose :: Cube a -> Cube a
transpose = (Matrix a -> Matrix a) -> Cube a -> Cube a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
V.farm11 Matrix a -> Matrix a
forall a. Matrix a -> Matrix a
M.transpose (Cube a -> Cube a) -> (Cube a -> Cube a) -> Cube a -> Cube a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Cube a -> Cube a
forall a. Matrix a -> Matrix a
M.transpose

-- | See 'ForSyDe.Atom.Skel.Vector.Cube.transpose''.
transpose' :: Cube a -- ^ dimensions @(Y,X,Z)@
            -> Cube a -- ^ dimensions @(Z,Y,X)@
transpose' :: Cube a -> Cube a
transpose' = Cube a -> Cube a
forall a. Matrix a -> Matrix a
M.transpose (Cube a -> Cube a) -> (Cube a -> Cube a) -> Cube a -> Cube a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Matrix a -> Matrix a) -> Cube a -> Cube a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
V.farm11 Matrix a -> Matrix a
forall a. Matrix a -> Matrix a
M.transpose

-- | See 'ForSyDe.Atom.Skel.Vector.Cube.farm11'.
farm11 :: (a -> b)
       -> Cube a -- ^ /size/ = @(xa,ya)@
       -> Cube b -- ^ /size/ = @(xa,ya)@
farm11 :: (a -> b) -> Cube a -> Cube b
farm11 = (Vector (Vector a) -> Vector (Vector b)) -> Cube a -> Cube b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
V.farm11 ((Vector (Vector a) -> Vector (Vector b)) -> Cube a -> Cube b)
-> ((a -> b) -> Vector (Vector a) -> Vector (Vector b))
-> (a -> b)
-> Cube a
-> Cube b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Vector a -> Vector b) -> Vector (Vector a) -> Vector (Vector b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
V.farm11 ((Vector a -> Vector b) -> Vector (Vector a) -> Vector (Vector b))
-> ((a -> b) -> Vector a -> Vector b)
-> (a -> b)
-> Vector (Vector a)
-> Vector (Vector b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b) -> Vector a -> Vector b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
V.farm11

-- | See 'ForSyDe.Atom.Skel.Vector.Cube.farm21'.
farm21 :: (a -> b -> c)
           -> Cube a -- ^ /size/ = @(xa,ya)@
           -> Cube b -- ^ /size/ = @(xb,yb)@
           -> Cube c -- ^ /size/ = @(minimum [xa,xb], minimum [ya,yb])@
farm21 :: (a -> b -> c) -> Cube a -> Cube b -> Cube c
farm21 f :: a -> b -> c
f = (Vector (Vector a) -> Vector (Vector b) -> Vector (Vector c))
-> Cube a -> Cube b -> Cube c
forall (f :: * -> *) a1 a2 b.
Applicative f =>
(a1 -> a2 -> b) -> f a1 -> f a2 -> f b
V.farm21 ((Vector a -> Vector b -> Vector c)
-> Vector (Vector a) -> Vector (Vector b) -> Vector (Vector c)
forall (f :: * -> *) a1 a2 b.
Applicative f =>
(a1 -> a2 -> b) -> f a1 -> f a2 -> f b
V.farm21 ((a -> b -> c) -> Vector a -> Vector b -> Vector c
forall (f :: * -> *) a1 a2 b.
Applicative f =>
(a1 -> a2 -> b) -> f a1 -> f a2 -> f b
V.farm21 a -> b -> c
f))

-- | See 'ForSyDe.Atom.Skel.Vector.Cube.farm31'.
farm31 :: (a -> b -> c -> d)
            -> Cube a -- ^ /size/ = @(xa,ya)@
            -> Cube b -- ^ /size/ = @(xb,yb)@
            -> Cube c -- ^ /size/ = @(xc,yc)@
            -> Cube d -- ^ /size/ = @(minimum [xa,xb,xc], minimum [ya,yb,yc])@
farm31 :: (a -> b -> c -> d) -> Cube a -> Cube b -> Cube c -> Cube d
farm31 f :: a -> b -> c -> d
f = (Vector (Vector a)
 -> Vector (Vector b) -> Vector (Vector c) -> Vector (Vector d))
-> Cube a -> Cube b -> Cube c -> Cube d
forall (f :: * -> *) a1 a2 a3 b.
Applicative f =>
(a1 -> a2 -> a3 -> b) -> f a1 -> f a2 -> f a3 -> f b
V.farm31 ((Vector a -> Vector b -> Vector c -> Vector d)
-> Vector (Vector a)
-> Vector (Vector b)
-> Vector (Vector c)
-> Vector (Vector d)
forall (f :: * -> *) a1 a2 a3 b.
Applicative f =>
(a1 -> a2 -> a3 -> b) -> f a1 -> f a2 -> f a3 -> f b
V.farm31 ((a -> b -> c -> d) -> Vector a -> Vector b -> Vector c -> Vector d
forall (f :: * -> *) a1 a2 a3 b.
Applicative f =>
(a1 -> a2 -> a3 -> b) -> f a1 -> f a2 -> f a3 -> f b
V.farm31 a -> b -> c -> d
f))

-- | See 'ForSyDe.Atom.Skel.Vector.Cube.reduce'.
reduce :: (a -> a -> a) -> Cube a -> a
reduce :: (a -> a -> a) -> Cube a -> a
reduce f :: a -> a -> a
f = (a -> a -> a) -> Vector a -> a
forall t. (t -> t -> t) -> Vector t -> t
V.reduce a -> a -> a
f (Vector a -> a) -> (Cube a -> Vector a) -> Cube a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Matrix a -> a) -> Cube a -> Vector a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
V.farm11 ((a -> a -> a) -> Matrix a -> a
forall a. (a -> a -> a) -> Matrix a -> a
M.reduce a -> a -> a
f)

-- | See 'ForSyDe.Atom.Skel.Vector.Cube.get'.
get :: Int       -- ^ X index starting from zero
    -> Int       -- ^ Y index starting from zero
    -> Int       -- ^ Z index starting from zero
    -> Cube a
    -> Maybe a
get :: Int -> Int -> Int -> Cube a -> Maybe a
get x :: Int
x y :: Int
y z :: Int
z = Maybe (Matrix a) -> Maybe a
forall a. Maybe (Matrix a) -> Maybe a
getMaybe (Maybe (Matrix a) -> Maybe a)
-> (Cube a -> Maybe (Matrix a)) -> Cube a -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Cube a -> Maybe (Matrix a)
forall a. Int -> Vector a -> Maybe a
V.get Int
z
  where getMaybe :: Maybe (Matrix a) -> Maybe a
getMaybe Nothing = Maybe a
forall a. Maybe a
Nothing
        getMaybe (Just a :: Matrix a
a) = Int -> Int -> Matrix a -> Maybe a
forall a. Int -> Int -> Matrix a -> Maybe a
M.get Int
x Int
y Matrix a
a

-- | See 'ForSyDe.Atom.Skel.Vector.Cube.take'.
take :: Int       -- ^ X index starting from zero
     -> Int       -- ^ Y index starting from zero
     -> Int       -- ^ > index starting from zero
     -> Cube a
     -> Cube a
take :: Int -> Int -> Int -> Cube a -> Cube a
take x :: Int
x y :: Int
y z :: Int
z = (Matrix a -> Matrix a) -> Cube a -> Cube a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
V.farm11 (Int -> Int -> Matrix a -> Matrix a
forall a. Int -> Int -> Matrix a -> Matrix a
M.take Int
x Int
y) (Cube a -> Cube a) -> (Cube a -> Cube a) -> Cube a -> Cube a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Cube a -> Cube a
forall a. Int -> Vector a -> Vector a
V.take Int
z

-- | See 'ForSyDe.Atom.Skel.Vector.Cube.drop'.
drop :: Int       -- ^ X index starting from zero
     -> Int       -- ^ Y index starting from zero
     -> Int       -- ^ Z index starting from zero
     -> Cube a
     -> Cube a
drop :: Int -> Int -> Int -> Cube a -> Cube a
drop x :: Int
x y :: Int
y z :: Int
z = (Matrix a -> Matrix a) -> Cube a -> Cube a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
V.farm11 (Int -> Int -> Matrix a -> Matrix a
forall a. Int -> Int -> Matrix a -> Matrix a
M.drop Int
x Int
y) (Cube a -> Cube a) -> (Cube a -> Cube a) -> Cube a -> Cube a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Cube a -> Cube a
forall a. Int -> Vector a -> Vector a
V.drop Int
z