{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ScopedTypeVariables#-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FunctionalDependencies #-}
module Numerical.Array.Pure where
import qualified Numerical.Array.Layout as L
import Numerical.Array.Locality
import Numerical.Array.Shape
import Numerical.Array.Range
import Numerical.Array.Storage as S
import Numerical.World
import qualified Data.Vector.Generic as VG
data family ImmArray world rep lay (view::Locality) (rank :: Nat ) el
data instance ImmArray Native rep lay locality rank el =
ImMutableNativeArray {
nativeBufferPure :: ! (S.BufferPure rep el )
,nativeFormatPure :: ! (L.Format lay locality rank rep)
}
class PureArray arr (rank:: Nat) a | arr -> rank where
type PureArrayAddress (arr :: * -> * ) :: *
basicShape :: arr a -> Index rank
basicSparseIndexToAddress :: ( address ~PureArrayAddress arr) => arr a -> Index rank -> (Maybe address)
basicAddressToIndex :: (address ~PureArrayAddress arr) => arr a -> address -> (Index rank )
basicAddressRange :: (address ~PureArrayAddress arr)=> arr a -> Maybe (Range address)
basicNextAddress :: (address ~PureArrayAddress arr)=> arr a -> address -> Maybe address
basicNextIndex :: (address ~PureArrayAddress arr)=>
arr a -> Index rank -> Maybe address -> Maybe ( Index rank, address)
basicUnsafeAddressRead :: (Monad m , address ~PureArrayAddress arr)=> arr a -> address-> m a
basicUnsafeSparseRead :: Monad m => arr a -> Index rank -> m (Maybe a)
instance (Buffer rep el , L.Layout (L.Format lay locality rank rep) rank)
=>PureArray (ImmArray Native rep lay locality rank ) rank el where
type PureArrayAddress (ImmArray Native rep lay locality rank )
=L.LayoutAddress (L.Format lay locality rank rep)
{-# INLINE basicShape #-}
basicShape = L.basicLogicalShape . nativeFormatPure
{-# INLINE basicSparseIndexToAddress #-}
basicSparseIndexToAddress= L.basicToAddress . nativeFormatPure
{-# INLINE basicAddressToIndex #-}
basicAddressToIndex = L.basicToIndex . nativeFormatPure
{-# INLINE basicAddressRange #-}
basicAddressRange = L.basicAddressRange . nativeFormatPure
{-# INLINE basicNextAddress #-}
basicNextAddress= L.basicNextAddress . nativeFormatPure
{-# INLINE basicNextIndex #-}
basicNextIndex = L.basicNextIndex . nativeFormatPure
{-# INLINE basicUnsafeSparseRead #-}
basicUnsafeSparseRead =
\ arr ix -> case basicSparseIndexToAddress arr ix of
Nothing -> return Nothing
(Just addr) -> basicUnsafeAddressRead arr addr >>= ( return . Just)
{-# INLINE basicUnsafeAddressRead #-}
basicUnsafeAddressRead =
\ arr addr ->
VG.basicUnsafeIndexM (nativeBufferPure arr)
(L.basicAddressAsInt (nativeFormatPure arr) $ addr)
class PureArray arr rank a => PureDenseArray arr rank a where
basicIndexInBounds :: arr a -> Index rank -> Bool
basicUnsafeAddressDenseRead :: (address ~PureArrayAddress arr,Monad m) => arr a -> address-> m a
basicUnsafeDenseReadM :: Monad m => arr a -> Index rank -> m a