From aa2c52cb01dceb245efe25b967e395b6e65f9387 Mon Sep 17 00:00:00 2001 From: Olivier Parcollet Date: Fri, 19 Jul 2013 13:25:31 +0200 Subject: [PATCH] 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 --- cmake/FindCompilerCheck.cmake | 5 +++++ cmake/triqs_config.h.in | 2 ++ test/triqs/utility/tuple_tools.cpp | 8 ++++++++ triqs/utility/complex_ops.hpp | 21 +++++++++++++-------- triqs/utility/mini_vector.hpp | 11 ++++++++++- triqs/utility/tuple_tools.hpp | 10 +++++----- 6 files changed, 43 insertions(+), 14 deletions(-) diff --git a/cmake/FindCompilerCheck.cmake b/cmake/FindCompilerCheck.cmake index ea676b77..b5f4d19d 100644 --- a/cmake/FindCompilerCheck.cmake +++ b/cmake/FindCompilerCheck.cmake @@ -22,6 +22,10 @@ if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") set(compiler_group 1) 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") 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(compiler_name "clang") set(compiler_group 1) + set(TRIQS_COMPILER_IS_C11_COMPLIANT ON) IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") # Apple which does not has the official clang version number ... string(REGEX REPLACE ".*LLVM ([2-5]\\.[0-9]).*" "\\1" compiler_version ${_compiler_output}) diff --git a/cmake/triqs_config.h.in b/cmake/triqs_config.h.in index 48604c75..a4eba415 100644 --- a/cmake/triqs_config.h.in +++ b/cmake/triqs_config.h.in @@ -11,6 +11,8 @@ #cmakedefine TRIQS_WORKAROUND_INTEL_COMPILER_BUGS #cmakedefine BOOST_MATH_DISABLE_STD_FPCLASSIFY +#cmakedefine TRIQS_COMPILER_IS_C11_COMPLIANT + #define TRIQS_GIT_HASH @GIT_HASH@ #define TRIQS_HOSTNAME @TRIQS_HOSTNAME@ diff --git a/test/triqs/utility/tuple_tools.cpp b/test/triqs/utility/tuple_tools.cpp index 6d4d4e18..be8e4ec3 100644 --- a/test/triqs/utility/tuple_tools.cpp +++ b/test/triqs/utility/tuple_tools.cpp @@ -23,6 +23,7 @@ #include #include #include +#include struct fun { 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 ; } + { // to mini_vector + + auto t = std::make_tuple(1,2,3.4); + auto m = triqs::utility::tuple_to_mini_vector(t); + std::cout << m<< std::endl ; + + } } diff --git a/triqs/utility/complex_ops.hpp b/triqs/utility/complex_ops.hpp index b88220d4..843ed64e 100644 --- a/triqs/utility/complex_ops.hpp +++ b/triqs/utility/complex_ops.hpp @@ -2,16 +2,21 @@ #define TRIQS_UTILITY_COMPLEX_OPS_H #include +#ifdef TRIQS_COMPILER_IS_C11_COMPLIANT +inline std::complex operator"" _j ( long double x ) { return std::complex(0,x); } +inline std::complex operator"" _j ( unsigned long long x ) { return std::complex(0,x); } +#endif + namespace std { // has to be in the right namespace for ADL ! -template std::complex operator+(std::complex const & a, long b) { return a+T(b);} -template std::complex operator+(long a, std::complex const & b) { return T(a)+b;} -template std::complex operator-(std::complex const & a, long b) { return a-T(b);} -template std::complex operator-(long a, std::complex const & b) { return T(a)-b;} -template std::complex operator*(std::complex const & a, long b) { return a*T(b);} -template std::complex operator*(long a, std::complex const & b) { return T(a)*b;} -template std::complex operator/(std::complex const & a, long b) { return a/T(b);} -template std::complex operator/(long a, std::complex const & b) { return T(a)/b;} + template std::complex operator+(std::complex const & a, long b) { return a+T(b);} + template std::complex operator+(long a, std::complex const & b) { return T(a)+b;} + template std::complex operator-(std::complex const & a, long b) { return a-T(b);} + template std::complex operator-(long a, std::complex const & b) { return T(a)-b;} + template std::complex operator*(std::complex const & a, long b) { return a*T(b);} + template std::complex operator*(long a, std::complex const & b) { return T(a)*b;} + template std::complex operator/(std::complex const & a, long b) { return a/T(b);} + template std::complex operator/(long a, std::complex const & b) { return T(a)/b;} } #endif diff --git a/triqs/utility/mini_vector.hpp b/triqs/utility/mini_vector.hpp index ae866ffb..4f1d31f5 100644 --- a/triqs/utility/mini_vector.hpp +++ b/triqs/utility/mini_vector.hpp @@ -26,6 +26,7 @@ #include "./exceptions.hpp" #include #include +#include #define TRIQS_MINI_VECTOR_NRANK_MAX 10 @@ -142,6 +143,14 @@ namespace triqs { namespace utility { return res; } - }}//namespace triqs::arrays + struct tuple_to_mini_vector_aux { template V * operator()(M const & m, V * v) { *v = m; return ++v;}}; + template + mini_vector tuple_to_mini_vector(std::tuple const & t) { + mini_vector res; + triqs::tuple::fold(tuple_to_mini_vector_aux(),t,&res[0]); + return res; + } + +}}//namespace triqs::arrays #endif diff --git a/triqs/utility/tuple_tools.hpp b/triqs/utility/tuple_tools.hpp index d9a24866..ea637f5f 100644 --- a/triqs/utility/tuple_tools.hpp +++ b/triqs/utility/tuple_tools.hpp @@ -125,20 +125,20 @@ namespace triqs { namespace tuple { * t a tuple * Returns : f(x0,f(x1,f(....)) on the tuple */ - template struct fold_impl { + template struct fold_impl { template auto operator()(F && f, T & t, R && r ) - DECL_AND_RETURN( fold_impl()(std::forward(f),t, f(std::get(t), std::forward(r)))); + DECL_AND_RETURN( fold_impl()(std::forward(f),t, f(std::get(t), std::forward(r)))); }; - template struct fold_impl<-1,T> { + template struct fold_impl { template R operator()(F && f, T & t, R && r) {return std::forward(r);} }; template - auto fold (F && f,T & t, R && r) DECL_AND_RETURN( fold_impl::value-1,T>()(std::forward(f),t,std::forward(r))); + auto fold (F && f,T & t, R && r) DECL_AND_RETURN( fold_impl::value,std::tuple_size::value-1,T>()(std::forward(f),t,std::forward(r))); template - auto fold (F && f,T const & t, R && r) DECL_AND_RETURN( fold_impl::value-1,T const>()(std::forward(f),t,std::forward(r))); + auto fold (F && f,T const & t, R && r) DECL_AND_RETURN( fold_impl::value,std::tuple_size::value-1,T const>()(std::forward(f),t,std::forward(r))); /** * fold_on_zip(f, t1, t2, init)