2018-02-15 13:09:04 +01:00
|
|
|
/*
|
|
|
|
Copyright (c) 2018 Contributors as noted in the AUTHORS file
|
|
|
|
|
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
|
|
|
|
|
|
|
libzmq is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU Lesser General Public License (LGPL) as published
|
|
|
|
by the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
As a special exception, the Contributors give you permission to link
|
|
|
|
this library with independent modules to produce an executable,
|
|
|
|
regardless of the license terms of these independent modules, and to
|
|
|
|
copy and distribute the resulting executable under terms of your choice,
|
|
|
|
provided that you also meet, for each linked independent module, the
|
|
|
|
terms and conditions of the license of that module. An independent
|
|
|
|
module is a module which is not derived from or based on this library.
|
|
|
|
If you modify this library, you must extend this exception to your
|
|
|
|
version of the library.
|
|
|
|
|
|
|
|
libzmq is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __ZMQ_GENERIC_MTRIE_HPP_INCLUDED__
|
|
|
|
#define __ZMQ_GENERIC_MTRIE_HPP_INCLUDED__
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <set>
|
|
|
|
|
2019-12-08 19:22:04 +01:00
|
|
|
#include "macros.hpp"
|
2018-02-15 13:09:04 +01:00
|
|
|
#include "stdint.hpp"
|
|
|
|
|
|
|
|
namespace zmq
|
|
|
|
{
|
2018-02-28 15:04:34 +01:00
|
|
|
// Multi-trie (prefix tree). Each node in the trie is a set of pointers.
|
2018-02-15 13:09:04 +01:00
|
|
|
template <typename T> class generic_mtrie_t
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef T value_t;
|
|
|
|
typedef const unsigned char *prefix_t;
|
|
|
|
|
2018-03-02 11:23:46 +01:00
|
|
|
enum rm_result
|
|
|
|
{
|
|
|
|
not_found,
|
|
|
|
last_value_removed,
|
|
|
|
values_remain
|
|
|
|
};
|
|
|
|
|
2018-02-15 13:09:04 +01:00
|
|
|
generic_mtrie_t ();
|
|
|
|
~generic_mtrie_t ();
|
|
|
|
|
2018-03-02 11:23:46 +01:00
|
|
|
// Add key to the trie. Returns true iff no entry with the same prefix_
|
|
|
|
// and size_ existed before.
|
2018-02-28 15:04:34 +01:00
|
|
|
bool add (prefix_t prefix_, size_t size_, value_t *value_);
|
2018-02-15 13:09:04 +01:00
|
|
|
|
2018-02-28 15:04:34 +01:00
|
|
|
// Remove all entries with a specific value from the trie.
|
2018-02-15 13:09:04 +01:00
|
|
|
// The call_on_uniq_ flag controls if the callback is invoked
|
2018-02-28 15:04:34 +01:00
|
|
|
// when there are no entries left on a prefix only (true)
|
|
|
|
// or on every removal (false). The arg_ argument is passed
|
|
|
|
// through to the callback function.
|
2018-02-21 12:06:23 +01:00
|
|
|
template <typename Arg>
|
2018-02-28 15:04:34 +01:00
|
|
|
void rm (value_t *value_,
|
2018-02-21 12:06:23 +01:00
|
|
|
void (*func_) (const unsigned char *data_, size_t size_, Arg arg_),
|
|
|
|
Arg arg_,
|
|
|
|
bool call_on_uniq_);
|
2018-02-15 13:09:04 +01:00
|
|
|
|
2018-03-02 11:23:46 +01:00
|
|
|
// Removes a specific entry from the trie.
|
|
|
|
// Returns the result of the operation.
|
|
|
|
rm_result rm (prefix_t prefix_, size_t size_, value_t *value_);
|
2018-02-15 13:09:04 +01:00
|
|
|
|
2018-02-28 15:04:34 +01:00
|
|
|
// Calls a callback function for all matching entries, i.e. any node
|
|
|
|
// corresponding to data_ or a prefix of it. The arg_ argument
|
|
|
|
// is passed through to the callback function.
|
2018-02-21 12:06:23 +01:00
|
|
|
template <typename Arg>
|
2018-02-15 13:09:04 +01:00
|
|
|
void match (prefix_t data_,
|
|
|
|
size_t size_,
|
2018-02-28 15:04:34 +01:00
|
|
|
void (*func_) (value_t *value_, Arg arg_),
|
2018-02-21 12:06:23 +01:00
|
|
|
Arg arg_);
|
2018-02-15 13:09:04 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool is_redundant () const;
|
|
|
|
|
|
|
|
typedef std::set<value_t *> pipes_t;
|
2018-05-27 11:10:39 +02:00
|
|
|
pipes_t *_pipes;
|
2018-02-15 13:09:04 +01:00
|
|
|
|
2018-05-27 11:10:39 +02:00
|
|
|
unsigned char _min;
|
|
|
|
unsigned short _count;
|
|
|
|
unsigned short _live_nodes;
|
2020-06-13 15:09:39 +01:00
|
|
|
union _next_t
|
2018-02-15 13:09:04 +01:00
|
|
|
{
|
|
|
|
class generic_mtrie_t<value_t> *node;
|
|
|
|
class generic_mtrie_t<value_t> **table;
|
2018-05-27 11:10:39 +02:00
|
|
|
} _next;
|
2018-02-15 13:09:04 +01:00
|
|
|
|
2020-06-13 15:09:39 +01:00
|
|
|
struct iter
|
|
|
|
{
|
|
|
|
generic_mtrie_t<value_t> *node;
|
|
|
|
generic_mtrie_t<value_t> *next_node;
|
|
|
|
prefix_t prefix;
|
|
|
|
size_t size;
|
|
|
|
unsigned short current_child;
|
|
|
|
unsigned char new_min;
|
|
|
|
unsigned char new_max;
|
|
|
|
bool processed_for_removal;
|
|
|
|
};
|
|
|
|
|
2019-12-08 19:22:04 +01:00
|
|
|
ZMQ_NON_COPYABLE_NOR_MOVABLE (generic_mtrie_t)
|
2018-02-15 13:09:04 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|