changeset 11610:14142d084366 release-3-0-x

[3-0-0-branch @ 2008-01-10 19:28:30 by jwe]
author jwe
date Thu, 10 Jan 2008 19:28:30 +0000
parents c3d1d911dfa8
children 807cf4d67705
files src/ChangeLog src/mex.cc
diffstat 2 files changed, 40 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,9 @@
+2008-01-10  John W. Eaton  <jwe@octave.org>
+
+	* 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  <jwe@octave.org>
 
 	* 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  <jpswensen@comcast.net>
 
 	* debug.cc (bp_table::do_remove_all_breakpoints_in_file):
--- 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