自定义内存分配池

This commit is contained in:
2026-07-29 18:08:57 +08:00
parent 5a3511bb58
commit d943d82554
122 changed files with 3602 additions and 1215 deletions
+44 -38
View File
@@ -1,6 +1,7 @@
#include "Tree_Model.h"
#include <QQueue>
#include <QStack>
#include <queue>
#include <stack>
#include <algorithm>
#include <iostream>
namespace Psc {
int Node::get_deep() {
@@ -15,7 +16,8 @@ int Node::get_deep() {
Node::Node() {
}
int Node::index() {
return par->sons.indexOf(static_cast<Node*>(this));
auto found = std::find(par->sons.begin(), par->sons.end(), static_cast<Node*>(this));
return found == par->sons.end() ? -1 : static_cast<int>(std::distance(par->sons.begin(), found));
}
bool Node::is_root() {
return par == nullptr;
@@ -56,15 +58,16 @@ Node* Node::root() {
}
return ret;
}
QVector<Node*> Node::deep_traversed() {
QVector<Node*> result;
std::vector<Node*> Node::deep_traversed() {
std::vector<Node*> result;
if (!this)
return result;
QStack<Node*> stack;
std::stack<Node*> stack;
stack.push(this);
while (!stack.isEmpty()) {
Node* node = stack.pop();
result.append(node);
while (!stack.empty()) {
Node* node = stack.top();
stack.pop();
result.push_back(node);
// 逆序压入子节点,保证从左到右的遍历顺序
for (int i = node->sons.size() - 1; i >= 0; --i) {
stack.push(node->sons[i]);
@@ -72,38 +75,40 @@ QVector<Node*> Node::deep_traversed() {
}
return result;
}
QVector<Node*> Node::sequence_traversed() {
QVector<Node*> ret;
QQueue<Node*> que;
std::vector<Node*> Node::sequence_traversed() {
std::vector<Node*> ret;
std::queue<Node*> que;
Node* that = this;
que.enqueue(that);
que.push(that);
while (!que.empty()) {
Node* cur = que.dequeue();
Node* cur = que.front();
que.pop();
int n = cur->sons.size();
ret.append(cur);
ret.push_back(cur);
for (int i = 0; i < n; ++i) {
que.enqueue(cur->sons[i]);
que.push(cur->sons[i]);
}
}
return ret;
}
QVector<Node*> Node::descendants() {
QVector<Node*>&& ret = sequence_traversed();
ret.pop_front();
std::vector<Node*> Node::descendants() {
std::vector<Node*>&& ret = sequence_traversed();
ret.erase(ret.begin());
return ret;
}
QVector<Node*> deep_traversed(const QVector<Node*>& nodes) {
QVector<Node*> result;
if (nodes.isEmpty())
std::vector<Node*> deep_traversed(const std::vector<Node*>& nodes) {
std::vector<Node*> result;
if (nodes.empty())
return result;
QStack<Node*> stack;
std::stack<Node*> stack;
for (Node* node : nodes) {
if (node)
stack.push_front(node);
stack.push(node);
}
while (!stack.isEmpty()) {
Node* node = stack.pop();
result.append(node);
while (!stack.empty()) {
Node* node = stack.top();
stack.pop();
result.push_back(node);
// 逆序压入子节点,保证从左到右的遍历顺序
for (int i = node->sons.size() - 1; i >= 0; --i) {
stack.push(node->sons[i]);
@@ -114,14 +119,14 @@ QVector<Node*> deep_traversed(const QVector<Node*>& nodes) {
#define ReMain (pos - total_height)
int Tree_Model::height() {
int total_height = 0;
QStack<QPair<Node*, int>> stack;
std::stack<std::pair<Node*, int>> stack;
for (Node* node : roots) {
if (node)
stack.push_front({node, 0});
stack.push({node, 0});
}
Node* cur{};
int deep{};
while (!stack.isEmpty()) {
while (!stack.empty()) {
auto t = stack.top();
cur = t.first;
deep = t.second;
@@ -135,16 +140,16 @@ int Tree_Model::height() {
}
return total_height;
}
Tree_Pos get_pos(int pos, const QVector<Node*>& roots, int space) {
Tree_Pos get_pos(int pos, const std::vector<Node*>& roots, int space) {
int total_height = 0;
QStack<QPair<Node*, int>> stack;
std::stack<std::pair<Node*, int>> stack;
for (Node* node : roots) {
if (node)
stack.push_front({node, 0});
stack.push({node, 0});
}
Node* cur{};
int deep{};
while (!stack.isEmpty()) {
while (!stack.empty()) {
auto t = stack.top();
cur = t.first;
deep = t.second;
@@ -161,13 +166,14 @@ Tree_Pos get_pos(int pos, const QVector<Node*>& roots, int space) {
}
return Tree_Pos{cur, deep, total_height, ReMain, stack};
}
void select_node(const QVector<Node*>& roots, int view_y, int length, int space, Node_Call_Back call_back) {
void select_node(const std::vector<Node*>& roots, int view_y, int length, int space, Node_Call_Back call_back) {
auto ret = get_pos(view_y, roots, space);
QStack<QPair<Node*, int>>& stack = ret.rest;
std::stack<std::pair<Node*, int>>& stack = ret.rest;
int cur_inside_y = ret.node_pos;
int cur_inside_end = cur_inside_y + length;
while (!stack.isEmpty()) {
auto [cur, depth] = stack.pop();
while (!stack.empty()) {
auto [cur, depth] = stack.top();
stack.pop();
if (cur->expand) {
for (int i = cur->sons.size() - 1; i >= 0; --i) {
stack.push({cur->sons[i], depth + 1});