diff --git a/tests/library/iter-tree-explorer-test.cpp b/tests/library/iter-tree-explorer-test.cpp index ff7e23755..67a0125fd 100644 --- a/tests/library/iter-tree-explorer-test.cpp +++ b/tests/library/iter-tree-explorer-test.cpp @@ -67,6 +67,7 @@ #include #include +#include #include #include @@ -120,7 +121,6 @@ namespace test{ }; - /** * A straight descending number sequence as basic test iterator. * It is built wrapping an opaque "state core" (of type CountDown). @@ -142,6 +142,51 @@ namespace test{ + /** + * Another iteration _"state core"_ to produce a sequence of random numbers. + * Used to build an infinite random search space... + */ + class RandomSeq + { + size_t lim_; + size_t cnt_; + char letter_; + + static char + rndLetter() + { + return 'A' + rand() % 26; + } + + public: + RandomSeq(size_t len =std::numeric_limits::max()) + : lim_{len} + , cnt_{0} + , letter_{rndLetter()} + { } + + bool + checkPoint () const + { + return cnt_ < lim_; + } + + char& + yield () const + { + return unConst(this)->letter_; + } + + void + iterNext () + { + ASSERT (checkPoint()); + ++cnt_; + letter_ = rndLetter(); + } + }; + + /** Diagnostic helper: join all the elements from a _copy_ of the iterator */ template inline string @@ -855,12 +900,73 @@ namespace test{ /** @test Demonstration how to build complex algorithms by layered tree expanding iteration - * @remarks this is the actual use case which inspired the design of TreeExplorer + * @remarks this is the actual use case which inspired the design of TreeExplorer: + * Search with backtracking over an opaque (abstracted), tree-shaped search space. + * - the first point to note is that the search algorithm knows nothing about its + * data source, beyond its ability to delve down (expand) into child nodes + * - in fact our data source for this test here is "infinite", since it is an + * very large random root sequence, where each individual number can be expanded + * into a limited random sub sequence, down to arbitrary depth. We just assume + * that the search has good chances to find its target sequence eventually and + * thus only ever visits a small fraction of the endless search space. */ void demonstrate_LayeredEvaluation() { - UNIMPLEMENTED("build algorithm by layering iterator evaluation"); + // Layer-1: the search space with "hidden" implementation + using DataSrc = IterExploreSource; + DataSrc searchSpace = treeExplore(RandomSeq{}) + .expand([](char){ return RandomSeq{15}; }) + .asIterSource(); + + // Layer-2: State for search algorithm + struct State + { + DataSrc& src; + string& toFind; + uint pos; + vector protocol; + + State(DataSrc& s, string& t) + : src{s} + , toFind{t} + , pos{0} + , protocol{0} + { } + + bool checkPoint() const { return src; } + State& yield() const { return *unConst(this); } + void iterNext() { ++src; ++protocol.back(); } + }; + + // Layer-3: Evaluation pipeline to drive search + string toFind = util::join (treeExplore (RandomSeq{5})); + cout << "Search in random tree: toFind = "<protocol) < - - + + - + + + + + + + + + + + +

+ ich brauche ihn nicht +

+ + +
+
+ + + + + + +

+ die Builder-Operationen moven den bisherigen Iterator-compound weg. +

+

+ Ich könnte mir vorstellen, daß das einen naiven User ziemlich schockiert.... +

+

+ +

+

+ Lösung wäre, das Iterator-API erst nach einem expliziten terminalen Aufruf freizuschalten +

+ + +
+ +
+ + +
@@ -5108,7 +5151,7 @@ - + @@ -5324,13 +5367,13 @@ - - + + - + @@ -5694,7 +5737,7 @@ - + @@ -5727,7 +5770,7 @@ - + @@ -5742,8 +5785,7 @@ denn im Moment brauchen wir das überhaupt nicht

- -
+
@@ -5756,8 +5798,7 @@ ...setzt eigentliche Expand-Operation darunter voraus

- - + @@ -5820,8 +5861,7 @@ - - + @@ -5843,14 +5883,13 @@ nicht den technischen Möglichkeiten

- - +
- + @@ -6168,7 +6207,7 @@ - + @@ -6530,7 +6569,9 @@ - + + + @@ -6556,8 +6597,8 @@ - - + + @@ -6609,7 +6650,7 @@ - + @@ -6711,7 +6752,7 @@ - + @@ -6720,7 +6761,7 @@ - + @@ -6745,7 +6786,7 @@ - + @@ -6795,7 +6836,7 @@ - + @@ -6808,7 +6849,7 @@ - + @@ -6854,30 +6895,17 @@ - + - - + + - - - - - - - - - - - - - - - + + @@ -6931,6 +6959,102 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

+ siehe std::shuffle +

+ + +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

+ vorgegebene Zahlenfolge
in untendlichem Zufalls-Baum finden +

+ + +
+
+ + + + + + + + + + + + + + + @@ -22381,8 +22505,7 @@ Thema: Monaden

- -
+ @@ -22406,8 +22529,7 @@ Python hat das auch, Java neuerdings auch

- - +
@@ -22453,8 +22575,7 @@ da eine Abstraktion hier sehr technisch werden würde

- - +
@@ -22488,8 +22609,7 @@ und führt dazu, daß die Abstraktion undicht wird

- - +
@@ -22519,8 +22639,7 @@ nach einer Seite der Glieder auszukoppeln.

- - +