changeset 30190:2429561a1415

replace sub2ind helper classes with lambda expression * Array-util.cc (sub2ind_helper): Delete. (sub2ind): Use lambda expression instead of sub2ind_helper class instance.
author John W. Eaton <jwe@octave.org>
date Thu, 16 Sep 2021 13:26:40 -0400
parents 41eecdf76372
children 57aac5b9cf9d
files liboctave/array/Array-util.cc
diffstat 1 files changed, 12 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/array/Array-util.cc	Thu Sep 16 12:54:15 2021 -0400
+++ b/liboctave/array/Array-util.cc	Thu Sep 16 13:26:40 2021 -0400
@@ -520,19 +520,6 @@
   return rdv;
 }
 
-// A helper class.
-class sub2ind_helper
-{
-public:
-  sub2ind_helper (octave_idx_type *ind, octave_idx_type n)
-    : m_ind (ind), m_n (n) { }
-
-  void operator () (octave_idx_type k) { (*m_ind++ *= m_n) += k; }
-
-private:
-  octave_idx_type *m_ind, m_n;
-};
-
 octave::idx_vector
 sub2ind (const dim_vector& dv, const Array<octave::idx_vector>& idxa)
 {
@@ -602,12 +589,22 @@
   else
     {
       Array<octave_idx_type> idx (idxa(0).orig_dimensions ());
-      octave_idx_type *idx_vec = idx.fortran_vec ();
 
       for (octave_idx_type i = len - 1; i >= 0; i--)
         {
+          // Initialized inside the loop so that each call to
+          // idx_vector::loop operates from the beginning of IDX_VEC.
+
+          octave_idx_type *idx_vec = idx.fortran_vec ();
+
           if (i < len - 1)
-            idxa(i).loop (clen, sub2ind_helper (idx_vec, dvx(i)));
+            {
+              octave_idx_type n = dvx(i);
+
+              idxa(i).loop (clen, [=, &idx_vec] (octave_idx_type k) {
+                (*idx_vec++ *= n) += k;
+              });
+            }
           else
             idxa(i).copy_data (idx_vec);
         }