From 2c77efe619aee03591d12c0109870370b828333a Mon Sep 17 00:00:00 2001 From: Olivier Parcollet Date: Thu, 25 Jul 2013 13:24:50 +0200 Subject: [PATCH] uility : add foreach_enumerate - to be tested --- triqs/utility/tuple_tools.hpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) 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