mirror of
https://github.com/zeromq/libzmq.git
synced 2025-11-07 05:58:45 +01:00
Problem: formatting inconsistent
Solution: applied clang-format
This commit is contained in:
125
src/trie.cpp
125
src/trie.cpp
@@ -37,11 +37,7 @@
|
||||
#include <new>
|
||||
#include <algorithm>
|
||||
|
||||
zmq::trie_t::trie_t () :
|
||||
refcnt (0),
|
||||
min (0),
|
||||
count (0),
|
||||
live_nodes (0)
|
||||
zmq::trie_t::trie_t () : refcnt (0), min (0), count (0), live_nodes (0)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -49,11 +45,10 @@ zmq::trie_t::~trie_t ()
|
||||
{
|
||||
if (count == 1) {
|
||||
zmq_assert (next.node);
|
||||
LIBZMQ_DELETE(next.node);
|
||||
}
|
||||
else if (count > 1) {
|
||||
LIBZMQ_DELETE (next.node);
|
||||
} else if (count > 1) {
|
||||
for (unsigned short i = 0; i != count; ++i) {
|
||||
LIBZMQ_DELETE(next.table[i]);
|
||||
LIBZMQ_DELETE (next.table[i]);
|
||||
}
|
||||
free (next.table);
|
||||
}
|
||||
@@ -69,50 +64,42 @@ bool zmq::trie_t::add (unsigned char *prefix_, size_t size_)
|
||||
|
||||
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) {
|
||||
} else if (count == 1) {
|
||||
unsigned char oldc = min;
|
||||
trie_t *oldp = next.node;
|
||||
count = (min < c ? c - min : min - c) + 1;
|
||||
next.table = (trie_t**)
|
||||
malloc (sizeof (trie_t*) * count);
|
||||
next.table = (trie_t **) malloc (sizeof (trie_t *) * count);
|
||||
alloc_assert (next.table);
|
||||
for (unsigned short i = 0; i != count; ++i)
|
||||
next.table [i] = 0;
|
||||
next.table[i] = 0;
|
||||
min = std::min (min, c);
|
||||
next.table [oldc - min] = oldp;
|
||||
}
|
||||
else
|
||||
if (min < c) {
|
||||
next.table[oldc - min] = oldp;
|
||||
} else if (min < c) {
|
||||
// The new character is above the current character range.
|
||||
unsigned short old_count = count;
|
||||
count = c - min + 1;
|
||||
next.table = (trie_t**) realloc ((void*) next.table,
|
||||
sizeof (trie_t*) * count);
|
||||
next.table = (trie_t **) realloc ((void *) next.table,
|
||||
sizeof (trie_t *) * count);
|
||||
zmq_assert (next.table);
|
||||
for (unsigned short i = old_count; i != count; i++)
|
||||
next.table [i] = NULL;
|
||||
}
|
||||
else {
|
||||
|
||||
next.table[i] = NULL;
|
||||
} else {
|
||||
// The new character is below the current character range.
|
||||
unsigned short old_count = count;
|
||||
count = (min + old_count) - c;
|
||||
next.table = (trie_t**) realloc ((void*) next.table,
|
||||
sizeof (trie_t*) * count);
|
||||
next.table = (trie_t **) realloc ((void *) next.table,
|
||||
sizeof (trie_t *) * count);
|
||||
zmq_assert (next.table);
|
||||
memmove (next.table + min - c, next.table,
|
||||
old_count * sizeof (trie_t*));
|
||||
old_count * sizeof (trie_t *));
|
||||
for (unsigned short i = 0; i != min - c; i++)
|
||||
next.table [i] = NULL;
|
||||
next.table[i] = NULL;
|
||||
min = c;
|
||||
}
|
||||
}
|
||||
@@ -126,15 +113,14 @@ bool zmq::trie_t::add (unsigned char *prefix_, size_t size_)
|
||||
zmq_assert (live_nodes == 1);
|
||||
}
|
||||
return next.node->add (prefix_ + 1, size_ - 1);
|
||||
}
|
||||
else {
|
||||
if (!next.table [c - min]) {
|
||||
next.table [c - min] = new (std::nothrow) trie_t;
|
||||
alloc_assert (next.table [c - min]);
|
||||
} else {
|
||||
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);
|
||||
return next.table[c - min]->add (prefix_ + 1, size_ - 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,8 +137,7 @@ bool zmq::trie_t::rm (unsigned char *prefix_, size_t size_)
|
||||
if (!count || c < min || c >= min + count)
|
||||
return false;
|
||||
|
||||
trie_t *next_node =
|
||||
count == 1 ? next.node : next.table [c - min];
|
||||
trie_t *next_node = count == 1 ? next.node : next.table[c - min];
|
||||
|
||||
if (!next_node)
|
||||
return false;
|
||||
@@ -161,7 +146,7 @@ bool zmq::trie_t::rm (unsigned char *prefix_, size_t size_)
|
||||
|
||||
// Prune redundant nodes
|
||||
if (next_node->is_redundant ()) {
|
||||
LIBZMQ_DELETE(next_node);
|
||||
LIBZMQ_DELETE (next_node);
|
||||
zmq_assert (count > 0);
|
||||
|
||||
if (count == 1) {
|
||||
@@ -170,9 +155,8 @@ bool zmq::trie_t::rm (unsigned char *prefix_, size_t size_)
|
||||
count = 0;
|
||||
--live_nodes;
|
||||
zmq_assert (live_nodes == 0);
|
||||
}
|
||||
else {
|
||||
next.table [c - min] = 0;
|
||||
} else {
|
||||
next.table[c - min] = 0;
|
||||
zmq_assert (live_nodes > 1);
|
||||
--live_nodes;
|
||||
|
||||
@@ -187,28 +171,24 @@ bool zmq::trie_t::rm (unsigned char *prefix_, size_t size_)
|
||||
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];
|
||||
node = next.table[count - 1];
|
||||
min += count - 1;
|
||||
}
|
||||
else
|
||||
if (c == 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];
|
||||
node = next.table[0];
|
||||
}
|
||||
zmq_assert (node);
|
||||
free (next.table);
|
||||
next.node = node;
|
||||
count = 1;
|
||||
}
|
||||
else
|
||||
if (c == min) {
|
||||
} 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]) {
|
||||
if (next.table[i]) {
|
||||
new_min = i + min;
|
||||
break;
|
||||
}
|
||||
@@ -220,23 +200,21 @@ bool zmq::trie_t::rm (unsigned char *prefix_, size_t size_)
|
||||
zmq_assert (count > new_min - min);
|
||||
|
||||
count = count - (new_min - min);
|
||||
next.table = (trie_t**) malloc (sizeof (trie_t*) * count);
|
||||
next.table = (trie_t **) malloc (sizeof (trie_t *) * count);
|
||||
alloc_assert (next.table);
|
||||
|
||||
memmove (next.table, old_table + (new_min - min),
|
||||
sizeof (trie_t*) * count);
|
||||
sizeof (trie_t *) * count);
|
||||
free (old_table);
|
||||
|
||||
min = new_min;
|
||||
}
|
||||
else
|
||||
if (c == min + count - 1) {
|
||||
} 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]) {
|
||||
if (next.table[count - 1 - i]) {
|
||||
new_count = count - i;
|
||||
break;
|
||||
}
|
||||
@@ -245,10 +223,10 @@ bool zmq::trie_t::rm (unsigned char *prefix_, size_t size_)
|
||||
count = new_count;
|
||||
|
||||
trie_t **old_table = next.table;
|
||||
next.table = (trie_t**) malloc (sizeof (trie_t*) * count);
|
||||
next.table = (trie_t **) malloc (sizeof (trie_t *) * count);
|
||||
alloc_assert (next.table);
|
||||
|
||||
memmove (next.table, old_table, sizeof (trie_t*) * count);
|
||||
memmove (next.table, old_table, sizeof (trie_t *) * count);
|
||||
free (old_table);
|
||||
}
|
||||
}
|
||||
@@ -262,7 +240,6 @@ bool zmq::trie_t::check (unsigned char *data_, size_t size_)
|
||||
// recursion to get a bit better performance.
|
||||
trie_t *current = this;
|
||||
while (true) {
|
||||
|
||||
// We've found a corresponding subscription!
|
||||
if (current->refcnt)
|
||||
return true;
|
||||
@@ -281,7 +258,7 @@ bool zmq::trie_t::check (unsigned char *data_, size_t size_)
|
||||
if (current->count == 1)
|
||||
current = current->next.node;
|
||||
else {
|
||||
current = current->next.table [c - current->min];
|
||||
current = current->next.table[c - current->min];
|
||||
if (!current)
|
||||
return false;
|
||||
}
|
||||
@@ -290,17 +267,21 @@ bool zmq::trie_t::check (unsigned char *data_, size_t size_)
|
||||
}
|
||||
}
|
||||
|
||||
void zmq::trie_t::apply (void (*func_) (unsigned char *data_, size_t size_,
|
||||
void *arg_), void *arg_)
|
||||
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_)
|
||||
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_)
|
||||
{
|
||||
// If this node is a subscription, apply the function.
|
||||
if (refcnt)
|
||||
@@ -309,7 +290,7 @@ void zmq::trie_t::apply_helper (
|
||||
// Adjust the buffer.
|
||||
if (buffsize_ >= maxbuffsize_) {
|
||||
maxbuffsize_ = buffsize_ + 256;
|
||||
*buff_ = (unsigned char*) realloc (*buff_, maxbuffsize_);
|
||||
*buff_ = (unsigned char *) realloc (*buff_, maxbuffsize_);
|
||||
zmq_assert (*buff_);
|
||||
}
|
||||
|
||||
@@ -319,7 +300,7 @@ void zmq::trie_t::apply_helper (
|
||||
|
||||
// If there's one subnode (optimisation).
|
||||
if (count == 1) {
|
||||
(*buff_) [buffsize_] = min;
|
||||
(*buff_)[buffsize_] = min;
|
||||
buffsize_++;
|
||||
next.node->apply_helper (buff_, buffsize_, maxbuffsize_, func_, arg_);
|
||||
return;
|
||||
@@ -327,10 +308,10 @@ void zmq::trie_t::apply_helper (
|
||||
|
||||
// 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_);
|
||||
(*buff_)[buffsize_] = min + c;
|
||||
if (next.table[c])
|
||||
next.table[c]->apply_helper (buff_, buffsize_ + 1, maxbuffsize_,
|
||||
func_, arg_);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user