From efd94d1cf1a3709365c4c144875524819a6a15a3 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Sun, 3 Dec 2023 23:06:43 +0100 Subject: core: Tree container impl. Partially broken, fix on laptop --- c++/core/tree.h | 80 +++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 25 deletions(-) (limited to 'c++/core/tree.h') diff --git a/c++/core/tree.h b/c++/core/tree.h index 32572fc..43f6c2c 100644 --- a/c++/core/tree.h +++ b/c++/core/tree.h @@ -19,27 +19,26 @@ class branch; * 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 tree final { +template +class tree_container final { private: /** * Object holding the treeed branch instances */ - using branch = std::variant>; - std::vector children_; + std::vector> children_; public: /** * Default constructor */ - tree() = default; + tree_container() = default; /** * Destructor */ - ~tree() = default; + ~tree_container() = default; - SAW_FORBID_COPY(tree); - SAW_DEFAULT_MOVE(tree); + SAW_FORBID_COPY(tree_container); + SAW_DEFAULT_MOVE(tree_container); /** * Reserve space for siz elements @@ -102,36 +101,36 @@ public: /** * Returns the branch at i */ - branch& at(std::size_t i){ + branch& at(std::size_t i){ return children_.at(i); } /** * Returns the branch at i */ - const branch& at(std::size_t i) const { + const branch& at(std::size_t i) const { return children_.at(i); } }; -template +template class branch final { private: - using type = std::variant,T>; + using type = std::variant; type tov_; /** * 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; + friend class Tree; public: /** * */ - branch():tov_{tree{}}{} + branch():tov_{Tree{}}{} - branch(tree nd):tov_{std::move(nd)}{} + branch(Tree nd):tov_{std::move(nd)}{} branch(T val):tov_{std::move(val)}{} @@ -144,7 +143,7 @@ public: } bool is_tree() const { - return std::holds_alternative(tov_); + return std::holds_alternative(tov_); } bool is_value() const { @@ -161,12 +160,12 @@ public: return std::get(tov_); } - tree& get_tree(){ - return std::get>(tov_); + Tree& get_tree(){ + return std::get(tov_); } - const tree& get_tree() const { - return std::get>(tov_); + const Tree& get_tree() const { + return std::get(tov_); } T& get_value(){ @@ -179,12 +178,13 @@ public: template error_or extract(){ + error_or reserve(std::size_t siz){ if(!is()){ return make_error(); } NT nd = std::move(std::get(tov_)); - tov_ = tree{}; + tov_ = Tree{}; return nd; } @@ -201,12 +201,12 @@ public: return eon; } - error_or> extract_tree() { - return extract>(); + error_or extract_tree() { + return extract(); } - error_or> replace_tree(type nd){ - return replace>(std::move(nd)); + error_or replace_tree(type nd){ + return replace(std::move(nd)); } error_or extract_value() { @@ -217,4 +217,34 @@ public: return replace(std::move(nd)); } }; + +template +class tree { + private: + tree_container> data_; + public: + error_or reserve(std::size_t size){ + return data_.reserve(size); + } + + size_t size() const { + return data_.size(); + } + + error_or add() { + return data_.add(); + } + + error_or add(T leaf){ + return data_.add(std::move(leaf)); + } + + branch>& at(size_t i){ + return data_.at(i); + } + + const branch>& at(size_t i) const { + return data_.at(i); + } +}; } -- cgit v1.2.3