mirror of
https://github.com/triqs/dft_tools
synced 2024-12-25 05:43:40 +01:00
c14, tuples : various fixes.
- Various details (for_each_enumerate impl) - also put all tuple stuff in tuple_tools.hpp - add std14 namespace for usage of c++14 helper types
This commit is contained in:
parent
a503ddae26
commit
96ef742344
@ -23,6 +23,7 @@
|
||||
#include <cmath>
|
||||
#include <stdexcept>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <triqs/utility/mini_vector.hpp>
|
||||
|
||||
struct fun {
|
||||
@ -107,6 +108,24 @@ int main(int argc, char **argv) {
|
||||
if ( std::abs((res - 35.6)) > 1.e-13) throw std::runtime_error(" ");
|
||||
}
|
||||
|
||||
{
|
||||
// ex of real code
|
||||
auto fl = [](int i, std::string s) {
|
||||
auto r = std::to_string(i);
|
||||
return s.empty() ? r : r + "_" + s;
|
||||
};
|
||||
#ifdef __cpp_generic_lambdas
|
||||
auto _name = [fl](auto... is) {
|
||||
auto t = std::make_tuple(is...);
|
||||
return triqs::tuple::fold(fl, t, std::string{});
|
||||
};
|
||||
auto r= _name(1,2,3);
|
||||
#else
|
||||
auto r = triqs::tuple::fold(fl, reverse(std::make_tuple(1,2,3)), std::string{});
|
||||
#endif
|
||||
std::cerr << r << std::endl;
|
||||
}
|
||||
|
||||
std::cerr << " ----- apply ----"<< std::endl ;
|
||||
{
|
||||
auto res = triqs::tuple::apply(my_print_str, std::make_tuple(1,2));
|
||||
@ -182,5 +201,8 @@ int main(int argc, char **argv) {
|
||||
std::cout << "replace 1,3,5"<< t << triqs::tuple::replace<1,3,5>(t,s)<< std::endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -21,9 +21,7 @@
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <tuple>
|
||||
#include "./macros.hpp"
|
||||
#include "./tuple_tools.hpp"
|
||||
|
||||
// a few that will be C++14, use in advance....
|
||||
|
||||
@ -36,6 +34,7 @@ namespace std {
|
||||
template <class T> using remove_reference_t = typename remove_reference<T>::type;
|
||||
template <class T> using add_const_t = typename add_const<T>::type;
|
||||
template <class T> using remove_const_t = typename remove_const<T>::type;
|
||||
template <class T> using decay_t = typename decay<T>::type;
|
||||
template <bool B, class T = void> using enable_if_t = typename enable_if<B, T>::type;
|
||||
|
||||
// use simply std::c14::plus<>() ...
|
||||
@ -49,21 +48,9 @@ namespace std {
|
||||
template<typename T, typename... Args>
|
||||
std::unique_ptr<T> make_unique(Args&&... args) { return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); }
|
||||
|
||||
// a little helper class to wait for the correction that tuple construct is NOT explicit
|
||||
template<typename ... Args>
|
||||
class tuple : public std::tuple<Args...> {
|
||||
public :
|
||||
template<typename ... Args2>
|
||||
tuple(Args2 && ... args2) : std::tuple<Args...> (std::forward<Args2>(args2)...){}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
// minimal hack to get the metaprogramming work with this tuple too....
|
||||
template<int i, typename ... Args>
|
||||
auto get(c14::tuple<Args...> const & t) DECL_AND_RETURN( std::get<i>(static_cast<std::tuple<Args...>>(t)));
|
||||
|
||||
template<typename ... Args> class tuple_size<c14::tuple<Args...>>: public tuple_size<std::tuple<Args...>>{};
|
||||
|
||||
}
|
||||
|
||||
namespace std14 {
|
||||
using namespace std::c14;
|
||||
}
|
||||
|
@ -22,6 +22,7 @@
|
||||
#define TRIQS_UTILITY_TUPLE_TOOLS_H
|
||||
#include<triqs/utility/macros.hpp>
|
||||
#include <tuple>
|
||||
#include "./c14.hpp"
|
||||
#include <sstream>
|
||||
|
||||
// adding to the std lib the reversed lazy tuple...
|
||||
@ -45,6 +46,7 @@ namespace std {
|
||||
template<typename TU> class tuple_size<_triqs_reversed_tuple<TU>> : public tuple_size<typename std::remove_const<typename std::remove_reference<TU>::type>::type>{};
|
||||
}
|
||||
|
||||
|
||||
namespace triqs { namespace tuple {
|
||||
|
||||
/**
|
||||
@ -140,21 +142,21 @@ namespace triqs { namespace tuple {
|
||||
*/
|
||||
template<int pos> struct for_each_enumerate_impl {
|
||||
template<typename T, typename F>
|
||||
void operator()(T const & t, F && f) {
|
||||
f(std::get<std::tuple_size<T>::value-1-pos>(t),std::tuple_size<T>::value-1-pos);
|
||||
for_each_impl<pos-1>()(t, f);
|
||||
void operator()(T & t, F && f) {
|
||||
f(std::get<std::tuple_size<std::c14::decay_t<T>>::value-1-pos>(t),std::tuple_size<T>::value-1-pos);
|
||||
for_each_enumerate_impl<pos-1>()(t, f);
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct for_each_enumerate_impl<0> {
|
||||
template<typename T, typename F>
|
||||
void operator() (T const & t, F && f) { f(std::get<std::tuple_size<T>::value-1>(t), std::tuple_size<T>::value-1); }
|
||||
void operator() (T & t, F && f) { f(std::get<std::tuple_size<std::c14::decay_t<T>>::value-1>(t), std::tuple_size<std::c14::decay_t<T>>::value-1); }
|
||||
};
|
||||
|
||||
template<typename T, typename F>
|
||||
void for_each_enumerate(T const & t, F && f) {
|
||||
for_each_enumerate_impl<std::tuple_size<T>::value-1>()(t, f);
|
||||
void for_each_enumerate(T & t, F && f) {
|
||||
for_each_enumerate_impl<std::tuple_size<std::c14::decay_t<T>>::value-1>()(t, f);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -250,18 +252,18 @@ namespace triqs { namespace tuple {
|
||||
*/
|
||||
template<int N, int pos, typename T> struct fold_impl {
|
||||
template<typename F, typename R>
|
||||
auto operator()(F && f, T & t, R && r )
|
||||
DECL_AND_RETURN( fold_impl<N,pos-1,T>()(std::forward<F>(f),t, f(std::get<N-1-pos>(t), std::forward<R>(r))));
|
||||
auto operator()(F && f, T && t, R && r )
|
||||
DECL_AND_RETURN( fold_impl<N,pos-1,T>()(std::forward<F>(f),std::forward<T>(t), f(std::get<N-1-pos>(t), std::forward<R>(r))));
|
||||
};
|
||||
|
||||
template<int N, typename T> struct fold_impl<N, -1,T> {
|
||||
template<typename F, typename R> R operator()(F && f, T & t, R && r) {return std::forward<R>(r);}
|
||||
template<typename F, typename R> R operator()(F && f, T && t, R && r) {return std::forward<R>(r);}
|
||||
};
|
||||
|
||||
template<typename F, typename T, typename R>
|
||||
auto fold (F && f,T & t, R && r) DECL_AND_RETURN( fold_impl<std::tuple_size<T>::value,std::tuple_size<T>::value-1,T>()(std::forward<F>(f),t,std::forward<R>(r)));
|
||||
template<typename F, typename T, typename R>
|
||||
auto fold (F && f,T const & t, R && r) DECL_AND_RETURN( fold_impl<std::tuple_size<T>::value,std::tuple_size<T>::value-1,T const>()(std::forward<F>(f),t,std::forward<R>(r)));
|
||||
auto fold (F && f,T && t, R && r) DECL_AND_RETURN( fold_impl<std::tuple_size<std::c14::decay_t<T>>::value,std::tuple_size<std::c14::decay_t<T>>::value-1,T>()(std::forward<F>(f),std::forward<T>(t),std::forward<R>(r)));
|
||||
//template<typename F, typename T, typename R>
|
||||
// auto fold (F && f,T const & t, R && r) DECL_AND_RETURN( fold_impl<std::tuple_size<T>::value,std::tuple_size<T>::value-1,T const>()(std::forward<F>(f),t,std::forward<R>(r)));
|
||||
|
||||
/**
|
||||
* fold_on_zip(f, t1, t2, init)
|
||||
@ -451,5 +453,23 @@ namespace std {
|
||||
}
|
||||
}
|
||||
|
||||
namespace std {
|
||||
namespace c14 {
|
||||
|
||||
// a little helper class to wait for the correction that tuple construct is NOT explicit
|
||||
template <typename... Args> class tuple : public std::tuple<Args...> {
|
||||
public:
|
||||
template <typename... Args2> tuple(Args2 &&... args2) : std::tuple<Args...>(std::forward<Args2>(args2)...) {}
|
||||
};
|
||||
}
|
||||
|
||||
// minimal hack to get the metaprogramming work with this tuple too....
|
||||
template <int i, typename... Args>
|
||||
auto get(c14::tuple<Args...> const &t) DECL_AND_RETURN(std::get<i>(static_cast<std::tuple<Args...>>(t)));
|
||||
|
||||
template <typename... Args> class tuple_size<c14::tuple<Args...>> : public tuple_size<std::tuple<Args...>> {};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user