3
0
mirror of https://github.com/triqs/dft_tools synced 2025-01-12 14:08:24 +01:00

utility : tuple, complex_ops

- add _j notation on C++11 compliant compilers
- correct tuple fold (was working in reverse)
- add construction of mini_vector from tuple
This commit is contained in:
Olivier Parcollet 2013-07-19 13:25:31 +02:00
parent f2c7d449cc
commit aa2c52cb01
6 changed files with 43 additions and 14 deletions

View File

@ -22,6 +22,10 @@ if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
set(compiler_group 1) set(compiler_group 1)
string(REGEX REPLACE ".*([2-5]\\.[0-9]\\.[0-9]).*" "\\1" compiler_version ${_compiler_output}) string(REGEX REPLACE ".*([2-5]\\.[0-9]\\.[0-9]).*" "\\1" compiler_version ${_compiler_output})
if(compiler_version NOT VERSION_LESS "4.8.0" )
set(TRIQS_COMPILER_IS_C11_COMPLIANT ON)
endif()
elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
EXECUTE_PROCESS(COMMAND ${CMAKE_CXX_COMPILER} --version EXECUTE_PROCESS(COMMAND ${CMAKE_CXX_COMPILER} --version
@ -29,6 +33,7 @@ elseif ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
set(CMAKE_COMPILER_IS_CLANG TRUE ) set(CMAKE_COMPILER_IS_CLANG TRUE )
set(compiler_name "clang") set(compiler_name "clang")
set(compiler_group 1) set(compiler_group 1)
set(TRIQS_COMPILER_IS_C11_COMPLIANT ON)
IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
# Apple which does not has the official clang version number ... # Apple which does not has the official clang version number ...
string(REGEX REPLACE ".*LLVM ([2-5]\\.[0-9]).*" "\\1" compiler_version ${_compiler_output}) string(REGEX REPLACE ".*LLVM ([2-5]\\.[0-9]).*" "\\1" compiler_version ${_compiler_output})

View File

@ -11,6 +11,8 @@
#cmakedefine TRIQS_WORKAROUND_INTEL_COMPILER_BUGS #cmakedefine TRIQS_WORKAROUND_INTEL_COMPILER_BUGS
#cmakedefine BOOST_MATH_DISABLE_STD_FPCLASSIFY #cmakedefine BOOST_MATH_DISABLE_STD_FPCLASSIFY
#cmakedefine TRIQS_COMPILER_IS_C11_COMPLIANT
#define TRIQS_GIT_HASH @GIT_HASH@ #define TRIQS_GIT_HASH @GIT_HASH@
#define TRIQS_HOSTNAME @TRIQS_HOSTNAME@ #define TRIQS_HOSTNAME @TRIQS_HOSTNAME@

View File

@ -23,6 +23,7 @@
#include <cmath> #include <cmath>
#include <stdexcept> #include <stdexcept>
#include <functional> #include <functional>
#include <triqs/utility/mini_vector.hpp>
struct fun { struct fun {
double operator()(int i, double x, double y, int k) { return 6*k + i - 1.3*x + 2*y;} double operator()(int i, double x, double y, int k) { return 6*k + i - 1.3*x + 2*y;}
@ -97,5 +98,12 @@ int main(int argc, char **argv) {
std::cerr << " " << res << std::endl ; std::cerr << " " << res << std::endl ;
} }
{ // to mini_vector
auto t = std::make_tuple(1,2,3.4);
auto m = triqs::utility::tuple_to_mini_vector<double>(t);
std::cout << m<< std::endl ;
}
} }

View File

@ -2,16 +2,21 @@
#define TRIQS_UTILITY_COMPLEX_OPS_H #define TRIQS_UTILITY_COMPLEX_OPS_H
#include <complex> #include <complex>
#ifdef TRIQS_COMPILER_IS_C11_COMPLIANT
inline std::complex<double> operator"" _j ( long double x ) { return std::complex<double>(0,x); }
inline std::complex<double> operator"" _j ( unsigned long long x ) { return std::complex<double>(0,x); }
#endif
namespace std { // has to be in the right namespace for ADL ! namespace std { // has to be in the right namespace for ADL !
template<typename T> std::complex<T> operator+(std::complex<T> const & a, long b) { return a+T(b);} template<typename T> std::complex<T> operator+(std::complex<T> const & a, long b) { return a+T(b);}
template<typename T> std::complex<T> operator+(long a, std::complex<T> const & b) { return T(a)+b;} template<typename T> std::complex<T> operator+(long a, std::complex<T> const & b) { return T(a)+b;}
template<typename T> std::complex<T> operator-(std::complex<T> const & a, long b) { return a-T(b);} template<typename T> std::complex<T> operator-(std::complex<T> const & a, long b) { return a-T(b);}
template<typename T> std::complex<T> operator-(long a, std::complex<T> const & b) { return T(a)-b;} template<typename T> std::complex<T> operator-(long a, std::complex<T> const & b) { return T(a)-b;}
template<typename T> std::complex<T> operator*(std::complex<T> const & a, long b) { return a*T(b);} template<typename T> std::complex<T> operator*(std::complex<T> const & a, long b) { return a*T(b);}
template<typename T> std::complex<T> operator*(long a, std::complex<T> const & b) { return T(a)*b;} template<typename T> std::complex<T> operator*(long a, std::complex<T> const & b) { return T(a)*b;}
template<typename T> std::complex<T> operator/(std::complex<T> const & a, long b) { return a/T(b);} template<typename T> std::complex<T> operator/(std::complex<T> const & a, long b) { return a/T(b);}
template<typename T> std::complex<T> operator/(long a, std::complex<T> const & b) { return T(a)/b;} template<typename T> std::complex<T> operator/(long a, std::complex<T> const & b) { return T(a)/b;}
} }
#endif #endif

View File

@ -26,6 +26,7 @@
#include "./exceptions.hpp" #include "./exceptions.hpp"
#include <boost/serialization/utility.hpp> #include <boost/serialization/utility.hpp>
#include <vector> #include <vector>
#include <triqs/utility/tuple_tools.hpp>
#define TRIQS_MINI_VECTOR_NRANK_MAX 10 #define TRIQS_MINI_VECTOR_NRANK_MAX 10
@ -142,6 +143,14 @@ namespace triqs { namespace utility {
return res; return res;
} }
}}//namespace triqs::arrays struct tuple_to_mini_vector_aux { template<typename M, typename V> V * operator()(M const & m, V * v) { *v = m; return ++v;}};
template<typename T, typename ... U>
mini_vector<T,sizeof...(U)> tuple_to_mini_vector(std::tuple<U...> const & t) {
mini_vector<T,sizeof...(U)> res;
triqs::tuple::fold(tuple_to_mini_vector_aux(),t,&res[0]);
return res;
}
}}//namespace triqs::arrays
#endif #endif

View File

@ -125,20 +125,20 @@ namespace triqs { namespace tuple {
* t a tuple * t a tuple
* Returns : f(x0,f(x1,f(....)) on the tuple * Returns : f(x0,f(x1,f(....)) on the tuple
*/ */
template<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>
auto operator()(F && f, T & t, R && r ) auto operator()(F && f, T & t, R && r )
DECL_AND_RETURN( fold_impl<pos-1,T>()(std::forward<F>(f),t, f(std::get<pos>(t), std::forward<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))));
}; };
template<typename T> struct fold_impl<-1,T> { 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> 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-1,T>()(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<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> 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-1,T const>()(std::forward<F>(f),t,std::forward<R>(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) * fold_on_zip(f, t1, t2, init)