changeset 24781:e6f380f9b2a4

use template for printing matrices in plus format * pr-output.cc (pr_plus_format_matrix): New template. (octave_print_internal): Where possible, use it to eliminate some duplicate code.
author John W. Eaton <jwe@octave.org>
date Wed, 21 Feb 2018 09:17:05 -0500
parents 0d21e2a1cdfc
children 0eb41237424e
files libinterp/corefcn/pr-output.cc
diffstat 1 files changed, 28 insertions(+), 83 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/pr-output.cc	Wed Feb 21 09:15:37 2018 -0500
+++ b/libinterp/corefcn/pr-output.cc	Wed Feb 21 09:17:05 2018 -0500
@@ -1785,6 +1785,27 @@
     os << ']';
 }
 
+template <typename MT>
+static inline void
+pr_plus_format_matrix (std::ostream& os, const MT& m)
+{
+  octave_idx_type nr = m.rows ();
+  octave_idx_type nc = m.columns ();
+
+  for (octave_idx_type i = 0; i < nr; i++)
+    {
+      for (octave_idx_type j = 0; j < nc; j++)
+        {
+          octave_quit ();
+
+          pr_plus_format (os, m(i,j));
+        }
+
+      if (i < nr - 1)
+        os << "\n";
+    }
+}
+
 void
 octave_print_internal (std::ostream& os, const Matrix& m,
                        bool pr_as_read_syntax, int extra_indent)
@@ -1795,20 +1816,7 @@
   if (nr == 0 || nc == 0)
     print_empty_matrix (os, nr, nc, pr_as_read_syntax);
   else if (plus_format && ! pr_as_read_syntax)
-    {
-      for (octave_idx_type i = 0; i < nr; i++)
-        {
-          for (octave_idx_type j = 0; j < nc; j++)
-            {
-              octave_quit ();
-
-              pr_plus_format (os, m(i,j));
-            }
-
-          if (i < nr - 1)
-            os << "\n";
-        }
-    }
+    pr_plus_format_matrix (os, m);
   else
     {
       int fw = 0;
@@ -1924,20 +1932,7 @@
   if (nr == 0 || nc == 0)
     print_empty_matrix (os, nr, nc, pr_as_read_syntax);
   else if (plus_format && ! pr_as_read_syntax)
-    {
-      for (octave_idx_type i = 0; i < nr; i++)
-        {
-          for (octave_idx_type j = 0; j < nc; j++)
-            {
-              octave_quit ();
-
-              pr_plus_format (os, m(i,j));
-            }
-
-          if (i < nr - 1)
-            os << "\n";
-        }
-    }
+    pr_plus_format_matrix (os, m);
   else
     {
       int fw;
@@ -2195,20 +2190,7 @@
   if (nr == 0 || nc == 0)
     print_empty_matrix (os, nr, nc, pr_as_read_syntax);
   else if (plus_format && ! pr_as_read_syntax)
-    {
-      for (octave_idx_type i = 0; i < nr; i++)
-        {
-          for (octave_idx_type j = 0; j < nc; j++)
-            {
-              octave_quit ();
-
-              pr_plus_format (os, cm(i,j));
-            }
-
-          if (i < nr - 1)
-            os << "\n";
-        }
-    }
+    pr_plus_format_matrix (os, cm);
   else
     {
       int r_fw, i_fw;
@@ -2326,20 +2308,7 @@
   if (nr == 0 || nc == 0)
     print_empty_matrix (os, nr, nc, pr_as_read_syntax);
   else if (plus_format && ! pr_as_read_syntax)
-    {
-      for (octave_idx_type i = 0; i < nr; i++)
-        {
-          for (octave_idx_type j = 0; j < nc; j++)
-            {
-              octave_quit ();
-
-              pr_plus_format (os, cm(i,j));
-            }
-
-          if (i < nr - 1)
-            os << "\n";
-        }
-    }
+    pr_plus_format_matrix (os, cm);
   else
     {
       int r_fw, i_fw;
@@ -2468,20 +2437,7 @@
   if (nr == 0 || nc == 0)
     print_empty_matrix (os, nr, nc, pr_as_read_syntax);
   else if (plus_format && ! pr_as_read_syntax)
-    {
-      for (octave_idx_type i = 0; i < nr; i++)
-        {
-          for (octave_idx_type j = 0; j < nc; j++)
-            {
-              octave_quit ();
-
-              pr_plus_format (os, m(i,j));
-            }
-
-          if (i < nr - 1)
-            os << "\n";
-        }
-    }
+    pr_plus_format_matrix (os, m);
   else
     {
       int fw = 2;
@@ -2661,16 +2617,7 @@
   octave_idx_type num_elem = r.numel ();
 
   if (plus_format && ! pr_as_read_syntax)
-    {
-      for (octave_idx_type i = 0; i < num_elem; i++)
-        {
-          octave_quit ();
-
-          double val = base + i * increment;
-
-          pr_plus_format (os, val);
-        }
-    }
+    pr_plus_format_matrix (os, r);
   else
     {
       int fw = 0;
@@ -3078,9 +3025,7 @@
                                 const octave_int<T>& val, bool)
 {
   if (plus_format)
-    {
-      pr_plus_format (os, val);
-    }
+    pr_plus_format (os, val);
   else
     {
       if (free_format)