dl
dl_tree.h
浏览该文件的文档.
1
13#pragma once
14
15#include <vector>
16#include <list>
17#include <functional>
18#include <unordered_map>
19#include <cassert>
20
21#include "io/dl_log.h"
22
23namespace dl
24{
26template<typename T>
27class NTree
28{
29public:
30 class Node
31 {
32 friend class NTree<T>;
33 public:
34 Node(const T& v, Node* parent) :
35 _parent{ parent },
36 _v{v}
37 {}
38
39 T& GetValue() { return _v; }
40 const T& GetValue() const { return _v; }
41 Node* GetParent() { return _parent; }
42 std::vector<Node*>& GetChild() { return _child; }
43 const std::vector<Node*>& GetChild() const { return _child; }
44 private:
45 T _v;
46 Node* _parent;
47 std::vector<Node*> _child;
48 };
49
51 {}
52
56 Node* AddRoot(const T& v)
57 {
58 if (!_allNode.empty())
59 {
60 log_err0("根节点已经存在,不能重复添加!");
61 return nullptr;
62 }
63 Node* node = new Node(v, nullptr);
64 _allNode.push_back(node);
65 return node;
66 }
67
71 Node* Add(const T& v, Node* node_parent)
72 {
73 if (!node_parent)
74 {
75 return AddRoot(v);
76 }
77 Node* node = new Node(v, node_parent);
78 _allNode.push_back(node);
79 node_parent->_child.push_back(node);
80 return node;
81 }
82 Node* Add(const T& v, const T& parent)
83 {
84 Node* node_parent = Get(parent);
85 if (!node_parent)
86 {
87 log_err0("父节点不存在!");
88 return nullptr;
89 }
90 return Add(v, node_parent);
91 }
92
96 bool Del(Node* node)
97 {
98 auto iter = std::find(_allNode.begin(), _allNode.end(), node);
99 if (iter == _allNode.end())
100 {
101 log_err0("节点不存在!");
102 return false;
103 }
104 // 从父节点移除
105 auto iter1 = std::find(node->_parent->_child.begin(), node->_parent->_child.end(), node);
106 if (iter1 == node->_parent->_child.end())
107 {
108 log_err0("父节点引用错误!");
109 return false;
110 }
111 node->_parent->_child.erase(iter1);
112
113 // 自己子节点连接到自己父节点
114 for (Node* node_child : node->_child)
115 {
116 node->_parent->_child.push_back(node_child);
117 node_child->_parent = node->_parent;
118 }
119
120 // 从主列表移除
121 delete node;
122 _allNode.erase(iter);
123 return true;
124 }
125
129 {
130 for (Node* node : _allNode)
131 {
132 if (v == node->_v)
133 return node;
134 }
135 return nullptr;
136 }
137
140 bool Has(Node* node)
141 {
142 for (Node* iter : _allNode)
143 {
144 if (iter == node)
145 return true;
146 }
147 return false;
148 }
149
152 void SetParent(Node* node, Node* parent)
153 {
154 if (!parent)
155 {
156 log_err0("parent不能为空!");
157 return;
158 }
159 if (!node->_parent)
160 {
161 log_err0("自己不能为空节点!");
162 return;
163 }
164
165 if (!Has(node))
166 {
167 log_err0("节点不存在!");
168 return;
169 }
170 if (!Has(parent))
171 {
172 log_err0("parent节点不存在!");
173 return;
174 }
175 auto& vec_child = node->_parent->GetChild();
176 auto iter_child = std::find(vec_child.begin(), vec_child.end(), node);
177 if (iter_child == vec_child.end())
178 {
179 log_err0("旧parent不包含自身!");
180 return;
181 }
182 vec_child.erase(iter_child);
183 parent->_child.push_back(node);
184 node->_parent = parent;
185 }
186
189 template<typename F>
190 void ForEach(F func)
191 {
192 if (_allNode.empty())
193 return;
194
195 std::stack<Node*> s;
196 s.push(_allNode.front());
197
198 while (!s.empty())
199 {
200 Node* node_this = s.top();
201 s.pop();
202 func(node_this->_v);
203
204 // 反向遍历
205 auto& c = node_this->_child;
206 for (auto iter = c.rbegin(); iter != c.rend(); ++iter)
207 {
208 Node* node = *iter;
209 s.push(node);
210 }
211 }
212 }
213 //std::function<void(const T1&, T&)> func
217 template<typename T1, typename F>
218 void Copy(const NTree<T1>& b, F func)
219 {
220 // 先清空
221 for (NTree::Node* node : _allNode)
222 {
223 delete node;
224 }
225 _allNode.clear();
226
227 // 指针映射表
228 std::unordered_map<NTree::Node*, NTree<T>::Node*> map_ptr;
229 // 构建Node,并生成 指针映射表
230 for (NTree::Node* node_b : b._allNode)
231 {
232 NTree<T>::Node* node = new NTree<T>::Node(0, nullptr);
233 _allNode.push_back(node);
234
235 map_ptr[node_b] = node;
236 }
237 // 构建 数据
238 for (NTree::Node* node_b : b._allNode)
239 {
240 NTree<T>::Node* node = map_ptr[node_b];
241 node->_parent = map_ptr[node_b->_parent];
242 for (auto& iter : node_b->_child)
243 {
244 node->_child.push_back(map_ptr[iter]);
245 }
246 func(node_b->_v, node->_v);
247 _allNode.push_back(node);
248 }
249 }
250
251 const std::list<Node*>& GetAll()
252 {
253 return _allNode;
254 }
255
256 const Node* GetRoot() const
257 {
258 if (_allNode.empty())
259 return nullptr;
260 return _allNode.front();
261 }
262
263 size_t GetSize() const
264 {
265 return _allNode.size();
266 }
267
269 auto begin() { return _allNode.begin(); }
270 auto end() { return _allNode.end(); }
271
272
274 {
275 for (Node* node : _allNode)
276 {
277 delete node;
278 }
279 }
280private:
281 std::list<Node*> _allNode;//所有节点,第一个为根节点
282};
283
284}
const std::vector< Node * > & GetChild() const
const T & GetValue() const
Node(const T &v, Node *parent)
std::vector< Node * > & GetChild()
void ForEach(F func)
遍历,保证父节点先遍历
const Node * GetRoot() const
返回根节点,空树返回空
Node * Get(T v)
查找,失败返回nullptr
const std::list< Node * > & GetAll()
返回所有节点
auto begin()
标准库迭代方法不保证父节点先遍历
void Copy(const NTree< T1 > &b, F func)
从另一个树填充
void SetParent(Node *node, Node *parent)
修改父节点
Node * AddRoot(const T &v)
添加根节点
Node * Add(const T &v, Node *node_parent)
添加子节点
Node * Add(const T &v, const T &parent)
bool Del(Node *node)
删除节点
bool Has(Node *node)
包含节点
size_t GetSize() const
返回总数量
日志系统
void log_err0(std::string_view str, const std::source_location location=std::source_location::current())