-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.h
204 lines (162 loc) · 4.44 KB
/
tree.h
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* VUT FIT Brno: PDS
*
* Longest-Prefix Match
*
* Jiri Petruzelka
* <xpetru07>
* 2012/2013
*/
#ifndef TREE_H
#define TREE_H
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stdlib.h>
#include <cstring>
using std::string;
using std::vector;
using std::ostream;
using std::istream;
using std::cerr;
using std::cout;
using std::endl;
double getTime();
typedef struct node {
string content;
string prefix;
struct node* children[2];
struct node* parent;
unsigned char childrenCount;
unsigned int as;
bool data;
} node;
typedef struct staticNode {
char staticPrefix[116];
unsigned char prefixSize;
struct staticNode* children[2];
struct staticNode* staticParent;
unsigned char childrenCount;
char as[9];
bool isData;
} staticNode;
const unsigned int allockBlock = sizeof(node) * 3;
class RadixTrie {
public:
static const char SEP = '|';
static const char SEP_META = '_';
static const char SEP_CHILD_START = '<';
static const char SEP_CHILD_END = '>';
RadixTrie();
virtual ~RadixTrie();
node* insert(const string data, const unsigned int as);
node* insert(const string data, const unsigned int as, node* parent);
void clear();
void dump();
void dumpFull();
void dumpStaticNode(staticNode* node, unsigned int level, bool full);
staticNode* find(const char* data);
staticNode* findNode(staticNode* from, const char* data, const unsigned char precalcMatchedParent);
node* getRoot();
staticNode* getStaticRoot();
int count();
unsigned int nodeCount();
int size();
void serialize(ostream& stream, node* root, unsigned int* total);
void parseFrom(istream& stream, const unsigned short bufferSize);
void printAsNodes(unsigned int as, node* root);
private:
void clearNode(node* node);
void dumpNode(node* node, unsigned int level, bool full);
void addChild(node* parent, node* child);
void addChildFast(node* parent, node* child);
void detachChild(node* parent, node* child);
unsigned int matchingCharacters(const string& first, const string& second);
unsigned int matchingPrefix(const string& first, const string& second, const unsigned int start);
void parseElement(istream& stream, staticNode* parent, const unsigned short bufferSize);
node* root;
staticNode* staticRoot;
int _size;
unsigned int _nodes;
unsigned int _alloc;
staticNode* allocation;
};
inline staticNode* RadixTrie::find(const char* data) {
return findNode(this->staticRoot, data, 0);
}
inline node* RadixTrie::insert(const string data, const unsigned int as) {
return insert(data, as, this->root);
}
inline int RadixTrie::count() {
return this->_size;
}
inline unsigned int RadixTrie::nodeCount() {
return this->_nodes;
}
inline int RadixTrie::size() {
return this->_alloc * sizeof(node);
}
inline node* RadixTrie::getRoot() {
return this->root;
}
inline staticNode* RadixTrie::getStaticRoot() {
return this->staticRoot;
}
inline void RadixTrie::addChildFast(node* parent, node* child) {
parent->children[parent->childrenCount] = child;
child->parent = parent;
parent->childrenCount++;
this->_size++;
}
inline unsigned int RadixTrie::matchingPrefix(const string& first, const string& second, const unsigned int start) {
unsigned int length = first.size();
if( second.size() < length ) {
length = second.size();
}
const char* str1 = first.c_str();
const char* str2 = second.c_str();
unsigned int i;
for(i = start; i < length; ++i) {
if( str1[i] != str2[i] ) {
return i;
}
}
return length;
}
inline staticNode* RadixTrie::findNode(staticNode* from, const char* data, const unsigned char precalcMatchedParent) {
staticNode* node = NULL;
// compare
for(unsigned char i = 0; i < from->childrenCount; ++i)
node = from->children[i];
if( data[precalcMatchedParent] == node->staticPrefix[0] ) {
unsigned int matchedChild = precalcMatchedParent + 1;
if( node->prefixSize > 1 ) {
unsigned int m = 1;
for(; m < node->prefixSize; ++matchedChild) {
if( data[matchedChild] != node->staticPrefix[m++] ) {
matchedChild--;
break;
}
}
}
const bool match = matchedChild - precalcMatchedParent >= node->prefixSize;
if( !match ) {
return NULL;
}
if( node->childrenCount > 0 ) {
staticNode* found = findNode(node, data, matchedChild);
if( found != NULL && found->isData ) {
return found;
} else {
return node->isData ? node : NULL;
}
} else {
return node;
}
break;
}
}
return from;
}
#endif /* TREE_H */