changeset 8815:af907aeedbf4

cache cellstr_value in ov-cell
author Jaroslav Hajek <highegg@gmail.com>
date Thu, 19 Feb 2009 16:37:17 +0100
parents de16ebeef93d
children a4a8f871be81
files src/ChangeLog src/ov-cell.cc src/ov-cell.h
diffstat 3 files changed, 106 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,14 @@
+2009-02-19  Jaroslav Hajek  <highegg@gmail.com>
+
+	* ov-cell.h (octave_cell::cellstr_cache): New field.
+	(octave_cell::clear_cellstr_cache, octave_cell::make_cellstr_cache,
+	octave_cell::assign, octave_cell::delete_elements,
+	octave_cell::mex_get_data): New methods.
+	(octave_cell::is_cellstr): Reuse cellstr cache if possible, create if
+	successful.
+	(octave_cell::cellstr_value): Reuse cellstr cache if possible.
+	(octave_cell::subsasgn): Clear cellstr cache.
+
 2009-02-19  Jaroslav Hajek  <highegg@gmail.com>
 
 	* DLD-FUNCTIONS/lookup.cc (Flookup): Use Array<T>::lookup if possible.
--- a/src/ov-cell.cc
+++ b/src/ov-cell.cc
@@ -214,6 +214,8 @@
 
   octave_value t_rhs = rhs;
 
+  clear_cellstr_cache ();
+
   if (n > 1)
     {
       switch (type[0])
@@ -376,6 +378,65 @@
   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 ())
+    retval = true;
+  else
+    {
+      retval = matrix.is_cellstr ();
+      // force cache to be created here
+      if (retval)
+        make_cellstr_cache ();
+    }
+
+  return retval;
+}
+
+void 
+octave_cell::assign (const octave_value_list& idx, const Cell& rhs)
+{
+  clear_cellstr_cache ();
+  octave_base_matrix<Cell>::assign (idx, rhs);
+}
+
+void 
+octave_cell::assign (const octave_value_list& idx, const octave_value& rhs)
+{
+  clear_cellstr_cache ();
+  octave_base_matrix<Cell>::assign (idx, rhs);
+}
+
+
+void 
+octave_cell::delete_elements (const octave_value_list& idx)
+{
+  clear_cellstr_cache ();
+  octave_base_matrix<Cell>::delete_elements (idx);
+}
+
 size_t
 octave_cell::byte_size (void) const
 {
@@ -515,20 +576,14 @@
 Array<std::string>
 octave_cell::cellstr_value (void) const
 {
-  Array<std::string> retval (dims ());
+  Array<std::string> retval;
 
   if (is_cellstr ())
     {
-      octave_idx_type n = numel ();
-
-      std::string *dst = retval.fortran_vec ();
-      const octave_value *src = matrix.data ();
-
-      for (octave_idx_type i = 0; i < n; i++)
-	dst[i] = src[i].string_value ();
+      retval = cellstr_cache;
     }
   else
-    error ("invalid conversion from cell array to Array<std::string>");
+    error ("invalid conversion from cell array to array of strings");
 
   return retval;
 }
@@ -662,6 +717,8 @@
 {
   bool success = true;
 
+  clear_cellstr_cache ();
+
   string_vector keywords(2);
 
   keywords[0] = "ndims";
@@ -826,8 +883,10 @@
 
 bool 
 octave_cell::load_binary (std::istream& is, bool swap,
-				 oct_mach_info::float_format fmt)
+                          oct_mach_info::float_format fmt)
 {
+  clear_cellstr_cache ();
+
   bool success = true;
   int32_t mdims;
   if (! is.read (reinterpret_cast<char *> (&mdims), 4))
@@ -900,6 +959,13 @@
   return success;
 }
 
+void *
+octave_cell::mex_get_data (void) const
+{
+  clear_cellstr_cache ();
+  return matrix.mex_get_data ();
+}
+
 #if defined (HAVE_HDF5)
 
 bool
@@ -986,6 +1052,8 @@
 octave_cell::load_hdf5 (hid_t loc_id, const char *name,
 			bool have_h5giterate_bug)
 {
+  clear_cellstr_cache ();
+
   bool retval = false;
 
   dim_vector dv;
--- a/src/ov-cell.h
+++ b/src/ov-cell.h
@@ -86,6 +86,12 @@
 			 const std::list<octave_value_list>& idx,
 			 const octave_value& rhs);
 
+  void assign (const octave_value_list& idx, const Cell& rhs);
+
+  void assign (const octave_value_list& idx, const octave_value& rhs);
+
+  void delete_elements (const octave_value_list& idx);
+
   size_t byte_size (void) const;
 
   octave_value sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const;
@@ -105,7 +111,7 @@
 
   bool is_cell (void) const { return true; }
 
-  bool is_cellstr (void) const { return matrix.is_cellstr (); }
+  bool is_cellstr (void) const;
 
   bool is_true (void) const;
 
@@ -160,8 +166,18 @@
 
   mxArray *as_mxArray (void) const;
 
+  // Unsafe.  This function exists to support the MEX interface.
+  // You should not use it anywhere else.
+  void *mex_get_data (void) const; 
+
 private:
 
+  void clear_cellstr_cache (void) const;
+
+  mutable Array<std::string> cellstr_cache;
+
+  void make_cellstr_cache (void) const;
+
   DECLARE_OCTAVE_ALLOCATOR
 
   DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA