{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}

-- |
-- Module      : Data.Massiv.Array.Manifest.Vector
-- Copyright   : (c) Alexey Kuleshevich 2018-2022
-- License     : BSD3
-- Maintainer  : Alexey Kuleshevich <lehins@yandex.ru>
-- Stability   : experimental
-- Portability : non-portable
module Data.Massiv.Array.Manifest.Vector (
  fromVectorM,
  fromVector',
  castFromVector,
  toVector,
  castToVector,
  ARepr,
  VRepr,
) where

import Control.Monad (guard, join, msum)
import Data.Kind
import Data.Massiv.Array.Manifest.Boxed
import Data.Massiv.Array.Manifest.Internal
import Data.Massiv.Array.Manifest.Primitive
import Data.Massiv.Array.Manifest.Storable
import Data.Massiv.Array.Manifest.Unboxed
import Data.Massiv.Core.Common
import Data.Maybe (fromMaybe)
import Data.Typeable
import qualified Data.Vector as VB
import qualified Data.Vector.Generic as VG
import qualified Data.Vector.Primitive as VP
import qualified Data.Vector.Storable as VS
import qualified Data.Vector.Unboxed as VU

-- | Match vector type to array representation
type family ARepr (v :: Type -> Type) :: Type where
  ARepr VU.Vector = U
  ARepr VS.Vector = S
  ARepr VP.Vector = P
  ARepr VB.Vector = BL

-- | Match array representation to a vector type
type family VRepr r :: Type -> Type where
  VRepr U = VU.Vector
  VRepr S = VS.Vector
  VRepr P = VP.Vector
  VRepr B = VB.Vector
  VRepr BN = VB.Vector
  VRepr BL = VB.Vector

-- | /O(1)/ - conversion from vector to an array with a corresponding representation. Will
-- return `Nothing` if there is a size mismatch or if some non-standard vector type is
-- supplied. Is suppplied is the boxed `Data.Vector.Vector` then it's all elements will be
-- evaluated toWHNF, therefore complexity will be /O(n)/
castFromVector
  :: forall v r ix e
   . (VG.Vector v e, Typeable v, Index ix, ARepr v ~ r)
  => Comp
  -> Sz ix
  -- ^ Size of the result Array
  -> v e
  -- ^ Source Vector
  -> Maybe (Array r ix e)
castFromVector :: forall (v :: * -> *) r ix e.
(Vector v e, Typeable v, Index ix, ARepr v ~ r) =>
Comp -> Sz ix -> v e -> Maybe (Array r ix e)
castFromVector Comp
comp Sz ix
sz v e
vector = do
  forall (f :: * -> *). Alternative f => Bool -> f ()
guard (forall ix. Index ix => Sz ix -> Int
totalElem Sz ix
sz forall a. Eq a => a -> a -> Bool
== forall (v :: * -> *) a. Vector v a => v a -> Int
VG.length v e
vector)
  forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, MonadPlus m) =>
t (m a) -> m a
msum
    [ do
        v :~: Vector
Refl <- forall {k} (a :: k) (b :: k).
(Typeable a, Typeable b) =>
Maybe (a :~: b)
eqT :: Maybe (v :~: VU.Vector)
        Vector e
uVector <- forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall a b. (a -> b) -> a -> b
$ forall {k1} {k2} (c :: k1 -> *) (t :: k2 -> k1) (t' :: k2 -> k1)
       (a :: k2).
(Typeable t, Typeable t') =>
c (t a) -> Maybe (c (t' a))
gcast1 (forall a. a -> Maybe a
Just v e
vector)
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ UArray{uComp :: Comp
uComp = Comp
comp, uSize :: Sz ix
uSize = Sz ix
sz, uData :: Vector e
uData = Vector e
uVector}
    , do
        v :~: Vector
Refl <- forall {k} (a :: k) (b :: k).
(Typeable a, Typeable b) =>
Maybe (a :~: b)
eqT :: Maybe (v :~: VS.Vector)
        Vector e
sVector <- forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall a b. (a -> b) -> a -> b
$ forall {k1} {k2} (c :: k1 -> *) (t :: k2 -> k1) (t' :: k2 -> k1)
       (a :: k2).
(Typeable t, Typeable t') =>
c (t a) -> Maybe (c (t' a))
gcast1 (forall a. a -> Maybe a
Just v e
vector)
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall r ix ix' e.
(Size r, Index ix, Index ix') =>
Sz ix' -> Array r ix e -> Array r ix' e
unsafeResize Sz ix
sz forall a b. (a -> b) -> a -> b
$ forall e. Comp -> Vector e -> Vector S e
fromStorableVector Comp
comp Vector e
sVector
    , do
        v :~: Vector
Refl <- forall {k} (a :: k) (b :: k).
(Typeable a, Typeable b) =>
Maybe (a :~: b)
eqT :: Maybe (v :~: VP.Vector)
        VP.Vector Int
o Int
_ ByteArray
ba <- forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall a b. (a -> b) -> a -> b
$ forall {k1} {k2} (c :: k1 -> *) (t :: k2 -> k1) (t' :: k2 -> k1)
       (a :: k2).
(Typeable t, Typeable t') =>
c (t a) -> Maybe (c (t' a))
gcast1 (forall a. a -> Maybe a
Just v e
vector)
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ PArray{pComp :: Comp
pComp = Comp
comp, pSize :: Sz ix
pSize = Sz ix
sz, pOffset :: Int
pOffset = Int
o, pData :: ByteArray
pData = ByteArray
ba}
    , do
        v :~: Vector
Refl <- forall {k} (a :: k) (b :: k).
(Typeable a, Typeable b) =>
Maybe (a :~: b)
eqT :: Maybe (v :~: VB.Vector)
        Vector e
bVector <- forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall a b. (a -> b) -> a -> b
$ forall {k1} {k2} (c :: k1 -> *) (t :: k2 -> k1) (t' :: k2 -> k1)
       (a :: k2).
(Typeable t, Typeable t') =>
c (t a) -> Maybe (c (t' a))
gcast1 (forall a. a -> Maybe a
Just v e
vector)
        forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall r ix ix' e.
(Size r, Index ix, Index ix') =>
Sz ix' -> Array r ix e -> Array r ix' e
unsafeResize Sz ix
sz forall a b. (a -> b) -> a -> b
$ forall r ix e. Strategy r => Comp -> Array r ix e -> Array r ix e
setComp Comp
comp forall a b. (a -> b) -> a -> b
$ forall a. Vector a -> Vector BL a
fromBoxedVector Vector e
bVector
    ]
{-# NOINLINE castFromVector #-}

-- | In case when resulting array representation matches the one of vector's it
-- will do a /O(1)/ - conversion using `castFromVector`, otherwise Vector elements
-- will be copied into a new array. Will throw an error if length of resulting
-- array doesn't match the source vector length.
--
-- @since 0.3.0
fromVectorM
  :: (MonadThrow m, Typeable v, VG.Vector v a, Manifest r a, Load (ARepr v) ix a, Load r ix a)
  => Comp
  -> Sz ix
  -- ^ Resulting size of the array
  -> v a
  -- ^ Source Vector
  -> m (Array r ix a)
fromVectorM :: forall (m :: * -> *) (v :: * -> *) a r ix.
(MonadThrow m, Typeable v, Vector v a, Manifest r a,
 Load (ARepr v) ix a, Load r ix a) =>
Comp -> Sz ix -> v a -> m (Array r ix a)
fromVectorM Comp
comp Sz ix
sz v a
v =
  case forall (v :: * -> *) r ix e.
(Vector v e, Typeable v, Index ix, ARepr v ~ r) =>
Comp -> Sz ix -> v e -> Maybe (Array r ix e)
castFromVector Comp
comp Sz ix
sz v a
v of
    Just Array (ARepr v) ix a
arr -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
convert Array (ARepr v) ix a
arr
    Maybe (Array (ARepr v) ix a)
Nothing -> do
      forall (m :: * -> *) ix ix'.
(MonadThrow m, Index ix, Index ix') =>
Sz ix -> Sz ix' -> m ()
guardNumberOfElements Sz ix
sz (forall ix. Index ix => ix -> Sz ix
Sz (forall (v :: * -> *) a. Vector v a => v a -> Int
VG.length v a
v))
      forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall r ix e.
Load r ix e =>
Comp -> Sz ix -> (Int -> e) -> Array r ix e
makeArrayLinear Comp
comp Sz ix
sz (forall (v :: * -> *) a. Vector v a => v a -> Int -> a
VG.unsafeIndex v a
v))
{-# NOINLINE fromVectorM #-}

-- | Just like `fromVectorM`, but will throw an exception on a mismatched size.
--
-- @since 0.3.0
fromVector'
  :: (HasCallStack, Typeable v, VG.Vector v a, Load (ARepr v) ix a, Load r ix a, Manifest r a)
  => Comp
  -> Sz ix
  -- ^ Resulting size of the array
  -> v a
  -- ^ Source Vector
  -> Array r ix a
fromVector' :: forall (v :: * -> *) a ix r.
(HasCallStack, Typeable v, Vector v a, Load (ARepr v) ix a,
 Load r ix a, Manifest r a) =>
Comp -> Sz ix -> v a -> Array r ix a
fromVector' Comp
comp Sz ix
sz = forall a. HasCallStack => Either SomeException a -> a
throwEither forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) (v :: * -> *) a r ix.
(MonadThrow m, Typeable v, Vector v a, Manifest r a,
 Load (ARepr v) ix a, Load r ix a) =>
Comp -> Sz ix -> v a -> m (Array r ix a)
fromVectorM Comp
comp Sz ix
sz
{-# INLINE fromVector' #-}

-- | /O(1)/ - conversion from `Mutable` array to a corresponding vector. Will
-- return `Nothing` only if source array representation was not one of `B`, `N`,
-- `P`, `S` or `U`.
castToVector
  :: forall v r ix e
   . (Manifest r e, Index ix, VRepr r ~ v)
  => Array r ix e
  -> Maybe (v e)
castToVector :: forall (v :: * -> *) r ix e.
(Manifest r e, Index ix, VRepr r ~ v) =>
Array r ix e -> Maybe (v e)
castToVector Array r ix e
arr =
  forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, MonadPlus m) =>
t (m a) -> m a
msum
    [ do
        r :~: U
Refl <- forall {k} (a :: k) (b :: k).
(Typeable a, Typeable b) =>
Maybe (a :~: b)
eqT :: Maybe (r :~: U)
        Array U ix e
uArr <- forall r ix e r'.
(Typeable r, Typeable r') =>
Array r' ix e -> Maybe (Array r ix e)
gcastArr Array r ix e
arr
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall ix e. Array U ix e -> Vector e
uData Array U ix e
uArr
    , do
        r :~: S
Refl <- forall {k} (a :: k) (b :: k).
(Typeable a, Typeable b) =>
Maybe (a :~: b)
eqT :: Maybe (r :~: S)
        Array S ix e
sArr <- forall r ix e r'.
(Typeable r, Typeable r') =>
Array r' ix e -> Maybe (Array r ix e)
gcastArr Array r ix e
arr
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall ix e. Index ix => Array S ix e -> Vector e
toStorableVector Array S ix e
sArr
    , do
        r :~: P
Refl <- forall {k} (a :: k) (b :: k).
(Typeable a, Typeable b) =>
Maybe (a :~: b)
eqT :: Maybe (r :~: P)
        Array P ix e
pArr <- forall r ix e r'.
(Typeable r, Typeable r') =>
Array r' ix e -> Maybe (Array r ix e)
gcastArr Array r ix e
arr
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Int -> Int -> ByteArray -> Vector a
VP.Vector (forall ix e. Array P ix e -> Int
pOffset Array P ix e
pArr) (forall ix. Index ix => Sz ix -> Int
totalElem (forall r ix e. Size r => Array r ix e -> Sz ix
size Array r ix e
arr)) forall a b. (a -> b) -> a -> b
$ forall ix e. Array P ix e -> ByteArray
pData Array P ix e
pArr
    , do
        r :~: B
Refl <- forall {k} (a :: k) (b :: k).
(Typeable a, Typeable b) =>
Maybe (a :~: b)
eqT :: Maybe (r :~: B)
        Array B ix e
bArr <- forall r ix e r'.
(Typeable r, Typeable r') =>
Array r' ix e -> Maybe (Array r ix e)
gcastArr Array r ix e
arr
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall ix a. Index ix => Array BL ix a -> Vector a
toBoxedVector forall a b. (a -> b) -> a -> b
$ forall ix e. Array B ix e -> Array BL ix e
toLazyArray Array B ix e
bArr
    , do
        r :~: BN
Refl <- forall {k} (a :: k) (b :: k).
(Typeable a, Typeable b) =>
Maybe (a :~: b)
eqT :: Maybe (r :~: BN)
        Array BN ix e
bArr <- forall r ix e r'.
(Typeable r, Typeable r') =>
Array r' ix e -> Maybe (Array r ix e)
gcastArr Array r ix e
arr
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall ix a. Index ix => Array BL ix a -> Vector a
toBoxedVector forall a b. (a -> b) -> a -> b
$ forall ix e. Array B ix e -> Array BL ix e
toLazyArray forall a b. (a -> b) -> a -> b
$ forall ix e. Array BN ix e -> Array B ix e
unwrapNormalForm Array BN ix e
bArr
    , do
        r :~: BL
Refl <- forall {k} (a :: k) (b :: k).
(Typeable a, Typeable b) =>
Maybe (a :~: b)
eqT :: Maybe (r :~: BL)
        Array BL ix e
bArr <- forall r ix e r'.
(Typeable r, Typeable r') =>
Array r' ix e -> Maybe (Array r ix e)
gcastArr Array r ix e
arr
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall ix a. Index ix => Array BL ix a -> Vector a
toBoxedVector Array BL ix e
bArr
    ]
{-# NOINLINE castToVector #-}

-- | Convert an array into a vector. Will perform a cast if resulting vector is
-- of compatible representation, otherwise memory copy will occur.
--
-- ==== __Examples__
--
-- In this example a `S`torable Array is created and then casted into a Storable
-- `VS.Vector` in costant time:
--
-- >>> import Data.Massiv.Array as A
-- >>> import Data.Massiv.Array.Manifest.Vector (toVector)
-- >>> import qualified Data.Vector.Storable as VS
-- >>> toVector (makeArrayR S Par (Sz2 5 6) (\(i :. j) -> i + j)) :: VS.Vector Int
-- [0,1,2,3,4,5,1,2,3,4,5,6,2,3,4,5,6,7,3,4,5,6,7,8,4,5,6,7,8,9]
--
-- While in this example `S`torable Array will first be converted into `U`nboxed
-- representation in `Par`allel and only after that will be coverted into Unboxed
-- `VU.Vector` in constant time.
--
-- >>> import qualified Data.Vector.Unboxed as VU
-- >>> toVector (makeArrayR S Par (Sz2 5 6) (\(i :. j) -> i + j)) :: VU.Vector Int
-- [0,1,2,3,4,5,1,2,3,4,5,6,2,3,4,5,6,7,3,4,5,6,7,8,4,5,6,7,8,9]
toVector
  :: forall r ix e v
   . ( Manifest r e
     , Load r ix e
     , Manifest (ARepr v) e
     , VG.Vector v e
     , VRepr (ARepr v) ~ v
     )
  => Array r ix e
  -> v e
toVector :: forall r ix e (v :: * -> *).
(Manifest r e, Load r ix e, Manifest (ARepr v) e, Vector v e,
 VRepr (ARepr v) ~ v) =>
Array r ix e -> v e
toVector Array r ix e
arr =
  forall a. a -> Maybe a -> a
fromMaybe
    (forall (v :: * -> *) a. Vector v a => Int -> (Int -> a) -> v a
VG.generate (forall ix. Index ix => Sz ix -> Int
totalElem (forall r ix e. Size r => Array r ix e -> Sz ix
size Array r ix e
arr)) (forall r e ix. (Source r e, Index ix) => Array r ix e -> Int -> e
unsafeLinearIndex Array r ix e
arr))
    (forall (v :: * -> *) r ix e.
(Manifest r e, Index ix, VRepr r ~ v) =>
Array r ix e -> Maybe (v e)
castToVector (forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
convert Array r ix e
arr :: Array (ARepr v) ix e))
{-# NOINLINE toVector #-}