# HG changeset patch # User Jaroslav Hajek # Date 1262693776 -3600 # Node ID 64a06079cae44481dff6f3503eae51c06e7c921a # Parent 17ce2a700a97108b09f1fe6a164c03325d8e65f0 improve cellstr cache implementation diff --git a/src/Cell.cc b/src/Cell.cc --- a/src/Cell.cc +++ b/src/Cell.cc @@ -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 +Cell::cellstr_value (void) const +{ + Array 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 { diff --git a/src/Cell.h b/src/Cell.h --- a/src/Cell.h +++ b/src/Cell.h @@ -71,6 +71,8 @@ bool is_cellstr (void) const; + Array cellstr_value (void) const; + Cell index (const octave_value_list& idx, bool resize_ok = false) const; Cell& delete_elements (const octave_value_list& idx); diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,16 @@ +2010-01-05 Jaroslav Hajek + + * 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 * Makefile.am (AM_CPPFLAGS): Include -I../libgnu in the list. diff --git a/src/ov-cell.cc b/src/ov-cell.cc --- a/src/ov-cell.cc +++ b/src/ov-cell.cc @@ -386,38 +386,18 @@ return retval; } -void -octave_cell::clear_cellstr_cache (void) const -{ - cellstr_cache = Array (); -} - -void -octave_cell::make_cellstr_cache (void) const -{ - cellstr_cache = Array (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 ()); } 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"); diff --git a/src/ov-cell.h b/src/ov-cell.h --- a/src/ov-cell.h +++ b/src/ov-cell.h @@ -28,6 +28,7 @@ #include #include +#include #include "mx-base.h" #include "oct-alloc.h" @@ -57,7 +58,7 @@ : octave_base_matrix (c) { } octave_cell (const Array& str) - : octave_base_matrix (Cell (str)), cellstr_cache (str) { } + : octave_base_matrix (Cell (str)), cellstr_cache (new Array (str)) { } octave_cell (const octave_cell& c) : octave_base_matrix (c) { } @@ -165,11 +166,10 @@ private: - void clear_cellstr_cache (void) const; + void clear_cellstr_cache (void) const + { cellstr_cache.reset (); } - mutable Array cellstr_cache; - - void make_cellstr_cache (void) const; + mutable std::auto_ptr > cellstr_cache; DECLARE_OCTAVE_ALLOCATOR