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
| #ifndef __NODE_H_INCLUDED__
#define __NODE_H_INCLUDED__
#include <stdexcept> // logic_error
#include <vector>
#include <queue>
#include <memory>
template<typename T>
class Node;
template<typename T, typename ...Args>
std::unique_ptr<Node<T>> make_node(Args&& ...args);
template<typename T, typename ...Args>
std::unique_ptr<Node<T>> make_node(std::vector<std::unique_ptr<Node<T>>>&& children, Args&& ...args);
template <typename T>
class Node{
public:
using data_type = T;
~Node() = default;
/*************************
/ Parent tracking
*************************/
bool has_parent() const{
return m_parent;
}
Node<T> const& get_parent() const{
if( ! has_parent() )
throw std::logic_error("no existing parent");
return *m_parent;
}
Node<T> & get_parent() {
if( ! has_parent() )
throw std::logic_error("no existing parent");
return *m_parent;
}
/*************************
/ Children tracking
*************************/
bool has_children() const{
return !m_children.empty();
}
/*************************
/ Data access
*************************/
T const& get_data() const{
return m_data;
}
T& get_data() {
return m_data;
}
/*************************
/ Topology edition
*************************/
template <typename... Args>
Node<T> & make_child(Args&&... args ) {
auto ptr = make_node<T>(std::forward<Args>(args)...);
ptr->m_parent = this;
m_children.push_back(std::move(ptr));
return *(m_children.back());
}
Node<T> & add_child(std::unique_ptr<Node<T>>&& child){
if( child->has_parent()){
throw std::logic_error("child already has a parent");
}
child->m_parent = this;
m_children.push_back(std::move(child));
return *(m_children.back());
}
/*************************
/ Tree traversal
*************************/
template<typename Treatment>
void pre_order_DFS(Treatment const& v) const {
v(this->get_data());
for(auto const& it : this->m_children){
it->pre_order_DFS(v);
}
}
private:
Node() = default;
Node(const Node&) = delete;
Node& operator=(const Node&) = delete;
template <typename... Args>
Node(Args&&... args ) : m_data(T(std::forward<Args>(args)...)) {}
template <typename... Args>
Node(std::vector<std::unique_ptr<Node<T>>>&& children, Args&&... args ) : m_children(std::move(children)), m_data(T(std::forward<Args>(args)...)) {
for(auto & it : m_children){
it->m_parent = this;
}
}
template <typename T1, typename ...Args>
friend std::unique_ptr<Node<T1>> make_node(Args&& ...args);
template <typename T1, typename ...Args>
friend std::unique_ptr<Node<T1>> make_node(std::vector<std::unique_ptr<Node<T1>>>&& children, Args&& ...args);
Node<T>* m_parent = nullptr; // inexistence possible
std::vector<std::unique_ptr<Node<T>>> m_children;
T m_data;
};
template <typename T, typename ...Args>
std::unique_ptr<Node<T>> make_node(Args&& ...args){
return std::unique_ptr<Node<T>>(new Node<T>(std::forward<Args>(args)...));
}
template <typename T, typename ...Args>
std::unique_ptr<Node<T>> make_node(std::vector<std::unique_ptr<Node<T>>>&& children, Args&& ...args){
return std::unique_ptr<Node<T>>(new Node<T>(std::move(children), std::forward<Args>(args)...));
}
#endif |
Partager