comparison liboctave/array/Range.h @ 30188:e424d966dca7

replace rangeidx helper classes with lambda expressions * Range.h, Range.cc (rangeidx_helper, __rangeidx_helper): Delete. (range<T>::index): Use lambda expression instead of rangeidx_helper class instance. (Range::index): Likewise, for __rangeidx_helper.
author John W. Eaton <jwe@octave.org>
date Thu, 16 Sep 2021 12:49:01 -0400
parents 4c88a452519c
children 41eecdf76372
comparison
equal deleted inserted replaced
30187:f655202744be 30188:e424d966dca7
37 37
38 template <typename T> class Array; 38 template <typename T> class Array;
39 39
40 namespace octave 40 namespace octave
41 { 41 {
42 // Helper class used solely for idx_vector.loop () function call
43
44 template <typename T>
45 class rangeidx_helper
46 {
47 public:
48
49 rangeidx_helper (T *array, T base, T increment, T final_value,
50 octave_idx_type numel)
51 : m_array (array), m_base (base), m_increment (increment),
52 m_final (final_value),
53 m_nmax (numel-1)
54 { }
55
56 void operator () (octave_idx_type i)
57 {
58 if (i == 0)
59 // Required for proper NaN handling.
60 *m_array++ = m_nmax == 0 ? m_final : m_base;
61 else if (i < m_nmax)
62 *m_array++ = m_base + T (i) * m_increment;
63 else
64 *m_array++ = m_final;
65 }
66
67 private:
68
69 T *m_array, m_base, m_increment, m_final;
70 octave_idx_type m_nmax;
71
72 };
73
74 template <typename T> 42 template <typename T>
75 class 43 class
76 OCTAVE_API 44 OCTAVE_API
77 range 45 range
78 { 46 {
263 T operator () (octave_idx_type i, octave_idx_type j) const 231 T operator () (octave_idx_type i, octave_idx_type j) const
264 { 232 {
265 return elem (i, j); 233 return elem (i, j);
266 } 234 }
267 235
268 Array<T> index (const idx_vector& i) const 236 Array<T> index (const idx_vector& idx) const
269 { 237 {
270 Array<T> retval; 238 Array<T> retval;
271 239
272 octave_idx_type n = m_numel; 240 octave_idx_type n = m_numel;
273 241
274 if (i.is_colon ()) 242 if (idx.is_colon ())
275 { 243 {
276 retval = array_value ().reshape (dim_vector (m_numel, 1)); 244 retval = array_value ().reshape (dim_vector (m_numel, 1));
277 } 245 }
278 else 246 else
279 { 247 {
280 if (i.extent (n) != n) 248 if (idx.extent (n) != n)
281 err_index_out_of_range (1, 1, i.extent (n), n, dims ()); 249 err_index_out_of_range (1, 1, idx.extent (n), n, dims ());
282 250
283 dim_vector rd = i.orig_dimensions (); 251 dim_vector idx_dims = idx.orig_dimensions ();
284 octave_idx_type il = i.length (n); 252 octave_idx_type idx_len = idx.length (n);
285 253
286 // taken from Array.cc. 254 // taken from Array.cc.
287 if (n != 1 && rd.isvector ()) 255 if (n != 1 && idx_dims.isvector ())
288 rd = dim_vector (1, il); 256 idx_dims = dim_vector (1, idx_len);
289 257
290 retval.clear (rd); 258 retval.clear (idx_dims);
291 259
292 // idx_vector loop across all values in i, 260 // idx_vector loop across all values in i,
293 // executing __rangeidx_helper (i) for each i 261 // executing the lambda expression for each index value.
294 i.loop (n, rangeidx_helper<T> (retval.fortran_vec (), 262
295 m_base, m_increment, final_value (), 263 T *array = retval.fortran_vec ();
296 m_numel)); 264
265 idx.loop (n, [=, &array] (octave_idx_type i) {
266 if (i == 0)
267 // Required for proper NaN handling.
268 *array++ = m_numel == 0 ? m_final : m_base;
269 else if (i < m_numel - 1)
270 *array++ = m_base + T (i) * m_increment;
271 else
272 *array++ = m_final;
273 });
297 } 274 }
298 275
299 return retval; 276 return retval;
300 } 277 }
301 278