2013-07-17 19:24:07 +02:00
|
|
|
.. highlight:: c
|
|
|
|
|
|
|
|
.. _Foreach:
|
|
|
|
|
2014-10-17 18:15:19 +02:00
|
|
|
Loops: the foreach constructs
|
2013-07-17 19:24:07 +02:00
|
|
|
========================================================
|
|
|
|
|
|
|
|
foreach and its variants are a compact and efficient way to
|
|
|
|
perform some action of the kind
|
|
|
|
|
2013-08-27 19:17:17 +02:00
|
|
|
*For all indices of the array, do something*
|
2013-07-17 19:24:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
While this kind of construct is equivalent to write manually the *for* loops, it has
|
2014-10-17 18:15:19 +02:00
|
|
|
several advantages:
|
2013-07-17 19:24:07 +02:00
|
|
|
|
|
|
|
* it is more compact, less error prone (one does not need to specify the bounds in the loop).
|
|
|
|
|
2014-09-07 14:34:10 +02:00
|
|
|
* The library orders the loops in way specified by the template parameters TraversalOrder (by default, the standard C order),
|
|
|
|
to obtain the most efficient way to traverse the memory.
|
2013-07-17 19:24:07 +02:00
|
|
|
|
|
|
|
* it is easier to write generic code for array of several dimensions.
|
|
|
|
|
|
|
|
foreach
|
|
|
|
------------
|
|
|
|
|
|
|
|
The *foreach* function call a given function *f* successively on the indices of an array *A*,
|
2014-09-07 14:34:10 +02:00
|
|
|
in the order specified by the TraversalOrder of the array.
|
2013-07-17 19:24:07 +02:00
|
|
|
|
|
|
|
* Synopsis::
|
|
|
|
|
|
|
|
template <typename ArrayType, typename Function>
|
|
|
|
void foreach (ArrayType const & A, Function F);
|
|
|
|
|
|
|
|
* A is an array/matrix/vector or the corresponding view.
|
|
|
|
* The template is enabled iif ImmutableArray<ArrayType>::value == true
|
|
|
|
* F is a function with the following synopsis ::
|
|
|
|
|
|
|
|
F(size_t ... indices)
|
|
|
|
|
|
|
|
* The foreach algorithm is equivalent to ::
|
|
|
|
|
|
|
|
for (i,j,k...) F(i,j,k...)
|
|
|
|
|
|
|
|
* The for loop are automatically organised to optimize the traversal order of A
|
2014-09-07 14:34:10 +02:00
|
|
|
using the TraversalOrder of the array.
|
2013-07-17 19:24:07 +02:00
|
|
|
|
|
|
|
* As a result this is always equally or more optimized than a manually written loop.
|
|
|
|
|
2014-10-17 18:15:19 +02:00
|
|
|
Example:
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2014-05-31 19:12:21 +02:00
|
|
|
.. triqs_example:: ./foreach_0.cpp
|
2013-07-17 19:24:07 +02:00
|
|
|
.. note::
|
|
|
|
You *can* pass a std::function as Function, but it is not recommended in critical parts of the code.
|
|
|
|
|
|
|
|
The indirection caused by std::function at each call may lead to big performance penalty.
|
|
|
|
|
|
|
|
The call to lambda, or a custom callable object will on the other hand by inlined.
|
|
|
|
|
|
|
|
assign_foreach
|
|
|
|
----------------
|
|
|
|
|
|
|
|
assign_foreach is a simpler form that assigns the return value of the function to the array elements.
|
|
|
|
Note that using the lazy expression is usually a lot simpler (except when you already have the function ready).
|
|
|
|
|
|
|
|
Synopsis::
|
|
|
|
|
|
|
|
template <typename ArrayType, typename Function>
|
|
|
|
void assign_foreach (ArrayType const & A, Function F);
|
|
|
|
|
|
|
|
* A is an array/matrix/vector or the corresponding view.
|
|
|
|
* The template is enabled iif ImmutableArray<ArrayType>::value == true
|
|
|
|
* F is a function with the following synopsis ::
|
|
|
|
|
|
|
|
F(size_t ... indices)
|
|
|
|
|
|
|
|
* The assign_foreach algorithm is equivalent to ::
|
|
|
|
|
|
|
|
for (i,j,k...) A(i,j,k...) = F(i,j,k...)
|
|
|
|
|
|
|
|
* The for loop are automatically organised to optimize the traversal order of A
|
2014-09-07 14:34:10 +02:00
|
|
|
using the TraversalOrder of the array.
|
2013-07-17 19:24:07 +02:00
|
|
|
|
2014-05-31 19:12:21 +02:00
|
|
|
.. triqs_example:: ./foreach_1.cpp
|
2013-07-17 19:24:07 +02:00
|
|
|
.. note::
|
|
|
|
Cf the note of the *foreach* function.
|