TreeExplorer: draft how depth-first-to-leafs might be implemented

...it can sensibly only be done within the Expander itself.
Question: is this nice-to-have-feature worth the additional complexity
of essentially loading two quite distinct code paths into a single
implementation object?

As it stands, this looks totally confusing to me...
This commit is contained in:
Fischlurch 2017-12-10 04:09:49 +01:00
parent 4d21baea6b
commit 4ef1801a6f
3 changed files with 70 additions and 18 deletions

View file

@ -487,6 +487,8 @@ namespace lib {
BaseAdapter(SRC && src) : SRC(src) { }
void expandChildren() { }
size_t depth() const { return 0; }
};
@ -512,7 +514,7 @@ namespace lib {
* @tparam SRC the wrapped source iterator, typically a TreeExplorer or nested decorator.
* @tparam FUN the concrete type of the functor passed. Will be dissected to find the signature
*/
template<class SRC, class FUN>
template<class SRC, class FUN, bool expandToLeaf =false>
class Expander
: public SRC
{
@ -535,7 +537,9 @@ namespace lib {
: SRC{move (parentExplorer)} // NOTE: slicing move to strip TreeExplorer (Builder)
, expandChildren_{forward<FUN> (expandFunctor)}
, expansions_{}
{ }
{
maybeExpandToExhaustion();
}
/** core operation: expand current head element */
@ -551,6 +555,22 @@ namespace lib {
expansions_.push (move(expanded));
}
void
maybeExpandToExhaustion()
{
if (not (expandToLeaf and this->checkPoint())) return;
while (true)
{
ResIter expanded{ 0 < depth()? expandChildren_(*expansions_)
: expandChildren_(*this)};
if (isnil(expanded))
break;
iterate(); // consume current head element
expansions_.push (move(expanded));
}
}
/** diagnostics: current level of nested child expansion */
size_t
depth() const
@ -577,6 +597,13 @@ namespace lib {
void
iterNext()
{
iterate();
maybeExpandToExhaustion();
}
void
iterate()
{
if (0 < depth())
{
@ -595,7 +622,7 @@ namespace lib {
class AutoExpander
: public SRC
{
static_assert(is_StateCore<SRC>::value, "need wrapped state as predecessor in pipeline");
static_assert(is_StateCore<SRC>::value, "need wrapped state core as predecessor in pipeline");
public:
/** pass through ctor */
@ -980,6 +1007,19 @@ namespace lib {
}
/** @todo WIP 12/17 auto expand down to the leafs
*/
template<class FUN>
auto
expandLeaf (FUN&& expandFunctor)
{
using ResCore = iter_explorer::Expander<SRC, FUN, true>;
using ResIter = typename _DecoratorTraits<ResCore>::SrcIter;
return TreeExplorer<ResIter> (ResCore {move(*this), forward<FUN>(expandFunctor)});
}
/** @todo WIP 12/17 auto expand all
*/
auto
@ -992,16 +1032,6 @@ namespace lib {
}
/** @todo WIP 12/17 auto expand down to the leafs
*/
auto
expandLeaf()
{
UNIMPLEMENTED ("depth-first expand immediately, until reaching the leaf elements");
return *this;
}
/** adapt this TreeExplorer to pipe each result value through a transformation function.
* Several "layers" of mapping can be piled on top of each other, possibly mixed with the
* other types of adaptation, like the child-expanding operation, or a filter. Obviously,

View file

@ -822,19 +822,19 @@ namespace test{
{
uint val = get<0>(tup);
uint sum = get<1>(tup);
return singleValIterator (Tu2{val-1, sum+val});
return val? singleValIterator (Tu2{val-1, sum+val})
: SingleValIter<Tu2>();
};
#if false /////////////////////////////////////////////////////////////////////////////////////////////////////////////UNIMPLEMENTED :: TICKET #888
cout << materialise(
treeExplore(CountDown{4})
.transform([](uint i){ return Tu2{i,0}; })
// .expand(summingExpander) //////////////////TODO why isn't that result iterator (singleValIterator<Tu2>) accepted? It should be
.expandLeaf()
.expandLeaf(summingExpander)
.transform([](Tu2 res){ return get<1>(res); })
) <<endl;
#endif /////////////////////////////////////////////////////////////////////////////////////////////////////////////UNIMPLEMENTED :: TICKET #888
#if false /////////////////////////////////////////////////////////////////////////////////////////////////////////////UNIMPLEMENTED :: TICKET #888
#endif /////////////////////////////////////////////////////////////////////////////////////////////////////////////UNIMPLEMENTED :: TICKET #888
}

View file

@ -5774,6 +5774,28 @@
<node CREATED="1512844769176" ID="ID_1306087014" MODIFIED="1512844774891" TEXT="expandChildren eager"/>
<node CREATED="1512844775893" ID="ID_781402390" MODIFIED="1512844784866" TEXT="sofort erstes Kind liefern"/>
<node CREATED="1512844785526" ID="ID_244222856" MODIFIED="1512844788897" TEXT="rekursiv"/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1512872529062" ID="ID_1993110543" MODIFIED="1512872543139" TEXT="geht nicht einfach">
<icon BUILTIN="messagebox_warning"/>
<node CREATED="1512872546187" ID="ID_633024773" MODIFIED="1512872616266" TEXT="Problem: Blatt erkennen">
<node CREATED="1512872574000" ID="ID_262264527" MODIFIED="1512872587090" TEXT="m&#xfc;&#xdf;te auf dem letzten Ergebnis stehen bleiben"/>
<node CREATED="1512872560761" ID="ID_1013659640" MODIFIED="1512872573339" TEXT="expandChildren() geht einfach weiter"/>
</node>
<node CREATED="1512872618801" ID="ID_1769639916" MODIFIED="1512872632495" TEXT="nur in Expander selber implementierbar">
<icon BUILTIN="idea"/>
<node CREATED="1512872646574" ID="ID_419478495" MODIFIED="1512872658944" TEXT="eager feed"/>
<node CREATED="1512872676162" ID="ID_1452543628" MODIFIED="1512872683205" TEXT="sofort weiter expanden"/>
<node CREATED="1512872684337" ID="ID_90684575" MODIFIED="1512872695451" TEXT="aber am Ende nicht weiterschalten"/>
</node>
<node CREATED="1512875104589" ID="ID_477086316" MODIFIED="1512875117813" TEXT="Frage: sinnvoll?">
<icon BUILTIN="help"/>
<node COLOR="#338800" CREATED="1512875141521" ID="ID_152225336" MODIFIED="1512875149513" TEXT="versuchshalber mal eingebaut">
<icon BUILTIN="button_ok"/>
</node>
<node BACKGROUND_COLOR="#fdfdcf" COLOR="#ff0000" CREATED="1512875161638" ID="ID_681111567" MODIFIED="1512875181807" TEXT="ist es die zus&#xe4;tzliche Komplexit&#xe4;t wert?">
<icon BUILTIN="help"/>
</node>
</node>
</node>
</node>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1512363286920" ID="ID_1420123769" MODIFIED="1512363305422" TEXT="Zusammenspiel">