# HG changeset patch # User jwe # Date 1199993310 0 # Node ID 14142d0843665c71191e489af691581271520c4d # Parent c3d1d911dfa821bfb4fcbb187819a1ee29072b3b [3-0-0-branch @ 2008-01-10 19:28:30 by jwe] diff --git a/src/ChangeLog b/src/ChangeLog --- a/src/ChangeLog +++ b/src/ChangeLog @@ -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): @@ -7,7 +13,6 @@ * error.cc (Vdebug_on_error, Vdebug_on_warning): No longer static. * error.h: Provide decls. - 2008-01-04 John Swensen * debug.cc (bp_table::do_remove_all_breakpoints_in_file): diff --git a/src/mex.cc b/src/mex.cc --- a/src/mex.cc +++ b/src/mex.cc @@ -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