generic_mtrie.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Copyright (c) 2018 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_GENERIC_MTRIE_HPP_INCLUDED__
  25. #define __ZMQ_GENERIC_MTRIE_HPP_INCLUDED__
  26. #include <stddef.h>
  27. #include <set>
  28. #include "macros.hpp"
  29. #include "stdint.hpp"
  30. #include "atomic_counter.hpp"
  31. namespace zmq
  32. {
  33. // Multi-trie (prefix tree). Each node in the trie is a set of pointers.
  34. template <typename T> class generic_mtrie_t
  35. {
  36. public:
  37. typedef T value_t;
  38. typedef const unsigned char *prefix_t;
  39. enum rm_result
  40. {
  41. not_found,
  42. last_value_removed,
  43. values_remain
  44. };
  45. generic_mtrie_t ();
  46. ~generic_mtrie_t ();
  47. // Add key to the trie. Returns true iff no entry with the same prefix_
  48. // and size_ existed before.
  49. bool add (prefix_t prefix_, size_t size_, value_t *value_);
  50. // Remove all entries with a specific value from the trie.
  51. // The call_on_uniq_ flag controls if the callback is invoked
  52. // when there are no entries left on a prefix only (true)
  53. // or on every removal (false). The arg_ argument is passed
  54. // through to the callback function.
  55. template <typename Arg>
  56. void rm (value_t *value_,
  57. void (*func_) (const unsigned char *data_, size_t size_, Arg arg_),
  58. Arg arg_,
  59. bool call_on_uniq_);
  60. // Removes a specific entry from the trie.
  61. // Returns the result of the operation.
  62. rm_result rm (prefix_t prefix_, size_t size_, value_t *value_);
  63. // Calls a callback function for all matching entries, i.e. any node
  64. // corresponding to data_ or a prefix of it. The arg_ argument
  65. // is passed through to the callback function.
  66. template <typename Arg>
  67. void match (prefix_t data_,
  68. size_t size_,
  69. void (*func_) (value_t *value_, Arg arg_),
  70. Arg arg_);
  71. // Retrieve the number of prefixes stored in this trie (added - removed)
  72. // Note this is a multithread safe function.
  73. uint32_t num_prefixes () const { return _num_prefixes.get (); }
  74. private:
  75. bool is_redundant () const;
  76. typedef std::set<value_t *> pipes_t;
  77. pipes_t *_pipes;
  78. atomic_counter_t _num_prefixes;
  79. unsigned char _min;
  80. unsigned short _count;
  81. unsigned short _live_nodes;
  82. union _next_t
  83. {
  84. class generic_mtrie_t<value_t> *node;
  85. class generic_mtrie_t<value_t> **table;
  86. } _next;
  87. struct iter
  88. {
  89. generic_mtrie_t<value_t> *node;
  90. generic_mtrie_t<value_t> *next_node;
  91. prefix_t prefix;
  92. size_t size;
  93. unsigned short current_child;
  94. unsigned char new_min;
  95. unsigned char new_max;
  96. bool processed_for_removal;
  97. };
  98. ZMQ_NON_COPYABLE_NOR_MOVABLE (generic_mtrie_t)
  99. };
  100. }
  101. #endif