radix_tree.hpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #ifndef RADIX_TREE_HPP
4 #define RADIX_TREE_HPP
5 
6 #include <stddef.h>
7 
8 #include "stdint.hpp"
9 #include "atomic_counter.hpp"
10 
11 // Wrapper type for a node's data layout.
12 //
13 // There are 3 32-bit unsigned integers that act as a header. These
14 // integers represent the following values in this order:
15 //
16 // (1) The reference count of the key held by the node. This is 0 if
17 // the node doesn't hold a key.
18 //
19 // (2) The number of characters in the node's prefix. The prefix is a
20 // part of one or more keys in the tree, e.g. the prefix of each node
21 // in a trie consists of a single character.
22 //
23 // (3) The number of outgoing edges from this node.
24 //
25 // The rest of the layout consists of 3 chunks in this order:
26 //
27 // (1) The node's prefix as a sequence of one or more bytes. The root
28 // node always has an empty prefix, unlike other nodes in the tree.
29 //
30 // (2) The first byte of the prefix of each of this node's children.
31 //
32 // (3) The pointer to each child node.
33 //
34 // The link to each child is looked up using its index, e.g. the child
35 // with index 0 will have its first byte and node pointer at the start
36 // of the chunk of first bytes and node pointers respectively.
37 struct node_t
38 {
39  explicit node_t (unsigned char *data_);
40 
41  bool operator== (node_t other_) const;
42  bool operator!= (node_t other_) const;
43 
44  uint32_t refcount ();
45  uint32_t prefix_length ();
46  uint32_t edgecount ();
47  unsigned char *prefix ();
48  unsigned char *first_bytes ();
49  unsigned char first_byte_at (size_t index_);
50  unsigned char *node_pointers ();
51  node_t node_at (size_t index_);
52  void set_refcount (uint32_t value_);
53  void set_prefix_length (uint32_t value_);
54  void set_edgecount (uint32_t value_);
55  void set_prefix (const unsigned char *bytes_);
56  void set_first_bytes (const unsigned char *bytes_);
57  void set_first_byte_at (size_t index_, unsigned char byte_);
58  void set_node_pointers (const unsigned char *pointers_);
59  void set_node_at (size_t index_, node_t node_);
60  void set_edge_at (size_t index_, unsigned char first_byte_, node_t node_);
61  void resize (size_t prefix_length_, size_t edgecount_);
62 
63  unsigned char *_data;
64 };
65 
66 node_t make_node (size_t refcount_, size_t prefix_length_, size_t edgecount_);
67 
69 {
70  match_result_t (size_t key_bytes_matched_,
71  size_t prefix_bytes_matched_,
72  size_t edge_index_,
73  size_t parent_edge_index_,
74  node_t current_,
75  node_t parent_,
76  node_t grandparent);
77 
80  size_t _edge_index;
85 };
86 
87 namespace zmq
88 {
90 {
91  public:
92  radix_tree_t ();
93  ~radix_tree_t ();
94 
95  // Add key to the tree. Returns true if this was a new key rather
96  // than a duplicate.
97  bool add (const unsigned char *key_, size_t key_size_);
98 
99  // Remove key from the tree. Returns true if the item is actually
100  // removed from the tree.
101  bool rm (const unsigned char *key_, size_t key_size_);
102 
103  // Check whether particular key is in the tree.
104  bool check (const unsigned char *key_, size_t key_size_);
105 
106  // Apply the function supplied to each key in the tree.
107  void apply (void (*func_) (unsigned char *data, size_t size, void *arg),
108  void *arg_);
109 
110  // Retrieve size of the radix tree. Note this is a multithread safe function.
111  size_t size () const;
112 
113  private:
115  match (const unsigned char *key_, size_t key_size_, bool is_lookup_) const;
116 
119 };
120 }
121 
122 #endif
zmq::radix_tree_t::check
bool check(const unsigned char *key_, size_t key_size_)
Definition: radix_tree.cpp:503
node_t::set_node_at
void set_node_at(size_t index_, node_t node_)
Definition: radix_tree.cpp:104
match_result_t::_prefix_bytes_matched
size_t _prefix_bytes_matched
Definition: radix_tree.hpp:79
match_result_t::match_result_t
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)
Definition: radix_tree.cpp:174
data_
StringPiece data_
Definition: bytestream_unittest.cc:60
match_result_t::_grandparent_node
node_t _grandparent_node
Definition: radix_tree.hpp:84
atomic_counter.hpp
zmq::radix_tree_t::apply
void apply(void(*func_)(unsigned char *data, size_t size, void *arg), void *arg_)
Definition: radix_tree.cpp:537
node_t::set_first_bytes
void set_first_bytes(const unsigned char *bytes_)
Definition: radix_tree.cpp:68
node_t::first_bytes
unsigned char * first_bytes()
Definition: radix_tree.cpp:63
node_t::node_t
node_t(unsigned char *data_)
Definition: radix_tree.cpp:13
node_t::_data
unsigned char * _data
Definition: radix_tree.hpp:63
node_t::prefix
unsigned char * prefix()
Definition: radix_tree.cpp:53
zmq::radix_tree_t::_size
atomic_counter_t _size
Definition: radix_tree.hpp:118
node_t::operator!=
bool operator!=(node_t other_) const
Definition: radix_tree.cpp:124
zmq::radix_tree_t::match
match_result_t match(const unsigned char *key_, size_t key_size_, bool is_lookup_) const
Definition: radix_tree.cpp:191
node_t::set_node_pointers
void set_node_pointers(const unsigned char *pointers_)
Definition: radix_tree.cpp:90
zmq::radix_tree_t::radix_tree_t
radix_tree_t()
Definition: radix_tree.cpp:158
zmq
Definition: zmq.hpp:229
node_t::set_prefix
void set_prefix(const unsigned char *bytes_)
Definition: radix_tree.cpp:58
zmq::radix_tree_t
Definition: radix_tree.hpp:89
node_t
Definition: radix_tree.hpp:37
stdint.hpp
node_t::set_edgecount
void set_edgecount(uint32_t value_)
Definition: radix_tree.cpp:48
match_result_t
Definition: radix_tree.hpp:68
zmq::atomic_counter_t
Definition: atomic_counter.hpp:61
make_node
node_t make_node(size_t refcount_, size_t prefix_length_, size_t edgecount_)
Definition: radix_tree.cpp:141
node_t::resize
void resize(size_t prefix_length_, size_t edgecount_)
Definition: radix_tree.cpp:129
node_t::set_prefix_length
void set_prefix_length(uint32_t value_)
Definition: radix_tree.cpp:36
zmq::radix_tree_t::_root
node_t _root
Definition: radix_tree.hpp:117
index_
int index_
Definition: gmock-matchers_test.cc:6752
match_result_t::_edge_index
size_t _edge_index
Definition: radix_tree.hpp:80
zmq::radix_tree_t::add
bool add(const unsigned char *key_, size_t key_size_)
Definition: radix_tree.cpp:259
match_result_t::_key_bytes_matched
size_t _key_bytes_matched
Definition: radix_tree.hpp:78
zmq::radix_tree_t::size
size_t size() const
Definition: radix_tree.cpp:548
value_
int value_
Definition: gmock-matchers_test.cc:571
node_t::set_edge_at
void set_edge_at(size_t index_, unsigned char first_byte_, node_t node_)
Definition: radix_tree.cpp:111
zmq::radix_tree_t::rm
bool rm(const unsigned char *key_, size_t key_size_)
Definition: radix_tree.cpp:383
size
GLsizeiptr size
Definition: glcorearb.h:2943
node_t::operator==
bool operator==(node_t other_) const
Definition: radix_tree.cpp:119
node_t::first_byte_at
unsigned char first_byte_at(size_t index_)
Definition: radix_tree.cpp:73
node_t::node_at
node_t node_at(size_t index_)
Definition: radix_tree.cpp:95
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
node_t::edgecount
uint32_t edgecount()
Definition: radix_tree.cpp:41
match_result_t::_parent_node
node_t _parent_node
Definition: radix_tree.hpp:83
zmq::radix_tree_t::~radix_tree_t
~radix_tree_t()
Definition: radix_tree.cpp:169
match_result_t::_parent_edge_index
size_t _parent_edge_index
Definition: radix_tree.hpp:81
node_t::set_first_byte_at
void set_first_byte_at(size_t index_, unsigned char byte_)
Definition: radix_tree.cpp:79
node_t::set_refcount
void set_refcount(uint32_t value_)
Definition: radix_tree.cpp:24
node_t::prefix_length
uint32_t prefix_length()
Definition: radix_tree.cpp:29
node_t::refcount
uint32_t refcount()
Definition: radix_tree.cpp:17
match_result_t::_current_node
node_t _current_node
Definition: radix_tree.hpp:82
node_t::node_pointers
unsigned char * node_pointers()
Definition: radix_tree.cpp:85


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:58