mirror of
https://github.com/triqs/dft_tools
synced 2024-12-26 14:23:38 +01:00
tuple tools : add reverse for a tuple...
- add in std:: the reverse to be able to apply the algorithms on a reversed tuple - overloaded std::get, std::tuple_size for this - added tests.
This commit is contained in:
parent
e8af74a030
commit
6f7deca96a
@ -41,7 +41,7 @@ struct print_t {
|
|||||||
|
|
||||||
struct A {
|
struct A {
|
||||||
template<typename T1, typename T2>
|
template<typename T1, typename T2>
|
||||||
std::string str(T1 const & x, T2 const &y) const { std::stringstream fs; fs << "the string is "<< x<<" " << y; return fs.str();}
|
std::string str(T1 const & x, T2 const &y) const { std::stringstream fs; fs << "A : the string is "<< x<<" " << y; return fs.str();}
|
||||||
};
|
};
|
||||||
|
|
||||||
std::string my_print_str(int x, int y) { std::stringstream fs; fs << "the string is "<< x<< " " << y; return fs.str();}
|
std::string my_print_str(int x, int y) { std::stringstream fs; fs << "the string is "<< x<< " " << y; return fs.str();}
|
||||||
@ -61,6 +61,11 @@ int main(int argc, char **argv) {
|
|||||||
std::cerr << std::endl;
|
std::cerr << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
triqs::tuple::for_each(reverse(t), print_t());
|
||||||
|
std::cerr << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
auto res = triqs::tuple::apply(fun(),t);
|
auto res = triqs::tuple::apply(fun(),t);
|
||||||
std::cerr << " f(t) =" << res << std::endl ;
|
std::cerr << " f(t) =" << res << std::endl ;
|
||||||
@ -76,21 +81,38 @@ int main(int argc, char **argv) {
|
|||||||
<< std::get<3>(r) << std::endl;
|
<< std::get<3>(r) << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::cerr << " ----- fold ----"<< std::endl ;
|
||||||
|
|
||||||
{
|
{
|
||||||
auto res = triqs::tuple::fold([](double x,double r) { return x+r;}, t, 0);
|
auto res = triqs::tuple::fold([](double x,double r) { return x+r;}, t, 0);
|
||||||
std::cerr << " " << res << std::endl ;
|
std::cerr << " " << res << std::endl ;
|
||||||
if ( std::abs((res - 15.6)) > 1.e-13) throw std::runtime_error(" ");
|
if ( std::abs((res - 15.6)) > 1.e-13) throw std::runtime_error(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto res = triqs::tuple::fold([](double x,double y) { return x+2*y;}, t, 0);
|
||||||
|
std::cerr << " " << res << std::endl ;
|
||||||
|
if ( std::abs((res - 33.8)) > 1.e-13) throw std::runtime_error(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto res = triqs::tuple::fold([](double x,double y) { return x+2*y;}, reverse(t), 0);
|
||||||
|
std::cerr << " " << res << std::endl ;
|
||||||
|
if ( std::abs((res - 86.8)) > 1.e-13) throw std::runtime_error(" ");
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
auto res = triqs::tuple::fold_on_zip([](double x, double y, double r) { return x+ 2*y +r;}, t1,t2, 0);
|
auto res = triqs::tuple::fold_on_zip([](double x, double y, double r) { return x+ 2*y +r;}, t1,t2, 0);
|
||||||
std::cerr << " " << res << std::endl ;
|
std::cerr << " " << res << std::endl ;
|
||||||
if ( std::abs((res - 35.6)) > 1.e-13) throw std::runtime_error(" ");
|
if ( std::abs((res - 35.6)) > 1.e-13) throw std::runtime_error(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::cerr << " ----- apply ----"<< std::endl ;
|
||||||
{
|
{
|
||||||
auto res = triqs::tuple::apply(my_print_str, std::make_tuple(1,2));
|
auto res = triqs::tuple::apply(my_print_str, std::make_tuple(1,2));
|
||||||
std::cerr << " " << res << std::endl ;
|
std::cerr << " " << res << std::endl ;
|
||||||
|
res = triqs::tuple::apply(my_print_str, reverse(std::make_tuple(1,2)));
|
||||||
|
std::cerr << " " << res << std::endl ;
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
@ -65,7 +65,7 @@ namespace triqs { namespace utility {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mini_vector & operator=(const mini_vector & x){ for (int i=0;i<Rank; ++i) _data[i] = x._data[i]; return *this;}
|
mini_vector & operator=(const mini_vector & x){ for (int i=0;i<Rank; ++i) _data[i] = x._data[i]; return *this;}
|
||||||
mini_vector & operator=(mini_vector && x){ swap(*this,x); return *this;}
|
mini_vector & operator=(mini_vector && x) = default;
|
||||||
|
|
||||||
friend void swap(mini_vector & a, mini_vector & b) { std::swap(a._data, b._data);}
|
friend void swap(mini_vector & a, mini_vector & b) { std::swap(a._data, b._data);}
|
||||||
|
|
||||||
|
@ -24,6 +24,22 @@
|
|||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
// adding to the std lib the reversed lazy tuple...
|
||||||
|
// overloading & specializing only the functions needed here.
|
||||||
|
namespace std {
|
||||||
|
template<typename TU> struct _triqs_reversed_tuple {TU _x;};
|
||||||
|
|
||||||
|
template<typename ... T> _triqs_reversed_tuple<std::tuple<T...>> reverse(std::tuple<T...> && x) { return {move(x)};}
|
||||||
|
template<typename ... T> _triqs_reversed_tuple<std::tuple<T...>&> reverse(std::tuple<T...> & x) { return {x};}
|
||||||
|
template<typename ... T> _triqs_reversed_tuple<std::tuple<T...>const &> reverse(std::tuple<T...> const & x) { return {x};}
|
||||||
|
|
||||||
|
template<int pos, typename TU> auto get(_triqs_reversed_tuple<TU> const & t) DECL_AND_RETURN(std::get<std::tuple_size<typename std::remove_reference<TU>::type>::value-1-pos>(t._x));
|
||||||
|
template<int pos, typename TU> auto get(_triqs_reversed_tuple<TU> & t) DECL_AND_RETURN(std::get<std::tuple_size<typename std::remove_reference<TU>::type>::value-1-pos>(t._x));
|
||||||
|
template<int pos, typename TU> auto get(_triqs_reversed_tuple<TU> && t) DECL_AND_RETURN(std::get<std::tuple_size<typename std::remove_reference<TU>::type>::value-1-pos>(move(t)._x));
|
||||||
|
|
||||||
|
template<typename TU> struct tuple_size<_triqs_reversed_tuple<TU>> : tuple_size<typename std::remove_reference<TU>::type>{};
|
||||||
|
}
|
||||||
|
|
||||||
namespace triqs { namespace tuple {
|
namespace triqs { namespace tuple {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -32,7 +48,7 @@ namespace triqs { namespace tuple {
|
|||||||
* push_back (t,x) -> returns new tuple with x append at the end
|
* push_back (t,x) -> returns new tuple with x append at the end
|
||||||
*/
|
*/
|
||||||
template<typename T, typename X>
|
template<typename T, typename X>
|
||||||
auto push_back(T && t, X &&x) DECL_AND_RETURN ( std::tuple_cat(std::forward<T>(t),std::make_tuple(std::forward<X>(x))));
|
auto push_back(T && t, X &&x) DECL_AND_RETURN ( std::tuple_cat(std::forward<T>(t),std::make_tuple(std::forward<X>(x))));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* t : a tuple
|
* t : a tuple
|
||||||
@ -40,9 +56,9 @@ namespace triqs { namespace tuple {
|
|||||||
* push_front (t,x) -> returns new tuple with x append at the first position
|
* push_front (t,x) -> returns new tuple with x append at the first position
|
||||||
*/
|
*/
|
||||||
template<typename T, typename X>
|
template<typename T, typename X>
|
||||||
auto push_front(T && t, X &&x) DECL_AND_RETURN ( std::tuple_cat(std::make_tuple(std::forward<X>(x)),std::forward<T>(t)));
|
auto push_front(T && t, X &&x) DECL_AND_RETURN ( std::tuple_cat(std::make_tuple(std::forward<X>(x)),std::forward<T>(t)));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* apply(f, t)
|
* apply(f, t)
|
||||||
* f : a callable object
|
* f : a callable object
|
||||||
* t a tuple
|
* t a tuple
|
||||||
@ -64,7 +80,7 @@ namespace triqs { namespace tuple {
|
|||||||
auto apply (F && f, T const & t) DECL_AND_RETURN( apply_impl<std::tuple_size<T>::value-1>()(std::forward<F>(f),t));
|
auto apply (F && f, T const & t) DECL_AND_RETURN( apply_impl<std::tuple_size<T>::value-1>()(std::forward<F>(f),t));
|
||||||
|
|
||||||
//template <typename T, typename ReturnType, typename... Args>
|
//template <typename T, typename ReturnType, typename... Args>
|
||||||
//ReturnType apply( ReturnType(*f)(Args...), T const & t) { return apply([f](Args const & ... args) { return (*f)(args...);} ,t);}
|
//ReturnType apply( ReturnType(*f)(Args...), T const & t) { return apply([f](Args const & ... args) { return (*f)(args...);} ,t);}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* apply_construct<F>(t)
|
* apply_construct<F>(t)
|
||||||
@ -207,8 +223,8 @@ namespace triqs { namespace tuple {
|
|||||||
template<int pos> struct call_on_zip3_impl {
|
template<int pos> struct call_on_zip3_impl {
|
||||||
template<typename F, typename T1, typename T2, typename T3>
|
template<typename F, typename T1, typename T2, typename T3>
|
||||||
void operator()(F && f, T1 && t1, T2 && t2, T3 && t3) {
|
void operator()(F && f, T1 && t1, T2 && t2, T3 && t3) {
|
||||||
f(std::get<pos>(std::forward<T1>(t1)),std::get<pos>(std::forward<T2>(t2)),std::get<pos>(std::forward<T3>(t3)));
|
f(std::get<pos>(std::forward<T1>(t1)),std::get<pos>(std::forward<T2>(t2)),std::get<pos>(std::forward<T3>(t3)));
|
||||||
call_on_zip3_impl<pos-1>()(std::forward<F>(f),std::forward<T1>(t1), std::forward<T2>(t2), std::forward<T3>(t3));
|
call_on_zip3_impl<pos-1>()(std::forward<F>(f),std::forward<T1>(t1), std::forward<T2>(t2), std::forward<T3>(t3));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -225,7 +241,7 @@ namespace triqs { namespace tuple {
|
|||||||
* fold(f, t1, init)
|
* fold(f, t1, init)
|
||||||
* f : a callable object
|
* f : a callable object
|
||||||
* t a tuple
|
* t a tuple
|
||||||
* Returns : f(x0,f(x1,f(....)) on the tuple
|
* Returns : f(xN,f(x_N-1,...f(x0,r)) on the tuple
|
||||||
*/
|
*/
|
||||||
template<int N, int pos, typename T> struct fold_impl {
|
template<int N, int pos, typename T> struct fold_impl {
|
||||||
template<typename F, typename R>
|
template<typename F, typename R>
|
||||||
@ -291,7 +307,7 @@ namespace triqs { namespace tuple {
|
|||||||
|
|
||||||
template<typename Tu,int ...I> using filter_t = typename filter_t_tr<Tu,I...>::type;
|
template<typename Tu,int ...I> using filter_t = typename filter_t_tr<Tu,I...>::type;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* filter_out<int ... I>(t) :
|
* filter_out<int ... I>(t) :
|
||||||
* Given a tuple t, and integers, returns the tuple where the elements at initial position I are dropped.
|
* Given a tuple t, and integers, returns the tuple where the elements at initial position I are dropped.
|
||||||
*/
|
*/
|
||||||
@ -319,7 +335,7 @@ namespace triqs { namespace tuple {
|
|||||||
|
|
||||||
template<typename Tu,int ...I> using filter_out_t = typename filter_out_t_tr<Tu,I...>::type;
|
template<typename Tu,int ...I> using filter_out_t = typename filter_out_t_tr<Tu,I...>::type;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* inverse_filter<int L, int ... I>(t,x)
|
* inverse_filter<int L, int ... I>(t,x)
|
||||||
* Given a tuple t, and integers, returns the tuple R of size L such that filter<I...>(R) == t
|
* Given a tuple t, and integers, returns the tuple R of size L such that filter<I...>(R) == t
|
||||||
* and the missing position are filled with object x.
|
* and the missing position are filled with object x.
|
||||||
@ -433,10 +449,5 @@ namespace std {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user