diff --git a/triqs/utility/tuple_tools.hpp b/triqs/utility/tuple_tools.hpp index bb6a61db..a484000d 100644 --- a/triqs/utility/tuple_tools.hpp +++ b/triqs/utility/tuple_tools.hpp @@ -32,7 +32,6 @@ namespace triqs { namespace tuple { * t a tuple * Returns : f(t[0], t[1], ...) * Equivalent to f(*t) in python .... - * Q : what about constructor */ template struct apply_impl { template @@ -54,6 +53,27 @@ namespace triqs { namespace tuple { template ReturnType apply( ReturnType(*f)(Args...), T const & t) { return apply([f](Args const & ... args) { return (*f)(args...);} ,t);} + /** + * apply_construct(t) + * F : a class + * t a tuple + * Returns : F { t[0], t[1]} + */ + template struct apply_construct_impl { + template + auto operator()(T const & t, Args && ... args) + DECL_AND_RETURN( apply_construct_impl()(t, std::get(t), std::forward(args)...)); + }; + + template struct apply_construct_impl<-1,F,T> { + template + auto operator()(T const & t, Args && ... args) DECL_AND_RETURN( F{std::forward(args)...}); + }; + + template + auto apply_construct (T const & t) DECL_AND_RETURN( apply_construct_impl::value-1,F,T>()(t)); + + /** * for_each(f, t) * f: a callable object @@ -103,6 +123,26 @@ namespace triqs { namespace tuple { for_each_enumerate_impl::value-1>()(t, f); } + /** + * apply_on_tuple(f, t1,t2) + * f : a callable object + * t1, t2 two tuples of the same size + * Returns : [f(i,j) for i,j in zip(t1,t2)] + */ + template struct apply_on_tuple_impl { + template + auto operator()(F && f, T1 && t1, Args && ... args) + DECL_AND_RETURN( apply_on_tuple_impl()(std::forward(f),std::forward(t1), f(std::get(t1)), std::forward(args)...)); + }; + + template<> struct apply_on_tuple_impl<-1> { + template + auto operator()(F && f, T1 && t1, Args && ... args) DECL_AND_RETURN( std::make_tuple(std::forward(args)...)); + }; + + template + auto apply_on_tuple (F && f,T1 && t1) DECL_AND_RETURN( apply_on_tuple_impl::type>::value-1>()(std::forward(f),std::forward(t1))); + /** * apply_on_zip(f, t1,t2) * f : a callable object