diff src/mex.cc @ 7357:3a1e5a965815

[project @ 2008-01-10 19:28:38 by jwe]
author jwe
date Thu, 10 Jan 2008 19:28:38 +0000
parents 745a8299c2b5
children ba8fcc115fee
line wrap: on
line diff
--- 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