diff --git a/triqs/utility/tuple_tools.hpp b/triqs/utility/tuple_tools.hpp index ea637f5f..83c37a2d 100644 --- a/triqs/utility/tuple_tools.hpp +++ b/triqs/utility/tuple_tools.hpp @@ -79,6 +79,30 @@ namespace triqs { namespace tuple { for_each_impl::value-1>()(t, f); } + /* for_each_enumerate(f, t) + * f: a callable object + * t: a tuple + * calls f on all tuple elements: f(x,n) for all x in t + */ + template struct for_each_enumerate_impl { + template + void operator()(T const & t, F && f) { + f(std::get::value-1-pos>(t),std::tuple_size::value-1-pos); + for_each_impl()(t, f); + } + }; + + template<> + struct for_each_enumerate_impl<0> { + template + void operator() (T const & t, F && f) { f(std::get::value-1>(t), std::tuple_size::value-1); } + }; + + template + void for_each_enumerate(T const & t, F && f) { + for_each_enumerate_impl::value-1>()(t, f); + } + /** * apply_on_zip(f, t1,t2) * f : a callable object