TreeExplorer: add yet another convoluted example

Yay!
...and all of this works flawless right away
This commit is contained in:
Fischlurch 2017-12-07 03:11:11 +01:00
parent 160a5e5465
commit 9b9dcb2b78
2 changed files with 84 additions and 17 deletions

View file

@ -620,6 +620,7 @@ namespace test{
}
/** @test add a filtering predicate into the pipeline.
* As in all the previously demonstrated cases, also the _filtering_ is added as decorator,
* wrapping the source and all previously attached decoration layers. And in a similar way,
@ -638,21 +639,22 @@ namespace test{
// Filter may lead to consuming util exhaustion...
auto i1 = treeExplore(CountDown{10})
auto ii = treeExplore(CountDown{10})
.filter([](int j){ return j > 9; });
CHECK (not isnil (i1));
CHECK (10 == *i1);
++ i1;
CHECK (isnil (i1));
VERIFY_ERROR (ITER_EXHAUST, ++i1 );
CHECK (not isnil (ii));
CHECK (10 == *ii);
++ ii;
CHECK (isnil (ii));
VERIFY_ERROR (ITER_EXHAUST, ++ii );
// none of the source elements can be approved here...
auto i2 = treeExplore(CountDown{5})
auto jj = treeExplore(CountDown{5})
.filter([](int j){ return j > 9; });
CHECK (isnil (i2));
CHECK (isnil (jj));
// a tricky example, where the predicate takes the source core as argument;
@ -666,6 +668,57 @@ namespace test{
.filter([](CountDown& core){ return core.p % 2; })
)
== "18.55-14.55-10.55");
// another convoluted example to demonstrate
// - a filter predicate with side-effect
// - and moreover the predicate is a generic lambda
// - accepting the iterator to trigger child expansion
// - which also causes re-evaluation of the preceding transformer
bool toggle = false;
auto kk = treeExplore(CountDown{10,5})
.expand([](uint j){ return CountDown{j-1}; })
.transform([](int v){ return 2*v; })
.filter([&](auto& it)
{
if (*it == 16)
{
it.expandChildren();
toggle = true;
}
return toggle;
});
CHECK (materialise(kk)
== "14-12-10-8-6-4-2-14-12");
// Explanation:
// The source starts at 10, but since the toggle is false,
// none of the initial values makes it though to the result.
// The interspersed transformer doubles the source values, and
// thus at source == 8 the trigger value (16) is hit. Thus the
// filter now flips the context-bound toggle (side-effect) and
// then expands children, which consumes current source value 8
// to replace it with the sequence 7,6,5,4,3,2,1, followed by
// the rest of the original sequence, 7,6 (which stops above 5).
CHECK (materialise(kk.filter([](long i){ return i % 7; }))
== "12-10-8-6-4-2-12");
// Explanation:
// Since the original TreeExplorer was assigned to variable kk,
// the materialise()-Function got a lvalue-ref and thus made a copy
// of the whole compound. For that reason, the original state within
// kk still rests at 7 -- because the filter evaluates eagerly, the
// source was pulled right at construction until we reached the first
// value to yield, which is the first child (7,....) within the
// expanded sequence. But now, in the second call to materialise(),
// we don't just copy, rather we add another filter layer on top,
// which happens to filter away this first result (== 2*7), and
// also the first element of the original sequence after the
// expanded children
CHECK (not isnil(kk));
CHECK (14 == *kk);
}

View file

@ -5691,8 +5691,8 @@
</node>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1511837279715" ID="ID_1694539348" MODIFIED="1512523928755" TEXT="filter-Operation">
<icon BUILTIN="pencil"/>
<node COLOR="#338800" CREATED="1511837279715" ID="ID_1694539348" MODIFIED="1512612114861" TEXT="filter-Operation">
<icon BUILTIN="button_ok"/>
<node CREATED="1511837285306" ID="ID_928417859" MODIFIED="1511837293629" TEXT="der Vollst&#xe4;ndigkeit halber">
<node CREATED="1511837296921" ID="ID_151298846" MODIFIED="1511837304547" TEXT="ist zwar redundant zu den Itertools"/>
<node CREATED="1511837305410" ID="ID_1979322634" MODIFIED="1511837324464" TEXT="treeExplorer k&#xf6;nnte Itertools abl&#xf6;sen">
@ -5705,7 +5705,8 @@
<node CREATED="1511837338643" ID="ID_1734683995" MODIFIED="1511837344590" TEXT="bestehende Dekorator-Technik"/>
<node CREATED="1511837345226" ID="ID_991593174" MODIFIED="1511837351797" TEXT="bestehende Funktor-Argument-Traits"/>
</node>
<node CREATED="1512523892775" ID="ID_673112014" MODIFIED="1512523896362" TEXT="Knackpunkte">
<node COLOR="#338800" CREATED="1512523892775" ID="ID_673112014" MODIFIED="1512612109927" TEXT="Knackpunkt">
<icon BUILTIN="button_ok"/>
<node COLOR="#338800" CREATED="1512523897422" ID="ID_1007287631" MODIFIED="1512606862773" TEXT="pullFilter">
<icon BUILTIN="button_ok"/>
</node>
@ -6294,8 +6295,8 @@
<icon BUILTIN="button_ok"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1511836579562" ID="ID_1718741335" MODIFIED="1512523829719" TEXT="verify_FilterIterator();">
<icon BUILTIN="pencil"/>
<node COLOR="#338800" CREATED="1511836579562" ID="ID_1718741335" MODIFIED="1512612068564" TEXT="verify_FilterIterator();">
<icon BUILTIN="button_ok"/>
<node COLOR="#338800" CREATED="1512523719350" ID="ID_351021178" MODIFIED="1512606882124" TEXT="einfaches Pr&#xe4;dikat">
<icon BUILTIN="button_ok"/>
</node>
@ -6308,11 +6309,24 @@
<icon BUILTIN="ksmiletris"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1512523783318" ID="ID_1808219280" MODIFIED="1512523797205" TEXT="Pr&#xe4;dikat&lt;Iter&gt; (generisch)">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1512523783318" ID="ID_1808219280" MODIFIED="1512611989218" TEXT="Pr&#xe4;dikat&lt;Iter&gt; (generisch)">
<icon BUILTIN="button_ok"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1512523810946" ID="ID_461047497" MODIFIED="1512523821311" TEXT="ggfs mit expand()">
<icon BUILTIN="help"/>
<node COLOR="#338800" CREATED="1512523810946" ID="ID_461047497" MODIFIED="1512612006569" TEXT="... mit expand()">
<icon BUILTIN="button_ok"/>
</node>
<node COLOR="#338800" CREATED="1512612008548" ID="ID_13525358" MODIFIED="1512612041105" TEXT="...und Seiteneffekt">
<icon BUILTIN="button_ok"/>
</node>
<node COLOR="#338800" CREATED="1512612015283" ID="ID_1560717932" MODIFIED="1512612042056" TEXT="...und geklontem Zustand">
<icon BUILTIN="button_ok"/>
</node>
<node COLOR="#338800" CREATED="1512612027105" ID="ID_739092273" MODIFIED="1512612042905" TEXT="...und nachtr&#xe4;glich hinzugef&#xfc;gtem Layer">
<icon BUILTIN="button_ok"/>
</node>
<node COLOR="#2b7d95" CREATED="1512612043592" ID="ID_1719204676" MODIFIED="1512612065051" TEXT="...es lebe die Komplexit&#xe4;t">
<font ITALIC="true" NAME="SansSerif" SIZE="12"/>
<icon BUILTIN="ksmiletris"/>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1511836589393" ID="ID_54165230" MODIFIED="1511836592073" TEXT="verify_asIterSource();">