Chain-Load: calculation node - basic properties

A »Node« represents one junction point in the dependency graph,
knows his predecessors and successors and carries out one step
of the chained hash calculation.
This commit is contained in:
Fischlurch 2023-11-11 23:55:11 +01:00
parent 3ff25c1e9f
commit 7bc2c80d3a
3 changed files with 149 additions and 11 deletions

View file

@ -32,13 +32,13 @@
//#include "lib/time/timevalue.hpp"
//#include "lib/format-cout.hpp"
#include "lib/test/diagnostic-output.hpp"//////////////////////////TODO TOD-oh
//#include "lib/util.hpp"
#include "lib/util.hpp"
//using lib::time::Time;
//using lib::time::FSecs;
//using util::isSameObject;
using util::isSameObject;
//using lib::test::randStr;
//using lib::test::randTime;
@ -85,12 +85,64 @@ namespace test {
{
using Node = TestChainLoad<>::Node;
Node n0;
Node n0; // Default-created empty Node
CHECK (n0.hash == 0);
CHECK (n0.pred.size() == DEFAULT_FAN);
CHECK (n0.succ.size() == DEFAULT_FAN);
CHECK (n0.pred == Node::Tab{0});
CHECK (n0.succ == Node::Tab{0});
Node n1{23}, n2{55};
Node n1{23}, n2{55}; // further Nodes with initial seed hash
CHECK (n1.hash == 23);
CHECK (n2.hash == 55);
CHECK (0 == n0.calculate()); // hash calculation is NOP on unconnected Nodes
CHECK (0 == n0.hash);
CHECK (23 == n1.calculate());
CHECK (23 == n1.hash);
CHECK (55 == n2.calculate());
CHECK (55 == n2.hash);
n0.addPred(n1); // establish bidirectional link between Nodes
CHECK (isSameObject(*n0.pred[0], n1));
CHECK (isSameObject(*n1.succ[0], n0));
CHECK (not n0.pred[1]);
CHECK (not n1.succ[1]);
CHECK (n2.pred == Node::Tab{0});
CHECK (n2.succ == Node::Tab{0});
n2.addSucc(n0); // works likewise in the other direction
CHECK (isSameObject(*n0.pred[0], n1));
CHECK (isSameObject(*n0.pred[1], n2)); // next link added into next free slot
CHECK (isSameObject(*n2.succ[0], n0));
CHECK (not n0.pred[2]);
CHECK (not n2.succ[1]);
CHECK (n0.hash == 0);
n0.calculate(); // but now hash calculation combines predecessors
CHECK (n0.hash == 6050854883719206282u);
Node n00; // another Node...
n00.addPred(n2) // just adding the predecessors in reversed order
.addPred(n1);
CHECK (n00.hash == 0);
n00.calculate(); // ==> hash is different, since it depends on order
CHECK (n00.hash == 17052526497278249714u);
CHECK (n0.hash == 6050854883719206282u);
CHECK (isSameObject(*n1.succ[0], n0));
CHECK (isSameObject(*n1.succ[1], n00));
CHECK (isSameObject(*n2.succ[0], n0));
CHECK (isSameObject(*n2.succ[1], n00));
CHECK (isSameObject(*n00.pred[0], n2));
CHECK (isSameObject(*n00.pred[1], n1));
CHECK (isSameObject(*n0.pred[0], n1));
CHECK (isSameObject(*n0.pred[1], n2));
CHECK (n00.hash == 17052526497278249714u);
n00.calculate(); // calculation is NOT idempotent (inherently statefull)
CHECK (n00.hash == 13151338213516862912u);
}

View file

@ -58,6 +58,7 @@
//#include "lib/format-util.hpp"
//#include "lib/util.hpp"
#include <boost/functional/hash.hpp>
//#include <functional>
//#include <utility>
//#include <string>
@ -81,11 +82,14 @@ namespace test {
// using util::isnil;
// using std::forward;
// using std::move;
using boost::hash_combine;
namespace {// Diagnostic markers
// const string MARK_INC{"IncSeq"};
// const string MARK_SEQ{"Seq"};
const size_t DEFAULT_FAN = 16;
const size_t DEFAULT_SIZ = 256;
// using SIG_JobDiagnostic = void(Time, int32_t);
}
@ -97,7 +101,7 @@ namespace test {
* @tparam maxFan maximal fan-in/out from a node, also limits maximal parallel strands.
* @see TestChainLoad_test
*/
template<size_t numNodes =256, size_t maxFan =16>
template<size_t numNodes =DEFAULT_SIZ, size_t maxFan =DEFAULT_FAN>
class TestChainLoad
: util::NonCopyable
{
@ -106,11 +110,60 @@ namespace test {
struct Node
: util::MoveOnly
{
using Tab = std::array<Node*, maxFan>;
size_t hash;
Tab pred;
Tab succ;
Node(size_t seed =0)
: hash{seed}
, pred{0}
, succ{0}
{ }
Node&
addPred (Node& other)
{
for (Node*& entry : pred)
if (not entry)
{
entry = &other;
for (Node*& backlink : other.succ)
if (not backlink)
{
backlink = this;
return *this;
}
}
NOTREACHED ("excess node linkage");
}
Node&
addSucc (Node& other)
{
for (Node*& entry : succ)
if (not entry)
{
entry = &other;
for (Node*& backlink : other.pred)
if (not backlink)
{
backlink = this;
return *this;
}
}
NOTREACHED ("excess node linkage");
}
size_t
calculate()
{
for (Node*& entry: pred)
if (entry)
hash_combine (hash, entry->hash);
return hash;
}
};
private:

View file

@ -95855,11 +95855,11 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
<icon BUILTIN="pencil"/>
<node BACKGROUND_COLOR="#eef0c5" COLOR="#990000" CREATED="1699746806718" ID="ID_1563586426" MODIFIED="1699747572592" TEXT="Grundstrukturen anlegen">
<icon BUILTIN="pencil"/>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1699746880572" ID="ID_1016750692" MODIFIED="1699746885284" TEXT="Rahmen">
<icon BUILTIN="flag-yellow"/>
<node BACKGROUND_COLOR="#eef0c5" COLOR="#990000" CREATED="1699746880572" ID="ID_1016750692" MODIFIED="1699758515003" TEXT="Rahmen">
<icon BUILTIN="pencil"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1699746814207" ID="ID_1830964354" MODIFIED="1699746816453" TEXT="Node">
<icon BUILTIN="flag-yellow"/>
<node BACKGROUND_COLOR="#eef0c5" COLOR="#990000" CREATED="1699746814207" ID="ID_1830964354" MODIFIED="1699758515002" TEXT="Node">
<icon BUILTIN="pencil"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1699746817756" ID="ID_490992503" MODIFIED="1699746834411" TEXT="Generator">
<icon BUILTIN="flag-yellow"/>
@ -95883,12 +95883,45 @@ Date:&#160;&#160;&#160;Thu Apr 20 18:53:17 2023 +0200<br/>
<node BACKGROUND_COLOR="#f8f1cb" COLOR="#a50125" CREATED="1699747433986" ID="ID_1557543092" MODIFIED="1699747548608" TEXT="setzt Berechnung aller Prefecessors vorraus">
<icon BUILTIN="messagebox_warning"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1699747511840" ID="ID_96811527" MODIFIED="1699747533636" TEXT="speichert das Ergebnis per Seiteneffekt">
<icon BUILTIN="flag-yellow"/>
<node COLOR="#338800" CREATED="1699747511840" ID="ID_96811527" MODIFIED="1699758489537" TEXT="speichert das Ergebnis per Seiteneffekt">
<icon BUILTIN="button_ok"/>
</node>
<node BACKGROUND_COLOR="#eee5c3" COLOR="#990000" CREATED="1699747521150" ID="ID_356376425" MODIFIED="1699747533637" TEXT="kann durch Repetitionen aufwendig gemacht werden">
<icon BUILTIN="flag-yellow"/>
</node>
<node COLOR="#435e98" CREATED="1699756460900" ID="ID_1877216077" MODIFIED="1699758391567" TEXT="Implementierung per boost-Hash">
<icon BUILTIN="yes"/>
<node CREATED="1699756475850" ID="ID_1420065302" MODIFIED="1699756491156" TEXT="wie &#xfc;blich: weil wesentlich einfacher zu benutzen"/>
<node CREATED="1699756491632" ID="ID_1814709620" MODIFIED="1699756499051" TEXT="und: Boost bietet bereits hash_combine"/>
<node BACKGROUND_COLOR="#f8f1cb" COLOR="#a50125" CREATED="1699758395938" ID="ID_1681365504" MODIFIED="1699758441229">
<richcontent TYPE="NODE"><html>
<head>
</head>
<body>
<p>
Konsequenz &#10233; <b>nicht</b>&#160;idempotent
</p>
</body>
</html>
</richcontent>
<icon BUILTIN="messagebox_warning"/>
</node>
</node>
</node>
<node BACKGROUND_COLOR="#eef0c5" COLOR="#990000" CREATED="1699758530360" ID="ID_1331344948" MODIFIED="1699758538983" TEXT="Verlinkung">
<icon BUILTIN="pencil"/>
<node BACKGROUND_COLOR="#f0d5c5" COLOR="#990033" CREATED="1699758540245" ID="ID_1948581542" MODIFIED="1699758595139" TEXT="Zahl der Verbindungen implizit codiert">
<icon BUILTIN="help"/>
<node CREATED="1699758599662" ID="ID_475795635" MODIFIED="1699758623119" TEXT="elegant zu coden so"/>
<node CREATED="1699758626410" ID="ID_254710650" MODIFIED="1699758644732" TEXT="aber nat&#xfc;rlich quadratisch"/>
</node>
<node COLOR="#338800" CREATED="1699758650056" ID="ID_232473697" MODIFIED="1699758655959" TEXT="Backlinks einf&#xfc;gen">
<icon BUILTIN="button_ok"/>
</node>
<node COLOR="#338800" CREATED="1699758656642" ID="ID_1868827896" MODIFIED="1699758667474" TEXT="Limit der Verlinkung pr&#xfc;fen">
<icon BUILTIN="button_ok"/>
</node>
</node>
</node>
</node>