trie.hpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #ifndef __ZMQ_TRIE_HPP_INCLUDED__
4 #define __ZMQ_TRIE_HPP_INCLUDED__
5 
6 #include <stddef.h>
7 
8 #include "macros.hpp"
9 #include "stdint.hpp"
10 #include "atomic_counter.hpp"
11 
12 namespace zmq
13 {
14 class trie_t
15 {
16  public:
17  trie_t ();
18  ~trie_t ();
19 
20  // Add key to the trie. Returns true if this is a new item in the trie
21  // rather than a duplicate.
22  bool add (unsigned char *prefix_, size_t size_);
23 
24  // Remove key from the trie. Returns true if the item is actually
25  // removed from the trie.
26  bool rm (unsigned char *prefix_, size_t size_);
27 
28  // Check whether particular key is in the trie.
29  bool check (const unsigned char *data_, size_t size_) const;
30 
31  // Apply the function supplied to each subscription in the trie.
32  void apply (void (*func_) (unsigned char *data_, size_t size_, void *arg_),
33  void *arg_);
34 
35  private:
36  void apply_helper (unsigned char **buff_,
37  size_t buffsize_,
38  size_t maxbuffsize_,
39  void (*func_) (unsigned char *data_,
40  size_t size_,
41  void *arg_),
42  void *arg_) const;
43  bool is_redundant () const;
44 
45  uint32_t _refcnt;
46  unsigned char _min;
47  unsigned short _count;
48  unsigned short _live_nodes;
49  union
50  {
51  class trie_t *node;
52  class trie_t **table;
53  } _next;
54 
56 };
57 
58 
59 // lightweight wrapper around trie_t adding tracking of total number of prefixes
61 {
62  public:
65 
66  bool add (unsigned char *prefix_, size_t size_)
67  {
68  if (_trie.add (prefix_, size_)) {
69  _num_prefixes.add (1);
70  return true;
71  } else
72  return false;
73  }
74 
75  bool rm (unsigned char *prefix_, size_t size_)
76  {
77  if (_trie.rm (prefix_, size_)) {
78  _num_prefixes.sub (1);
79  return true;
80  } else
81  return false;
82  }
83 
84  bool check (const unsigned char *data_, size_t size_) const
85  {
86  return _trie.check (data_, size_);
87  }
88 
89  void apply (void (*func_) (unsigned char *data_, size_t size_, void *arg_),
90  void *arg_)
91  {
92  _trie.apply (func_, arg_);
93  }
94 
95  // Retrieve the number of prefixes stored in this trie (added - removed)
96  // Note this is a multithread safe function.
97  uint32_t num_prefixes () const { return _num_prefixes.get (); }
98 
99  private:
102 };
103 
104 }
105 
106 #endif
zmq::trie_with_size_t::num_prefixes
uint32_t num_prefixes() const
Definition: trie.hpp:97
zmq::trie_t::apply_helper
void apply_helper(unsigned char **buff_, size_t buffsize_, size_t maxbuffsize_, void(*func_)(unsigned char *data_, size_t size_, void *arg_), void *arg_) const
Definition: trie.cpp:253
data_
StringPiece data_
Definition: bytestream_unittest.cc:60
zmq::trie_t::check
bool check(const unsigned char *data_, size_t size_) const
Definition: trie.cpp:212
zmq::trie_t::_next
union zmq::trie_t::@71 _next
zmq::trie_t
Definition: trie.hpp:14
atomic_counter.hpp
zmq::trie_with_size_t::_trie
trie_t _trie
Definition: trie.hpp:101
zmq::trie_t::_refcnt
uint32_t _refcnt
Definition: trie.hpp:45
zmq::trie_with_size_t::check
bool check(const unsigned char *data_, size_t size_) const
Definition: trie.hpp:84
zmq::trie_t::rm
bool rm(unsigned char *prefix_, size_t size_)
Definition: trie.cpp:100
zmq::trie_t::trie_t
trie_t()
Definition: trie.cpp:13
zmq
Definition: zmq.hpp:229
zmq::trie_t::apply
void apply(void(*func_)(unsigned char *data_, size_t size_, void *arg_), void *arg_)
Definition: trie.cpp:245
macros.hpp
stdint.hpp
zmq::trie_t::_live_nodes
unsigned short _live_nodes
Definition: trie.hpp:48
zmq::atomic_counter_t
Definition: atomic_counter.hpp:61
zmq::atomic_counter_t::get
integer_t get() const ZMQ_NOEXCEPT
Definition: atomic_counter.hpp:173
zmq::atomic_counter_t::add
integer_t add(integer_t increment_) ZMQ_NOEXCEPT
Definition: atomic_counter.hpp:73
ZMQ_NON_COPYABLE_NOR_MOVABLE
#define ZMQ_NON_COPYABLE_NOR_MOVABLE(classname)
Definition: macros.hpp:58
zmq::trie_t::~trie_t
~trie_t()
Definition: trie.cpp:17
zmq::trie_with_size_t::trie_with_size_t
trie_with_size_t()
Definition: trie.hpp:63
zmq::trie_with_size_t::add
bool add(unsigned char *prefix_, size_t size_)
Definition: trie.hpp:66
zmq::trie_with_size_t::rm
bool rm(unsigned char *prefix_, size_t size_)
Definition: trie.hpp:75
zmq::trie_with_size_t::~trie_with_size_t
~trie_with_size_t()
Definition: trie.hpp:64
zmq::trie_with_size_t
Definition: trie.hpp:60
zmq::trie_t::is_redundant
bool is_redundant() const
Definition: trie.cpp:293
zmq::trie_with_size_t::apply
void apply(void(*func_)(unsigned char *data_, size_t size_, void *arg_), void *arg_)
Definition: trie.hpp:89
zmq::atomic_counter_t::sub
bool sub(integer_t decrement_) ZMQ_NOEXCEPT
Definition: atomic_counter.hpp:118
zmq::trie_t::_count
unsigned short _count
Definition: trie.hpp:47
zmq::trie_with_size_t::_num_prefixes
atomic_counter_t _num_prefixes
Definition: trie.hpp:100
zmq::trie_t::add
bool add(unsigned char *prefix_, size_t size_)
Definition: trie.cpp:30
zmq::trie_t::node
class trie_t * node
Definition: trie.hpp:51
zmq::trie_t::_min
unsigned char _min
Definition: trie.hpp:46
zmq::trie_t::table
class trie_t ** table
Definition: trie.hpp:52
prefix_
std::string prefix_
Definition: src/google/protobuf/descriptor.cc:378


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:07:00