forked from zeromq/libzmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.cpp
296 lines (264 loc) · 9.83 KB
/
trie.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* SPDX-License-Identifier: MPL-2.0 */
#include "precompiled.hpp"
#include "macros.hpp"
#include "err.hpp"
#include "trie.hpp"
#include <stdlib.h>
#include <new>
#include <algorithm>
zmq::trie_t::trie_t () : _refcnt (0), _min (0), _count (0), _live_nodes (0)
{
}
zmq::trie_t::~trie_t ()
{
if (_count == 1) {
zmq_assert (_next.node);
LIBZMQ_DELETE (_next.node);
} else if (_count > 1) {
for (unsigned short i = 0; i != _count; ++i) {
LIBZMQ_DELETE (_next.table[i]);
}
free (_next.table);
}
}
bool zmq::trie_t::add (unsigned char *prefix_, size_t size_)
{
// We are at the node corresponding to the prefix. We are done.
if (!size_) {
++_refcnt;
return _refcnt == 1;
}
const unsigned char c = *prefix_;
if (c < _min || c >= _min + _count) {
// The character is out of range of currently handled
// characters. We have to extend the table.
if (!_count) {
_min = c;
_count = 1;
_next.node = NULL;
} else if (_count == 1) {
const unsigned char oldc = _min;
trie_t *oldp = _next.node;
_count = (_min < c ? c - _min : _min - c) + 1;
_next.table =
static_cast<trie_t **> (malloc (sizeof (trie_t *) * _count));
alloc_assert (_next.table);
for (unsigned short i = 0; i != _count; ++i)
_next.table[i] = 0;
_min = std::min (_min, c);
_next.table[oldc - _min] = oldp;
} else if (_min < c) {
// The new character is above the current character range.
const unsigned short old_count = _count;
_count = c - _min + 1;
_next.table = static_cast<trie_t **> (
realloc (_next.table, sizeof (trie_t *) * _count));
zmq_assert (_next.table);
for (unsigned short i = old_count; i != _count; i++)
_next.table[i] = NULL;
} else {
// The new character is below the current character range.
const unsigned short old_count = _count;
_count = (_min + old_count) - c;
_next.table = static_cast<trie_t **> (
realloc (_next.table, sizeof (trie_t *) * _count));
zmq_assert (_next.table);
memmove (_next.table + _min - c, _next.table,
old_count * sizeof (trie_t *));
for (unsigned short i = 0; i != _min - c; i++)
_next.table[i] = NULL;
_min = c;
}
}
// If next node does not exist, create one.
if (_count == 1) {
if (!_next.node) {
_next.node = new (std::nothrow) trie_t;
alloc_assert (_next.node);
++_live_nodes;
zmq_assert (_live_nodes == 1);
}
return _next.node->add (prefix_ + 1, size_ - 1);
}
if (!_next.table[c - _min]) {
_next.table[c - _min] = new (std::nothrow) trie_t;
alloc_assert (_next.table[c - _min]);
++_live_nodes;
zmq_assert (_live_nodes > 1);
}
return _next.table[c - _min]->add (prefix_ + 1, size_ - 1);
}
bool zmq::trie_t::rm (unsigned char *prefix_, size_t size_)
{
// TODO: Shouldn't an error be reported if the key does not exist?
if (!size_) {
if (!_refcnt)
return false;
_refcnt--;
return _refcnt == 0;
}
const unsigned char c = *prefix_;
if (!_count || c < _min || c >= _min + _count)
return false;
trie_t *next_node = _count == 1 ? _next.node : _next.table[c - _min];
if (!next_node)
return false;
const bool ret = next_node->rm (prefix_ + 1, size_ - 1);
// Prune redundant nodes
if (next_node->is_redundant ()) {
LIBZMQ_DELETE (next_node);
zmq_assert (_count > 0);
if (_count == 1) {
// The just pruned node is was the only live node
_next.node = 0;
_count = 0;
--_live_nodes;
zmq_assert (_live_nodes == 0);
} else {
_next.table[c - _min] = 0;
zmq_assert (_live_nodes > 1);
--_live_nodes;
// Compact the table if possible
if (_live_nodes == 1) {
// We can switch to using the more compact single-node
// representation since the table only contains one live node
trie_t *node = 0;
// Since we always compact the table the pruned node must
// either be the left-most or right-most ptr in the node
// table
if (c == _min) {
// The pruned node is the left-most node ptr in the
// node table => keep the right-most node
node = _next.table[_count - 1];
_min += _count - 1;
} else if (c == _min + _count - 1) {
// The pruned node is the right-most node ptr in the
// node table => keep the left-most node
node = _next.table[0];
}
zmq_assert (node);
free (_next.table);
_next.node = node;
_count = 1;
} else if (c == _min) {
// We can compact the table "from the left".
// Find the left-most non-null node ptr, which we'll use as
// our new min
unsigned char new_min = _min;
for (unsigned short i = 1; i < _count; ++i) {
if (_next.table[i]) {
new_min = i + _min;
break;
}
}
zmq_assert (new_min != _min);
trie_t **old_table = _next.table;
zmq_assert (new_min > _min);
zmq_assert (_count > new_min - _min);
_count = _count - (new_min - _min);
_next.table =
static_cast<trie_t **> (malloc (sizeof (trie_t *) * _count));
alloc_assert (_next.table);
memmove (_next.table, old_table + (new_min - _min),
sizeof (trie_t *) * _count);
free (old_table);
_min = new_min;
} else if (c == _min + _count - 1) {
// We can compact the table "from the right".
// Find the right-most non-null node ptr, which we'll use to
// determine the new table size
unsigned short new_count = _count;
for (unsigned short i = 1; i < _count; ++i) {
if (_next.table[_count - 1 - i]) {
new_count = _count - i;
break;
}
}
zmq_assert (new_count != _count);
_count = new_count;
trie_t **old_table = _next.table;
_next.table =
static_cast<trie_t **> (malloc (sizeof (trie_t *) * _count));
alloc_assert (_next.table);
memmove (_next.table, old_table, sizeof (trie_t *) * _count);
free (old_table);
}
}
}
return ret;
}
bool zmq::trie_t::check (const unsigned char *data_, size_t size_) const
{
// This function is on critical path. It deliberately doesn't use
// recursion to get a bit better performance.
const trie_t *current = this;
while (true) {
// We've found a corresponding subscription!
if (current->_refcnt)
return true;
// We've checked all the data and haven't found matching subscription.
if (!size_)
return false;
// If there's no corresponding slot for the first character
// of the prefix, the message does not match.
const unsigned char c = *data_;
if (c < current->_min || c >= current->_min + current->_count)
return false;
// Move to the next character.
if (current->_count == 1)
current = current->_next.node;
else {
current = current->_next.table[c - current->_min];
if (!current)
return false;
}
data_++;
size_--;
}
}
void zmq::trie_t::apply (
void (*func_) (unsigned char *data_, size_t size_, void *arg_), void *arg_)
{
unsigned char *buff = NULL;
apply_helper (&buff, 0, 0, func_, arg_);
free (buff);
}
void zmq::trie_t::apply_helper (unsigned char **buff_,
size_t buffsize_,
size_t maxbuffsize_,
void (*func_) (unsigned char *data_,
size_t size_,
void *arg_),
void *arg_) const
{
// If this node is a subscription, apply the function.
if (_refcnt)
func_ (*buff_, buffsize_, arg_);
// Adjust the buffer.
if (buffsize_ >= maxbuffsize_) {
maxbuffsize_ = buffsize_ + 256;
*buff_ = static_cast<unsigned char *> (realloc (*buff_, maxbuffsize_));
zmq_assert (*buff_);
}
// If there are no subnodes in the trie, return.
if (_count == 0)
return;
// If there's one subnode (optimisation).
if (_count == 1) {
(*buff_)[buffsize_] = _min;
buffsize_++;
_next.node->apply_helper (buff_, buffsize_, maxbuffsize_, func_, arg_);
return;
}
// If there are multiple subnodes.
for (unsigned short c = 0; c != _count; c++) {
(*buff_)[buffsize_] = _min + c;
if (_next.table[c])
_next.table[c]->apply_helper (buff_, buffsize_ + 1, maxbuffsize_,
func_, arg_);
}
}
bool zmq::trie_t::is_redundant () const
{
return _refcnt == 0 && _live_nodes == 0;
}