mirror of
https://github.com/triqs/dft_tools
synced 2024-10-31 19:23:45 +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 {
|
||||
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();}
|
||||
@ -61,6 +61,11 @@ int main(int argc, char **argv) {
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
|
||||
{
|
||||
triqs::tuple::for_each(reverse(t), print_t());
|
||||
std::cerr << std::endl;
|
||||
}
|
||||
|
||||
{
|
||||
auto res = triqs::tuple::apply(fun(),t);
|
||||
std::cerr << " f(t) =" << res << std::endl ;
|
||||
@ -76,21 +81,38 @@ int main(int argc, char **argv) {
|
||||
<< 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);
|
||||
std::cerr << " " << res << std::endl ;
|
||||
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);
|
||||
std::cerr << " " << res << std::endl ;
|
||||
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));
|
||||
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=(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);}
|
||||
|
||||
|
@ -24,6 +24,22 @@
|
||||
#include <tuple>
|
||||
#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 {
|
||||
|
||||
/**
|
||||
@ -42,7 +58,7 @@ namespace triqs { namespace tuple {
|
||||
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)));
|
||||
|
||||
/**
|
||||
/**
|
||||
* apply(f, t)
|
||||
* f : a callable object
|
||||
* t a tuple
|
||||
@ -225,7 +241,7 @@ namespace triqs { namespace tuple {
|
||||
* fold(f, t1, init)
|
||||
* f : a callable object
|
||||
* 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<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;
|
||||
|
||||
/**
|
||||
/**
|
||||
* filter_out<int ... I>(t) :
|
||||
* Given a tuple t, and integers, returns the tuple where the elements at initial position I are dropped.
|
||||
*/
|
||||
@ -433,10 +449,5 @@ namespace std {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user