# HG changeset patch # User Jaroslav Hajek # Date 1235057837 -3600 # Node ID af907aeedbf4a0ebdeb87c7be6dd254576757499 # Parent de16ebeef93dc628cee477332567752e5414b130 cache cellstr_value in ov-cell diff -r de16ebeef93d -r af907aeedbf4 src/ChangeLog --- a/src/ChangeLog Thu Feb 19 15:19:59 2009 +0100 +++ b/src/ChangeLog Thu Feb 19 16:37:17 2009 +0100 @@ -1,3 +1,14 @@ +2009-02-19 Jaroslav Hajek + + * 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 * DLD-FUNCTIONS/lookup.cc (Flookup): Use Array::lookup if possible. diff -r de16ebeef93d -r af907aeedbf4 src/ov-cell.cc --- a/src/ov-cell.cc Thu Feb 19 15:19:59 2009 +0100 +++ b/src/ov-cell.cc Thu Feb 19 16:37:17 2009 +0100 @@ -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 (); +} + +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 ()) + 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::assign (idx, rhs); +} + +void +octave_cell::assign (const octave_value_list& idx, const octave_value& rhs) +{ + clear_cellstr_cache (); + octave_base_matrix::assign (idx, rhs); +} + + +void +octave_cell::delete_elements (const octave_value_list& idx) +{ + clear_cellstr_cache (); + octave_base_matrix::delete_elements (idx); +} + size_t octave_cell::byte_size (void) const { @@ -515,20 +576,14 @@ Array octave_cell::cellstr_value (void) const { - Array retval (dims ()); + Array 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"); + 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 (&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; diff -r de16ebeef93d -r af907aeedbf4 src/ov-cell.h --- a/src/ov-cell.h Thu Feb 19 15:19:59 2009 +0100 +++ b/src/ov-cell.h Thu Feb 19 16:37:17 2009 +0100 @@ -86,6 +86,12 @@ const std::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 cellstr_cache; + + void make_cellstr_cache (void) const; + DECLARE_OCTAVE_ALLOCATOR DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA