From d206499057ceaf6927be4fbc3f54e25dacd83034 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Thu, 9 Nov 2023 15:29:49 +0100 Subject: core: Renaming tree and branch --- c++/core/tree.h | 108 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 78 insertions(+), 30 deletions(-) (limited to 'c++/core') diff --git a/c++/core/tree.h b/c++/core/tree.h index 0ed7fe1..6316a5e 100644 --- a/c++/core/tree.h +++ b/c++/core/tree.h @@ -7,23 +7,42 @@ namespace saw { /** - * Container with a simplistic approach to a tree + * Container with a simplistic approach to a branch */ template -class tree; +class branch; /** - * Branch object holding sub trees + * Tree object holding branches. + * + * The name comes from the fact a tree is acting as a node while the branch class is the + * edge to a leaf or other nodes. A tree holds the branches while the branch either has + * a leaf or another sub tree. */ template -class branch final { +class tree final { private: - std::vector> children_; + /** + * Object holding the treeed branch instances + */ + std::vector> children_; public: - branch() = default; - - ~branch() = default; - + /** + * Default constructor + */ + tree() = default; + + /** + * Destructor + */ + ~tree() = default; + + SAW_FORBID_COPY(tree); + SAW_DEFAULT_MOVE(tree); + + /** + * Reserve space for siz elements + */ error_or reserve(std::size_t siz){ try{ children_.reserve(siz); @@ -36,10 +55,16 @@ public: return void_t{}; } + /** + * Add a branch with a leaf attached to the tree + */ error_or add(T leaf) { std::size_t index = size(); try { - children_.push_back(std::move(leaf)); + /** + * Technically we're adding a leaf on a branch + */ + children_.emplace_back(std::move(leaf)); }catch(const std::exception& e){ (void)e; @@ -49,10 +74,14 @@ public: return index; } + /** + * Add a branch to the tree with a tree attached + */ error_or add() { std::size_t index = size(); try { - children_.push_back(branch{}); + + children_.emplace_back(tree{}); }catch(const std::exception& e){ (void)e; @@ -62,40 +91,59 @@ public: return index; } + /** + * Returns the amount of branches contained within this tree level + */ std::size_t size() const { return children_.size(); } - tree& at(std::size_t i){ + /** + * Returns the branch at i + */ + branch& at(std::size_t i){ return children_.at(i); } - const tree& at(std::size_t i) const { + /** + * Returns the branch at i + */ + const branch& at(std::size_t i) const { return children_.at(i); } }; template -class tree final { +class branch final { private: - using type = std::variant,T>; + using type = std::variant,T>; type tov_; - friend class branch; + /** + * We're friend classing the tree since it's way easier this way and the branch and tree + * class are intertwined heavily anyway. + */ + friend class tree; public: - tree():tov_{branch{}}{} + /** + * + */ + branch():tov_{tree{}}{} + + branch(tree nd):tov_{std::move(nd)}{} - tree(branch nd):tov_{std::move(nd)}{} + branch(T val):tov_{std::move(val)}{} - tree(T val):tov_{std::move(val)}{} + SAW_FORBID_COPY(branch); + SAW_DEFAULT_MOVE(branch); template bool is() const { return std::holds_alternative(tov_); } - bool is_branch() const { - return std::holds_alternative(tov_); + bool is_tree() const { + return std::holds_alternative(tov_); } bool is_value() const { @@ -112,12 +160,12 @@ public: return std::get(tov_); } - branch& get_branch(){ - return std::get>(tov_); + tree& get_tree(){ + return std::get>(tov_); } - const branch& get_branch() const { - return std::get>(tov_); + const tree& get_tree() const { + return std::get>(tov_); } T& get_value(){ @@ -135,7 +183,7 @@ public: } NT nd = std::move(std::get(tov_)); - tov_ = branch{}; + tov_ = tree{}; return nd; } @@ -152,12 +200,12 @@ public: return eon; } - error_or> extract_branch() { - return extract>(); + error_or> extract_tree() { + return extract>(); } - error_or> replace_branch(type nd){ - return replace>(std::move(nd)); + error_or> replace_tree(type nd){ + return replace>(std::move(nd)); } error_or extract_value() { -- cgit v1.2.3