module Data.Repa.Array.Internals.Shape
(
Shape(..)
, inShape
, showShape
, Z (..)
, (:.) (..)
, SH0, SH1, SH2, SH3, SH4, SH5
, ish0, ish1, ish2, ish3, ish4, ish5)
where
#include "repa-array.h"
class Eq sh => Shape sh where
rank :: sh -> Int
zeroDim :: sh
unitDim :: sh
intersectDim :: sh -> sh -> sh
addDim :: sh -> sh -> sh
size :: sh -> Int
inShapeRange :: sh -> sh -> sh -> Bool
listOfShape :: sh -> [Int]
shapeOfList :: [Int] -> Maybe sh
inShape :: Shape sh => sh -> sh -> Bool
inShape sh ix
= inShapeRange zeroDim sh ix
showShape :: Shape sh => sh -> String
showShape = foldr (\sh str -> str ++ " :. " ++ show sh) "Z" . listOfShape
instance Shape Int where
rank _ = 1
zeroDim = 0
unitDim = 1
intersectDim s1 s2 = min s1 s2
addDim s1 s2 = s1 + s2
size s = s
inShapeRange i1 i2 i = i >= i1 && i <= i2
listOfShape i = [i]
shapeOfList [i] = Just i
shapeOfList _ = Nothing
data Z = Z
deriving (Show, Read, Eq, Ord)
infixl 3 :.
data tail :. head
= !tail :. !head
deriving (Show, Read, Eq, Ord)
instance Shape Z where
rank _ = 0
zeroDim = Z
unitDim = Z
intersectDim _ _ = Z
addDim _ _ = Z
size _ = 1
inShapeRange Z Z Z = True
listOfShape _ = []
shapeOfList [] = Just Z
shapeOfList _ = Nothing
instance Shape sh => Shape (sh :. Int) where
rank (sh :. _)
= rank sh + 1
zeroDim = zeroDim :. 0
unitDim = unitDim :. 1
intersectDim (sh1 :. n1) (sh2 :. n2)
= (intersectDim sh1 sh2 :. (min n1 n2))
addDim (sh1 :. n1) (sh2 :. n2)
= addDim sh1 sh2 :. (n1 + n2)
size (sh1 :. n)
= size sh1 * n
inShapeRange (zs :. z) (sh1 :. n1) (sh2 :. n2)
= (n2 >= z) && (n2 < n1) && (inShapeRange zs sh1 sh2)
listOfShape (sh :. n)
= n : listOfShape sh
shapeOfList xx
= case xx of
[] -> Nothing
x : xs -> do ss <- shapeOfList xs
return $ ss :. x
type SH0 = Z
type SH1 = SH0 :. Int
type SH2 = SH1 :. Int
type SH3 = SH2 :. Int
type SH4 = SH3 :. Int
type SH5 = SH4 :. Int
ish0 :: SH0
ish0 = Z
ish1 :: Int -> SH1
ish1 x1 = Z :. x1
ish2 :: Int -> Int -> SH2
ish2 x2 x1 = Z :. x2 :. x1
ish3 :: Int -> Int -> Int -> SH3
ish3 x3 x2 x1 = Z :. x3 :. x2 :. x1
ish4 :: Int -> Int -> Int -> Int -> SH4
ish4 x4 x3 x2 x1 = Z :. x4 :. x3 :. x2 :. x1
ish5 :: Int -> Int -> Int -> Int -> Int -> SH5
ish5 x5 x4 x3 x2 x1 = Z :. x5 :. x4 :. x3 :. x2 :. x1