From 7ed1948a8991ba8362912d6d5c333410cc8aba85 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sat, 16 Dec 2017 19:21:22 +0100 Subject: [PATCH] TreeExplorer: refactor to make depth() reflect the logical expansion depth ...so sad. The existing implementation was way more elegant, just it discarded an exahusted parent element right while in expansion, so effectively the child sequence took its place. Resolved that by decomposing the iterNext() operation. And to keep it still readable, I make the invariant of this class explicit and check it (which caught yet another undsicovered bug. Yay!) --- src/lib/iter-tree-explorer.hpp | 57 +++++++-- tests/library/iter-tree-explorer-test.cpp | 1 + wiki/thinkPad.ichthyo.mm | 143 +++++++++++++++++++++- 3 files changed, 185 insertions(+), 16 deletions(-) diff --git a/src/lib/iter-tree-explorer.hpp b/src/lib/iter-tree-explorer.hpp index 449f840cc..fb22357ca 100644 --- a/src/lib/iter-tree-explorer.hpp +++ b/src/lib/iter-tree-explorer.hpp @@ -545,11 +545,15 @@ namespace lib { { REQUIRE (this->checkPoint(), "attempt to expand an empty explorer"); - ResIter expanded{ 0 < depth()? expandChildren_(*expansions_) - : expandChildren_(*this)}; - iterNext(); // consume current head element + ResIter expanded{ hasChildren()? expandChildren_(*expansions_) + : expandChildren_(*this)}; + incrementCurrent(); // consume current head element if (not isnil(expanded)) expansions_.push (move(expanded)); + else + dropExhaustedChildren(); + + ENSURE (invariant()); } /** diagnostics: current level of nested child expansion */ @@ -565,29 +569,56 @@ namespace lib { bool checkPoint() const { - return 0 < depth() - or this->isValid(); + ENSURE (invariant()); + + return hasChildren() + or SRC::isValid(); } typename SRC::reference yield() const { - return 0 < depth()? **expansions_ - : **this; + return hasChildren()? **expansions_ + : **this; } void iterNext() { - if (0 < depth()) - { - ++(*expansions_); - while (0 < depth() and not *expansions_) - ++expansions_; - } + incrementCurrent(); + dropExhaustedChildren(); + ENSURE (invariant()); + } + + private: + bool + invariant() const + { + return not hasChildren() + or *expansions_; + } + + bool + hasChildren() const + { + return 0 < depth(); + } + + void + incrementCurrent() + { + if (hasChildren()) + ++(*expansions_); else ++(*this); } + + void + dropExhaustedChildren() + { + while (not invariant()) + ++expansions_; + } }; diff --git a/tests/library/iter-tree-explorer-test.cpp b/tests/library/iter-tree-explorer-test.cpp index 7b0be788d..f9c41ad6b 100644 --- a/tests/library/iter-tree-explorer-test.cpp +++ b/tests/library/iter-tree-explorer-test.cpp @@ -961,6 +961,7 @@ namespace test{ expandChildren() { src.expandChildren(); + protocol.resize (1+src.depth()); } bool diff --git a/wiki/thinkPad.ichthyo.mm b/wiki/thinkPad.ichthyo.mm index afd3789ae..e0d59f22a 100644 --- a/wiki/thinkPad.ichthyo.mm +++ b/wiki/thinkPad.ichthyo.mm @@ -5151,7 +5151,7 @@ - + @@ -6330,6 +6330,41 @@ + + + + + + + + + + + + + + +

+ wir hatten bisher eine auto-Aufräum-Routine in iterNext(), +

+

+ welche dazu führt, daß ein erschöpfter Vater sofort weggeräumt wird, +

+

+ noch bevor wir dazu kommen, die Kinder zu pushen +

+ + +
+
+ + + + + + + +
@@ -7256,8 +7291,9 @@ - - + + + @@ -7391,6 +7427,107 @@ + + + + + + +

+ rLet(77943 < 18446744073709551615) → R +

+

+ |↯| R ... 77943 +

+

+ rLet(77944 < 18446744073709551615) → X +

+

+ |!| expand 77944 +

+

+ rLet(0 < 4) → U +

+

+ rLet(77945 < 18446744073709551615) → I +

+

+ |.| U -->> 77944-0 +

+

+ |↯| U ... 77944-0 +

+

+ rLet(1 < 4) → X +

+

+ |!| expand 77944-1 +

+

+ rLet(0 < 4) → K +

+

+ rLet(2 < 4) → Z +

+

+ |.| K -->> 77944-1-0 +

+

+ |!| expand 77944-1-0 +

+

+ rLet(0 < 4) → V +

+

+ rLet(1 < 4) → Y +

+

+ |.| V -->> 77944-1-0-0 +

+

+ |↯| V ... 77944-1-0-0 +

+

+ rLet(1 < 4) → I +

+

+ |↯| I ... 77944-1-0-1 +

+

+ rLet(2 < 4) → I +

+

+ |↯| I ... 77944-1-0-2 +

+

+ rLet(3 < 4) → Z +

+

+ |!| expand 77944-1-0-3 +

+

+ rLet(0 < 4) → Q +

+

+ rLet(4 < 4) → X +

+

+ |.| Q -->> 77944-1-0-3-0 +

+

+ |↯| Q ... 77944-1-0-3-0 +

+

+ rLet(1 < 4) → M +

+

+ Protocol of the search: 77944-1-0-3-1 +

+ + +
+ +