forked from fangpin/miniDFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiletree.cpp
59 lines (53 loc) · 1.73 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
#include <iostream>
#include <string>
#include <cmath>
#include "filetree.h"
#include "util.h"
FileTree::FileTree(){
_root = new TreeNode("/", false);
}
bool FileTree::insert_node(const std::string &path, const bool isFile){
TreeNode * parent = nullptr;
bool isFound = find_node(path, &parent);
if(isFound) return false;
std::vector<std::string> path_folders = split(path, '/');
TreeNode *newNode = new TreeNode(path, isFile);
newNode->parent = parent;
TreeNode* son = parent->firstSon;
if(!son)
parent->firstSon = newNode;
else{
while(son->nextSibling)
son = son->nextSibling;
son->nextSibling = newNode;
}
while(son) son = son->nextSibling;
son = newNode;
return true;
}
bool FileTree::find_node(const std::string &path, TreeNode **last_node)const{
std::vector<std::string> path_folders = split(path, '/');
TreeNode *node = _root->firstSon;
*last_node = _root;
for(const auto &name: path_folders){
while(node && node->value_ != name)
node = node->nextSibling;
if(!node)
return false;
*last_node = node;
node = node->firstSon;
}
return true && node->isFile;
}
void FileTree::list(TreeNode *node, std::map<std::string, std::pair<int, int>>& meta){
static int chunkSize = 2 * 1024 * 1024;
if(node){
std::cout << node->value_ << "\t" << meta[node->value_].first << "\t" << (int)ceil(1.0 * meta[node->value_].second/chunkSize) << std::endl;
// std::cout << node->value_ << "\t" << 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);
}