aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Data/Array/Nested/Internal/Mixed.hs20
-rw-r--r--src/Data/Array/Nested/Internal/Ranked.hs14
-rw-r--r--src/Data/Array/Nested/Internal/Shaped.hs14
3 files changed, 39 insertions, 9 deletions
diff --git a/src/Data/Array/Nested/Internal/Mixed.hs b/src/Data/Array/Nested/Internal/Mixed.hs
index 5730354..53b4f05 100644
--- a/src/Data/Array/Nested/Internal/Mixed.hs
+++ b/src/Data/Array/Nested/Internal/Mixed.hs
@@ -197,9 +197,17 @@ newtype ShowViaToListLinear sh a = ShowViaToListLinear (Mixed sh a)
instance (Show a, Elt a) => Show (ShowViaToListLinear sh a) where
showsPrec d (ShowViaToListLinear arr) = showParen (d > 10) $
- -- TODO: to avoid ambiguity, this should type-apply the shape to mfromListLinear
- showString "mfromListLinear " . shows (shxToList (mshape arr)) . showString " "
- . shows (mtoListLinear arr)
+ let defaultResult =
+ -- TODO: to avoid ambiguity, this should type-apply the shape to mfromListLinear
+ showString "mfromListLinear " . shows (shxToList (mshape arr)) . showString " "
+ . shows (mtoListLinear arr)
+ in if stridesAreZero (shxLength $ mshape arr) (mstrideTree arr)
+ then case mtoListLinear arr of
+ [] -> defaultResult
+ [_] -> defaultResult
+ hd : _ -> showString "mreplicate " . shows (shxToList (mshape arr)) . showString " "
+ . showsPrec 11 hd
+ else defaultResult
newtype ShowViaPrimitive sh a = ShowViaPrimitive (Mixed sh (Primitive a))
@@ -279,6 +287,12 @@ data StrideTree =
StrideLeaf [Int]
| StrideNode StrideTree StrideTree
+stridesAreZero :: Int -> StrideTree -> Bool
+stridesAreZero prefixLen (StrideLeaf ss) =
+ all (== 0) (take prefixLen ss)
+stridesAreZero prefixLen (StrideNode ss1 ss2) =
+ stridesAreZero prefixLen ss1 && stridesAreZero prefixLen ss2
+
-- | Allowable element types in a mixed array, and by extension in a 'Ranked' or
-- 'Shaped' array. Note the polymorphic instance for 'Elt' of @'Primitive'
-- a@; see the documentation for 'Primitive' for more details.
diff --git a/src/Data/Array/Nested/Internal/Ranked.hs b/src/Data/Array/Nested/Internal/Ranked.hs
index c501015..cb8aae0 100644
--- a/src/Data/Array/Nested/Internal/Ranked.hs
+++ b/src/Data/Array/Nested/Internal/Ranked.hs
@@ -65,9 +65,17 @@ deriving instance Eq (Mixed (Replicate n Nothing) a) => Eq (Ranked n a)
deriving instance Ord (Mixed (Replicate n Nothing) a) => Ord (Ranked n a)
instance (Show a, Elt a) => Show (Ranked n a) where
- showsPrec d arr = showParen (d > 10) $
- showString "rfromListLinear " . shows (toList (rshape arr)) . showString " "
- . shows (rtoListLinear arr)
+ showsPrec d arr@(Ranked marr) = showParen (d > 10) $
+ let defaultResult =
+ showString "rfromListLinear " . shows (toList (rshape arr)) . showString " "
+ . shows (rtoListLinear arr)
+ in if stridesAreZero (shxLength $ mshape marr) (mstrideTree marr)
+ then case rtoListLinear arr of
+ [] -> defaultResult
+ [_] -> defaultResult
+ hd : _ -> showString "rreplicate " . shows (toList (rshape arr)) . showString " "
+ . showsPrec 11 hd
+ else defaultResult
instance Elt a => NFData (Ranked n a) where
rnf (Ranked arr) = rnf arr
diff --git a/src/Data/Array/Nested/Internal/Shaped.hs b/src/Data/Array/Nested/Internal/Shaped.hs
index eebf66a..d5c9612 100644
--- a/src/Data/Array/Nested/Internal/Shaped.hs
+++ b/src/Data/Array/Nested/Internal/Shaped.hs
@@ -66,9 +66,17 @@ deriving instance Eq (Mixed (MapJust sh) a) => Eq (Shaped sh a)
deriving instance Ord (Mixed (MapJust sh) a) => Ord (Shaped sh a)
instance (Show a, Elt a) => Show (Shaped sh a) where
- showsPrec d arr = showParen (d > 10) $
- showString "sfromListLinear " . shows (shsToList (sshape arr)) . showString " "
- . shows (stoListLinear arr)
+ showsPrec d arr@(Shaped marr) = showParen (d > 10) $
+ let defaultResult =
+ showString "sfromListLinear " . shows (shsToList (sshape arr)) . showString " "
+ . shows (stoListLinear arr)
+ in if stridesAreZero (shxLength $ mshape marr) (mstrideTree marr)
+ then case stoListLinear arr of
+ [] -> defaultResult
+ [_] -> defaultResult
+ hd : _ -> showString "sreplicate " . shows (shsToList (sshape arr)) . showString " "
+ . showsPrec 11 hd
+ else defaultResult
instance Elt a => NFData (Shaped sh a) where
rnf (Shaped arr) = rnf arr