mirror of
https://github.com/zeromq/libzmq.git
synced 2024-12-13 10:52:56 +01:00
mtrie: Code simplification
This commit is contained in:
parent
692648de96
commit
1b0e6ef8b4
@ -107,7 +107,7 @@ bool zmq::mtrie_t::add_helper (unsigned char *prefix_, size_t size_,
|
||||
// The new character is above the current character range.
|
||||
unsigned short old_count = count;
|
||||
count = c - min + 1;
|
||||
next.table = (mtrie_t**) realloc ((void*) next.table,
|
||||
next.table = (mtrie_t**) realloc (next.table,
|
||||
sizeof (mtrie_t*) * count);
|
||||
alloc_assert (next.table);
|
||||
for (unsigned short i = old_count; i != count; i++)
|
||||
@ -118,7 +118,7 @@ bool zmq::mtrie_t::add_helper (unsigned char *prefix_, size_t size_,
|
||||
// The new character is below the current character range.
|
||||
unsigned short old_count = count;
|
||||
count = (min + old_count) - c;
|
||||
next.table = (mtrie_t**) realloc ((void*) next.table,
|
||||
next.table = (mtrie_t**) realloc (next.table,
|
||||
sizeof (mtrie_t*) * count);
|
||||
alloc_assert (next.table);
|
||||
memmove (next.table + min - c, next.table,
|
||||
@ -133,16 +133,16 @@ bool zmq::mtrie_t::add_helper (unsigned char *prefix_, size_t size_,
|
||||
if (count == 1) {
|
||||
if (!next.node) {
|
||||
next.node = new (std::nothrow) mtrie_t;
|
||||
++live_nodes;
|
||||
alloc_assert (next.node);
|
||||
++live_nodes;
|
||||
}
|
||||
return next.node->add_helper (prefix_ + 1, size_ - 1, pipe_);
|
||||
}
|
||||
else {
|
||||
if (!next.table [c - min]) {
|
||||
next.table [c - min] = new (std::nothrow) mtrie_t;
|
||||
++live_nodes;
|
||||
alloc_assert (next.table [c - min]);
|
||||
++live_nodes;
|
||||
}
|
||||
return next.table [c - min]->add_helper (prefix_ + 1, size_ - 1, pipe_);
|
||||
}
|
||||
@ -330,61 +330,46 @@ bool zmq::mtrie_t::rm_helper (unsigned char *prefix_, size_t size_,
|
||||
// If there's only one live node in the table we can
|
||||
// switch to using the more compact single-node
|
||||
// representation
|
||||
mtrie_t *node = 0;
|
||||
for (unsigned short i = 0; i < count; ++i) {
|
||||
if (next.table [i]) {
|
||||
node = next.table [i];
|
||||
min = i + min;
|
||||
unsigned short i;
|
||||
for (i = 0; i < count; ++i)
|
||||
if (next.table [i])
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
zmq_assert (node);
|
||||
free (next.table);
|
||||
next.node = node;
|
||||
zmq_assert (i < count);
|
||||
min += i;
|
||||
count = 1;
|
||||
mtrie_t *oldp = next.table [i];
|
||||
free (next.table);
|
||||
next.node = oldp;
|
||||
}
|
||||
else if (c == min) {
|
||||
// We can compact the table "from the left"
|
||||
unsigned char new_min = min;
|
||||
for (unsigned short i = 1; i < count; ++i) {
|
||||
if (next.table [i]) {
|
||||
new_min = i + min;
|
||||
unsigned short i;
|
||||
for (i = 1; i < count; ++i)
|
||||
if (next.table [i])
|
||||
break;
|
||||
}
|
||||
}
|
||||
zmq_assert (new_min != min);
|
||||
|
||||
zmq_assert (i < count);
|
||||
min += i;
|
||||
count -= i;
|
||||
mtrie_t **old_table = next.table;
|
||||
zmq_assert (new_min > min);
|
||||
zmq_assert (count > new_min - min);
|
||||
|
||||
count = count - (new_min - min);
|
||||
next.table = (mtrie_t**) malloc (sizeof (mtrie_t*) * count);
|
||||
alloc_assert (next.table);
|
||||
|
||||
memmove (next.table, old_table + (new_min - min),
|
||||
sizeof (mtrie_t*) * count);
|
||||
memmove (next.table, old_table + i, sizeof (mtrie_t*) * count);
|
||||
free (old_table);
|
||||
|
||||
min = new_min;
|
||||
}
|
||||
else if (c == min + count - 1) {
|
||||
// We can compact the table "from the right"
|
||||
unsigned short new_count = count;
|
||||
for (unsigned short i = 1; i < count; ++i) {
|
||||
if (next.table [count - 1 - i]) {
|
||||
new_count = count - i;
|
||||
unsigned short i;
|
||||
for (i = 1; i < count; ++i)
|
||||
if (next.table [count - 1 - i])
|
||||
break;
|
||||
}
|
||||
}
|
||||
zmq_assert (new_count != count);
|
||||
count = new_count;
|
||||
|
||||
zmq_assert (i < count);
|
||||
count -= i;
|
||||
mtrie_t **old_table = next.table;
|
||||
next.table = (mtrie_t**) malloc (sizeof (mtrie_t*) * count);
|
||||
alloc_assert (next.table);
|
||||
|
||||
memmove (next.table, old_table, sizeof (mtrie_t*) * count);
|
||||
free (old_table);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user