summaryrefslogtreecommitdiff
path: root/c++/core/tree.h
blob: 32572fc4bdbccade31173a30fbe71f34497dd409 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#pragma once

#include <variant>
#include <vector>

#include "error.h"

namespace saw {
/**
 * Container with a simplistic approach to a branch
 */
template<typename T>
class branch;

/**
 * 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<typename T>
class tree final {
private:
	/**
	 * Object holding the treeed branch instances
	 */
    using branch = std::variant<T, tree<T>>;
	std::vector<branch> children_;
public:
	/**
	 * Default constructor
	 */
	tree() = default;

	/**
	 * Destructor
	 */
	~tree() = default;

	SAW_FORBID_COPY(tree);
	SAW_DEFAULT_MOVE(tree);

	/**
	 * Reserve space for siz elements
	 */
	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{};
	}

	/**
	 * Add a branch with a leaf attached to the tree
	 */
	error_or<std::size_t> add(T leaf) {
		std::size_t index = size();
		try {
			/**
			 * Technically we're adding a leaf on a branch
			 */
			children_.emplace_back(std::move(leaf));
		}catch(const std::exception& e){
			(void)e;

			return make_error<err::critical>();
		}

		return index;
	}

	/**
	 * Add a branch to the tree with a tree attached
	 */
	error_or<std::size_t> add() {
		std::size_t index = size();
		try {

			children_.emplace_back(tree{});
		}catch(const std::exception& e){
			(void)e;

			return make_error<err::critical>();
		}

		return index;
	}

	/**
	 * Returns the amount of branches contained within this tree level
	 */
	std::size_t size() const {
		return children_.size();
	}

	/**
	 * Returns the branch at i
	 */
	branch& at(std::size_t i){
		return children_.at(i);
	}

	/**
	 * Returns the branch at i
	 */
	const branch& at(std::size_t i) const {
		return children_.at(i);
	}
};

template<typename T>
class branch final {
private:
	using type = std::variant<tree<T>,T>;
	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<T>;
public:
	/**
	 * 
	 */
	branch():tov_{tree<T>{}}{}

	branch(tree<T> nd):tov_{std::move(nd)}{}

	branch(T val):tov_{std::move(val)}{}

	SAW_FORBID_COPY(branch);
	SAW_DEFAULT_MOVE(branch);

	template<typename NT>
	bool is() const {
		return std::holds_alternative<NT>(tov_);
	}

	bool is_tree() const {
		return std::holds_alternative<tree>(tov_);
	}

	bool is_value() const {
		return std::holds_alternative<T>(tov_);
	}

	template<typename NT>
	NT& get() {
		return std::get<NT>(tov_);
	}
	
	template<typename NT>
	const NT& get() const {
		return std::get<NT>(tov_);
	}

	tree<T>& get_tree(){
		return std::get<tree<T>>(tov_);
	}

	const tree<T>& get_tree() const {
		return std::get<tree<T>>(tov_);
	}

	T& get_value(){
		return std::get<T>(tov_);
	}

	const T& get_value() const {
		return std::get<T>(tov_);
	}

	template<typename NT>
	error_or<NT> extract(){
		if(!is<NT>()){
			return make_error<err::invalid_state>();
		}
		
		NT nd = std::move(std::get<NT>(tov_));
		tov_ = tree<T>{};

		return nd;
	}
	
	template<typename NT>
	error_or<NT> replace(type nd){
		auto eon = extract<NT>();
		if(eon.is_error()){
			return eon;
		}

		tov_ = std::move(nd);

		return eon;
	}

	error_or<tree<T>> extract_tree() {
		return extract<tree<T>>();
	}

	error_or<tree<T>> replace_tree(type nd){
		return replace<tree<T>>(std::move(nd));
	}

	error_or<T> extract_value() {
		return extract<T>();
	}

	error_or<T> replace_value(type nd){
		return replace<T>(std::move(nd));
	}
};
}