1 /*
2 Copyright (c) 2018 Contributors as noted in the AUTHORS file
3 
4 This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6 libzmq is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License (LGPL) as published
8 by the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10 
11 As a special exception, the Contributors give you permission to link
12 this library with independent modules to produce an executable,
13 regardless of the license terms of these independent modules, and to
14 copy and distribute the resulting executable under terms of your choice,
15 provided that you also meet, for each linked independent module, the
16 terms and conditions of the license of that module. An independent
17 module is a module which is not derived from or based on this library.
18 If you modify this library, you must extend this exception to your
19 version of the library.
20 
21 libzmq is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24 License for more details.
25 
26 You should have received a copy of the GNU Lesser General Public License
27 along with this program.  If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #ifndef __ZMQ_GENERIC_MTRIE_HPP_INCLUDED__
31 #define __ZMQ_GENERIC_MTRIE_HPP_INCLUDED__
32 
33 #include <stddef.h>
34 #include <set>
35 
36 #include "macros.hpp"
37 #include "stdint.hpp"
38 
39 namespace zmq
40 {
41 //  Multi-trie (prefix tree). Each node in the trie is a set of pointers.
42 template <typename T> class generic_mtrie_t
43 {
44   public:
45     typedef T value_t;
46     typedef const unsigned char *prefix_t;
47 
48     enum rm_result
49     {
50         not_found,
51         last_value_removed,
52         values_remain
53     };
54 
55     generic_mtrie_t ();
56     ~generic_mtrie_t ();
57 
58     //  Add key to the trie. Returns true iff no entry with the same prefix_
59     //  and size_ existed before.
60     bool add (prefix_t prefix_, size_t size_, value_t *value_);
61 
62     //  Remove all entries with a specific value from the trie.
63     //  The call_on_uniq_ flag controls if the callback is invoked
64     //  when there are no entries left on a prefix only (true)
65     //  or on every removal (false). The arg_ argument is passed
66     //  through to the callback function.
67     template <typename Arg>
68     void rm (value_t *value_,
69              void (*func_) (const unsigned char *data_, size_t size_, Arg arg_),
70              Arg arg_,
71              bool call_on_uniq_);
72 
73     //  Removes a specific entry from the trie.
74     //  Returns the result of the operation.
75     rm_result rm (prefix_t prefix_, size_t size_, value_t *value_);
76 
77     //  Calls a callback function for all matching entries, i.e. any node
78     //  corresponding to data_ or a prefix of it. The arg_ argument
79     //  is passed through to the callback function.
80     template <typename Arg>
81     void match (prefix_t data_,
82                 size_t size_,
83                 void (*func_) (value_t *value_, Arg arg_),
84                 Arg arg_);
85 
86   private:
87     bool is_redundant () const;
88 
89     typedef std::set<value_t *> pipes_t;
90     pipes_t *_pipes;
91 
92     unsigned char _min;
93     unsigned short _count;
94     unsigned short _live_nodes;
95     union _next_t
96     {
97         class generic_mtrie_t<value_t> *node;
98         class generic_mtrie_t<value_t> **table;
99     } _next;
100 
101     struct iter
102     {
103         generic_mtrie_t<value_t> *node;
104         generic_mtrie_t<value_t> *next_node;
105         prefix_t prefix;
106         size_t size;
107         unsigned short current_child;
108         unsigned char new_min;
109         unsigned char new_max;
110         bool processed_for_removal;
111     };
112 
113     ZMQ_NON_COPYABLE_NOR_MOVABLE (generic_mtrie_t)
114 };
115 }
116 
117 #endif
118