module Data.Array.FixedStorableArray
( FixedStorableArray
, newFixedStorableArray
, newFixedStorableArray_
, toStorableArray
, N(..)
, fromNat
, Bounds(..)
) where
import GHC.TypeLits
import Data.Array.Storable
import Data.Functor ((<$>))
import Foreign.Storable (Storable(..))
import Foreign.Marshal.Array (copyArray)
import Foreign.Ptr (castPtr)
data N (n :: Nat) = N deriving (Eq, Ord, Enum)
instance SingI n => Show (N n) where
show N = "<N " ++ show (fromNat (N :: N n)) ++ ">"
fromNat :: forall (proxy :: Nat -> *) (n :: Nat). SingI n => proxy n -> Int
fromNat _ = fromInteger $ fromSing (sing :: Sing n)
newtype FixedStorableArray dimensions e =
FixedStorableArray {
toStorableArray :: StorableArray (Bound dimensions) e }
class Bounds d where
type Bound d :: *
bounds :: FixedStorableArray d e -> (Bound d, Bound d)
newFixedStorableArray :: (Bounds d, Ix (Bound d), Storable e) =>
e -> IO (FixedStorableArray d e)
newFixedStorableArray x = do
rec let b = bounds ma
ma <- FixedStorableArray <$> newArray b x
return ma
newFixedStorableArray_ :: (Bounds d, Ix (Bound d), Storable e) =>
IO (FixedStorableArray d e)
newFixedStorableArray_ = do
rec let b = bounds ma
ma <- FixedStorableArray <$> newArray_ b
return ma
instance (Bounds d, Ix (Bound d), Storable e) =>
Storable (FixedStorableArray d e) where
sizeOf a = sizeOf (undefined :: e) * rangeSize (bounds a)
alignment _ = alignment (undefined :: e)
peek src' = do
ma <- newFixedStorableArray_
let sa = toStorableArray ma
src = castPtr src'
count <- rangeSize <$> getBounds sa
withStorableArray sa $ \dst -> copyArray dst src count
return ma
poke dst' ma = do
let sa = toStorableArray ma
dst = castPtr dst'
count <- rangeSize <$> getBounds sa
withStorableArray sa $ \src -> copyArray dst src count
instance SingI a => Bounds (N a) where
type Bound (N a) = Int
bounds _ = (0, fromNat (N :: N a) 1)
instance (SingI a, SingI b) => Bounds (N a, N b) where
type Bound (N a, N b) = (Int, Int)
bounds _ = ((0, 0),
(fromNat (N :: N a) 1,
fromNat (N :: N b) 1))
instance (SingI a, SingI b, SingI c) => Bounds (N a, N b, N c) where
type Bound (N a, N b, N c) = (Int, Int, Int)
bounds _ = ((0, 0, 0),
(fromNat (N :: N a) 1,
fromNat (N :: N b) 1,
fromNat (N :: N c) 1))
instance (SingI a, SingI b, SingI c, SingI d) => Bounds (N a, N b, N c, N d) where
type Bound (N a, N b, N c, N d) = (Int, Int, Int, Int)
bounds _ = ((0, 0, 0, 0),
(fromNat (N :: N a) 1,
fromNat (N :: N b) 1,
fromNat (N :: N c) 1,
fromNat (N :: N d) 1))
instance (SingI a, SingI b, SingI c, SingI d, SingI e) =>
Bounds (N a, N b, N c, N d, N e) where
type Bound (N a, N b, N c, N d, N e) = (Int, Int, Int, Int, Int)
bounds _ = ((0, 0, 0, 0, 0),
(fromNat (N :: N a) 1,
fromNat (N :: N b) 1,
fromNat (N :: N c) 1,
fromNat (N :: N d) 1,
fromNat (N :: N e) 1))
instance (SingI a, SingI b, SingI c, SingI d, SingI e, SingI f) =>
Bounds (N a, N b, N c, N d, N e, N f) where
type Bound (N a, N b, N c, N d, N e, N f) = (Int, Int, Int, Int, Int, Int)
bounds _ = ((0, 0, 0, 0, 0, 0),
(fromNat (N :: N a) 1,
fromNat (N :: N b) 1,
fromNat (N :: N c) 1,
fromNat (N :: N d) 1,
fromNat (N :: N e) 1,
fromNat (N :: N f) 1))
instance (SingI a, SingI b, SingI c, SingI d, SingI e, SingI f, SingI g) =>
Bounds (N a, N b, N c, N d, N e, N f, N g) where
type Bound (N a, N b, N c, N d, N e, N f, N g) =
(Int, Int, Int, Int, Int, Int, Int)
bounds _ = ((0, 0, 0, 0, 0, 0, 0),
(fromNat (N :: N a) 1,
fromNat (N :: N b) 1,
fromNat (N :: N c) 1,
fromNat (N :: N d) 1,
fromNat (N :: N e) 1,
fromNat (N :: N f) 1,
fromNat (N :: N g) 1))
instance (SingI a, SingI b, SingI c, SingI d, SingI e, SingI f, SingI g,
SingI h) =>
Bounds (N a, N b, N c, N d, N e, N f, N g, N h) where
type Bound (N a, N b, N c, N d, N e, N f, N g, N h) =
(Int, Int, Int, Int, Int, Int, Int, Int)
bounds _ = ((0, 0, 0, 0, 0, 0, 0, 0),
(fromNat (N :: N a) 1,
fromNat (N :: N b) 1,
fromNat (N :: N c) 1,
fromNat (N :: N d) 1,
fromNat (N :: N e) 1,
fromNat (N :: N f) 1,
fromNat (N :: N g) 1,
fromNat (N :: N h) 1))
instance (SingI a, SingI b, SingI c, SingI d, SingI e, SingI f, SingI g,
SingI h, SingI i) =>
Bounds (N a, N b, N c, N d, N e, N f, N g, N h, N i) where
type Bound (N a, N b, N c, N d, N e, N f, N g, N h, N i) =
(Int, Int, Int, Int, Int, Int, Int, Int, Int)
bounds _ = ((0, 0, 0, 0, 0, 0, 0, 0, 0),
(fromNat (N :: N a) 1,
fromNat (N :: N b) 1,
fromNat (N :: N c) 1,
fromNat (N :: N d) 1,
fromNat (N :: N e) 1,
fromNat (N :: N f) 1,
fromNat (N :: N g) 1,
fromNat (N :: N h) 1,
fromNat (N :: N i) 1))
instance (SingI a, SingI b, SingI c, SingI d, SingI e, SingI f, SingI g,
SingI h, SingI i, SingI j) =>
Bounds (N a, N b, N c, N d, N e, N f, N g, N h, N i, N j) where
type Bound (N a, N b, N c, N d, N e, N f, N g, N h, N i, N j) =
(Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)
bounds _ = ((0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
(fromNat (N :: N a) 1,
fromNat (N :: N b) 1,
fromNat (N :: N c) 1,
fromNat (N :: N d) 1,
fromNat (N :: N e) 1,
fromNat (N :: N f) 1,
fromNat (N :: N g) 1,
fromNat (N :: N h) 1,
fromNat (N :: N i) 1,
fromNat (N :: N j) 1))
instance (SingI a, SingI b, SingI c, SingI d, SingI e, SingI f, SingI g,
SingI h, SingI i, SingI j, SingI k) =>
Bounds (N a, N b, N c, N d, N e, N f, N g, N h, N i, N j, N k) where
type Bound (N a, N b, N c, N d, N e, N f, N g, N h, N i, N j, N k) =
(Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)
bounds _ = ((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
(fromNat (N :: N a) 1,
fromNat (N :: N b) 1,
fromNat (N :: N c) 1,
fromNat (N :: N d) 1,
fromNat (N :: N e) 1,
fromNat (N :: N f) 1,
fromNat (N :: N g) 1,
fromNat (N :: N h) 1,
fromNat (N :: N i) 1,
fromNat (N :: N j) 1,
fromNat (N :: N k) 1))
instance (SingI a, SingI b, SingI c, SingI d, SingI e, SingI f, SingI g,
SingI h, SingI i, SingI j, SingI k, SingI l) =>
Bounds (N a, N b, N c, N d, N e, N f, N g, N h, N i, N j, N k,
N l) where
type Bound (N a, N b, N c, N d, N e, N f, N g, N h, N i, N j, N k, N l) =
(Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)
bounds _ = ((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
(fromNat (N :: N a) 1,
fromNat (N :: N b) 1,
fromNat (N :: N c) 1,
fromNat (N :: N d) 1,
fromNat (N :: N e) 1,
fromNat (N :: N f) 1,
fromNat (N :: N g) 1,
fromNat (N :: N h) 1,
fromNat (N :: N i) 1,
fromNat (N :: N j) 1,
fromNat (N :: N k) 1,
fromNat (N :: N l) 1))
instance (SingI a, SingI b, SingI c, SingI d, SingI e, SingI f, SingI g,
SingI h, SingI i, SingI j, SingI k, SingI l, SingI m) =>
Bounds (N a, N b, N c, N d, N e, N f, N g, N h, N i, N j, N k, N l,
N m) where
type Bound (N a, N b, N c, N d, N e, N f, N g, N h, N i, N j, N k, N l,
N m) =
(Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)
bounds _ = ((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
(fromNat (N :: N a) 1,
fromNat (N :: N b) 1,
fromNat (N :: N c) 1,
fromNat (N :: N d) 1,
fromNat (N :: N e) 1,
fromNat (N :: N f) 1,
fromNat (N :: N g) 1,
fromNat (N :: N h) 1,
fromNat (N :: N i) 1,
fromNat (N :: N j) 1,
fromNat (N :: N k) 1,
fromNat (N :: N l) 1,
fromNat (N :: N m) 1))