Commit 1186e986 authored by Shubham Lagwankar's avatar Shubham Lagwankar

Problem: code doesn't follow libzmq naming conventions

Solution: rename types/variables that are inconsistent
parent 4a855fba
...@@ -48,28 +48,28 @@ const char *chars = "abcdefghijklmnopqrstuvwxyz0123456789"; ...@@ -48,28 +48,28 @@ const char *chars = "abcdefghijklmnopqrstuvwxyz0123456789";
const int chars_len = 36; const int chars_len = 36;
template <class T> template <class T>
void benchmark_lookup (T &t, void benchmark_lookup (T &subscriptions_,
std::vector<unsigned char *> &input_set, std::vector<unsigned char *> &input_set_,
std::vector<unsigned char *> &queries) std::vector<unsigned char *> &queries_)
{ {
using namespace std::chrono; using namespace std::chrono;
std::vector<duration<long, std::nano>> samples_vec; std::vector<duration<long, std::nano>> samples_vec;
samples_vec.reserve (samples); samples_vec.reserve (samples);
for (std::size_t run = 0; run < warmup_runs; ++run) { for (std::size_t run = 0; run < warmup_runs; ++run) {
for (auto &query : queries) for (auto &query : queries_)
t.check (query, key_length); subscriptions_.check (query, key_length);
} }
for (std::size_t run = 0; run < samples; ++run) { for (std::size_t run = 0; run < samples; ++run) {
duration<long, std::nano> interval (0); duration<long, std::nano> interval (0);
for (auto &query : queries) { for (auto &query : queries_) {
auto start = steady_clock::now (); auto start = steady_clock::now ();
t.check (query, key_length); subscriptions_.check (query, key_length);
auto end = steady_clock::now (); auto end = steady_clock::now ();
interval += end - start; interval += end - start;
} }
samples_vec.push_back (interval / queries.size ()); samples_vec.push_back (interval / queries_.size ());
} }
std::size_t sum = 0; std::size_t sum = 0;
...@@ -102,7 +102,7 @@ int main () ...@@ -102,7 +102,7 @@ int main ()
// Keeping initialization out of the benchmarking function helps // Keeping initialization out of the benchmarking function helps
// heaptrack detect peak memory consumption of the radix tree. // heaptrack detect peak memory consumption of the radix tree.
zmq::trie_t trie; zmq::trie_t trie;
zmq::radix_tree radix_tree; zmq::radix_tree_t radix_tree;
for (auto &key : input_set) { for (auto &key : input_set) {
trie.add (key, key_length); trie.add (key, key_length);
radix_tree.add (key, key_length); radix_tree.add (key, key_length);
......
...@@ -36,54 +36,54 @@ ...@@ -36,54 +36,54 @@
#include <string.h> #include <string.h>
#include <vector> #include <vector>
node_t::node_t (unsigned char *data) : data_ (data) node_t::node_t (unsigned char *data_) : _data (data_)
{ {
} }
uint32_t node_t::refcount () uint32_t node_t::refcount ()
{ {
uint32_t u32; uint32_t u32;
memcpy (&u32, data_, sizeof (u32)); memcpy (&u32, _data, sizeof (u32));
return u32; return u32;
} }
void node_t::set_refcount (uint32_t value) void node_t::set_refcount (uint32_t value_)
{ {
memcpy (data_, &value, sizeof (value)); memcpy (_data, &value_, sizeof (value_));
} }
uint32_t node_t::prefix_length () uint32_t node_t::prefix_length ()
{ {
uint32_t u32; uint32_t u32;
memcpy (&u32, data_ + sizeof (uint32_t), sizeof (u32)); memcpy (&u32, _data + sizeof (uint32_t), sizeof (u32));
return u32; return u32;
} }
void node_t::set_prefix_length (uint32_t value) void node_t::set_prefix_length (uint32_t value_)
{ {
memcpy (data_ + sizeof (value), &value, sizeof (value)); memcpy (_data + sizeof (value_), &value_, sizeof (value_));
} }
uint32_t node_t::edgecount () uint32_t node_t::edgecount ()
{ {
uint32_t u32; uint32_t u32;
memcpy (&u32, data_ + 2 * sizeof (uint32_t), sizeof (u32)); memcpy (&u32, _data + 2 * sizeof (uint32_t), sizeof (u32));
return u32; return u32;
} }
void node_t::set_edgecount (uint32_t value) void node_t::set_edgecount (uint32_t value_)
{ {
memcpy (data_ + 2 * sizeof (value), &value, sizeof (value)); memcpy (_data + 2 * sizeof (value_), &value_, sizeof (value_));
} }
unsigned char *node_t::prefix () unsigned char *node_t::prefix ()
{ {
return data_ + 3 * sizeof (uint32_t); return _data + 3 * sizeof (uint32_t);
} }
void node_t::set_prefix (const unsigned char *bytes) void node_t::set_prefix (const unsigned char *bytes_)
{ {
memcpy (prefix (), bytes, prefix_length ()); memcpy (prefix (), bytes_, prefix_length ());
} }
unsigned char *node_t::first_bytes () unsigned char *node_t::first_bytes ()
...@@ -91,21 +91,21 @@ unsigned char *node_t::first_bytes () ...@@ -91,21 +91,21 @@ unsigned char *node_t::first_bytes ()
return prefix () + prefix_length (); return prefix () + prefix_length ();
} }
void node_t::set_first_bytes (const unsigned char *bytes) void node_t::set_first_bytes (const unsigned char *bytes_)
{ {
memcpy (first_bytes (), bytes, edgecount ()); memcpy (first_bytes (), bytes_, edgecount ());
} }
unsigned char node_t::first_byte_at (size_t index) unsigned char node_t::first_byte_at (size_t index_)
{ {
zmq_assert (index < edgecount ()); zmq_assert (index_ < edgecount ());
return first_bytes ()[index]; return first_bytes ()[index_];
} }
void node_t::set_first_byte_at (size_t index, unsigned char byte) void node_t::set_first_byte_at (size_t index_, unsigned char byte_)
{ {
zmq_assert (index < edgecount ()); zmq_assert (index_ < edgecount ());
first_bytes ()[index] = byte; first_bytes ()[index_] = byte_;
} }
unsigned char *node_t::node_pointers () unsigned char *node_t::node_pointers ()
...@@ -113,113 +113,115 @@ unsigned char *node_t::node_pointers () ...@@ -113,113 +113,115 @@ unsigned char *node_t::node_pointers ()
return prefix () + prefix_length () + edgecount (); return prefix () + prefix_length () + edgecount ();
} }
void node_t::set_node_pointers (const unsigned char *pointers) void node_t::set_node_pointers (const unsigned char *pointers_)
{ {
memcpy (node_pointers (), pointers, edgecount () * sizeof (void *)); memcpy (node_pointers (), pointers_, edgecount () * sizeof (void *));
} }
node_t node_t::node_at (size_t index) node_t node_t::node_at (size_t index_)
{ {
zmq_assert (index < edgecount ()); zmq_assert (index_ < edgecount ());
unsigned char *data; unsigned char *data;
memcpy (&data, node_pointers () + index * sizeof (void *), sizeof (data)); memcpy (&data, node_pointers () + index_ * sizeof (void *), sizeof (data));
return node_t (data); return node_t (data);
} }
void node_t::set_node_at (size_t index, node_t node) void node_t::set_node_at (size_t index_, node_t node_)
{ {
zmq_assert (index < edgecount ()); zmq_assert (index_ < edgecount ());
memcpy (node_pointers () + index * sizeof (void *), &node.data_, memcpy (node_pointers () + index_ * sizeof (void *), &node_._data,
sizeof (node.data_)); sizeof (node_._data));
} }
void node_t::set_edge_at (size_t index, unsigned char first_byte, node_t node) void node_t::set_edge_at (size_t index_,
unsigned char first_byte_,
node_t node_)
{ {
set_first_byte_at (index, first_byte); set_first_byte_at (index_, first_byte_);
set_node_at (index, node); set_node_at (index_, node_);
} }
bool node_t::operator== (node_t other) const bool node_t::operator== (node_t other_) const
{ {
return data_ == other.data_; return _data == other_._data;
} }
bool node_t::operator!= (node_t other) const bool node_t::operator!= (node_t other_) const
{ {
return !(*this == other); return !(*this == other_);
} }
void node_t::resize (size_t prefix_length, size_t edgecount) void node_t::resize (size_t prefix_length_, size_t edgecount_)
{ {
size_t node_size = size_t node_size = 3 * sizeof (uint32_t) + prefix_length_
3 * sizeof (uint32_t) + prefix_length + edgecount * (1 + sizeof (void *)); + edgecount_ * (1 + sizeof (void *));
unsigned char *new_data = unsigned char *new_data =
static_cast<unsigned char *> (realloc (data_, node_size)); static_cast<unsigned char *> (realloc (_data, node_size));
zmq_assert (new_data); zmq_assert (new_data);
data_ = new_data; _data = new_data;
set_prefix_length (static_cast<uint32_t> (prefix_length)); set_prefix_length (static_cast<uint32_t> (prefix_length_));
set_edgecount (static_cast<uint32_t> (edgecount)); set_edgecount (static_cast<uint32_t> (edgecount_));
} }
node_t make_node (size_t refcount, size_t prefix_length, size_t edgecount) node_t make_node (size_t refcount_, size_t prefix_length_, size_t edgecount_)
{ {
size_t node_size = size_t node_size = 3 * sizeof (uint32_t) + prefix_length_
3 * sizeof (uint32_t) + prefix_length + edgecount * (1 + sizeof (void *)); + edgecount_ * (1 + sizeof (void *));
unsigned char *data = static_cast<unsigned char *> (malloc (node_size)); unsigned char *data = static_cast<unsigned char *> (malloc (node_size));
zmq_assert (data); zmq_assert (data);
node_t node (data); node_t node (data);
node.set_refcount (static_cast<uint32_t> (refcount)); node.set_refcount (static_cast<uint32_t> (refcount_));
node.set_prefix_length (static_cast<uint32_t> (prefix_length)); node.set_prefix_length (static_cast<uint32_t> (prefix_length_));
node.set_edgecount (static_cast<uint32_t> (edgecount)); node.set_edgecount (static_cast<uint32_t> (edgecount_));
return node; return node;
} }
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
zmq::radix_tree::radix_tree () : root_ (make_node (0, 0, 0)), size_ (0) zmq::radix_tree_t::radix_tree_t () : _root (make_node (0, 0, 0)), _size (0)
{ {
} }
static void free_nodes (node_t node) static void free_nodes (node_t node_)
{ {
for (size_t i = 0; i < node.edgecount (); ++i) for (size_t i = 0; i < node_.edgecount (); ++i)
free_nodes (node.node_at (i)); free_nodes (node_.node_at (i));
free (node.data_); free (node_._data);
} }
zmq::radix_tree::~radix_tree () zmq::radix_tree_t::~radix_tree_t ()
{ {
free_nodes (root_); free_nodes (_root);
} }
match_result_t::match_result_t (size_t key_bytes_matched, match_result_t::match_result_t (size_t key_bytes_matched_,
size_t prefix_bytes_matched, size_t prefix_bytes_matched_,
size_t edge_index, size_t edge_index_,
size_t parent_edge_index, size_t parent_edge_index_,
node_t current, node_t current_,
node_t parent, node_t parent_,
node_t grandparent) : node_t grandparent_) :
key_bytes_matched (key_bytes_matched), _key_bytes_matched (key_bytes_matched_),
prefix_bytes_matched (prefix_bytes_matched), _prefix_bytes_matched (prefix_bytes_matched_),
edge_index (edge_index), _edge_index (edge_index_),
parent_edge_index (parent_edge_index), _parent_edge_index (parent_edge_index_),
current_node (current), _current_node (current_),
parent_node (parent), _parent_node (parent_),
grandparent_node (grandparent) _grandparent_node (grandparent_)
{ {
} }
match_result_t zmq::radix_tree::match (const unsigned char *key, match_result_t zmq::radix_tree_t::match (const unsigned char *key_,
size_t key_size, size_t key_size_,
bool is_lookup = false) const bool is_lookup_ = false) const
{ {
zmq_assert (key); zmq_assert (key_);
// Node we're currently at in the traversal and its predecessors. // Node we're currently at in the traversal and its predecessors.
node_t current_node = root_; node_t current_node = _root;
node_t parent_node = current_node; node_t parent_node = current_node;
node_t grandparent_node = current_node; node_t grandparent_node = current_node;
// Index of the next byte to match in the key. // Index of the next byte to match in the key.
...@@ -234,32 +236,32 @@ match_result_t zmq::radix_tree::match (const unsigned char *key, ...@@ -234,32 +236,32 @@ match_result_t zmq::radix_tree::match (const unsigned char *key,
while (current_node.prefix_length () > 0 || current_node.edgecount () > 0) { while (current_node.prefix_length () > 0 || current_node.edgecount () > 0) {
for (prefix_byte_index = 0; for (prefix_byte_index = 0;
prefix_byte_index < current_node.prefix_length () prefix_byte_index < current_node.prefix_length ()
&& key_byte_index < key_size; && key_byte_index < key_size_;
++prefix_byte_index, ++key_byte_index) { ++prefix_byte_index, ++key_byte_index) {
if (current_node.prefix ()[prefix_byte_index] if (current_node.prefix ()[prefix_byte_index]
!= key[key_byte_index]) != key_[key_byte_index])
break; break;
} }
// Even if a prefix of the key matches and we're doing a // Even if a prefix of the key matches and we're doing a
// lookup, this means we've found a matching subscription. // lookup, this means we've found a matching subscription.
if (is_lookup && prefix_byte_index == current_node.prefix_length () if (is_lookup_ && prefix_byte_index == current_node.prefix_length ()
&& current_node.refcount () > 0) { && current_node.refcount () > 0) {
key_byte_index = key_size; key_byte_index = key_size_;
break; break;
} }
// There was a mismatch or we've matched the whole key, so // There was a mismatch or we've matched the whole key, so
// there's nothing more to do. // there's nothing more to do.
if (prefix_byte_index != current_node.prefix_length () if (prefix_byte_index != current_node.prefix_length ()
|| key_byte_index == key_size) || key_byte_index == key_size_)
break; break;
// We need to match the rest of the key. Check if there's an // We need to match the rest of the key. Check if there's an
// outgoing edge from this node. // outgoing edge from this node.
node_t next_node = current_node; node_t next_node = current_node;
for (size_t i = 0; i < current_node.edgecount (); ++i) { for (size_t i = 0; i < current_node.edgecount (); ++i) {
if (current_node.first_byte_at (i) == key[key_byte_index]) { if (current_node.first_byte_at (i) == key_[key_byte_index]) {
parent_edge_index = edge_index; parent_edge_index = edge_index;
edge_index = i; edge_index = i;
next_node = current_node.node_at (i); next_node = current_node.node_at (i);
...@@ -279,24 +281,24 @@ match_result_t zmq::radix_tree::match (const unsigned char *key, ...@@ -279,24 +281,24 @@ match_result_t zmq::radix_tree::match (const unsigned char *key,
grandparent_node); grandparent_node);
} }
bool zmq::radix_tree::add (const unsigned char *key, size_t key_size) bool zmq::radix_tree_t::add (const unsigned char *key_, size_t key_size_)
{ {
match_result_t match_result = match (key, key_size); match_result_t match_result = match (key_, key_size_);
size_t key_bytes_matched = match_result.key_bytes_matched; size_t key_bytes_matched = match_result._key_bytes_matched;
size_t prefix_bytes_matched = match_result.prefix_bytes_matched; size_t prefix_bytes_matched = match_result._prefix_bytes_matched;
size_t edge_index = match_result.edge_index; size_t edge_index = match_result._edge_index;
node_t current_node = match_result.current_node; node_t current_node = match_result._current_node;
node_t parent_node = match_result.parent_node; node_t parent_node = match_result._parent_node;
if (key_bytes_matched != key_size) { if (key_bytes_matched != key_size_) {
// Not all characters match, we might have to split the node. // Not all characters match, we might have to split the node.
if (key_bytes_matched == 0 if (key_bytes_matched == 0
|| prefix_bytes_matched == current_node.prefix_length ()) { || prefix_bytes_matched == current_node.prefix_length ()) {
// The mismatch is at one of the outgoing edges, so we // The mismatch is at one of the outgoing edges, so we
// create an edge from the current node to a new leaf node // create an edge from the current node to a new leaf node
// that has the rest of the key as the prefix. // that has the rest of the key as the prefix.
node_t key_node = make_node (1, key_size - key_bytes_matched, 0); node_t key_node = make_node (1, key_size_ - key_bytes_matched, 0);
key_node.set_prefix (key + key_bytes_matched); key_node.set_prefix (key_ + key_bytes_matched);
// Reallocate for one more edge. // Reallocate for one more edge.
current_node.resize (current_node.prefix_length (), current_node.resize (current_node.prefix_length (),
...@@ -315,15 +317,15 @@ bool zmq::radix_tree::add (const unsigned char *key, size_t key_size) ...@@ -315,15 +317,15 @@ bool zmq::radix_tree::add (const unsigned char *key, size_t key_size)
// Add an edge to the new node. // Add an edge to the new node.
current_node.set_edge_at (current_node.edgecount () - 1, current_node.set_edge_at (current_node.edgecount () - 1,
key[key_bytes_matched], key_node); key_[key_bytes_matched], key_node);
// We need to update all pointers to the current node // We need to update all pointers to the current node
// after the call to resize(). // after the call to resize().
if (current_node.prefix_length () == 0) if (current_node.prefix_length () == 0)
root_.data_ = current_node.data_; _root._data = current_node._data;
else else
parent_node.set_node_at (edge_index, current_node); parent_node.set_node_at (edge_index, current_node);
++size_; ++_size;
return true; return true;
} }
...@@ -333,14 +335,14 @@ bool zmq::radix_tree::add (const unsigned char *key, size_t key_size) ...@@ -333,14 +335,14 @@ bool zmq::radix_tree::add (const unsigned char *key, size_t key_size)
// One node will have the rest of the characters from the key, // One node will have the rest of the characters from the key,
// and the other node will have the rest of the characters // and the other node will have the rest of the characters
// from the current node's prefix. // from the current node's prefix.
node_t key_node = make_node (1, key_size - key_bytes_matched, 0); node_t key_node = make_node (1, key_size_ - key_bytes_matched, 0);
node_t split_node = node_t split_node =
make_node (current_node.refcount (), make_node (current_node.refcount (),
current_node.prefix_length () - prefix_bytes_matched, current_node.prefix_length () - prefix_bytes_matched,
current_node.edgecount ()); current_node.edgecount ());
// Copy the prefix chunks to the new nodes. // Copy the prefix chunks to the new nodes.
key_node.set_prefix (key + key_bytes_matched); key_node.set_prefix (key_ + key_bytes_matched);
split_node.set_prefix (current_node.prefix () + prefix_bytes_matched); split_node.set_prefix (current_node.prefix () + prefix_bytes_matched);
// Copy the current node's edges to the new node. // Copy the current node's edges to the new node.
...@@ -359,7 +361,7 @@ bool zmq::radix_tree::add (const unsigned char *key, size_t key_size) ...@@ -359,7 +361,7 @@ bool zmq::radix_tree::add (const unsigned char *key, size_t key_size)
current_node.set_edge_at (0, key_node.prefix ()[0], key_node); current_node.set_edge_at (0, key_node.prefix ()[0], key_node);
current_node.set_edge_at (1, split_node.prefix ()[0], split_node); current_node.set_edge_at (1, split_node.prefix ()[0], split_node);
++size_; ++_size;
parent_node.set_node_at (edge_index, current_node); parent_node.set_node_at (edge_index, current_node);
return true; return true;
} }
...@@ -391,42 +393,42 @@ bool zmq::radix_tree::add (const unsigned char *key, size_t key_size) ...@@ -391,42 +393,42 @@ bool zmq::radix_tree::add (const unsigned char *key, size_t key_size)
current_node.set_edge_at (0, split_node.prefix ()[0], split_node); current_node.set_edge_at (0, split_node.prefix ()[0], split_node);
current_node.set_refcount (1); current_node.set_refcount (1);
++size_; ++_size;
parent_node.set_node_at (edge_index, current_node); parent_node.set_node_at (edge_index, current_node);
return true; return true;
} }
zmq_assert (key_bytes_matched == key_size); zmq_assert (key_bytes_matched == key_size_);
zmq_assert (prefix_bytes_matched == current_node.prefix_length ()); zmq_assert (prefix_bytes_matched == current_node.prefix_length ());
++size_; ++_size;
current_node.set_refcount (current_node.refcount () + 1); current_node.set_refcount (current_node.refcount () + 1);
return current_node.refcount () == 1; return current_node.refcount () == 1;
} }
bool zmq::radix_tree::rm (const unsigned char *key, size_t key_size) bool zmq::radix_tree_t::rm (const unsigned char *key_, size_t key_size_)
{ {
match_result_t match_result = match (key, key_size); match_result_t match_result = match (key_, key_size_);
size_t key_bytes_matched = match_result.key_bytes_matched; size_t key_bytes_matched = match_result._key_bytes_matched;
size_t prefix_bytes_matched = match_result.prefix_bytes_matched; size_t prefix_bytes_matched = match_result._prefix_bytes_matched;
size_t edge_index = match_result.edge_index; size_t edge_index = match_result._edge_index;
size_t parent_edge_index = match_result.parent_edge_index; size_t parent_edge_index = match_result._parent_edge_index;
node_t current_node = match_result.current_node; node_t current_node = match_result._current_node;
node_t parent_node = match_result.parent_node; node_t parent_node = match_result._parent_node;
node_t grandparent_node = match_result.grandparent_node; node_t grandparent_node = match_result._grandparent_node;
if (key_bytes_matched != key_size if (key_bytes_matched != key_size_
|| prefix_bytes_matched != current_node.prefix_length () || prefix_bytes_matched != current_node.prefix_length ()
|| current_node.refcount () == 0) || current_node.refcount () == 0)
return false; return false;
current_node.set_refcount (current_node.refcount () - 1); current_node.set_refcount (current_node.refcount () - 1);
--size_; --_size;
if (current_node.refcount () > 0) if (current_node.refcount () > 0)
return false; return false;
// Don't delete the root node. // Don't delete the root node.
if (current_node == root_) if (current_node == _root)
return true; return true;
size_t outgoing_edges = current_node.edgecount (); size_t outgoing_edges = current_node.edgecount ();
...@@ -455,13 +457,13 @@ bool zmq::radix_tree::rm (const unsigned char *key, size_t key_size) ...@@ -455,13 +457,13 @@ bool zmq::radix_tree::rm (const unsigned char *key, size_t key_size)
current_node.set_node_pointers (child.node_pointers ()); current_node.set_node_pointers (child.node_pointers ());
current_node.set_refcount (child.refcount ()); current_node.set_refcount (child.refcount ());
free (child.data_); free (child._data);
parent_node.set_node_at (edge_index, current_node); parent_node.set_node_at (edge_index, current_node);
return true; return true;
} }
if (parent_node.edgecount () == 2 && parent_node.refcount () == 0 if (parent_node.edgecount () == 2 && parent_node.refcount () == 0
&& parent_node != root_) { && parent_node != _root) {
// Removing this node leaves the parent with one child. // Removing this node leaves the parent with one child.
// If the parent doesn't hold a key or if it isn't the root, // If the parent doesn't hold a key or if it isn't the root,
// we can merge it with its single child node. // we can merge it with its single child node.
...@@ -484,8 +486,8 @@ bool zmq::radix_tree::rm (const unsigned char *key, size_t key_size) ...@@ -484,8 +486,8 @@ bool zmq::radix_tree::rm (const unsigned char *key, size_t key_size)
parent_node.set_node_pointers (other_child.node_pointers ()); parent_node.set_node_pointers (other_child.node_pointers ());
parent_node.set_refcount (other_child.refcount ()); parent_node.set_refcount (other_child.refcount ());
free (current_node.data_); free (current_node._data);
free (other_child.data_); free (other_child._data);
grandparent_node.set_node_at (parent_edge_index, parent_node); grandparent_node.set_node_at (parent_edge_index, parent_node);
return true; return true;
} }
...@@ -515,59 +517,59 @@ bool zmq::radix_tree::rm (const unsigned char *key, size_t key_size) ...@@ -515,59 +517,59 @@ bool zmq::radix_tree::rm (const unsigned char *key, size_t key_size)
parent_node.edgecount () - 1); parent_node.edgecount () - 1);
// Nothing points to this node now, so we can reclaim it. // Nothing points to this node now, so we can reclaim it.
free (current_node.data_); free (current_node._data);
if (parent_node.prefix_length () == 0) if (parent_node.prefix_length () == 0)
root_.data_ = parent_node.data_; _root._data = parent_node._data;
else else
grandparent_node.set_node_at (parent_edge_index, parent_node); grandparent_node.set_node_at (parent_edge_index, parent_node);
return true; return true;
} }
bool zmq::radix_tree::check (const unsigned char *key, size_t key_size) bool zmq::radix_tree_t::check (const unsigned char *key_, size_t key_size_)
{ {
if (root_.refcount () > 0) if (_root.refcount () > 0)
return true; return true;
match_result_t match_result = match (key, key_size, true); match_result_t match_result = match (key_, key_size_, true);
return match_result.key_bytes_matched == key_size return match_result._key_bytes_matched == key_size_
&& match_result.prefix_bytes_matched && match_result._prefix_bytes_matched
== match_result.current_node.prefix_length () == match_result._current_node.prefix_length ()
&& match_result.current_node.refcount () > 0; && match_result._current_node.refcount () > 0;
} }
static void static void
visit_keys (node_t node, visit_keys (node_t node_,
std::vector<unsigned char> &buffer, std::vector<unsigned char> &buffer_,
void (*func) (unsigned char *data, size_t size, void *arg), void (*func_) (unsigned char *data, size_t size, void *arg),
void *arg) void *arg_)
{ {
for (size_t i = 0; i < node.prefix_length (); ++i) for (size_t i = 0; i < node_.prefix_length (); ++i)
buffer.push_back (node.prefix ()[i]); buffer_.push_back (node_.prefix ()[i]);
if (node.refcount () > 0) { if (node_.refcount () > 0) {
zmq_assert (!buffer.empty ()); zmq_assert (!buffer_.empty ());
func (&buffer[0], buffer.size (), arg); func_ (&buffer_[0], buffer_.size (), arg_);
} }
for (size_t i = 0; i < node.edgecount (); ++i) for (size_t i = 0; i < node_.edgecount (); ++i)
visit_keys (node.node_at (i), buffer, func, arg); visit_keys (node_.node_at (i), buffer_, func_, arg_);
for (size_t i = 0; i < node.prefix_length (); ++i) for (size_t i = 0; i < node_.prefix_length (); ++i)
buffer.pop_back (); buffer_.pop_back ();
} }
void zmq::radix_tree::apply ( void zmq::radix_tree_t::apply (
void (*func) (unsigned char *data, size_t size, void *arg), void *arg) void (*func_) (unsigned char *data, size_t size, void *arg), void *arg_)
{ {
if (root_.refcount () > 0) if (_root.refcount () > 0)
func (NULL, 0, arg); // Root node is always empty. func_ (NULL, 0, arg_); // Root node is always empty.
std::vector<unsigned char> buffer; std::vector<unsigned char> buffer;
for (size_t i = 0; i < root_.edgecount (); ++i) for (size_t i = 0; i < _root.edgecount (); ++i)
visit_keys (root_.node_at (i), buffer, func, arg); visit_keys (_root.node_at (i), buffer, func_, arg_);
} }
size_t zmq::radix_tree::size () const size_t zmq::radix_tree_t::size () const
{ {
return size_; return _size;
} }
...@@ -62,62 +62,62 @@ ...@@ -62,62 +62,62 @@
// of the chunk of first bytes and node pointers respectively. // of the chunk of first bytes and node pointers respectively.
struct node_t struct node_t
{ {
unsigned char *data_; explicit node_t (unsigned char *data_);
explicit node_t (unsigned char *data); bool operator== (node_t other_) const;
bool operator!= (node_t other_) const;
bool operator== (node_t other) const;
bool operator!= (node_t other) const;
inline uint32_t refcount (); inline uint32_t refcount ();
inline uint32_t prefix_length (); inline uint32_t prefix_length ();
inline uint32_t edgecount (); inline uint32_t edgecount ();
inline unsigned char *prefix (); inline unsigned char *prefix ();
inline unsigned char *first_bytes (); inline unsigned char *first_bytes ();
inline unsigned char first_byte_at (size_t index); inline unsigned char first_byte_at (size_t index_);
inline unsigned char *node_pointers (); inline unsigned char *node_pointers ();
inline node_t node_at (size_t index); inline node_t node_at (size_t index_);
inline void set_refcount (uint32_t value); inline void set_refcount (uint32_t value_);
inline void set_prefix_length (uint32_t value); inline void set_prefix_length (uint32_t value_);
inline void set_edgecount (uint32_t value); inline void set_edgecount (uint32_t value_);
inline void set_prefix (const unsigned char *prefix); inline void set_prefix (const unsigned char *prefix_);
inline void set_first_bytes (const unsigned char *bytes); inline void set_first_bytes (const unsigned char *bytes_);
inline void set_first_byte_at (size_t index, unsigned char byte); inline void set_first_byte_at (size_t index_, unsigned char byte_);
inline void set_node_pointers (const unsigned char *pointers); inline void set_node_pointers (const unsigned char *pointers_);
inline void set_node_at (size_t index, node_t node); inline void set_node_at (size_t index_, node_t node_);
inline void inline void
set_edge_at (size_t index, unsigned char first_byte, node_t node); set_edge_at (size_t index_, unsigned char first_byte_, node_t node_);
void resize (size_t prefix_length, size_t edgecount); void resize (size_t prefix_length_, size_t edgecount_);
unsigned char *_data;
}; };
node_t make_node (size_t refcount, size_t prefix_length, size_t edgecount); node_t make_node (size_t refcount_, size_t prefix_length_, size_t edgecount_);
struct match_result_t struct match_result_t
{ {
size_t key_bytes_matched; match_result_t (size_t key_bytes_matched_,
size_t prefix_bytes_matched; size_t prefix_bytes_matched_,
size_t edge_index; size_t edge_index_,
size_t parent_edge_index; size_t parent_edge_index_,
node_t current_node; node_t current_,
node_t parent_node; node_t parent_,
node_t grandparent_node;
match_result_t (size_t key_bytes_matched,
size_t prefix_bytes_matched,
size_t edge_index,
size_t parent_edge_index,
node_t current,
node_t parent,
node_t grandparent); node_t grandparent);
size_t _key_bytes_matched;
size_t _prefix_bytes_matched;
size_t _edge_index;
size_t _parent_edge_index;
node_t _current_node;
node_t _parent_node;
node_t _grandparent_node;
}; };
namespace zmq namespace zmq
{ {
class radix_tree class radix_tree_t
{ {
public: public:
radix_tree (); radix_tree_t ();
~radix_tree (); ~radix_tree_t ();
// Add key to the tree. Returns true if this was a new key rather // Add key to the tree. Returns true if this was a new key rather
// than a duplicate. // than a duplicate.
...@@ -138,10 +138,10 @@ class radix_tree ...@@ -138,10 +138,10 @@ class radix_tree
private: private:
inline match_result_t inline match_result_t
match (const unsigned char *key, size_t key_size, bool is_lookup) const; match (const unsigned char *key_, size_t key_size_, bool is_lookup_) const;
node_t root_; node_t _root;
size_t size_; size_t _size;
}; };
} }
......
...@@ -83,7 +83,7 @@ class xsub_t : public socket_base_t ...@@ -83,7 +83,7 @@ class xsub_t : public socket_base_t
// The repository of subscriptions. // The repository of subscriptions.
#ifdef ZMQ_USE_RADIX_TREE #ifdef ZMQ_USE_RADIX_TREE
radix_tree _subscriptions; radix_tree_t _subscriptions;
#else #else
trie_t _subscriptions; trie_t _subscriptions;
#endif #endif
......
...@@ -35,41 +35,41 @@ void tearDown () ...@@ -35,41 +35,41 @@ void tearDown ()
{ {
} }
bool tree_add (zmq::radix_tree &tree, const std::string &key) bool tree_add (zmq::radix_tree_t &tree_, const std::string &key_)
{ {
return tree.add (reinterpret_cast<const unsigned char *> (key.data ()), return tree_.add (reinterpret_cast<const unsigned char *> (key_.data ()),
key.size ()); key_.size ());
} }
bool tree_rm (zmq::radix_tree &tree, const std::string &key) bool tree_rm (zmq::radix_tree_t &tree_, const std::string &key_)
{ {
return tree.rm (reinterpret_cast<const unsigned char *> (key.data ()), return tree_.rm (reinterpret_cast<const unsigned char *> (key_.data ()),
key.size ()); key_.size ());
} }
bool tree_check (zmq::radix_tree &tree, const std::string &key) bool tree_check (zmq::radix_tree_t &tree_, const std::string &key_)
{ {
return tree.check (reinterpret_cast<const unsigned char *> (key.data ()), return tree_.check (reinterpret_cast<const unsigned char *> (key_.data ()),
key.size ()); key_.size ());
} }
void test_empty () void test_empty ()
{ {
zmq::radix_tree tree; zmq::radix_tree_t tree;
TEST_ASSERT_TRUE (tree.size () == 0); TEST_ASSERT_TRUE (tree.size () == 0);
} }
void test_add_single_entry () void test_add_single_entry ()
{ {
zmq::radix_tree tree; zmq::radix_tree_t tree;
TEST_ASSERT_TRUE (tree_add (tree, "foo")); TEST_ASSERT_TRUE (tree_add (tree, "foo"));
} }
void test_add_same_entry_twice () void test_add_same_entry_twice ()
{ {
zmq::radix_tree tree; zmq::radix_tree_t tree;
TEST_ASSERT_TRUE (tree_add (tree, "test")); TEST_ASSERT_TRUE (tree_add (tree, "test"));
TEST_ASSERT_FALSE (tree_add (tree, "test")); TEST_ASSERT_FALSE (tree_add (tree, "test"));
...@@ -77,14 +77,14 @@ void test_add_same_entry_twice () ...@@ -77,14 +77,14 @@ void test_add_same_entry_twice ()
void test_rm_when_empty () void test_rm_when_empty ()
{ {
zmq::radix_tree tree; zmq::radix_tree_t tree;
TEST_ASSERT_FALSE (tree_rm (tree, "test")); TEST_ASSERT_FALSE (tree_rm (tree, "test"));
} }
void test_rm_single_entry () void test_rm_single_entry ()
{ {
zmq::radix_tree tree; zmq::radix_tree_t tree;
tree_add (tree, "temporary"); tree_add (tree, "temporary");
TEST_ASSERT_TRUE (tree_rm (tree, "temporary")); TEST_ASSERT_TRUE (tree_rm (tree, "temporary"));
...@@ -92,7 +92,7 @@ void test_rm_single_entry () ...@@ -92,7 +92,7 @@ void test_rm_single_entry ()
void test_rm_unique_entry_twice () void test_rm_unique_entry_twice ()
{ {
zmq::radix_tree tree; zmq::radix_tree_t tree;
tree_add (tree, "test"); tree_add (tree, "test");
TEST_ASSERT_TRUE (tree_rm (tree, "test")); TEST_ASSERT_TRUE (tree_rm (tree, "test"));
...@@ -101,7 +101,7 @@ void test_rm_unique_entry_twice () ...@@ -101,7 +101,7 @@ void test_rm_unique_entry_twice ()
void test_rm_duplicate_entry () void test_rm_duplicate_entry ()
{ {
zmq::radix_tree tree; zmq::radix_tree_t tree;
tree_add (tree, "test"); tree_add (tree, "test");
tree_add (tree, "test"); tree_add (tree, "test");
...@@ -111,7 +111,7 @@ void test_rm_duplicate_entry () ...@@ -111,7 +111,7 @@ void test_rm_duplicate_entry ()
void test_rm_common_prefix () void test_rm_common_prefix ()
{ {
zmq::radix_tree tree; zmq::radix_tree_t tree;
tree_add (tree, "checkpoint"); tree_add (tree, "checkpoint");
tree_add (tree, "checklist"); tree_add (tree, "checklist");
...@@ -120,7 +120,7 @@ void test_rm_common_prefix () ...@@ -120,7 +120,7 @@ void test_rm_common_prefix ()
void test_rm_common_prefix_entry () void test_rm_common_prefix_entry ()
{ {
zmq::radix_tree tree; zmq::radix_tree_t tree;
tree_add (tree, "checkpoint"); tree_add (tree, "checkpoint");
tree_add (tree, "checklist"); tree_add (tree, "checklist");
...@@ -130,7 +130,7 @@ void test_rm_common_prefix_entry () ...@@ -130,7 +130,7 @@ void test_rm_common_prefix_entry ()
void test_rm_null_entry () void test_rm_null_entry ()
{ {
zmq::radix_tree tree; zmq::radix_tree_t tree;
tree_add (tree, ""); tree_add (tree, "");
TEST_ASSERT_TRUE (tree_rm (tree, "")); TEST_ASSERT_TRUE (tree_rm (tree, ""));
...@@ -138,14 +138,14 @@ void test_rm_null_entry () ...@@ -138,14 +138,14 @@ void test_rm_null_entry ()
void test_check_empty () void test_check_empty ()
{ {
zmq::radix_tree tree; zmq::radix_tree_t tree;
TEST_ASSERT_FALSE (tree_check (tree, "foo")); TEST_ASSERT_FALSE (tree_check (tree, "foo"));
} }
void test_check_added_entry () void test_check_added_entry ()
{ {
zmq::radix_tree tree; zmq::radix_tree_t tree;
tree_add (tree, "entry"); tree_add (tree, "entry");
TEST_ASSERT_TRUE (tree_check (tree, "entry")); TEST_ASSERT_TRUE (tree_check (tree, "entry"));
...@@ -153,7 +153,7 @@ void test_check_added_entry () ...@@ -153,7 +153,7 @@ void test_check_added_entry ()
void test_check_common_prefix () void test_check_common_prefix ()
{ {
zmq::radix_tree tree; zmq::radix_tree_t tree;
tree_add (tree, "introduce"); tree_add (tree, "introduce");
tree_add (tree, "introspect"); tree_add (tree, "introspect");
...@@ -162,7 +162,7 @@ void test_check_common_prefix () ...@@ -162,7 +162,7 @@ void test_check_common_prefix ()
void test_check_prefix () void test_check_prefix ()
{ {
zmq::radix_tree tree; zmq::radix_tree_t tree;
tree_add (tree, "toasted"); tree_add (tree, "toasted");
TEST_ASSERT_FALSE (tree_check (tree, "toast")); TEST_ASSERT_FALSE (tree_check (tree, "toast"));
...@@ -172,7 +172,7 @@ void test_check_prefix () ...@@ -172,7 +172,7 @@ void test_check_prefix ()
void test_check_nonexistent_entry () void test_check_nonexistent_entry ()
{ {
zmq::radix_tree tree; zmq::radix_tree_t tree;
tree_add (tree, "red"); tree_add (tree, "red");
TEST_ASSERT_FALSE (tree_check (tree, "blue")); TEST_ASSERT_FALSE (tree_check (tree, "blue"));
...@@ -180,7 +180,7 @@ void test_check_nonexistent_entry () ...@@ -180,7 +180,7 @@ void test_check_nonexistent_entry ()
void test_check_query_longer_than_entry () void test_check_query_longer_than_entry ()
{ {
zmq::radix_tree tree; zmq::radix_tree_t tree;
tree_add (tree, "foo"); tree_add (tree, "foo");
TEST_ASSERT_TRUE (tree_check (tree, "foobar")); TEST_ASSERT_TRUE (tree_check (tree, "foobar"));
...@@ -188,7 +188,7 @@ void test_check_query_longer_than_entry () ...@@ -188,7 +188,7 @@ void test_check_query_longer_than_entry ()
void test_check_null_entry_added () void test_check_null_entry_added ()
{ {
zmq::radix_tree tree; zmq::radix_tree_t tree;
tree_add (tree, ""); tree_add (tree, "");
TEST_ASSERT_TRUE (tree_check (tree, "all queries return true")); TEST_ASSERT_TRUE (tree_check (tree, "all queries return true"));
...@@ -196,7 +196,7 @@ void test_check_null_entry_added () ...@@ -196,7 +196,7 @@ void test_check_null_entry_added ()
void test_size () void test_size ()
{ {
zmq::radix_tree tree; zmq::radix_tree_t tree;
// Adapted from the example on wikipedia. // Adapted from the example on wikipedia.
std::vector<std::string> keys; std::vector<std::string> keys;
...@@ -234,7 +234,7 @@ void return_key (unsigned char *data, size_t size, void *arg) ...@@ -234,7 +234,7 @@ void return_key (unsigned char *data, size_t size, void *arg)
void test_apply () void test_apply ()
{ {
zmq::radix_tree tree; zmq::radix_tree_t tree;
std::set<std::string> keys; std::set<std::string> keys;
keys.insert ("tester"); keys.insert ("tester");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment