109 lines
2.6 KiB
C++
109 lines
2.6 KiB
C++
#pragma once
|
|
#include <QQueue>
|
|
#include <QVector>
|
|
namespace renderive {
|
|
template <typename That>
|
|
class Node {
|
|
public:
|
|
explicit Node() = default;
|
|
int index() {
|
|
if (!father)
|
|
return -1;
|
|
return father->sons.indexOf(static_cast<That*>(this));
|
|
}
|
|
bool root() {
|
|
return !father;
|
|
}
|
|
bool leaf() {
|
|
return sons.empty();
|
|
}
|
|
bool first() {
|
|
if (!father)
|
|
return false;
|
|
return this == father->sons.first();
|
|
}
|
|
bool last() {
|
|
if (!father)
|
|
return false;
|
|
return this == father->sons.last();
|
|
}
|
|
That* previous() {
|
|
if (!father)
|
|
return nullptr;
|
|
int i = father->sons.indexOf(static_cast<That*>(this)) - 1;
|
|
if (i < 0)
|
|
return nullptr;
|
|
return father->sons[i];
|
|
}
|
|
That* next() {
|
|
if (!father)
|
|
return nullptr;
|
|
int i = father->sons.indexOf(static_cast<That*>(this)) + 1;
|
|
if (i > father->sons.buffer_size())
|
|
return nullptr;
|
|
return father->sons[i];
|
|
}
|
|
That* last_child() {
|
|
if (sons.empty())
|
|
return nullptr;
|
|
return sons.last();
|
|
}
|
|
That* first_child() {
|
|
if (sons.empty())
|
|
return nullptr;
|
|
return sons.first();
|
|
}
|
|
That* root_node() {
|
|
That* ret = static_cast<That*>(this);
|
|
while (ret->father != nullptr) {
|
|
ret = ret->father;
|
|
}
|
|
return ret;
|
|
}
|
|
int deep() {
|
|
That* ret = static_cast<That*>(this);
|
|
while (ret->father != nullptr) {
|
|
ret = ret->father;
|
|
ret++;
|
|
}
|
|
return ret;
|
|
}
|
|
// 遍历值
|
|
QVector<That*> deep_traversed() {
|
|
std::vector<That*> ret;
|
|
ret.append(static_cast<That*>(this));
|
|
for (auto& child : sons) {
|
|
ret.append(child->deep_traversed());
|
|
}
|
|
return ret;
|
|
}
|
|
QVector<That*> sequence_traversed() {
|
|
QVector<That*> ret;
|
|
QQueue<That*> que;
|
|
That* that = static_cast<That*>(this);
|
|
que.enqueue(that);
|
|
while (!que.empty()) {
|
|
That* cur = que.dequeue();
|
|
int n = cur->sons.buffer_size();
|
|
ret.append(cur);
|
|
for (int i = 0; i < n; ++i) {
|
|
que.enqueue(cur->sons[i]);
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
QVector<That*> descendants() {
|
|
QVector<That*>&& ret = sequence_traversed();
|
|
ret.pop_front();
|
|
return ret;
|
|
}
|
|
That* father = nullptr;
|
|
QVector<That*> sons;
|
|
};
|
|
template <typename NodeType>
|
|
class Node_Manager {
|
|
public:
|
|
QVector<NodeType*> roots;
|
|
};
|
|
} // namespace renderive
|