summaryrefslogtreecommitdiff
path: root/c++
diff options
context:
space:
mode:
Diffstat (limited to 'c++')
-rw-r--r--c++/core/tree.h69
1 files changed, 44 insertions, 25 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() {