-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASCIINode.cpp
62 lines (51 loc) · 1.67 KB
/
ASCIINode.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
//
// Created by ychen on 2021/1/7.
//
#include "ASCIINode.hpp"
// i really want to add this comment
void ACSIINode::insertStreetName(const string& streetName, const int& street_id) {
//special case when streetName is empty,insertNothing
if (0 == streetName.length()) {
return;
}
ACSIINode* cptr = this; //define a current layer ASCIINode pointer
for (char c : streetName) {
//for loop through each letter in streetName
auto idx = (unsigned)(c & 0xff);
//determine whether current ASCIINode is nullptr, if yes dynmaic allocate
if (cptr->next_char[idx] == nullptr) {
cptr->next_char[idx] = new ACSIINode();
}
//save streetId into current layer id_pool
cptr->next_char[idx]->id_pool.emplace_back(street_id);
//set pointer point to nextChar ASCIINode
cptr = cptr->next_char[idx];
}
}
//code useless
vector<int> ACSIINode::getPrefixId(const string& prefix) {
//special case where prefix is ""
if (0 == prefix.length()) {
return vector<int>();
}
ACSIINode* cptr = this; //temporary ASCIINode pointer
for (char c : prefix) {
//loop through every char in prefix
auto idx = (unsigned)(c & 0xff);
//if nextChar NULL, prefix no found
if (cptr->next_char[idx] == nullptr) {
return vector<int>();
}
cptr = cptr->next_char[idx];
//set cptr towards nextChar
}
return cptr->id_pool;
}
void ACSIINode::clear() {
for (int i = 0; i < 128; i++) {
if (this->next_char[i] != nullptr) {
this->next_char[i]->clear();
delete this->next_char[i];
}
}
}