diff options
author | Tom Smeding <tom@tomsmeding.com> | 2024-06-09 20:47:09 +0200 |
---|---|---|
committer | Tom Smeding <tom@tomsmeding.com> | 2024-06-09 20:47:30 +0200 |
commit | 5763bf70dc67c5437207ff8e9dd08585d2ea5384 (patch) | |
tree | 8b68dae165940368925a3cbe816a61a65eb23b68 /src/Data/Array/Nested/Internal/Mixed.hs | |
parent | cb98a56767d50fe92790ae4f48a3efbb28aab90a (diff) |
Concatenation of arrays for M and R
What should the type of sconcat be?
Diffstat (limited to 'src/Data/Array/Nested/Internal/Mixed.hs')
-rw-r--r-- | src/Data/Array/Nested/Internal/Mixed.hs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/Data/Array/Nested/Internal/Mixed.hs b/src/Data/Array/Nested/Internal/Mixed.hs index dcd86d1..6d601b8 100644 --- a/src/Data/Array/Nested/Internal/Mixed.hs +++ b/src/Data/Array/Nested/Internal/Mixed.hs @@ -18,15 +18,19 @@ {-# LANGUAGE ViewPatterns #-} module Data.Array.Nested.Internal.Mixed where +import Prelude hiding (mconcat) + import Control.DeepSeq (NFData) import Control.Monad (forM_, when) import Control.Monad.ST import Data.Array.RankedS qualified as S +import Data.Bifunctor (bimap) import Data.Coerce import Data.Foldable (toList) import Data.Int import Data.Kind (Type) import Data.List.NonEmpty (NonEmpty(..)) +import Data.List.NonEmpty qualified as NE import Data.Proxy import Data.Type.Equality import Data.Vector.Storable qualified as VS @@ -280,6 +284,10 @@ class Elt a where mtranspose :: forall is sh. (IsPermutation is, Rank is <= Rank sh) => Perm is -> Mixed sh a -> Mixed (PermutePrefix is sh) a + -- | All arrays in the input must have equal shapes, including subarrays + -- inside their elements. + mconcat :: NonEmpty (Mixed (Nothing : sh) a) -> Mixed (Nothing : sh) a + -- ====== PRIVATE METHODS ====== -- -- | Tree giving the shape of every array component. @@ -366,6 +374,11 @@ instance Storable a => Elt (Primitive a) where M_Primitive (shxPermutePrefix perm sh) (X.transpose (ssxFromShape sh) perm arr) + mconcat :: forall sh. NonEmpty (Mixed (Nothing : sh) (Primitive a)) -> Mixed (Nothing : sh) (Primitive a) + mconcat l@(M_Primitive (_ :$% sh) _ :| _) = + let result = X.concat (ssxFromShape sh) (fmap (\(M_Primitive _ arr) -> arr) l) + in M_Primitive (X.shape (SUnknown () :!% ssxFromShape sh) result) result + type ShapeTree (Primitive a) = () mshapeTree _ = () mshapeTreeEq _ () () = True @@ -424,6 +437,11 @@ instance (Elt a, Elt b) => Elt (a, b) where M_Tup2 (mcast ssh1 sh2 psh' a) (mcast ssh1 sh2 psh' b) mtranspose perm (M_Tup2 a b) = M_Tup2 (mtranspose perm a) (mtranspose perm b) + mconcat = + let unzipT2l [] = ([], []) + unzipT2l (M_Tup2 a b : l) = let (l1, l2) = unzipT2l l in (a : l1, b : l2) + unzipT2 (M_Tup2 a b :| l) = let (l1, l2) = unzipT2l l in (a :| l1, b :| l2) + in uncurry M_Tup2 . bimap mconcat mconcat . unzipT2 type ShapeTree (a, b) = (ShapeTree a, ShapeTree b) mshapeTree (x, y) = (mshapeTree x, mshapeTree y) @@ -526,6 +544,11 @@ instance Elt a => Elt (Mixed sh' a) where = M_Nest (shxPermutePrefix perm sh) (mtranspose perm arr) + mconcat :: NonEmpty (Mixed (Nothing : sh) (Mixed sh' a)) -> Mixed (Nothing : sh) (Mixed sh' a) + mconcat l@(M_Nest sh1 _ :| _) = + let result = mconcat (fmap (\(M_Nest _ arr) -> arr) l) + in M_Nest (fst (shxSplitApp (Proxy @sh') (ssxFromShape sh1) (mshape result))) result + type ShapeTree (Mixed sh' a) = (IShX sh', ShapeTree a) mshapeTree :: Mixed sh' a -> ShapeTree (Mixed sh' a) |