diff options
author | Claudius "keldu" Holeksa <mail@keldu.de> | 2023-11-05 21:49:24 +0100 |
---|---|---|
committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2023-11-05 21:49:24 +0100 |
commit | fd29f23d000db081da1976659e72a679b4ebb9c4 (patch) | |
tree | 5a26aae022eb141f173906915f5e0951b042a399 | |
parent | c5607d106449cc3c8680e95ec8595370996d5fb1 (diff) |
core: Renamed tree related classes
-rw-r--r-- | c++/core/tree.h | 69 | ||||
-rw-r--r-- | tests/tree.cpp | 16 |
2 files changed, 54 insertions, 31 deletions
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<typename T> -class tree_or; +class tree; +/** + * Branch object holding sub trees + */ template<typename T> -class tree final { +class branch final { private: - std::vector<tree_or<T>> children_; + std::vector<tree<T>> children_; public: - tree() = default; - ~tree() = default; + branch() = default; + + ~branch() = default; + + error_or<void> reserve(std::size_t siz){ + try{ + children_.reserve(siz); + }catch(const std::exception& e){ + (void) e; + + return make_error<err::out_of_memory>(); + } + + return void_t{}; + } error_or<std::size_t> add(T leaf) { std::size_t index = size(); @@ -33,7 +52,7 @@ public: error_or<std::size_t> 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<T>& at(std::size_t i){ + tree<T>& at(std::size_t i){ return children_.at(i); } - const tree_or<T>& at(std::size_t i) const { + const tree<T>& at(std::size_t i) const { return children_.at(i); } }; template<typename T> -class tree_or final { +class tree final { private: - using type = std::variant<tree<T>,T>; + using type = std::variant<branch<T>,T>; type tov_; - friend class tree<T>; + friend class branch<T>; public: - tree_or():tov_{tree<T>{}}{} + tree():tov_{branch<T>{}}{} - tree_or(tree<T> nd):tov_{std::move(nd)}{} + tree(branch<T> nd):tov_{std::move(nd)}{} - tree_or(T val):tov_{std::move(val)}{} + tree(T val):tov_{std::move(val)}{} template<typename NT> bool is() const { return std::holds_alternative<NT>(tov_); } - bool is_tree() const { - return std::holds_alternative<tree>(tov_); + bool is_branch() const { + return std::holds_alternative<branch>(tov_); } bool is_value() const { @@ -93,12 +112,12 @@ public: return std::get<NT>(tov_); } - tree<T>& get_tree(){ - return std::get<tree>(tov_); + branch<T>& get_branch(){ + return std::get<branch<T>>(tov_); } - const tree<T>& get_tree() const { - return std::get<tree>(tov_); + const branch<T>& get_branch() const { + return std::get<branch<T>>(tov_); } T& get_value(){ @@ -116,7 +135,7 @@ public: } NT nd = std::move(std::get<NT>(tov_)); - tov_ = tree<T>{}; + tov_ = branch<T>{}; return nd; } @@ -133,12 +152,12 @@ public: return eon; } - error_or<tree<T>> extract_tree() { - return extract<tree<T>>(); + error_or<branch<T>> extract_branch() { + return extract<branch<T>>(); } - error_or<tree<T>> replace_tree(type nd){ - return replace<tree<T>>(std::move(nd)); + error_or<branch<T>> replace_branch(type nd){ + return replace<branch<T>>(std::move(nd)); } error_or<T> extract_value() { diff --git a/tests/tree.cpp b/tests/tree.cpp index de6bda6..ca57a17 100644 --- a/tests/tree.cpp +++ b/tests/tree.cpp @@ -5,16 +5,16 @@ namespace { SAW_TEST("Tree insert"){ using namespace saw; - tree_or<int> troi; + tree<int> troi; { // Replace with value - auto eor = troi.replace_tree(5); + auto eor = troi.replace_branch(5); SAW_EXPECT(eor.is_value(), "Didn't manage to replace with value"); } { - // Replace with tree again - auto eor = troi.replace_value(tree<int>{}); + // Replace with branch again + auto eor = troi.replace_value(branch<int>{}); SAW_EXPECT(eor.is_value(), "Not a value"); } @@ -23,16 +23,20 @@ SAW_TEST("Tree insert"){ SAW_TEST("Tree add child"){ using namespace saw; - tree<int> tr; + branch<int> tr; { auto eov = tr.add(10); SAW_EXPECT(eov.is_value(), "Didn't manage to add value"); } { auto eov = tr.add(); - SAW_EXPECT(eov.is_value(), "Didn't manage to add tree"); + SAW_EXPECT(eov.is_value(), "Didn't manage to add branch"); std::size_t index = eov.get_value(); + auto& inner_tr = tr.at(index); + + auto eov2 = inner_tr.get_branch().add(420); + SAW_EXPECT(eov2.is_value(), "Didn't manage to add to inner branch"); } } } |