changeset 9350:16a5f9e1fdb3

cache idx_vector result in matrices once used for indexing
author Jaroslav Hajek <highegg@gmail.com>
date Mon, 15 Jun 2009 10:40:10 +0200
parents 93664cbb732c
children e2344f4af0cb
files src/ChangeLog src/ov-base-mat.cc src/ov-base-mat.h src/ov-bool-mat.h src/ov-flt-re-mat.h src/ov-intx.h src/ov-range.h src/ov-re-mat.h
diffstat 8 files changed, 90 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,22 @@
+2009-06-15  Jaroslav Hajek  <highegg@gmail.com>
+
+	* ov-base-mat.h (octave_base_matrix::idx_cache): New member field.
+	(octave_base_matrix::typ): Turn to MatrixType *.
+	(octave_base_matrix::octave_base_matrix (...)): Update constructors.
+	(octave_base_matrix::clear_cached_info,
+	octave_base_matrix::set_idx_cache): New member functions.
+	* ov-base-mat.cc (octave_base_matrix::assign,
+	octave_base_matrix::delete_elements): Call clear_cached_info here.
+	* ov-re-mat.h (octave_matrix::index_vector): Use idx_cache.
+	* ov-flt-re-mat.h (octave_float_matrix::index_vector): Ditto.
+	* ov-bool-mat.h (octave_bool_matrix::index_vector): Ditto.
+	* ov-intx.h (OCTAVE_VALUE_INT_MATRIX_T::index_vector): Ditto.
+	* ov-range.h (octave_range::idx_cache): New member field.
+	(octave_range::octave_range (...)): Update constructors.
+	(octave_range::clear_cached_info,
+	octave_base_matrix::set_idx_cache): New member functions.
+	(octave_range::idx_vector): Use idx_cache.
+
 2009-06-14  Michael Goffioul  <michael.goffioul@gmail.com>
 
 	* graphics.h.in (axes::properties::xtick, axes::properties::ytick,
--- a/src/ov-base-mat.cc
+++ b/src/ov-base-mat.cc
@@ -273,8 +273,17 @@
       break;
     }
 
-  // Invalidate the matrix type
-  typ.invalidate_type ();
+  // Clear cache.
+  clear_cached_info ();
+}
+
+template <class MT>
+MatrixType 
+octave_base_matrix<MT>::matrix_type (const MatrixType& _typ) const
+{ 
+  delete typ; 
+  typ = new MatrixType (_typ);
+  return *typ;
 }
 
 template <class MT>
@@ -368,8 +377,8 @@
       break;
     }
 
-  // Invalidate the matrix type
-  typ.invalidate_type ();
+  // Clear cache.
+  clear_cached_info ();
 }
 
 template <class MT>
@@ -385,8 +394,8 @@
 
   matrix.delete_elements (ra_idx);
 
-  // Invalidate the matrix type
-  typ.invalidate_type ();
+  // Clear cache.
+  clear_cached_info ();
 }
 
 template <class MT>
--- a/src/ov-base-mat.h
+++ b/src/ov-base-mat.h
@@ -51,19 +51,23 @@
 public:
 
   octave_base_matrix (void)
-    : octave_base_value (), typ (MatrixType ()) { }
+    : octave_base_value (), typ (), idx_cache () { }
 
   octave_base_matrix (const MT& m, const MatrixType& t = MatrixType ())
-    : octave_base_value (), matrix (m), typ (t)
+    : octave_base_value (), matrix (m), 
+      typ (t.is_known () ? new MatrixType(t) : 0), idx_cache ()
   {
     if (matrix.ndims () == 0)
       matrix.resize (dim_vector (0, 0));
   }
 
   octave_base_matrix (const octave_base_matrix& m)
-    : octave_base_value (), matrix (m.matrix), typ (m.typ) { }
+    : octave_base_value (), matrix (m.matrix), 
+      typ (m.typ ? new MatrixType (*m.typ) : 0), 
+      idx_cache (m.idx_cache ? new idx_vector (*m.idx_cache) : 0) 
+    { }
 
-  ~octave_base_matrix (void) { }
+  ~octave_base_matrix (void) { clear_cached_info (); }
 
   octave_base_value *clone (void) const { return new octave_base_matrix (*this); }
   octave_base_value *empty_clone (void) const { return new octave_base_matrix (); }
@@ -113,9 +117,8 @@
   octave_value all (int dim = 0) const { return matrix.all (dim); }
   octave_value any (int dim = 0) const { return matrix.any (dim); }
 
-  MatrixType matrix_type (void) const { return typ; }
-  MatrixType matrix_type (const MatrixType& _typ) const
-    { MatrixType ret = typ; typ = _typ; return ret; }
+  MatrixType matrix_type (void) const { return typ ? *typ : MatrixType (); }
+  MatrixType matrix_type (const MatrixType& _typ) const;
 
   octave_value diag (octave_idx_type k = 0) const
     { return octave_value (matrix.diag (k)); }
@@ -159,7 +162,21 @@
 
   MT matrix;
 
-  mutable MatrixType typ;
+  idx_vector set_idx_cache (const idx_vector& idx) const
+    {
+      delete idx_cache;
+      idx_cache = idx ? new idx_vector (idx) : 0;
+      return idx;
+    }
+
+  void clear_cached_info (void) const
+    {
+      delete typ; typ = 0;
+      delete idx_cache; idx_cache = 0;
+    } 
+
+  mutable MatrixType *typ;
+  mutable idx_vector *idx_cache;
 };
 
 #endif
--- a/src/ov-bool-mat.h
+++ b/src/ov-bool-mat.h
@@ -77,7 +77,8 @@
 
   octave_base_value *try_narrowing_conversion (void);
 
-  idx_vector index_vector (void) const { return idx_vector (matrix); }
+  idx_vector index_vector (void) const 
+    { return idx_cache ? *idx_cache : set_idx_cache (idx_vector (matrix)); }
 
   bool is_bool_matrix (void) const { return true; }
 
--- a/src/ov-flt-re-mat.h
+++ b/src/ov-flt-re-mat.h
@@ -88,7 +88,8 @@
 
   octave_base_value *try_narrowing_conversion (void);
 
-  idx_vector index_vector (void) const { return idx_vector (matrix); }
+  idx_vector index_vector (void) const 
+    { return idx_cache ? *idx_cache : set_idx_cache (idx_vector (matrix)); }
 
   bool is_real_matrix (void) const { return true; }
 
--- a/src/ov-intx.h
+++ b/src/ov-intx.h
@@ -316,7 +316,8 @@
       OCTAVE_INT_T::clear_conv_flag ();
    }
 
-  idx_vector index_vector (void) const { return idx_vector (matrix); }
+  idx_vector index_vector (void) const 
+    { return idx_cache ? *idx_cache : set_idx_cache (idx_vector (matrix)); }
 
   int write (octave_stream& os, int block_size,
 	     oct_data_conv::data_type output_type, int skip,
--- a/src/ov-range.h
+++ b/src/ov-range.h
@@ -56,26 +56,28 @@
 public:
 
   octave_range (void)
-    : octave_base_value () { }
+    : octave_base_value (), idx_cache () { }
 
   octave_range (double base, double limit, double inc)
-    : octave_base_value (), range (base, limit, inc)
+    : octave_base_value (), range (base, limit, inc), idx_cache ()
       {
 	if (range.nelem () < 0)
 	  ::error ("invalid range");
       }
 
   octave_range (const Range& r)
-    : octave_base_value (), range (r)
+    : octave_base_value (), range (r), idx_cache ()
       {
 	if (range.nelem () < 0 && range.nelem () != -2)
 	  ::error ("invalid range");
       }
 
   octave_range (const octave_range& r)
-    : octave_base_value (), range (r.range) { }
+    : octave_base_value (), range (r.range), 
+      idx_cache (r.idx_cache ? new idx_vector (*r.idx_cache) : 0)
+    { }
 
-  ~octave_range (void) { }
+  ~octave_range (void) { clear_cached_info (); }
 
   octave_base_value *clone (void) const { return new octave_range (*this); }
 
@@ -98,7 +100,8 @@
   octave_value do_index_op (const octave_value_list& idx,
 			    bool resize_ok = false);
 
-  idx_vector index_vector (void) const { return idx_vector (range); }
+  idx_vector index_vector (void) const 
+    { return idx_cache ? *idx_cache : set_idx_cache (idx_vector (range)); }
 
   dim_vector dims (void) const
     { 
@@ -335,6 +338,20 @@
 
   Range range;
 
+  idx_vector set_idx_cache (const idx_vector& idx) const
+    {
+      delete idx_cache;
+      idx_cache = idx ? new idx_vector (idx) : 0;
+      return idx;
+    }
+
+  void clear_cached_info (void) const
+    {
+      delete idx_cache; idx_cache = 0;
+    } 
+
+  mutable idx_vector *idx_cache;
+
   DECLARE_OCTAVE_ALLOCATOR
 
   DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
--- a/src/ov-re-mat.h
+++ b/src/ov-re-mat.h
@@ -90,7 +90,8 @@
 
   octave_base_value *try_narrowing_conversion (void);
 
-  idx_vector index_vector (void) const { return idx_vector (matrix); }
+  idx_vector index_vector (void) const
+    { return idx_cache ? *idx_cache : set_idx_cache (idx_vector (matrix)); }
 
   bool is_real_matrix (void) const { return true; }