From fd29f23d000db081da1976659e72a679b4ebb9c4 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Sun, 5 Nov 2023 21:49:24 +0100 Subject: core: Renamed tree related classes --- c++/core/tree.h | 69 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 25 deletions(-) (limited to 'c++/core') diff --git a/c++/core/tree.h b/c++/core/tree.h index 6ffd1f0..0ed7fe1 100644 --- a/c++/core/tree.h +++ b/c++/core/tree.h @@ -6,16 +6,35 @@ #include "error.h" namespace saw { +/** + * Container with a simplistic approach to a tree + */ template -class tree_or; +class tree; +/** + * Branch object holding sub trees + */ template -class tree final { +class branch final { private: - std::vector> children_; + std::vector> children_; public: - tree() = default; - ~tree() = default; + branch() = default; + + ~branch() = default; + + error_or reserve(std::size_t siz){ + try{ + children_.reserve(siz); + }catch(const std::exception& e){ + (void) e; + + return make_error(); + } + + return void_t{}; + } error_or add(T leaf) { std::size_t index = size(); @@ -33,7 +52,7 @@ public: error_or add() { std::size_t index = size(); try { - children_.push_back(tree{}); + children_.push_back(branch{}); }catch(const std::exception& e){ (void)e; @@ -47,36 +66,36 @@ public: return children_.size(); } - tree_or& at(std::size_t i){ + tree& at(std::size_t i){ return children_.at(i); } - const tree_or& at(std::size_t i) const { + const tree& at(std::size_t i) const { return children_.at(i); } }; template -class tree_or final { +class tree final { private: - using type = std::variant,T>; + using type = std::variant,T>; type tov_; - friend class tree; + friend class branch; public: - tree_or():tov_{tree{}}{} + tree():tov_{branch{}}{} - tree_or(tree nd):tov_{std::move(nd)}{} + tree(branch nd):tov_{std::move(nd)}{} - tree_or(T val):tov_{std::move(val)}{} + tree(T val):tov_{std::move(val)}{} template bool is() const { return std::holds_alternative(tov_); } - bool is_tree() const { - return std::holds_alternative(tov_); + bool is_branch() const { + return std::holds_alternative(tov_); } bool is_value() const { @@ -93,12 +112,12 @@ public: return std::get(tov_); } - tree& get_tree(){ - return std::get(tov_); + branch& get_branch(){ + return std::get>(tov_); } - const tree& get_tree() const { - return std::get(tov_); + const branch& get_branch() const { + return std::get>(tov_); } T& get_value(){ @@ -116,7 +135,7 @@ public: } NT nd = std::move(std::get(tov_)); - tov_ = tree{}; + tov_ = branch{}; return nd; } @@ -133,12 +152,12 @@ public: return eon; } - error_or> extract_tree() { - return extract>(); + error_or> extract_branch() { + return extract>(); } - error_or> replace_tree(type nd){ - return replace>(std::move(nd)); + error_or> replace_branch(type nd){ + return replace>(std::move(nd)); } error_or extract_value() { -- cgit v1.2.3