From 5616b287f5bffb753bdfdeecca0c42aa34b5a71d Mon Sep 17 00:00:00 2001 From: John Fletcher Date: Tue, 3 Mar 2015 22:28:18 +0000 Subject: [PATCH] function/lazy_prelude.hpp Added enum_from and enum_from_to --- .../boost/phoenix/function/lazy_prelude.hpp | 166 ++++++++++++++++++ 1 file changed, 166 insertions(+) diff --git a/include/boost/phoenix/function/lazy_prelude.hpp b/include/boost/phoenix/function/lazy_prelude.hpp index 51e2696..6f4e98e 100644 --- a/include/boost/phoenix/function/lazy_prelude.hpp +++ b/include/boost/phoenix/function/lazy_prelude.hpp @@ -50,6 +50,8 @@ // all_but_last(list) // take(n,list) // drop(n,list) +// enum_from(x) +// enum_from_to(x,y) // //////////////////////////////////////////////////////////////////////////// // Interdependence: @@ -223,11 +225,41 @@ namespace boost { } }; + + // This will apply f() at least once. + struct Apply0 { + + template + struct result; + + template + struct result + { + // Need result type from F + typedef typename F::result_type type; + }; + + template + typename result::type operator()(N n, const F &f, + reuser2 r = NIL ) const { + if ( n <= 1 ) + return f()(); + else { + A0 a1 = r( Apply(), n-1, f)(); + return f()(); + } + } + + }; + + } typedef boost::phoenix::function Pow; typedef boost::phoenix::function Apply; + typedef boost::phoenix::function Apply0; Pow pow; Apply apply; + Apply apply0; namespace impl { using fcpp::INV; @@ -379,6 +411,136 @@ namespace boost { } }; + template + struct EFH + { + typedef typename boost::remove_reference TT; + typedef typename boost::remove_const::type TTT; + mutable T x; + EFH( const T& xx) : x(xx) {} + template struct result; + + template + struct result + { + typedef typename boost::phoenix::UseList::template + List::type LType; + typedef typename boost::phoenix::result_of:: + ListType::delay_result_type type; + }; + typename result::type operator()() const { + typedef typename UseList::template List::type LType; + typedef typename result_of::ListType:: + delay_result_type result_type; + typedef boost::function0 fun1_R_TTT; + //std::cout << "EFH (" << x << ")" << std::endl; + ++x; + fun1_R_TTT efh_R_TTT = EFH(x); + typedef boost::phoenix::function EFH_R_T; + EFH_R_T efh_R_T(efh_R_TTT); + if (x > BOOST_PHOENIX_FUNCTION_MAX_LAZY_LIST_LENGTH) + throw lazy_exception("Running away in EFH!!"); + return cons( x-1, efh_R_T() ); + } + }; + + struct Enum_from { + template struct result; + + template + struct result + { + typedef typename boost::remove_reference::type TT; + typedef typename boost::remove_const::type TTT; + typedef typename UseList::template List::type LType; + typedef typename result_of::ListType:: + delay_result_type type; + }; + + template + typename result::type operator() + (const T & x) const + { + typedef typename boost::remove_reference::type TT; + typedef typename boost::remove_const::type TTT; + typedef typename UseList::template List::type LType; + typedef typename result_of::ListType:: + delay_result_type result_type; + typedef boost::function0 fun1_R_TTT; + fun1_R_TTT efh_R_TTT = EFH(x); + typedef boost::phoenix::function EFH_R_T; + EFH_R_T efh_R_T(efh_R_TTT); + //std::cout << "enum_from (" << x << ")" << std::endl; + return efh_R_T(); + } + }; + + template + struct EFTH + { + typedef typename boost::remove_reference TT; + typedef typename boost::remove_const::type TTT; + mutable T x; + T y; + EFTH( const T& xx, const T& yy) : x(xx), y(yy) {} + template struct result; + + template + struct result + { + typedef typename boost::phoenix::UseList::template + List::type LType; + typedef typename boost::phoenix::result_of:: + ListType::delay_result_type type; + }; + typename result::type operator()() const { + typedef typename UseList::template List::type LType; + typedef typename result_of::ListType:: + delay_result_type result_type; + typedef boost::function0 fun1_R_TTT; + //std::cout << "EFTH (" << x << ")" << std::endl; + if (x > y ) return NIL; + ++x; + fun1_R_TTT efth_R_TTT = EFTH(x,y); + typedef boost::phoenix::function EFTH_R_T; + EFTH_R_T efth_R_T(efth_R_TTT); + if (x > BOOST_PHOENIX_FUNCTION_MAX_LAZY_LIST_LENGTH) + throw lazy_exception("Running away in EFTH!!"); + return cons( x-1, efth_R_T() ); + } + }; + + struct Enum_from_to { + template struct result; + + template + struct result + { + typedef typename boost::remove_reference::type TT; + typedef typename boost::remove_const::type TTT; + typedef typename UseList::template List::type LType; + typedef typename result_of::ListType:: + delay_result_type type; + }; + + template + typename result::type operator() + (const T & x, const T & y) const + { + typedef typename boost::remove_reference::type TT; + typedef typename boost::remove_const::type TTT; + typedef typename UseList::template List::type LType; + typedef typename result_of::ListType:: + delay_result_type result_type; + typedef boost::function0 fun1_R_TTT; + fun1_R_TTT efth_R_TTT = EFTH(x,y); + typedef boost::phoenix::function EFTH_R_T; + EFTH_R_T efth_R_T(efth_R_TTT); + //std::cout << "enum_from (" << x << ")" << std::endl; + return efth_R_T(); + } + }; + } @@ -392,12 +554,16 @@ namespace boost { typedef boost::phoenix::function Init; typedef boost::phoenix::function Take; typedef boost::phoenix::function Drop; + typedef boost::phoenix::function Enum_from; + typedef boost::phoenix::function Enum_from_to; Until until; Until2 until2; Last last; Init all_but_last; // renamed from init which is not available. Take take; Drop drop; + Enum_from enum_from; + Enum_from_to enum_from_to; namespace fcpp {