-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiletree.cpp
78 lines (67 loc) · 2.27 KB
/
filetree.cpp
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
//
// Created by oneman233 on 2023/2/21.
//
#include "filetree.h"
FileTree::FileTree() {
root = new TreeNode("/", false);
}
bool FileTree::findNode(const std::string &path, TreeNode **parent) const {
std::vector<std::string> pathFolders = Split(path, '/');
TreeNode *node = root->firstSon;
*parent = root;
for (const auto &name: pathFolders) {
if (!node->isUploaded) {
while (node && node->value != name)
node = node->nextSibling;
if (node == nullptr)
return false;
*parent = node;
node = node->firstSon;
} else {
// 提升后的节点直接查找 map
if (node->sonIdx.count(name) == 0)
return false;
*parent = node;
node = node->sons[node->sonIdx[name]];
}
}
return node->isFile;
}
bool FileTree::insertNode(const std::string &path, const bool isFile) const {
TreeNode *parent = nullptr;
bool isFound = findNode(path, &parent);
if (isFound) return false;
std::vector<std::string> pathFolders = Split(path, '/');
auto *newNode = new TreeNode(path, isFile);
// 新节点首先更新其 parent 指针
newNode->parent = parent;
// 根据 parent 是否提升来更新 parent
if (!parent->isUploaded) {
TreeNode *son = parent->firstSon;
if (son == nullptr)
parent->firstSon = newNode;
else {
while (son->nextSibling != nullptr)
son = son->nextSibling;
son->nextSibling = newNode;
}
} else {
// 首先在子数组中插入新节点
// 随后更新 map
parent->sons.push_back(newNode);
parent->sonIdx[path] = (int) parent->sons.size() - 1;
}
return true;
}
void FileTree::list(TreeNode *node, std::map<std::string, std::pair<int, int>> &meta) {
static int chunkSize = 2 * 1024 * 1024;
if (node != nullptr) {
std::cout << node->value << "\t" << meta[node->value].first << "\t"
<< (int) ceil(1.0 * meta[node->value].second / chunkSize) << std::endl;
list(node->firstSon, meta);
list(node->nextSibling, meta);
}
}
void FileTree::list(std::map<std::string, std::pair<int, int>> &meta) {
list(root, meta);
}