help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Removing disadvantages on struct array and cell struct array?


From: John W. Eaton
Subject: Re: Removing disadvantages on struct array and cell struct array?
Date: Sun, 9 Jan 2011 16:27:48 -0500

On  9-Jan-2011, veryhappy wrote:

| I knew about struct_levels_to_print but for some reason that i can't
| understand struct arrays only show the name of the fields (they show the
| full information if you specify a concrete element of the array) :S
| Thanks for your reply i'll try to make it clearer giving you an example.
| If execute a script with the following content:
|   % Struct array example
|   clear circuit
|   struct_levels_to_print(10);
|   component(1)=struct("type",'r',"value",1e4);
|   component(2)=struct("type",'c',"value",1e-7);
|   component(3)=struct("type",'r',"value",2.2e3);
|   component
|   circuit(1).component=component;
|   circuit(2).component=component;
|   circuit
| I get the next results:
| component =
| {
|   1x3 struct array containing the fields:
| 
|     type
|     value
| }
| 
| circuit =
| {
|   1x2 struct array containing the fields:
| 
|     component
| }
| 
| As you see i don't get the contents of the struct but using struct arrays i
| can do things like
| [circuit(1).component.print_value]={'10K','100nF','2K2'}{:}. I would like to
| retain that functionality and get something like:

I think old versions of Octave used to print the struct array
contents, but then people didn't like that because it tended to spew a
lot of info.  But I would consider making it possible again, but off
by default.  For example, with the attached changeset, the development
version of Octave will do the following for your struct array:

  octave:10> print_struct_array_contents (true);
  octave:11> component
  component =
  {
    type =

    {
      [1,1] = r
      [1,2] = c
      [1,3] = r
    }

    value =

    {
      [1,1] =  10000
      [1,2] =  1.0000e-07
      [1,3] =  2200
    }

  }

But maybe you want the order to be rearranged as follows

  component =
  {
    (1,1) =
    {
      type = r
      value = 10000
    }
    (1,2) =
    {
      type = c
      value = 1.000e-07
    }
    (1,3) =
    {
      type = r
      value = 2200
    }
  }

or similar?  I'm not sure how to make it clear that the numbers in
parens correspond the elements of the structure array.  Because of
that, and because I'm not sure which output format is best, I hesitate
to check in this change.

jwe

# HG changeset patch
# User John W. Eaton <address@hidden>
# Date 1294608360 18000
# Node ID ee1b6a09382ec8197cdc82aaa7bf15e5a28efd4a
# Parent  7aab48b6e903f6b4e842f0acfb556ae63955584e
struct printing changes

diff --git a/src/ChangeLog b/src/ChangeLog
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,15 @@
+2011-01-09  John W. Eaton  <address@hidden>
+
+       * ov-struct.cc (Fstruct_levels_to_print): Move here from pr-output.cc
+       (Vstruct_levels_to_print): Move here from pr-output.cc.  Now static.
+       (Vprint_struct_array_contents): New static variable.
+       (Fprint_struct_array_contents): New function.
+       (octave_struct::print_raw): Use Vprint_struct_array_contents.
+       (octave_scalar_struct::print_raw): Simplify.
+       * pr-output.h (Vstruct_levels_to_print): Delete decl.
+       * ov-class.cc (octave_class::print_raw): Don't unwind_protect
+       Vstruct_levels_to_print.
+
 2011-01-09  John W. Eaton  <address@hidden>
 
        * token.h, token.cc (token::plot_tok_typ): Delete unused enum.
diff --git a/src/ov-class.cc b/src/ov-class.cc
--- a/src/ov-class.cc
+++ b/src/ov-class.cc
@@ -956,8 +956,6 @@
 {
   unwind_protect frame;
 
-  frame.protect_var (Vstruct_levels_to_print);
-
   indent (os);
   os << "  <class " << class_name () << ">";
   newline (os);
diff --git a/src/ov-struct.cc b/src/ov-struct.cc
--- a/src/ov-struct.cc
+++ b/src/ov-struct.cc
@@ -51,6 +51,13 @@
 
 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(octave_struct, "struct", "struct");
 
+// How many levels of structure elements should we print?
+static int Vstruct_levels_to_print = 2;
+
+// TRUE means print struct array contents, up to the number of levels
+// specified by struct_levels_to_print.
+static bool Vprint_struct_array_contents = false;
+
 octave_base_value *
 octave_struct::try_narrowing_conversion (void)
 {
@@ -601,7 +608,10 @@
 
   if (Vstruct_levels_to_print >= 0)
     {
-      bool print_keys_only = Vstruct_levels_to_print-- == 0;
+      bool max_depth_reached = Vstruct_levels_to_print-- == 0;
+
+      bool print_fieldnames_only
+        = (max_depth_reached || ! Vprint_struct_array_contents);
 
       indent (os);
       os << "{";
@@ -611,7 +621,7 @@
 
       octave_idx_type n = map.numel ();
 
-      if (n != 1 || print_keys_only)
+      if (print_fieldnames_only)
         {
           indent (os);
           dim_vector dv = dims ();
@@ -630,24 +640,20 @@
 
           Cell val = map.contents (key);
 
-          octave_value tmp = (n == 1) ? val(0) : octave_value (val, true);
-
-          if (n != 1 || print_keys_only)
+          if (print_fieldnames_only)
             {
               indent (os);
               os << key;
-              if (n == 1)
-                {
-                  dim_vector dv = tmp.dims ();
-                  os << ": " << dv.str () << " " << tmp.type_name ();
-                }
               newline (os);
             }
           else
-            tmp.print_with_name (os, key);
+            {
+              octave_value tmp (val);
+              tmp.print_with_name (os, key);
+            }
         }
 
-      if (n != 1 || print_keys_only)
+      if (print_fieldnames_only)
         decrement_indent_level ();
 
       decrement_indent_level ();
@@ -1323,7 +1329,9 @@
 
   if (Vstruct_levels_to_print >= 0)
     {
-      bool print_keys_only = Vstruct_levels_to_print-- == 0;
+      bool max_depth_reached = Vstruct_levels_to_print-- == 0;
+
+      bool print_fieldnames_only = max_depth_reached;
 
       indent (os);
       os << "{";
@@ -1331,9 +1339,7 @@
 
       increment_indent_level ();
 
-      octave_idx_type n = 1;
-
-      if (n != 1 || print_keys_only)
+      if (print_fieldnames_only)
         {
           indent (os);
           dim_vector dv = dims ();
@@ -1352,24 +1358,21 @@
 
           Cell val = map.contents (key);
 
-          octave_value tmp = (n == 1) ? val(0) : octave_value (val, true);
-
-          if (n != 1 || print_keys_only)
+          octave_value tmp = val(0);
+
+          if (print_fieldnames_only)
             {
               indent (os);
               os << key;
-              if (n == 1)
-                {
-                  dim_vector dv = tmp.dims ();
-                  os << ": " << dv.str () << " " << tmp.type_name ();
-                }
+              dim_vector dv = tmp.dims ();
+              os << ": " << dv.str () << " " << tmp.type_name ();
               newline (os);
             }
           else
             tmp.print_with_name (os, key);
         }
 
-      if (n != 1 || print_keys_only)
+      if (print_fieldnames_only)
         decrement_indent_level ();
 
       decrement_indent_level ();
@@ -2194,3 +2197,28 @@
 %!  assert (size (y), [1, 6]);
 */
 
+DEFUN (struct_levels_to_print, args, nargout,
+  "-*- texinfo -*-\n\
address@hidden  {Built-in Function} address@hidden =} struct_levels_to_print 
()\n\
address@hidden {Built-in Function} address@hidden =} struct_levels_to_print 
(@var{new_val})\n\
+Query or set the internal variable that specifies the number of\n\
+structure levels to display.\n\
address@hidden deftypefn")
+{
+  return SET_INTERNAL_VARIABLE_WITH_LIMITS (struct_levels_to_print,
+                                            -1, INT_MAX);
+}
+
+DEFUN (print_struct_array_contents, args, nargout,
+  "-*- texinfo -*-\n\
address@hidden  {Built-in Function} address@hidden =} 
print_struct_array_contents ()\n\
address@hidden {Built-in Function} address@hidden =} 
print_struct_array_contents (@var{new_val})\n\
+Query or set the internal variable that specifies whether to print struct\n\
+array contents.  If true, values of struct array elements are printed.\n\
+This variable does not affect scalar structures.  Their elements\n\
+are always printed.  In both cases, however, printing will be limited to\n\
+the number of levels specified by @var{struct_levels_to_print}.\n\
address@hidden deftypefn")
+{
+  return SET_INTERNAL_VARIABLE (print_struct_array_contents);
+}
diff --git a/src/pr-output.cc b/src/pr-output.cc
--- a/src/pr-output.cc
+++ b/src/pr-output.cc
@@ -79,9 +79,6 @@
 // smaller slices that fit on the screen.
 static bool Vsplit_long_rows = true;
 
-// How many levels of structure elements should we print?
-int Vstruct_levels_to_print = 2;
-
 // TRUE means don't do any fancy formatting.
 static bool free_format = false;
 
@@ -4028,15 +4025,3 @@
 {
   return SET_INTERNAL_VARIABLE_WITH_LIMITS (output_precision, -1, INT_MAX);
 }
-
-DEFUN (struct_levels_to_print, args, nargout,
-  "-*- texinfo -*-\n\
address@hidden  {Built-in Function} address@hidden =} struct_levels_to_print 
()\n\
address@hidden {Built-in Function} address@hidden =} struct_levels_to_print 
(@var{new_val})\n\
-Query or set the internal variable that specifies the number of\n\
-structure levels to display.\n\
address@hidden deftypefn")
-{
-  return SET_INTERNAL_VARIABLE_WITH_LIMITS (struct_levels_to_print,
-                                            -1, INT_MAX);
-}
diff --git a/src/pr-output.h b/src/pr-output.h
--- a/src/pr-output.h
+++ b/src/pr-output.h
@@ -257,7 +257,4 @@
 // like this: x = [](2x0).
 extern bool Vprint_empty_dimensions;
 
-// How many levels of structure elements should we print?
-extern OCTINTERP_API int Vstruct_levels_to_print;
-
 #endif

reply via email to

[Prev in Thread] Current Thread [Next in Thread]