changeset 24646:d36e1f768bfa

allow diagonal and permutation matrices to be display in variable editor * ov-perm.h, ov-perm.cc (octave_perm_matrix::short_disp, octave_perm_matrix::edit_display): New functions. * PermMatrix.h (PermMatrix::isempty, PermMatrix::ndims): New functions. * ov-base-diag.h, ov-base-diag.cc (octave_base_diag::short_disp, octave_base_diag::edit_display): New functions. * DiagArray2.h (DiagArray2::isempty, DiagArray2::ndims): New functions.
author John W. Eaton <jwe@octave.org>
date Sat, 27 Jan 2018 13:51:31 -0500
parents f61502510d08
children 41a87a2a8d38
files libinterp/octave-value/ov-base-diag.cc libinterp/octave-value/ov-base-diag.h libinterp/octave-value/ov-perm.cc libinterp/octave-value/ov-perm.h liboctave/array/DiagArray2.h liboctave/array/PermMatrix.h
diffstat 6 files changed, 147 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/octave-value/ov-base-diag.cc	Sat Jan 27 09:06:50 2018 -0500
+++ b/libinterp/octave-value/ov-base-diag.cc	Sat Jan 27 13:51:31 2018 -0500
@@ -558,6 +558,71 @@
   matrix.print_info (os, prefix);
 }
 
+// FIXME: this function is duplicated in octave_base_matrix<T>.  Could
+// it somehow be shared instead?
+
+template <typename DMT, typename MT>
+void
+octave_base_diag<DMT, MT>::short_disp (std::ostream& os) const
+{
+  if (matrix.isempty ())
+    os << "[]";
+  else if (matrix.ndims () == 2)
+    {
+      // FIXME: should this be configurable?
+      octave_idx_type max_elts = 10;
+      octave_idx_type elts = 0;
+
+      octave_idx_type nel = matrix.numel ();
+
+      octave_idx_type nr = matrix.rows ();
+      octave_idx_type nc = matrix.columns ();
+
+      os << '[';
+
+      for (octave_idx_type i = 0; i < nr; i++)
+        {
+          for (octave_idx_type j = 0; j < nc; j++)
+            {
+              std::ostringstream buf;
+              octave_print_internal (buf, matrix(i,j));
+              std::string tmp = buf.str ();
+              size_t pos = tmp.find_first_not_of (' ');
+              if (pos != std::string::npos)
+                os << tmp.substr (pos);
+              else if (! tmp.empty ())
+                os << tmp[0];
+
+              if (++elts >= max_elts)
+                goto done;
+
+              if (j < nc - 1)
+                os << ", ";
+            }
+
+          if (i < nr - 1 && elts < max_elts)
+            os << "; ";
+        }
+
+    done:
+
+      if (nel <= max_elts)
+        os << ']';
+    }
+  else
+    os << "...";
+}
+
+template <typename DMT, typename MT>
+std::string
+octave_base_diag<DMT, MT>::edit_display (octave_idx_type i,
+                                         octave_idx_type j) const
+{
+  std::ostringstream buf;
+  octave_print_internal (buf, matrix(i,j));
+  return buf.str ();
+}
+
 template <typename DMT, typename MT>
 octave_value
 octave_base_diag<DMT, MT>::fast_elem_extract (octave_idx_type n) const
--- a/libinterp/octave-value/ov-base-diag.h	Sat Jan 27 09:06:50 2018 -0500
+++ b/libinterp/octave-value/ov-base-diag.h	Sat Jan 27 13:51:31 2018 -0500
@@ -214,6 +214,10 @@
 
   void print_info (std::ostream& os, const std::string& prefix) const;
 
+  void short_disp (std::ostream& os) const;
+
+  std::string edit_display (octave_idx_type i, octave_idx_type j) const;
+
   octave_value fast_elem_extract (octave_idx_type n) const;
 
 protected:
--- a/libinterp/octave-value/ov-perm.cc	Sat Jan 27 09:06:50 2018 -0500
+++ b/libinterp/octave-value/ov-perm.cc	Sat Jan 27 13:51:31 2018 -0500
@@ -477,6 +477,61 @@
                                             octave_matrix::static_type_id ());
 }
 
+// FIXME: This is duplicated from octave_base_matrix<T>.  Could
+// octave_perm_matrix be derived from octave_base_matrix<T>?
+
+void
+octave_perm_matrix::short_disp (std::ostream& os) const
+{
+  if (matrix.isempty ())
+    os << "[]";
+  else if (matrix.ndims () == 2)
+    {
+      // FIXME: should this be configurable?
+      octave_idx_type max_elts = 10;
+      octave_idx_type elts = 0;
+
+      octave_idx_type nel = matrix.numel ();
+
+      octave_idx_type nr = matrix.rows ();
+      octave_idx_type nc = matrix.columns ();
+
+      os << '[';
+
+      for (octave_idx_type i = 0; i < nr; i++)
+        {
+          for (octave_idx_type j = 0; j < nc; j++)
+            {
+              std::ostringstream buf;
+              octave_int<octave_idx_type> tval (matrix(i,j));
+              octave_print_internal (buf, tval);
+              std::string tmp = buf.str ();
+              size_t pos = tmp.find_first_not_of (' ');
+              if (pos != std::string::npos)
+                os << tmp.substr (pos);
+              else if (! tmp.empty ())
+                os << tmp[0];
+
+              if (++elts >= max_elts)
+                goto done;
+
+              if (j < nc - 1)
+                os << ", ";
+            }
+
+          if (i < nr - 1 && elts < max_elts)
+            os << "; ";
+        }
+
+    done:
+
+      if (nel <= max_elts)
+        os << ']';
+    }
+  else
+    os << "...";
+}
+
 octave_base_value *
 octave_perm_matrix::try_narrowing_conversion (void)
 {
@@ -488,6 +543,17 @@
   return retval;
 }
 
+std::string
+octave_perm_matrix::edit_display (octave_idx_type i, octave_idx_type j) const
+{
+  // FIXME: maybe we should have octave_print_internal functions for
+  // standard int types, not just octave_int<T> types.
+
+  std::ostringstream buf;
+  octave_print_internal (buf, octave_int<octave_idx_type> (matrix(i,j)));
+  return buf.str ();
+}
+
 octave_value
 octave_perm_matrix::fast_elem_extract (octave_idx_type n) const
 {
--- a/libinterp/octave-value/ov-perm.h	Sat Jan 27 09:06:50 2018 -0500
+++ b/libinterp/octave-value/ov-perm.h	Sat Jan 27 13:51:31 2018 -0500
@@ -230,6 +230,10 @@
 
   void print_info (std::ostream& os, const std::string& prefix) const;
 
+  void short_disp (std::ostream& os) const;
+
+  std::string edit_display (octave_idx_type i, octave_idx_type j) const;
+
   octave_value map (unary_mapper_t umap) const
   { return to_dense ().map (umap); }
 
--- a/liboctave/array/DiagArray2.h	Sat Jan 27 09:06:50 2018 -0500
+++ b/liboctave/array/DiagArray2.h	Sat Jan 27 13:51:31 2018 -0500
@@ -98,6 +98,10 @@
 
   dim_vector dims (void) const { return dim_vector (d1, d2); }
 
+  bool isempty (void) const { return numel () == 0; }
+
+  int ndims (void) const { return 2; }
+
   Array<T> extract_diag (octave_idx_type k = 0) const;
 
   DiagArray2<T> build_diag_matrix () const
--- a/liboctave/array/PermMatrix.h	Sat Jan 27 09:06:50 2018 -0500
+++ b/liboctave/array/PermMatrix.h	Sat Jan 27 13:51:31 2018 -0500
@@ -70,6 +70,10 @@
 
   dim_vector dims (void) const { return dim_vector (dim1 (), dim2 ()); }
 
+  bool isempty (void) const { return numel () == 0; }
+
+  int ndims (void) const { return 2; }
+
   const Array<octave_idx_type>& col_perm_vec (void) const
   { return *this; }