From 38081664947db4238f5a6789f4f9ac76e276122d Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Wed, 22 Nov 2023 23:54:20 +0100 Subject: [PATCH] Library: RandomDraw - invent new scheme for dynamic configuration ...now using the reworked partial-application helper... ...bind to *this and then recursively re-invoke the adaptation process ...need also to copy-capture the previously existing mapping-function first test seems to work now --- src/lib/random-draw.hpp | 47 +++++++++++++--- tests/library/random-draw-test.cpp | 88 ++++++++++++++---------------- wiki/thinkPad.ichthyo.mm | 33 +++++++++-- 3 files changed, 108 insertions(+), 60 deletions(-) diff --git a/src/lib/random-draw.hpp b/src/lib/random-draw.hpp index 464ddb7c9..eeed7be57 100644 --- a/src/lib/random-draw.hpp +++ b/src/lib/random-draw.hpp @@ -302,6 +302,15 @@ namespace lib { private: + template + struct is_Manipulator + : std::false_type { }; + + template + struct is_Manipulator + : std::true_type { }; + + /** @internal adapt input side of a given function to conform to the * global input arguments as defined in the Policy base function. * @return a function pre-fitted with a suitable Adapter from the Policy */ @@ -309,6 +318,7 @@ namespace lib { decltype(auto) adaptIn (FUN&& fun) { + using lib::meta::func::applyFirst; using _Fun = lib::meta::_Fun; static_assert (_Fun(), "Need something function-like."); @@ -320,6 +330,10 @@ namespace lib { if constexpr (std::is_same_v) // function accepts same arguments as this RandomDraw return forward (fun); // pass-through directly + else + if constexpr (is_Manipulator()) + // function wants to manipulate *this dynamically... + return adaptIn (applyFirst(forward (fun), *this)); else return Adaptor::build (forward (fun)); } @@ -356,17 +370,36 @@ namespace lib { ,[this](double rand){ return limited(rand); } ); else - if constexpr (std::is_same_v)// ◁────────────────────┨ function yields parametrised RandomDraw to invoke - return [functor=std::forward(fun), this] - (auto&& ...inArgs) -> _FunRet - { // invoke with copy - RandomDraw parametricDraw = functor(inArgs...); - return parametricDraw (forward (inArgs)...); - }; // forward arguments + if constexpr (std::is_same_v) // ◁──────────────────────────┨ function manipulates parameters by side-effect + return [functor=std::forward(fun) + ,processDraw=getCurrMapping()] + (auto&& ...inArgs) mutable -> _FunRet + { + functor(inArgs...); // invoke manipulator with copy + return processDraw (forward (inArgs)...); + }; // forward arguments to *this else static_assert (not sizeof(Res), "unable to adapt / handle result type"); NOTREACHED("Handle based on return type"); } + + + /** @internal capture the current mapping processing-chain as function. + * RandomDraw is-a function to process and map the input argument into a + * limited and quantised output value. The actual chain can be re-configured. + * This function picks up a snapshot copy of the current configuration; it is + * used to build a chain of functions, which incorporates this current function. + * If no function was configured yet, the default processing chain is returned. + */ + Fun + getCurrMapping() + { + Fun& currentProcessingFunction = *this; + if (not currentProcessingFunction) + return Fun{adaptOut(POL::defaultSrc)}; + else + return Fun{currentProcessingFunction}; + } }; diff --git a/tests/library/random-draw-test.cpp b/tests/library/random-draw-test.cpp index 067d73096..23ed96ec9 100644 --- a/tests/library/random-draw-test.cpp +++ b/tests/library/random-draw-test.cpp @@ -79,7 +79,7 @@ namespace test{ build (FUN&& fun) { return [functor=std::forward(fun)] - (size_t hash) -> _FunRet + (size_t hash) mutable -> _FunRet { return functor(uint(hash/64), uint(hash%64)); }; @@ -731,53 +731,47 @@ namespace test{ void verify_dynamicChange() { -// auto d1 = Draw([](Draw& draw, uint cycle, uint){ -// draw.probability(cycle*0.2); -// }); -// -//SHOW_EXPR(int(d1( 0))); -//SHOW_EXPR(int(d1( 1))); -//SHOW_EXPR(int(d1( 2))); -//SHOW_EXPR(int(d1( 16))); -//SHOW_EXPR(int(d1( 32))); -//SHOW_EXPR(int(d1( 48))); -//SHOW_EXPR(int(d1( 63))); -//SHOW_EXPR(int(d1( 64))); -//SHOW_EXPR(int(d1( 64 +1))); -//SHOW_EXPR(int(d1( 64 +2))); -//SHOW_EXPR(int(d1( 64+16))); -//SHOW_EXPR(int(d1( 64+32))); -//SHOW_EXPR(int(d1( 64+48))); -//SHOW_EXPR(int(d1( 64+64))); -//SHOW_EXPR(int(d1(128 +16))); -//SHOW_EXPR(int(d1(128 +32))); -//SHOW_EXPR(int(d1(128 +48))); -//SHOW_EXPR(int(d1(128 +64))); -//SHOW_EXPR(int(d1(128+64+16))); -//SHOW_EXPR(int(d1(128+64+32))); -//SHOW_EXPR(int(d1(128+64+48))); -//SHOW_EXPR(int(d1(128+64+64))); - int yy = 99; - float ff = 88; - auto fuK = std::function{[](float& f, int& i, size_t s) -> double { return f + i + s; }}; - auto& f1 = fuK; - auto f2 = lib::meta::func::applyFirst(f1, ff); - using lib::meta::_Fun; - using Sig1 = _Fun::Sig; - using Sig2 = _Fun::Sig; - yy = 22; - ff = 42; -SHOW_TYPE(Sig1) -SHOW_TYPE(Sig2) -SHOW_EXPR(f1 (ff,yy,33)) -SHOW_EXPR(f2 ( yy,33)) -SHOW_TYPE(decltype(f2 ( yy,33))) -SHOW_TYPE(decltype(f2)) + auto d1 = Draw([](Draw& draw, uint cycle, uint){ + draw.probability((cycle+1)*0.25); + }); - fuK = [](float& f, int& i, size_t s) -> double { return f * i * s; }; - -SHOW_EXPR(f1 (ff,yy,33)) -SHOW_EXPR(f2 ( yy,33)) +SHOW_EXPR(int(d1( 0))); +SHOW_EXPR(int(d1( 8))); +SHOW_EXPR(int(d1( 16))); +SHOW_EXPR(int(d1( 24))); +SHOW_EXPR(int(d1( 32))); +SHOW_EXPR(int(d1( 40))); +SHOW_EXPR(int(d1( 48))); +SHOW_EXPR(int(d1( 56))); +SHOW_EXPR(int(d1( 63))); +SHOW_EXPR(int(d1( 64 +0))); +SHOW_EXPR(int(d1( 64 +8))); +SHOW_EXPR(int(d1( 64+16))); +SHOW_EXPR(int(d1( 64+24))); +SHOW_EXPR(int(d1( 64+32))); +SHOW_EXPR(int(d1( 64+40))); +SHOW_EXPR(int(d1( 64+48))); +SHOW_EXPR(int(d1( 64+56))); +SHOW_EXPR(int(d1( 64+63))); +SHOW_EXPR(int(d1(128 +0))); +SHOW_EXPR(int(d1(128 +8))); +SHOW_EXPR(int(d1(128 +16))); +SHOW_EXPR(int(d1(128 +24))); +SHOW_EXPR(int(d1(128 +32))); +SHOW_EXPR(int(d1(128 +40))); +SHOW_EXPR(int(d1(128 +48))); +SHOW_EXPR(int(d1(128 +56))); +SHOW_EXPR(int(d1(128 +63))); +SHOW_EXPR(int(d1(128+64 +0))); +SHOW_EXPR(int(d1(128+64 +8))); +SHOW_EXPR(int(d1(128+64+16))); +SHOW_EXPR(int(d1(128+64+24))); +SHOW_EXPR(int(d1(128+64+32))); +SHOW_EXPR(int(d1(128+64+40))); +SHOW_EXPR(int(d1(128+64+48))); +SHOW_EXPR(int(d1(128+64+56))); +SHOW_EXPR(int(d1(128+64+63))); +SHOW_EXPR(int(d1(128+64+64))); } }; diff --git a/wiki/thinkPad.ichthyo.mm b/wiki/thinkPad.ichthyo.mm index c0a2c2bca..9fdf71e76 100644 --- a/wiki/thinkPad.ichthyo.mm +++ b/wiki/thinkPad.ichthyo.mm @@ -97271,17 +97271,35 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
- - + + + + + + + + + + + + + - - - - + + + + + + + + + + + @@ -97290,6 +97308,9 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
+ + +