forked from ChunelFeng/CGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUTrie.cpp
More file actions
116 lines (86 loc) · 2.21 KB
/
UTrie.cpp
File metadata and controls
116 lines (86 loc) · 2.21 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: UTrie.cpp
@Time: 2021/9/19 5:40 下午
@Desc:
***************************/
#include "UTrie.h"
CGRAPH_NAMESPACE_BEGIN
UTrie::UTrie() {
head_ = CGRAPH_SAFE_MALLOC_COBJECT(UTrieNode)
}
UTrie::~UTrie() {
clear();
CGRAPH_DELETE_PTR(head_)
}
void UTrie::insert(const std::string& path) {
innerInsert(head_, path, 0);
}
bool UTrie::find(const std::string& path) {
return innerFind(head_, path, 0);
}
void UTrie::clear() {
innerClear(head_);
}
void UTrie::eraser(const std::string& path) {
bool isErased = false;
innerEraser(head_, path, 0, isErased);
}
bool UTrie::innerFind(UTrieNodePtr node, const std::string& path, int index) {
if (nullptr == node) {
return false;
}
int i = (int)path[index]; // char转int,用数字表示
if (index == path.size()) {
return node->is_end_;
}
index++;
bool result = false;
if (node->children_[i]) {
result = innerFind(node->children_[i], path, index);
}
return result;
}
void UTrie::innerInsert(UTrieNodePtr node, const std::string& path, int index) {
if (nullptr == node) {
return;
}
if (index == path.size()) {
node->is_end_ = true;
return;
}
int i = (int)path[index];
index++;
if (!node->children_[i]) {
std::string curPath = path.substr(0, index);
node->children_[i] = new UTrieNode(curPath);
}
innerInsert(node->children_[i], path, index);
}
void UTrie::innerClear(UTrieNodePtr node) {
if (nullptr == node) {
return;
}
for (auto& child : node->children_) {
innerClear(child);
CGRAPH_DELETE_PTR(child)
}
}
void UTrie::innerEraser(UTrieNodePtr node, const std::string& path, int index, bool& isErased) {
if (nullptr == node || isErased) {
return;
}
if (index == path.size() && node->path_ == path && node->is_end_) {
node->is_end_ = false;
isErased = true; // 剪枝逻辑
return;
}
index++;
for (auto& child : node->children_) {
if (child) {
innerEraser(child, path, index, isErased);
}
}
}
CGRAPH_NAMESPACE_END