/******************************************************************************* * * TRIQS: a Toolbox for Research in Interacting Quantum Systems * * Copyright (C) 2012 by M. Ferrero, O. Parcollet * * TRIQS is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * TRIQS is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * TRIQS. If not, see . * ******************************************************************************/ #ifndef TRIQS_STD_VECTOR_EXPR_TEMPL_H #define TRIQS_STD_VECTOR_EXPR_TEMPL_H #include #include #include #include #include namespace triqs { namespace utility { // a trait to identify the std::vector ... TRIQS_DEFINE_CONCEPT_AND_ASSOCIATED_TRAIT(ImmutableStdVector); template struct ImmutableStdVector> : std::true_type{}; namespace expr_temp_vec_tools { template struct scalar_wrap {// S can be a T or a T & typedef typename std::remove_reference::type value_type; S s; template scalar_wrap(T && x):s(std::forward(x)){} S operator[](size_t i) const {return s;} friend std::ostream &operator <<(std::ostream &sout, scalar_wrap const &expr){return sout << expr.s;} }; // Combine the two sizes of LHS and RHS : need to specialize where there is a scalar struct combine_size { template size_t operator() (L const & l, R const & r) const { if (!(l.size() == r.size())) TRIQS_RUNTIME_ERROR << "size mismatch : ";//<< l.size()<<" vs" < size_t operator() (scalar_wrap const &, R const & r) const {return r.size();} template size_t operator() (L const & l, scalar_wrap const &) const {return l.size();} }; template struct node_t : std::conditional::value, scalar_wrap, typename remove_rvalue_ref::type> {}; template auto make_vector(V const & v) -> std::vector::type> { //auto make_vector(V const & v) -> std::vector(v)[0])>::type> { std::vector::type> res; std::cout << "makeVector"<< std::endl; res.reserve(v.size()); for (size_t i =0; i struct std_vec_expr : triqs::utility::ImmutableStdVector__concept_tag { L l; R r; template std_vec_expr(LL && l_, RR && r_) : l(std::forward(l_)), r(std::forward(r_)) {} std_vec_expr(std_vec_expr const &) = delete; std_vec_expr(std_vec_expr &&) = default; size_t size() const { return triqs::utility::expr_temp_vec_tools::combine_size()(l,r); } auto operator[](size_t i) const DECL_AND_RETURN(triqs::utility::operation()(l[i] , r[i])); friend std::ostream &operator <<(std::ostream &sout, std_vec_expr const &expr){return sout << "("<::name <<" "< auto make_vector(std_vec_expr const & v) DECL_AND_RETURN(triqs::utility::expr_temp_vec_tools::make_vector(v)); // ------------------------------------------------------------------- //a special case : the unary operator ! template struct std_vec_expr_unary : triqs::utility::ImmutableStdVector__concept_tag { L l; template std_vec_expr_unary(LL && l_) : l(std::forward(l_)) {} std_vec_expr_unary(std_vec_expr_unary const &) = delete; std_vec_expr_unary(std_vec_expr_unary &&) = default; size_t size() const { return l.size(); } auto operator[](size_t i) const DECL_AND_RETURN( -l[i]); friend std::ostream &operator <<(std::ostream &sout, std_vec_expr_unary const &expr){return sout << '-'< auto make_vector(std_vec_expr_unary const & v) DECL_AND_RETURN(triqs::utility::expr_temp_vec_tools::make_vector(v)); // ------------------------------------------------------------------- // Now we can define all the C++ operators ... #define DEFINE_OPERATOR(TAG, OP, TRAIT1, TRAIT2) \ template\ typename std::enable_if::value && triqs::utility::TRAIT2 ::value, \ std_vec_expr::type, typename triqs::utility::expr_temp_vec_tools::node_t::type>>::type\ operator OP (A1 && a1, A2 && a2) { return {std::forward(a1),std::forward(a2)};} DEFINE_OPERATOR(plus, +, ImmutableStdVector,ImmutableStdVector); DEFINE_OPERATOR(minus, -, ImmutableStdVector,ImmutableStdVector); DEFINE_OPERATOR(multiplies, *, ImmutableStdVector,ImmutableStdVector); DEFINE_OPERATOR(multiplies, *, is_in_ZRC,ImmutableStdVector); DEFINE_OPERATOR(multiplies, *, ImmutableStdVector,is_in_ZRC); DEFINE_OPERATOR(divides, /, ImmutableStdVector,ImmutableStdVector); DEFINE_OPERATOR(divides, /, is_in_ZRC,ImmutableStdVector); DEFINE_OPERATOR(divides, /, ImmutableStdVector,is_in_ZRC); #undef DEFINE_OPERATOR // the unary is special template typename std::enable_if::value, std_vec_expr_unary>::type operator - (A1 const & a1) { return std_vec_expr_unary(a1);} } /* namespace triqs { namespace utility { namespace expr_temp_vec_tools { //make sure expression are never taken by const & template struct expr_node_storage_t &> ;// { typedef std::std_vec_expr type;}; template struct expr_node_storage_t const &> ;//{ typedef std::std_vec_expr type;}; template struct expr_node_storage_t &> ;// { typedef std::std_vec_expr_unary type;}; template struct expr_node_storage_t const &>;// { typedef std::std_vec_expr_unary type;}; }// std_vec_expr_tools }} */ #endif