changeset 21475:4f3e63d75f33

use std::deque instead of Array to hold scanf and printf format elements * oct-stream.h, oct-stream.cc (class scanf_format_list, class_printf_format_list): Use std::deque instead of Array to hold format elements. * libinterp/template-inst/Array-os.cc: Don't instantiate Array objects of pointers to scanf_format_elt or printf_format_elt objects.
author John W. Eaton <jwe@octave.org>
date Fri, 18 Mar 2016 12:11:15 -0400
parents deeb9b4b2846
children 19e952b82819
files libinterp/corefcn/oct-stream.cc libinterp/corefcn/oct-stream.h libinterp/template-inst/Array-os.cc
diffstat 3 files changed, 72 insertions(+), 99 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/oct-stream.cc	Fri Mar 18 08:35:58 2016 -0700
+++ b/libinterp/corefcn/oct-stream.cc	Fri Mar 18 12:11:15 2016 -0400
@@ -170,10 +170,8 @@
 }
 
 scanf_format_list::scanf_format_list (const std::string& s)
-  : nconv (0), curr_idx (0), list (dim_vector (16, 1)), buf (0)
+  : nconv (0), curr_idx (0), fmt_elts (), buf (0)
 {
-  octave_idx_type num_elts = 0;
-
   size_t n = s.length ();
 
   size_t i = 0;
@@ -196,8 +194,7 @@
         {
           // Process percent-escape conversion type.
 
-          process_conversion (s, i, n, width, discard, type, modifier,
-                              num_elts);
+          process_conversion (s, i, n, width, discard, type, modifier);
 
           have_more = (buf != 0);
         }
@@ -213,7 +210,7 @@
           while (++i < n && isspace (s[i]))
             ; // skip whitespace
 
-          add_elt_to_list (width, discard, type, modifier, num_elts);
+          add_elt_to_list (width, discard, type, modifier);
 
           have_more = false;
         }
@@ -228,7 +225,7 @@
           while (i < n && ! isspace (s[i]) && s[i] != '%')
             *buf << s[i++];
 
-          add_elt_to_list (width, discard, type, modifier, num_elts);
+          add_elt_to_list (width, discard, type, modifier);
 
           have_more = false;
         }
@@ -241,27 +238,25 @@
     }
 
   if (have_more)
-    add_elt_to_list (width, discard, type, modifier, num_elts);
-
-  list.resize (dim_vector (num_elts, 1));
+    add_elt_to_list (width, discard, type, modifier);
 
   delete buf;
 }
 
 scanf_format_list::~scanf_format_list (void)
 {
-  octave_idx_type n = list.numel ();
-
-  for (octave_idx_type i = 0; i < n; i++)
+  size_t n = fmt_elts.size ();
+
+  for (size_t i = 0; i < n; i++)
     {
-      scanf_format_elt *elt = list(i);
+      scanf_format_elt *elt = fmt_elts[i];
       delete elt;
     }
 }
 
 void
 scanf_format_list::add_elt_to_list (int width, bool discard, char type,
-                                    char modifier, octave_idx_type& num_elts,
+                                    char modifier,
                                     const std::string& char_class)
 {
   if (buf)
@@ -274,10 +269,7 @@
             = new scanf_format_elt (text.c_str (), width, discard, type,
                                     modifier, char_class);
 
-          if (num_elts == list.numel ())
-            list.resize (dim_vector (2 * num_elts, 1));
-
-          list(num_elts++) = elt;
+          fmt_elts.push_back (elt);
         }
 
       delete buf;
@@ -324,8 +316,7 @@
 void
 scanf_format_list::process_conversion (const std::string& s, size_t& i,
                                        size_t n, int& width, bool& discard,
-                                       char& type, char& modifier,
-                                       octave_idx_type& num_elts)
+                                       char& type, char& modifier)
 {
   width = 0;
   discard = false;
@@ -406,8 +397,8 @@
 
         fini:
           {
-            if (finish_conversion (s, i, n, width, discard, type,
-                                   modifier, num_elts) == 0)
+            if (finish_conversion (s, i, n, width, discard,
+                                   type, modifier) == 0)
               return;
           }
           break;
@@ -427,8 +418,7 @@
 int
 scanf_format_list::finish_conversion (const std::string& s, size_t& i,
                                       size_t n, int& width, bool discard,
-                                      char& type, char modifier,
-                                      octave_idx_type& num_elts)
+                                      char& type, char modifier)
 {
   int retval = 0;
 
@@ -495,7 +485,7 @@
         char_class = expand_char_class (s.substr (beg_idx,
                                         end_idx - beg_idx + 1));
 
-      add_elt_to_list (width, discard, type, modifier, num_elts, char_class);
+      add_elt_to_list (width, discard, type, modifier, char_class);
     }
 
   return retval;
@@ -504,11 +494,11 @@
 void
 scanf_format_list::printme (void) const
 {
-  octave_idx_type n = list.numel ();
-
-  for (octave_idx_type i = 0; i < n; i++)
+  size_t n = fmt_elts.size ();
+
+  for (size_t i = 0; i < n; i++)
     {
-      scanf_format_elt *elt = list(i);
+      scanf_format_elt *elt = fmt_elts[i];
 
       std::cerr
         << "width:      " << elt->width << "\n"
@@ -532,13 +522,13 @@
 bool
 scanf_format_list::all_character_conversions (void)
 {
-  octave_idx_type n = list.numel ();
+  size_t n = fmt_elts.size ();
 
   if (n > 0)
     {
-      for (octave_idx_type i = 0; i < n; i++)
+      for (size_t i = 0; i < n; i++)
         {
-          scanf_format_elt *elt = list(i);
+          scanf_format_elt *elt = fmt_elts[i];
 
           switch (elt->type)
             {
@@ -562,13 +552,13 @@
 bool
 scanf_format_list::all_numeric_conversions (void)
 {
-  octave_idx_type n = list.numel ();
+  size_t n = fmt_elts.size ();
 
   if (n > 0)
     {
-      for (octave_idx_type i = 0; i < n; i++)
+      for (size_t i = 0; i < n; i++)
         {
-          scanf_format_elt *elt = list(i);
+          scanf_format_elt *elt = fmt_elts[i];
 
           switch (elt->type)
             {
@@ -591,10 +581,8 @@
 // Ugh again.
 
 printf_format_list::printf_format_list (const std::string& s)
-  : nconv (0), curr_idx (0), list (dim_vector (16, 1)), buf (0)
+  : nconv (0), curr_idx (0), fmt_elts (), buf (0)
 {
-  octave_idx_type num_elts = 0;
-
   size_t n = s.length ();
 
   size_t i = 0;
@@ -614,9 +602,7 @@
       printf_format_elt *elt
         = new printf_format_elt ("", args, fw, prec, flags, type, modifier);
 
-      list(num_elts++) = elt;
-
-      list.resize (dim_vector (num_elts, 1));
+      fmt_elts.push_back (elt);
     }
   else
     {
@@ -637,13 +623,12 @@
                 if (empty_buf)
                   {
                     process_conversion (s, i, n, args, flags, fw, prec,
-                                        type, modifier, num_elts);
+                                        type, modifier);
 
                     have_more = (buf != 0);
                   }
                 else
-                  add_elt_to_list (args, flags, fw, prec, type, modifier,
-                                   num_elts);
+                  add_elt_to_list (args, flags, fw, prec, type, modifier);
               }
               break;
 
@@ -669,9 +654,7 @@
         }
 
       if (have_more)
-        add_elt_to_list (args, flags, fw, prec, type, modifier, num_elts);
-
-      list.resize (dim_vector (num_elts, 1));
+        add_elt_to_list (args, flags, fw, prec, type, modifier);
 
       delete buf;
     }
@@ -679,11 +662,11 @@
 
 printf_format_list::~printf_format_list (void)
 {
-  octave_idx_type n = list.numel ();
-
-  for (octave_idx_type i = 0; i < n; i++)
+  size_t n = fmt_elts.size ();
+
+  for (size_t i = 0; i < n; i++)
     {
-      printf_format_elt *elt = list(i);
+      printf_format_elt *elt = fmt_elts[i];
       delete elt;
     }
 }
@@ -691,7 +674,7 @@
 void
 printf_format_list::add_elt_to_list (int args, const std::string& flags,
                                      int fw, int prec, char type,
-                                     char modifier, octave_idx_type& num_elts)
+                                     char modifier)
 {
   if (buf)
     {
@@ -703,10 +686,7 @@
             = new printf_format_elt (text.c_str (), args, fw, prec, flags,
                                      type, modifier);
 
-          if (num_elts == list.numel ())
-            list.resize (dim_vector (2 * num_elts, 1));
-
-          list(num_elts++) = elt;
+          fmt_elts.push_back (elt);
         }
 
       delete buf;
@@ -716,9 +696,10 @@
 
 void
 printf_format_list::process_conversion (const std::string& s, size_t& i,
-                                        size_t n, int& args, std::string& flags,
-                                        int& fw, int& prec, char& modifier,
-                                        char& type, octave_idx_type& num_elts)
+                                        size_t n, int& args,
+                                        std::string& flags, int& fw,
+                                        int& prec, char& modifier,
+                                        char& type)
 {
   args = 0;
   flags = "";
@@ -823,7 +804,7 @@
     }
 
   if (i < n)
-    finish_conversion (s, i, args, flags, fw, prec, modifier, type, num_elts);
+    finish_conversion (s, i, args, flags, fw, prec, modifier, type);
   else
     nconv = -1;
 }
@@ -832,7 +813,7 @@
 printf_format_list::finish_conversion (const std::string& s, size_t& i,
                                        int args, const std::string& flags,
                                        int fw, int prec, char modifier,
-                                       char& type, octave_idx_type& num_elts)
+                                       char& type)
 {
   switch (s[i])
     {
@@ -873,7 +854,7 @@
       if (type != '%')
         args++;
 
-      add_elt_to_list (args, flags, fw, prec, type, modifier, num_elts);
+      add_elt_to_list (args, flags, fw, prec, type, modifier);
 
       break;
 
@@ -886,11 +867,11 @@
 void
 printf_format_list::printme (void) const
 {
-  int n = list.numel ();
-
-  for (int i = 0; i < n; i++)
+  size_t n = fmt_elts.size ();
+
+  for (size_t i = 0; i < n; i++)
     {
-      printf_format_elt *elt = list(i);
+      printf_format_elt *elt = fmt_elts[i];
 
       std::cerr
         << "args:     " << elt->args << "\n"
--- a/libinterp/corefcn/oct-stream.h	Fri Mar 18 08:35:58 2016 -0700
+++ b/libinterp/corefcn/oct-stream.h	Fri Mar 18 12:11:15 2016 -0400
@@ -30,11 +30,12 @@
 class octave_value;
 class octave_value_list;
 
+#include <deque>
 #include <iosfwd>
+#include <list>
+#include <map>
 #include <sstream>
 #include <string>
-#include <list>
-#include <map>
 
 #include "Array.h"
 #include "data-conv.h"
@@ -119,7 +120,7 @@
   // the list is 3 because of the characters that appear after the
   // last conversion.
 
-  octave_idx_type length (void) { return list.numel (); }
+  size_t length (void) const { return fmt_elts.size (); }
 
   const scanf_format_elt *first (void)
   {
@@ -128,7 +129,9 @@
   }
 
   const scanf_format_elt *current (void) const
-  { return list.numel () > 0 ? list.elem (curr_idx) : 0; }
+  {
+    return length () > 0 ? fmt_elts[curr_idx] : 0;
+  }
 
   const scanf_format_elt *next (bool cycle = true)
   {
@@ -137,7 +140,7 @@
 
     curr_idx++;
 
-    if (curr_idx >= list.numel ())
+    if (curr_idx >= length ())
       {
         if (cycle)
           curr_idx = 0;
@@ -165,26 +168,24 @@
   octave_idx_type nconv;
 
   // Index to current element;
-  octave_idx_type curr_idx;
+  size_t curr_idx;
 
-  // FIXME: maybe LIST should be a std::list object?
   // List of format elements.
-  Array<scanf_format_elt*> list;
+  std::deque<scanf_format_elt*> fmt_elts;
 
   // Temporary buffer.
   std::ostringstream *buf;
 
   void add_elt_to_list (int width, bool discard, char type, char modifier,
-                        octave_idx_type& num_elts,
                         const std::string& char_class = "");
 
   void process_conversion (const std::string& s, size_t& i, size_t n,
                            int& width, bool& discard, char& type,
-                           char& modifier, octave_idx_type& num_elts);
+                           char& modifier);
 
   int finish_conversion (const std::string& s, size_t& i, size_t n,
                          int& width, bool discard, char& type,
-                         char modifier, octave_idx_type& num_elts);
+                         char modifier);
   // No copying!
 
   scanf_format_list (const scanf_format_list&);
@@ -267,13 +268,17 @@
   }
 
   const printf_format_elt *current (void) const
-  { return list.numel () > 0 ? list.elem (curr_idx) : 0; }
+  {
+    return length () > 0 ? fmt_elts[curr_idx] : 0;
+  }
+
+  size_t length (void) const { return fmt_elts.size (); }
 
   const printf_format_elt *next (bool cycle = true)
   {
     curr_idx++;
 
-    if (curr_idx >= list.numel ())
+    if (curr_idx >= length ())
       {
         if (cycle)
           curr_idx = 0;
@@ -284,7 +289,7 @@
     return current ();
   }
 
-  bool last_elt_p (void) { return (curr_idx + 1 == list.numel ()); }
+  bool last_elt_p (void) { return (curr_idx + 1 == length ()); }
 
   void printme (void) const;
 
@@ -299,28 +304,24 @@
   octave_idx_type nconv;
 
   // Index to current element;
-  octave_idx_type curr_idx;
+  size_t curr_idx;
 
-  // FIXME: maybe LIST should be a std::list object?
   // List of format elements.
-  Array<printf_format_elt*> list;
+  std::deque<printf_format_elt*> fmt_elts;
 
   // Temporary buffer.
   std::ostringstream *buf;
 
   void add_elt_to_list (int args, const std::string& flags, int fw,
-                        int prec, char type, char modifier,
-                        octave_idx_type& num_elts);
+                        int prec, char type, char modifier);
 
   void process_conversion (const std::string& s, size_t& i, size_t n,
                            int& args, std::string& flags, int& fw,
-                           int& prec, char& modifier, char& type,
-                           octave_idx_type& num_elts);
+                           int& prec, char& modifier, char& type);
 
   void finish_conversion (const std::string& s, size_t& i, int args,
                           const std::string& flags, int fw, int prec,
-                          char modifier, char& type,
-                          octave_idx_type& num_elts);
+                          char modifier, char& type);
 
   // No copying!
 
--- a/libinterp/template-inst/Array-os.cc	Fri Mar 18 08:35:58 2016 -0700
+++ b/libinterp/template-inst/Array-os.cc	Fri Mar 18 12:11:15 2016 -0400
@@ -34,14 +34,5 @@
 
 #include "oct-stream.h"
 
-typedef scanf_format_elt* scanf_format_elt_ptr;
-typedef printf_format_elt* printf_format_elt_ptr;
-
-NO_INSTANTIATE_ARRAY_SORT (scanf_format_elt_ptr);
-INSTANTIATE_ARRAY (scanf_format_elt_ptr, OCTINTERP_API);
-
-NO_INSTANTIATE_ARRAY_SORT (printf_format_elt_ptr);
-INSTANTIATE_ARRAY (printf_format_elt_ptr, OCTINTERP_API);
-
 NO_INSTANTIATE_ARRAY_SORT (octave_stream);
 INSTANTIATE_ARRAY (octave_stream, OCTINTERP_API);