diff --git a/Documentation/Examples/Algorithms/TemplateStaticForExample.cpp b/Documentation/Examples/Algorithms/TemplateStaticForExample.cpp index 47757458d71505fa585f3624575bcdaa55869602..a2fce79ae670bcda93da5fa6c1a3e95d1d260475 100644 --- a/Documentation/Examples/Algorithms/TemplateStaticForExample.cpp +++ b/Documentation/Examples/Algorithms/TemplateStaticForExample.cpp @@ -1,28 +1,31 @@ #include <iostream> #include <cstdlib> #include <TNL/Containers/StaticVector.h> -#include <TNL/Algorithms/StaticFor.h> +#include <TNL/Algorithms/TemplateStaticFor.h> using namespace TNL; using namespace TNL::Containers; +const int Size( 5 ); + +template< int I > +struct LoopBody +{ + static void exec( const StaticVector< Size, double >& v ) { + std::cout << "v[ " << I << " ] = " << v[ I ] << std::endl; + } +}; + int main( int argc, char* argv[] ) { /**** - * Create two static vectors + * Initiate static vector */ - const int Size( 3 ); - StaticVector< Size, double > a, b; - a = 1.0; - b = 2.0; - double sum( 0.0 ); + StaticVector< Size, double > v{ 1.0, 2.0, 3.0, 4.0, 5.0 }; /**** - * Compute an addition of a vector and a constant number. + * Print out the vector using template parameters for indexing. */ - auto addition = [&]( int i, const double& c ) { a[ i ] = b[ i ] + c; sum += a[ i ]; }; - Algorithms::StaticFor< 0, Size >::exec( addition, 3.14 ); - std::cout << "a = " << a << std::endl; - std::cout << "sum = " << sum << std::endl; + Algorithms::TemplateStaticFor< 0, Size, LoopBody >::exec( v ); } diff --git a/src/TNL/Algorithms/TemplateStaticFor.h b/src/TNL/Algorithms/TemplateStaticFor.h index 753ad9b2618b2704292517e9b74ffff7192d22b7..de6eebbeea48aac8287f11e16befbde1ca569a56 100644 --- a/src/TNL/Algorithms/TemplateStaticFor.h +++ b/src/TNL/Algorithms/TemplateStaticFor.h @@ -17,6 +17,31 @@ namespace TNL { namespace Algorithms { + +/** + * \brief TemplateStaticFor serves for coding for-loops in template parameters. + * + * The result of calling this loop with a templated class \p LoopBody is as follows: + * + * LoopBody< begin >::exec( ... ); + * + * LoodBody< begin + 1 >::exec( ... ); + * + * ... + * + * LoopBody< end - 1 >::exec( ... ); + * + * \tparam IndexType is type of the loop indexes + * \tparam begin the loop iterates over index interval [begin,end). + * \tparam end the loop iterates over index interval [begin,end). + * \tparam LoopBody is a templated class having one template parameter of IndexType. + */ +template< typename IndexType, + IndexType begin, + IndexType end, + template< IndexType > class LoopBody > +struct TemplateStaticFor; + namespace detail { template< typename IndexType,