# HG changeset patch # User jwe # Date 1199993318 0 # Node ID 3a1e5a965815d43f8007830dcf368dfbe4e16031 # Parent 164e98cdee8b2e82c3089b1702860a0869aab638 [project @ 2008-01-10 19:28:38 by jwe] diff -r 164e98cdee8b -r 3a1e5a965815 src/ChangeLog --- a/src/ChangeLog Thu Jan 10 09:23:45 2008 +0000 +++ b/src/ChangeLog Thu Jan 10 19:28:38 2008 +0000 @@ -1,3 +1,9 @@ +2008-01-10 John W. Eaton + + * mex.cc (calc_single_subscript_internal): New static function. + (mxArray_octave_value::calc_single_subscript): Use it. + (mxArray_matlab::calc_single_subscript): Use it. + 2008-01-07 John W. Eaton * src/pt-except.cc (tree_try_catch_command::eval): diff -r 164e98cdee8b -r 3a1e5a965815 src/mex.cc --- a/src/mex.cc Thu Jan 10 09:23:45 2008 +0000 +++ b/src/mex.cc Thu Jan 10 19:28:38 2008 +0000 @@ -271,6 +271,38 @@ } }; +static mwIndex +calc_single_subscript_internal (mwSize ndims, const mwSize *dims, + mwSize nsubs, const mwIndex *subs) +{ + mwIndex retval = 0; + + switch (nsubs) + { + case 0: + break; + + case 1: + retval = subs[0]; + break; + + default: + { + // Both nsubs and ndims should be at least 2 here. + + mwSize n = nsubs <= ndims ? nsubs : ndims; + + retval = subs[--n]; + + while (--n >= 0) + retval = dims[n] * retval + subs[n]; + } + break; + } + + return retval; +} + // The object that handles values pass to MEX files from Octave. Some // methods in this class may set mutate_flag to TRUE to tell the // mxArray class to convert to the Matlab-style representation and @@ -605,17 +637,10 @@ mwIndex calc_single_subscript (mwSize nsubs, mwIndex *subs) const { - mwIndex retval = 0; - // Force ndims, dims to be cached. get_dimensions (); - mwIndex n = nsubs <= ndims ? nsubs : ndims; - - while (--n > 0) - retval = retval * dims[n] + subs[n]; - - return retval; + return calc_single_subscript_internal (ndims, dims, nsubs, subs); } size_t get_element_size (void) const @@ -996,14 +1021,7 @@ mwIndex calc_single_subscript (mwSize nsubs, mwIndex *subs) const { - mwIndex retval = 0; - - mwSize n = nsubs <= ndims ? nsubs : ndims; - - while (--n > 0) - retval = retval * dims[n] + subs[n]; - - return retval; + return calc_single_subscript_internal (ndims, dims, nsubs, subs); } size_t get_element_size (void) const