/*============================================================================= This file is part of FLINT. FLINT 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 2 of the License, or (at your option) any later version. FLINT 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 FLINT; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA =============================================================================*/ /****************************************************************************** Copyright (C) 2013 Tom Bachmann ******************************************************************************/ #ifndef CXX_EXPRESSION_H #define CXX_EXPRESSION_H // TODO // * static asserts #include #include #include #include "evaluation_tools.h" #include "expression_traits.h" #include "mp.h" #include "rules.h" #include "traits.h" #include "tuple.h" namespace flint { namespace detail { // Helper traits used by the "expression" class, in particular the evaluate() // method. This is the general (i.e. non-immediate) case, // which requires actual work. template struct evaluation_traits { typedef typename Expr::derived_t derived_t; typedef typename mp::find_evaluation< Operation, Data, false>::type rule_t; typedef typename mp::find_evaluation< Operation, Data, true>::type temp_rule_t; typedef typename rule_t::return_t evaluation_return_t; typedef evaluation_return_t evaluated_t; static evaluation_return_t evaluate(const derived_t& from) { evaluated_t res = rules::instantiate_temporaries::get(from); evaluate_into_fresh(res, from); return res; } template static void evaluate_into(T& to, const derived_t& from) { typedef mp::back_tuple back_t; typename back_t::type temps_backing = mp::htuples::fill( tools::temporaries_filler(from)); typename rule_t::temporaries_t temps; back_t::init(temps, temps_backing, 0); rule_t::doit(from._data(), temps, &to); } static void evaluate_into_fresh(evaluation_return_t& to, const derived_t& from) { typedef mp::back_tuple< typename temp_rule_t::temporaries_t, evaluation_return_t > back_t; typename back_t::type temps_backing = mp::htuples::fill( tools::temporaries_filler(from)); typename temp_rule_t::temporaries_t temps; back_t::init(temps, temps_backing, &to); temp_rule_t::doit(from._data(), temps, &to); } }; // This is the special case of an immediate argument, where "evaluation" is // at most assignment. template struct evaluation_traits { typedef typename Expr::derived_t derived_t; typedef typename Expr::derived_t evaluated_t; typedef evaluated_t& evaluation_return_t; static evaluated_t& evaluate(derived_t& d) {return d;} static const evaluated_t& evaluate(const derived_t& d) {return d;} template static void evaluate_into(T& to, const derived_t& from) { rules::assignment::doit(to, from); } static void evaluate_into_fresh(derived_t& to, const derived_t& from) { evaluate_into(to, from); } }; } // detail // The main expression template class. // // The argument Derived must have the following form: // struct derived // { // template // struct type // { // typedef XYZ result; // }; // }; // See derived_wrapper below for a common example. // // Note that, while Data does not have to be default constructible, // it *does* need to be copy-constructible, and have a working destructor. template class expression { private: Data data; protected: explicit expression(const Data& d) : data(d) {} public: // internal -- see is_expression implementation. typedef void IS_EXPRESSION_MARKER; typedef detail::evaluation_traits ev_traits_t; typedef typename Derived::template type::result derived_t; typedef typename ev_traits_t::evaluated_t evaluated_t; typedef typename ev_traits_t::evaluation_return_t evaluation_return_t; typedef Data data_t; typedef Operation operation_t; private: derived_t& downcast() {return *static_cast(this);} const derived_t& downcast() const { return *static_cast(this); } // Some helpers for initialization, since it is not possible to // conditionally enable constructors in C++98 template static data_t get_data(const T& t, typename mp::disable_if >::type* = 0) { return data_t(t); } template static data_t get_data(T& t, typename mp::disable_if >::type* = 0) { return data_t(t); } template static data_t get_data(const T& t, typename mp::enable_if >::type* = 0, typename mp::disable_if< mp::equal_types >::type* = 0) { return data_t(t.evaluate()); } template static data_t get_data(const T& t, typename mp::enable_if >::type* = 0, typename mp::enable_if< mp::equal_types >::type* = 0) { return data_t(t.evaluate()._data()); } // Invoke the data copy constructor when appropriate static data_t get_data(const derived_t& o) { return data_t(o._data()); } static data_t get_data(derived_t& o) { return data_t(o._data()); } // Having the empty constructor here delays its instantiation, and allows // compiling even if data is *not* default constructible. static data_t get_data() {return data_t();} public: // forwarded constructors template explicit expression(const T& t) : data(get_data(t)) {} template explicit expression(T& t) : data(get_data(t)) {} template expression(const T& t, const U& u) : data(t, u) {} template expression(T& t, const U& u) : data(t, u) {} template expression(const T& t, const U& u, const V& v) : data(t, u, v) {} template expression(T& t, const U& u, const V& v) : data(t, u, v) {} template expression(const T& t, const U& u, const V& v, const W& w) : data(t, u, v, w) {} template expression(T& t, const U& u, const V& v, const W& w) : data(t, u, v, w) {} expression() : data(get_data()) {} expression& operator=(const expression& o) { this->set(o.downcast()); return *this; } // See rules::instantiate_temporaries for explanation. evaluated_t create_temporary() const { return evaluated_t(); } Data& _data() {return data;} const Data& _data() const {return data;} void print(std::ostream& o) const { tools::print_using_str::doit(evaluate(), o); } std::string to_string(int base = 10) const { return rules::to_string::get(evaluate(), base); } template T to() const { return rules::conversion::get(evaluate()); } int print(FILE* f = stdout) const { return rules::cprint::doit(f, evaluate()); } int print_pretty(FILE* f = stdout) const { return rules::print_pretty::doit(f, evaluate()); } template int print_pretty(const T& extra, FILE* f = stdout) const { return rules::print_pretty::doit(f, evaluate(), extra); } int read(FILE* f = stdin) { return rules::read::doit(f, downcast()); } typename traits::make_const::type evaluate() const { return ev_traits_t::evaluate(downcast()); } evaluation_return_t evaluate() {return ev_traits_t::evaluate(downcast());} template void set(const T& t, typename mp::enable_if >::type* = 0, typename mp::enable_if >::type* = 0) { T::ev_traits_t::evaluate_into(downcast(), t); } template void set(const T& t, typename mp::enable_if >::type* = 0, typename mp::disable_if >::type* = 0) { rules::assignment::doit( downcast(), t.evaluate()); } template void set(const T& t, typename mp::disable_if >::type* = 0) { rules::assignment::doit(downcast(), t); } template bool equals(const T& t, typename mp::enable_if >::type* = 0) const { return equals(t.evaluate()); } template bool equals(const T& t, typename mp::disable_if >::type* = 0) const { return tools::equals_using_cmp::get(evaluate(), t); } template struct make_helper { typedef typename Derived::template type::result type; static type make(const NData& ndata) { return type(ndata); } }; }; // If your expression template is of the form // template // class my_expression ... // then derived_wrapper is a valid argument for Derived in // the expression class above. template class Derived> struct derived_wrapper { template struct type { typedef Derived result; }; }; // If your expression template is of the form // template // class my_expression2 ... // where Extra is some extra information which should be passed on unchanged, // then derived_wrapper2 is a valid argument for Derived // in the expression class above. template class Derived, class Extra> struct derived_wrapper2 { template struct type { typedef Derived result; }; }; // operators namespace detail { // These traits determine how arguments of an expression template are stored. // E.g. (e1 + e2) yields a new expression template with a two-argument tuple // as Data. If e1 is an immediate, then we want to (usually) store it by // reference, to avoid copies. If not, we can just store by value (since // copying e1 just copies the references anyway) and avoid indirection. // (Similarly for e2.) template struct storage_traits : mp::if_< traits::is_immediate, typename traits::forwarding::type, Expr > { }; // See tuple.h. template<> struct storage_traits {typedef detail::UNUSED type;}; template struct nary_op_helper_step2 { typedef typename ev_t::return_t Expr; typedef typename Expr::template make_helper make_helper; typedef typename make_helper::type return_t; }; template struct nary_op_helper_step2 { struct return_t { }; struct make_helper { }; }; // Helper to determine the return type of an expression, where Data is already // the correct tuple type. // The step1/step2 splitting above is necessary to avoid compiler errors in // case there is not actually any rule. template struct nary_op_helper { typedef typename mp::find_evaluation::type ev_t; typedef nary_op_helper_step2 nohs2; typedef typename nohs2::return_t return_t; typedef typename nohs2::make_helper make_helper; typedef traits::is_implemented cond; typedef mp::enable_if enable; }; template struct nary_op_helper_maker : nary_op_helper { typedef Maker maker; }; #define FLINTXX_NARY_OP_HELPER_MACRO(arg) typename storage_traits< arg >::type // nary_op_helper invokes nary_op_helper with the correct // tuple type as argument. template struct nary_op_helper2 : nary_op_helper_maker > { typedef nary_op_helper2 noh2; static typename noh2::return_t make(FLINTXX_MAKE_TUPLE_FUNC_ARGS) { return noh2::make_helper::make(noh2::maker::make( FLINTXX_MAKE_TUPLE_FUNC_ARG_NAMES)); } }; // Special casing for binary operators. template struct binary_op_helper : nary_op_helper2 { }; // Special casing for unary operations. template struct unary_op_helper : nary_op_helper2 { }; // For unary member operators, determining the return type the normal way can // lead to cyclic dependencies. See FLINTXX_DEFINE_MEMBER_UNOP_RTYPE. template struct unary_op_helper_with_rettype { typedef mp::make_tuple::type> maker; typedef typename Ret::template make_helper< Op, typename maker::type>::type return_t; }; // Common helper for implementing comparison operators. template struct order_op_helper { typedef typename tools::evaluation_helper::type ev1_t; typedef typename tools::evaluation_helper::type ev2_t; typedef tools::symmetric_cmp scmp; typedef mp::enable_if< mp::and_< traits::is_implemented, mp::or_< traits::is_expression, traits::is_expression > >, bool> enable; static int get(const Expr1& e1, const Expr2& e2) { return scmp::get(tools::evaluation_helper::get(e1), tools::evaluation_helper::get(e2)); } }; } // detail template inline typename mp::enable_if, std::ostream&>::type operator<<(std::ostream& o, const Expr& e) { e.print(o); return o; } template inline typename mp::enable_if, bool>::type operator==(const Expr1& e1, const Expr2& e2) { return e1.equals(e2); } template inline typename mp::enable_if >, traits::is_expression >, bool>::type operator==(const Expr1& e1, const Expr2& e2) { return e2.equals(e1); } template inline typename mp::enable_if, traits::is_expression >, bool>::type operator!=(const Expr1& e1, const Expr2& e2) { return !(e1 == e2); } template inline typename detail::order_op_helper::enable::type operator<(const Expr1& e1, const Expr2& e2) { return detail::order_op_helper::get(e1, e2) < 0; } template inline typename detail::order_op_helper::enable::type operator<=(const Expr1& e1, const Expr2& e2) { return detail::order_op_helper::get(e1, e2) <= 0; } template inline typename detail::order_op_helper::enable::type operator>(const Expr1& e1, const Expr2& e2) { return detail::order_op_helper::get(e1, e2) > 0; } template inline typename detail::order_op_helper::enable::type operator>=(const Expr1& e1, const Expr2& e2) { return detail::order_op_helper::get(e1, e2) >= 0; } template inline typename detail::binary_op_helper< Expr1, operations::plus, Expr2>::enable::type operator+(const Expr1& e1, const Expr2& e2) { return detail::binary_op_helper::make(e1, e2); } template inline typename detail::binary_op_helper< Expr1, operations::minus, Expr2>::enable::type operator-(const Expr1& e1, const Expr2& e2) { return detail::binary_op_helper::make(e1, e2); } template inline typename detail::binary_op_helper< Expr1, operations::times, Expr2>::enable::type operator*(const Expr1& e1, const Expr2& e2) { return detail::binary_op_helper::make(e1, e2); } template inline typename detail::binary_op_helper< Expr1, operations::divided_by, Expr2>::enable::type operator/(const Expr1& e1, const Expr2& e2) { return detail::binary_op_helper::make(e1, e2); } template inline typename detail::binary_op_helper< Expr1, operations::modulo, Expr2>::enable::type operator%(const Expr1& e1, const Expr2& e2) { return detail::binary_op_helper::make(e1, e2); } template inline typename detail::binary_op_helper< Expr1, operations::binary_and, Expr2>::enable::type operator&(const Expr1& e1, const Expr2& e2) { return detail::binary_op_helper::make(e1, e2); } template inline typename detail::binary_op_helper< Expr1, operations::binary_or, Expr2>::enable::type operator|(const Expr1& e1, const Expr2& e2) { return detail::binary_op_helper::make(e1, e2); } template inline typename detail::binary_op_helper< Expr1, operations::binary_xor, Expr2>::enable::type operator^(const Expr1& e1, const Expr2& e2) { return detail::binary_op_helper::make(e1, e2); } template inline typename detail::binary_op_helper< Expr1, operations::shift, Expr2>::enable::type operator<<(const Expr1& e1, const Expr2& e2) { return detail::binary_op_helper::make(e1, e2); } template inline typename detail::binary_op_helper< Expr1, operations::shift, Expr2>::enable::type operator>>(const Expr1& e1, const Expr2& e2) { return detail::binary_op_helper::make(e1, -e2); } template inline typename detail::unary_op_helper::enable::type operator-(const Expr& e) { return detail::unary_op_helper::make(e); } template inline typename detail::unary_op_helper::enable::type operator~(const Expr& e) { return detail::unary_op_helper::make(e); } template inline typename mp::enable_if, Expr1&>::type operator+=(Expr1& e1, const Expr2& e2) { e1.set(e1 + e2); return e1; } template inline typename mp::enable_if, Expr1&>::type operator-=(Expr1& e1, const Expr2& e2) { e1.set(e1 - e2); return e1; } template inline typename mp::enable_if, Expr1&>::type operator*=(Expr1& e1, const Expr2& e2) { e1.set(e1 * e2); return e1; } template inline typename mp::enable_if, Expr1&>::type operator/=(Expr1& e1, const Expr2& e2) { e1.set(e1 / e2); return e1; } template inline typename mp::enable_if, Expr1&>::type operator%=(Expr1& e1, const Expr2& e2) { e1.set(e1 % e2); return e1; } template inline typename mp::enable_if, Expr1&>::type operator|=(Expr1& e1, const Expr2& e2) { e1.set(e1 | e2); return e1; } template inline typename mp::enable_if, Expr1&>::type operator&=(Expr1& e1, const Expr2& e2) { e1.set(e1 & e2); return e1; } template inline typename mp::enable_if, Expr1&>::type operator^=(Expr1& e1, const Expr2& e2) { e1.set(e1 ^ e2); return e1; } // c-style IO template typename mp::enable_if >, int>::type print(const T& t) { return t.print(); } template typename mp::enable_if >, int>::type print(FILE* f, const T& t) { return t.print(f); } template typename mp::enable_if >, int>::type print_pretty(const T& t, const U& extra) { return t.print_pretty(extra); } template typename mp::enable_if >, int>::type print_pretty(FILE* f, const T& t, const U& extra) { return t.print_pretty(extra, f); } template typename mp::enable_if >, int>::type print_pretty(const T& t) { return t.print_pretty(); } template typename mp::enable_if >, int>::type print_pretty(FILE* f, const T& t) { return t.print_pretty(f); } template typename mp::enable_if >, int>::type read(T& t) { return t.read(); } template typename mp::enable_if >, int>::type read(FILE* f, T& t) { return t.read(f); } // TODO move to std? template inline typename mp::enable_if > >::type swap(Expr1& e1, Expr2& e2) { rules::swap::doit(e1, e2); } } // TODO remove this? #include "default_rules.h" //////////////////////////////////////////////////////////////////////// // HELPER MACROS //////////////////////////////////////////////////////////////////////// // To be called in any namespace // Make the binary operation "name" available in current namespace #define FLINT_DEFINE_BINOP_HERE(name) \ template \ inline typename ::flint::detail::binary_op_helper<\ T1, ::flint::operations::name##_op, T2>::enable::type \ name(const T1& t1, const T2& t2) \ { \ return ::flint::detail::binary_op_helper< \ T1, ::flint::operations::name##_op, T2>::make(t1, t2); \ } // Make the unary operation "name" available in current namespace #define FLINT_DEFINE_UNOP_HERE(name) \ template \ inline typename ::flint::detail::unary_op_helper<\ ::flint::operations::name##_op, T1>::enable::type \ name(const T1& t1) \ { \ return ::flint::detail::unary_op_helper< ::flint::operations::name##_op, T1>::make(t1); \ } // Make the threeary operation "name" available in current namespace #define FLINT_DEFINE_THREEARY_HERE(name) \ template \ inline typename ::flint::detail::nary_op_helper2<\ ::flint::operations::name##_op, T1, T2, T3>::enable::type \ name(const T1& t1, const T2& t2, const T3& t3) \ { \ return ::flint::detail::nary_op_helper2< \ ::flint::operations::name##_op, T1, T2, T3>::make(t1, t2, t3); \ } // Make the threeary operation "name" available in current namespace, // but with only two arguments, the second of which is of type type1 and // defaults to val1, and the third argument always (implicitly) of type type2 // and value val2. // The suggested usage of this macro is to first call FLINT_DEFINE_THREEARY_HERE, // and then call FLINT_DEFINE_THREEARY_HERE_2DEFAULT. The effect will be an // operation which can be invoked with 1, 2 or 3 arguments. #define FLINT_DEFINE_THREEARY_HERE_2DEFAULT(name, type1, val1, type2, val2) \ template \ inline typename ::flint::detail::nary_op_helper2<\ ::flint::operations::name##_op, T1, type1, type2 >::enable::type \ name(const T1& t1, type1 t2 = val1) \ { \ return ::flint::detail::nary_op_helper2< \ ::flint::operations::name##_op, T1, type1, type2>::make(t1, t2, val2); \ } // Make the fourary operation "name" available in current namespace #define FLINT_DEFINE_FOURARY_HERE(name) \ template \ inline typename ::flint::detail::nary_op_helper2<\ ::flint::operations::name##_op, T1, T2, T3, T4>::enable::type \ name(const T1& t1, const T2& t2, const T3& t3, const T4& t4) \ { \ return ::flint::detail::nary_op_helper2< \ ::flint::operations::name##_op, T1, T2, T3, T4>::make(t1, t2, t3, t4); \ } // Make the fiveary operation "name" available in current namespace #define FLINT_DEFINE_FIVEARY_HERE(name) \ template \ inline typename ::flint::detail::nary_op_helper2<\ ::flint::operations::name##_op, T1, T2, T3, T4, T5>::enable::type \ name(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5) \ { \ return ::flint::detail::nary_op_helper2< \ ::flint::operations::name##_op, T1, T2, T3, T4, T5>::make(t1, t2, t3, t4, t5); \ } // Make the sixary operation "name" available in current namespace #define FLINT_DEFINE_SIXARY_HERE(name) \ template \ inline typename ::flint::detail::nary_op_helper2<\ ::flint::operations::name##_op, T1, T2, T3, T4, T5, T6>::enable::type \ name(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6) \ { \ return ::flint::detail::nary_op_helper2< \ ::flint::operations::name##_op, T1, T2, T3, T4, T5, T6>::make(t1, t2, t3, t4, t5, t6); \ } // Make the sevenary operation "name" available in current namespace #define FLINT_DEFINE_SEVENARY_HERE(name) \ template \ inline typename ::flint::detail::nary_op_helper2<\ ::flint::operations::name##_op, T1, T2, T3, T4, T5, T6, T7>::enable::type \ name(const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7) \ { \ return ::flint::detail::nary_op_helper2< \ ::flint::operations::name##_op, T1, T2, T3, T4, T5, T6, T7>::make(t1, t2, t3, t4, t5, t6, t7); \ } // This set of macros should be called in namespace flint. // Introduce a new binary operation called "name" // NB: because of ADL bugs in g++ <= 4.4, the operation tag is called "name_op", // whereas the function corresponding to it is just called "name" #define FLINT_DEFINE_BINOP(name) \ namespace operations { \ struct name##_op { }; \ } \ FLINT_DEFINE_BINOP_HERE(name) // This macro can be used to conditionally enable a function, and is mostly // used for forwarding. // A typical usage is // template // FLINT_BINOP_ENABLE_RETTYPE(myop, T, U) myop_other(const T& t, const U& u) // { // // perhaps something more interesting // return myop(t, u); // } #define FLINT_BINOP_ENABLE_RETTYPE(name, T1, T2) \ typename detail::binary_op_helper::enable::type // Introduce a new unary operation called "name" #define FLINT_DEFINE_UNOP(name) \ namespace operations { \ struct name##_op { }; \ } \ FLINT_DEFINE_UNOP_HERE(name) #define FLINT_UNOP_ENABLE_RETTYPE(name, T) \ typename detail::unary_op_helper::return_t // See FLINTXX_DEFINE_MEMBER_UNOP_RTYPE #define FLINT_UNOP_BUILD_RETTYPE(name, rettype, T) \ typename detail::unary_op_helper_with_rettype::return_t #define FLINT_DEFINE_THREEARY(name) \ namespace operations { \ struct name##_op { }; \ } \ FLINT_DEFINE_THREEARY_HERE(name) #define FLINT_THREEARY_ENABLE_RETTYPE(name, T1, T2, T3) \ typename detail::nary_op_helper2::enable::type #define FLINT_DEFINE_FOURARY(name) \ namespace operations { \ struct name##_op { }; \ } \ FLINT_DEFINE_FOURARY_HERE(name) #define FLINT_FOURARY_ENABLE_RETTYPE(name, T1, T2, T3, T4) \ typename detail::nary_op_helper2::enable::type #define FLINT_DEFINE_FIVEARY(name) \ namespace operations { \ struct name##_op { }; \ } \ FLINT_DEFINE_FIVEARY_HERE(name) #define FLINT_FIVEARY_ENABLE_RETTYPE(name, T1, T2, T3, T4, T5) \ typename detail::nary_op_helper2::enable::type #define FLINT_DEFINE_SIXARY(name) \ namespace operations { \ struct name##_op { }; \ } \ FLINT_DEFINE_SIXARY_HERE(name) #define FLINT_DEFINE_SEVENARY(name) \ namespace operations { \ struct name##_op { }; \ } \ FLINT_DEFINE_SEVENARY_HERE(name) #endif