changeset 22210:30eefd9ddb48

prepad, postpad: do nothing wen input is already of the right size.
author Carnë Draug <carandraug@octave.org>
date Fri, 05 Aug 2016 22:04:12 +0100
parents cfa684a0539d
children 6065bd58db2b
files scripts/general/postpad.m scripts/general/prepad.m
diffstat 2 files changed, 26 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/general/postpad.m	Fri Aug 05 20:09:09 2016 +0100
+++ b/scripts/general/postpad.m	Fri Aug 05 22:04:12 2016 +0100
@@ -73,7 +73,15 @@
 
   d = sz(dim);
 
-  if (d >= l)
+  if (d == l)
+    ## This optimization makes sense because the function is used to match
+    ## the length between two vectors not knowing a priori is larger, and
+    ## allow for:
+    ##    ml = max (numel (v1), numel (v2));
+    ##    v1 = postpad (v1, ml);
+    ##    v2 = postpad (v2, ml);
+    y = x;
+  elseif (d >= l)
     idx = repmat ({':'}, nd, 1);
     idx{dim} = 1:l;
     y = x(idx{:});
@@ -93,6 +101,10 @@
 %!assert (postpad ([1;2], 2, 2, 3), reshape ([1;2;2;2], 2, 1, 2))
 %!assert (postpad ([1,2], 2, 2, 3), reshape ([1,2,2,2], 1, 2, 2))
 
+%!assert (postpad ([1 2], 2), [1 2])
+%!assert (postpad ([1; 2], 2), [1; 2])
+%!assert (postpad ([1; 2], 2, 3, 2), [1 3; 2 3])
+
 %! ## Test with string concatenation (bug #44162)
 %!assert (postpad ("Octave", 16, "x"), "Octavexxxxxxxxxx")
 %!assert (postpad ("Octave", 4), "Octa")
--- a/scripts/general/prepad.m	Fri Aug 05 20:09:09 2016 +0100
+++ b/scripts/general/prepad.m	Fri Aug 05 22:04:12 2016 +0100
@@ -73,7 +73,15 @@
 
   d = sz(dim);
 
-  if (d >= l)
+  if (d == l)
+    ## This optimization makes sense because the function is used to match
+    ## the length between two vectors not knowing a priori is larger, and
+    ## allow for:
+    ##    ml = max (numel (v1), numel (v2));
+    ##    v1 = prepad (v1, ml);
+    ##    v2 = prepad (v2, ml);
+    y = x;
+  elseif (d >= l)
     idx = repmat ({':'}, nd, 1);
     idx{dim} = d-l+1:d;
     y = x(idx{:});
@@ -91,6 +99,10 @@
 %!assert (prepad ([1,2], 4, 2), [2,2,1,2])
 %!assert (prepad ([1;2], 4, 2), [2;2;1;2])
 
+%!assert (prepad ([1 2], 2), [1 2])
+%!assert (prepad ([1; 2], 2), [1; 2])
+%!assert (prepad ([1; 2], 2, 3, 2), [3 1; 3 2])
+
 %!assert (prepad ([1,2], 2, 2, 1), [2,2;1,2])
 
 %!assert (prepad ([1,2], 2, 2, 3), reshape ([2,2,1,2], 1, 2, 2))