Files
Radio/module/radio/Toolbox_private/Node.hpp
T
2026-06-16 13:38:08 +08:00

97 lines
2.9 KiB
C++

#ifndef Toolbox_Node_H
#define Toolbox_Node_H
#include <QVector>
#include <QQueue>
namespace Toolbox {
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.mBufferSize()) return nullptr;
return father->sons[i];
}
That *lastChild() {
if (sons.empty()) return nullptr;
return sons.last();
}
That *firstChild() {
if (sons.empty()) return nullptr;
return sons.first();
}
That *rootNode() {
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 *> deepTraversed() {
std::vector<That *> ret;
ret.append(static_cast<That *>(this));
for (auto &child: sons) {
ret.append(child->deepTraversed());
}
return ret;
}
QVector<That *> sequenceTraversed() {
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.mBufferSize();
ret.append(cur);
for (int i = 0; i < n; ++i) {
que.enqueue(cur->sons[i]);
}
}
return ret;
}
QVector<That *> descendants() {
QVector<That *> &&ret = sequenceTraversed();
ret.pop_front();
return ret;
}
That *father = nullptr;
QVector<That *> sons;
};
template<typename NodeType>
class NodeManager {
public:
QVector<NodeType *> roots;
};
}
#endif