From 619a5173b03c21940c34da84e662e55c47dbe41b Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sun, 26 Nov 2023 22:28:12 +0100 Subject: [PATCH] Chain-Load: handle node seed and recalculation - with the new pruning option, start-Nodes can now be anywhere - introduce predicates to detect start-Nodes and exit-Nodes - ensure each new seed node gets the global seed on graph construction - provide functionality to re-propagate a seed and clear hashes - provide functionality to recalculate the hashes over the graph --- src/lib/iter-explorer.hpp | 7 +++ tests/vault/gear/test-chain-load-test.cpp | 37 +++++++++++++-- tests/vault/gear/test-chain-load.hpp | 58 +++++++++++++++++++++-- wiki/thinkPad.ichthyo.mm | 31 +++++++++++- 4 files changed, 124 insertions(+), 9 deletions(-) diff --git a/src/lib/iter-explorer.hpp b/src/lib/iter-explorer.hpp index 9e2323574..e0f6b65ef 100644 --- a/src/lib/iter-explorer.hpp +++ b/src/lib/iter-explorer.hpp @@ -1647,6 +1647,13 @@ namespace lib { return IterExplorer::reduce ([](const reference val){ return val; }); } + /** simplified _terminal builder_ to count number of elements from this sequence. */ + size_t + count() + { + return IterExplorer::reduce ([](auto){ return size_t(1); }); + } + /** simplified _terminal builder_ to check if any result yields `true` (short-circuit) */ bool has_any() diff --git a/tests/vault/gear/test-chain-load-test.cpp b/tests/vault/gear/test-chain-load-test.cpp index 4f874d6dd..5fe80fd67 100644 --- a/tests/vault/gear/test-chain-load-test.cpp +++ b/tests/vault/gear/test-chain-load-test.cpp @@ -64,6 +64,7 @@ namespace test { verify_Node(); verify_Topology(); control_Topology(); + reseed_recalculate(); witch_gate(); } @@ -215,21 +216,51 @@ namespace test { /** @test flexible control of generated topology - * @todo WIP 11/23 🔁 define ⟶ implement + * @todo WIP 11/23 🔁 define ⟶ 🔁 implement */ void control_Topology() { auto graph = TestChainLoad<32>{}; - graph.expansionRule(graph.rule().probability(0.25).maxVal(4).shuffle()) - .pruningRule(graph.rule().probability(0.2).shuffle()) + graph.expansionRule(graph.rule().probability(0.8).maxVal(1)) + .pruningRule(graph.rule().probability(0.6)) .buildToplolgy() .printTopologyDOT(); } + /** @test set and propagate seed values and recalculate all node hashes + * @todo WIP 11/23 🔁 define ⟶ implement + */ + void + reseed_recalculate() + { + auto graph = TestChainLoad<32>{}; + graph.expansionRule(graph.rule().probability(0.8).maxVal(1)) + .pruningRule(graph.rule().probability(0.6)) + .buildToplolgy(); + + using Node = TestChainLoad<32>::Node; + auto isStartNode = [](Node& n){ return isStart(n); }; + auto isExitNode = [](Node& n){ return isExit(n); }; + + CHECK (8 == graph.allNodes().filter(isStartNode).count()); + CHECK (15 == graph.allNodes().filter(isExitNode).count()); + + CHECK (graph.getHash() == 14172386810742845390u); + + graph.setSeed(55).clearNodeHashes(); + CHECK (graph.getSeed() == 55); + CHECK (graph.getHash() == 0); + + graph.recalculate(); + CHECK (graph.getHash() == 6093128458724583708u); + } + + + /** @test TODO diagnostic blah * @todo WIP 11/23 🔁 define ⟶ implement */ diff --git a/tests/vault/gear/test-chain-load.hpp b/tests/vault/gear/test-chain-load.hpp index 22c680086..e1ab3e753 100644 --- a/tests/vault/gear/test-chain-load.hpp +++ b/tests/vault/gear/test-chain-load.hpp @@ -237,6 +237,12 @@ namespace test { hash_combine (hash, entry->hash); return hash; } + + friend bool isStart (Node const& n) { return isnil (n.pred); }; + friend bool isStart (Node const* n) { return n and isnil (n->pred); }; + + friend bool isExit (Node const& n) { return isnil (n.succ); }; + friend bool isExit (Node const* n) { return n and isnil (n->succ); }; }; @@ -336,10 +342,12 @@ namespace test { auto moreNext = [&]{ return next->size() < maxFan; }; auto moreNodes = [&]{ return node < &nodes_->back(); }; auto spaceLeft = [&]{ return moreNext() and moreNodes(); }; - auto addNode = [&]{ + auto addNode = [&](size_t seed =0) + { Node* n = *next->add (node++); n->clear(); n->level = level; + n->hash = seed; return n; }; auto apply = [&](Rule& rule, Node* n) @@ -364,8 +372,7 @@ namespace test { size_t toExpand = apply (expansionRule,o); while (0 < toSeed and spaceLeft()) { // start a new chain from seed - Node* n = addNode(); - n->hash = this->getSeed(); + addNode(this->getSeed()); --toSeed; } while (0 < toExpand and spaceLeft()) @@ -391,8 +398,8 @@ namespace test { } } ENSURE (not isnil(next) or spaceLeft()); - if (isnil(next)) - addNode(); // ensure parent + if (isnil(next)) // ensure graph continues + addNode(this->getSeed()); ENSURE (not next->empty()); ++level; } @@ -411,6 +418,47 @@ namespace test { } + /** + * Set the overall seed value. + * @note does not propagate seed to consecutive start nodes + */ + TestChainLoad&& + setSeed (size_t seed = rand()) + { + nodes_->front().hash = seed; + return move(*this); + } + + + /** + * Recalculate all node hashes and propagate seed value. + */ + TestChainLoad&& + recalculate() + { + size_t seed = this->getSeed(); + for (Node& n : allNodes()) + { + n.hash = isStart(n)? seed : 0; + n.calculate(); + } + return move(*this); + } + + + /** + * Clear node hashes and propagate seed value. + */ + TestChainLoad&& + clearNodeHashes() + { + size_t seed = this->getSeed(); + for (Node& n : allNodes()) + n.hash = isStart(n)? seed : 0; + return move(*this); + } + + /* ===== Operators ===== */ diff --git a/wiki/thinkPad.ichthyo.mm b/wiki/thinkPad.ichthyo.mm index 4bc5f7ac8..6ca44d4be 100644 --- a/wiki/thinkPad.ichthyo.mm +++ b/wiki/thinkPad.ichthyo.mm @@ -98722,6 +98722,12 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
+ + + + + + @@ -99438,9 +99444,32 @@ Date:   Thu Apr 20 18:53:17 2023 +0200
- + + + + + + + + + + + + + + + + + + + + + + + +