trie.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #ifndef __ZMQ_TRIE_HPP_INCLUDED__
  25. #define __ZMQ_TRIE_HPP_INCLUDED__
  26. #include <stddef.h>
  27. #include "macros.hpp"
  28. #include "stdint.hpp"
  29. #include "atomic_counter.hpp"
  30. namespace zmq
  31. {
  32. class trie_t
  33. {
  34. public:
  35. trie_t ();
  36. ~trie_t ();
  37. // Add key to the trie. Returns true if this is a new item in the trie
  38. // rather than a duplicate.
  39. bool add (unsigned char *prefix_, size_t size_);
  40. // Remove key from the trie. Returns true if the item is actually
  41. // removed from the trie.
  42. bool rm (unsigned char *prefix_, size_t size_);
  43. // Check whether particular key is in the trie.
  44. bool check (const unsigned char *data_, size_t size_) const;
  45. // Apply the function supplied to each subscription in the trie.
  46. void apply (void (*func_) (unsigned char *data_, size_t size_, void *arg_),
  47. void *arg_);
  48. private:
  49. void apply_helper (unsigned char **buff_,
  50. size_t buffsize_,
  51. size_t maxbuffsize_,
  52. void (*func_) (unsigned char *data_,
  53. size_t size_,
  54. void *arg_),
  55. void *arg_) const;
  56. bool is_redundant () const;
  57. uint32_t _refcnt;
  58. unsigned char _min;
  59. unsigned short _count;
  60. unsigned short _live_nodes;
  61. union
  62. {
  63. class trie_t *node;
  64. class trie_t **table;
  65. } _next;
  66. ZMQ_NON_COPYABLE_NOR_MOVABLE (trie_t)
  67. };
  68. // lightweight wrapper around trie_t adding tracking of total number of prefixes
  69. class trie_with_size_t
  70. {
  71. public:
  72. trie_with_size_t () {}
  73. ~trie_with_size_t () {}
  74. bool add (unsigned char *prefix_, size_t size_)
  75. {
  76. if (_trie.add (prefix_, size_)) {
  77. _num_prefixes.add (1);
  78. return true;
  79. } else
  80. return false;
  81. }
  82. bool rm (unsigned char *prefix_, size_t size_)
  83. {
  84. if (_trie.rm (prefix_, size_)) {
  85. _num_prefixes.sub (1);
  86. return true;
  87. } else
  88. return false;
  89. }
  90. bool check (const unsigned char *data_, size_t size_) const
  91. {
  92. return _trie.check (data_, size_);
  93. }
  94. void apply (void (*func_) (unsigned char *data_, size_t size_, void *arg_),
  95. void *arg_)
  96. {
  97. _trie.apply (func_, arg_);
  98. }
  99. // Retrieve the number of prefixes stored in this trie (added - removed)
  100. // Note this is a multithread safe function.
  101. uint32_t num_prefixes () const { return _num_prefixes.get (); }
  102. private:
  103. atomic_counter_t _num_prefixes;
  104. trie_t _trie;
  105. };
  106. }
  107. #endif