changeset 10065:64a06079cae4

improve cellstr cache implementation
author Jaroslav Hajek <highegg@gmail.com>
date Tue, 05 Jan 2010 13:16:16 +0100
parents 17ce2a700a97
children 2cd940306a06
files src/Cell.cc src/Cell.h src/ChangeLog src/ov-cell.cc src/ov-cell.h
diffstat 5 files changed, 43 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/src/Cell.cc	Wed Jan 06 07:49:40 2010 -0500
+++ b/src/Cell.cc	Tue Jan 05 13:16:16 2010 +0100
@@ -109,7 +109,9 @@
 {
   bool retval = true;
 
-  for (int i = 0; i < numel (); i++)
+  octave_idx_type n = numel ();
+
+  for (octave_idx_type i = 0; i < n; i++)
     {
       if (! elem(i).is_string ())
 	{
@@ -121,6 +123,19 @@
   return retval;
 }
 
+Array<std::string>
+Cell::cellstr_value (void) const
+{
+  Array<std::string> retval (dims ());
+
+  octave_idx_type n = numel ();
+
+  for (octave_idx_type i = 0; i < n; i++)
+    retval.xelem (i) = elem (i).string_value ();
+
+  return retval;
+}
+
 Cell
 Cell::index (const octave_value_list& idx_arg, bool resize_ok) const
 {
--- a/src/Cell.h	Wed Jan 06 07:49:40 2010 -0500
+++ b/src/Cell.h	Tue Jan 05 13:16:16 2010 +0100
@@ -71,6 +71,8 @@
 
   bool is_cellstr (void) const;
 
+  Array<std::string> cellstr_value (void) const;
+
   Cell index (const octave_value_list& idx, bool resize_ok = false) const;
 
   Cell& delete_elements (const octave_value_list& idx);
--- a/src/ChangeLog	Wed Jan 06 07:49:40 2010 -0500
+++ b/src/ChangeLog	Tue Jan 05 13:16:16 2010 +0100
@@ -1,3 +1,16 @@
+2010-01-05  Jaroslav Hajek  <highegg@gmail.com>
+
+	* Cell.cc (Cell::cellstr_value): New method.
+	* Cell.h (Cell::cellstr_value): Declare it.
+
+	* ov-cell.h (octave_cell::cellstr_cache): Declare as autopointer.
+	(octave_cell::clear_cellstr_cache): Move here.
+	* ov-cell.cc (octave_cell::is_cellstr): Only allocate the pointer, but
+	don't actually create strings.
+	(octave_cell::cellstr_value): Use cached test, call
+	Cell::cellstr_value.
+	(octave_cell::make_cellstr_cache): Remove.
+
 2010-01-05  John W. Eaton  <jwe@octave.org>
 
 	* Makefile.am (AM_CPPFLAGS): Include -I../libgnu in the list.
--- a/src/ov-cell.cc	Wed Jan 06 07:49:40 2010 -0500
+++ b/src/ov-cell.cc	Tue Jan 05 13:16:16 2010 +0100
@@ -386,38 +386,18 @@
   return retval;
 }
 
-void 
-octave_cell::clear_cellstr_cache (void) const
-{
-  cellstr_cache = Array<std::string> ();
-}
-
-void 
-octave_cell::make_cellstr_cache (void) const
-{
-  cellstr_cache = Array<std::string> (matrix.dims ());
-
-  octave_idx_type n = numel ();
-
-  std::string *dst = cellstr_cache.fortran_vec ();
-  const octave_value *src = matrix.data ();
-
-  for (octave_idx_type i = 0; i < n; i++)
-    dst[i] = src[i].string_value ();
-}
-
 bool 
 octave_cell::is_cellstr (void) const
 {
   bool retval;
-  if (! cellstr_cache.is_empty ())
+  if (cellstr_cache.get ())
     retval = true;
   else
     {
       retval = matrix.is_cellstr ();
-      // force cache to be created here
+      // Allocate empty cache to mark that this is indeed a cellstr.
       if (retval)
-        make_cellstr_cache ();
+        cellstr_cache.reset (new Array<std::string> ());
     }
 
   return retval;
@@ -634,7 +614,10 @@
 
   if (is_cellstr ())
     {
-      retval = cellstr_cache;
+      if (cellstr_cache->is_empty ())
+        *cellstr_cache = matrix.cellstr_value ();
+
+      return *cellstr_cache;
     }
   else
     error ("invalid conversion from cell array to array of strings");
--- a/src/ov-cell.h	Wed Jan 06 07:49:40 2010 -0500
+++ b/src/ov-cell.h	Tue Jan 05 13:16:16 2010 +0100
@@ -28,6 +28,7 @@
 
 #include <iosfwd>
 #include <string>
+#include <memory>
 
 #include "mx-base.h"
 #include "oct-alloc.h"
@@ -57,7 +58,7 @@
     : octave_base_matrix<Cell> (c) { }
 
   octave_cell (const Array<std::string>& str)
-    : octave_base_matrix<Cell> (Cell (str)), cellstr_cache (str) { }
+    : octave_base_matrix<Cell> (Cell (str)), cellstr_cache (new Array<std::string> (str)) { }
 
   octave_cell (const octave_cell& c)
     : octave_base_matrix<Cell> (c) { }
@@ -165,11 +166,10 @@
 
 private:
 
-  void clear_cellstr_cache (void) const;
+  void clear_cellstr_cache (void) const
+    { cellstr_cache.reset (); }
 
-  mutable Array<std::string> cellstr_cache;
-
-  void make_cellstr_cache (void) const;
+  mutable std::auto_ptr<Array<std::string> > cellstr_cache;
 
   DECLARE_OCTAVE_ALLOCATOR