2023-06-05 01:16:05 +02:00
|
|
|
/* SPDX-License-Identifier: MPL-2.0 */
|
2010-01-31 20:14:30 +01:00
|
|
|
|
2010-08-28 13:26:19 +02:00
|
|
|
#ifndef __ZMQ_TRIE_HPP_INCLUDED__
|
|
|
|
#define __ZMQ_TRIE_HPP_INCLUDED__
|
2010-01-31 20:14:30 +01:00
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
2019-12-08 19:22:04 +01:00
|
|
|
#include "macros.hpp"
|
2010-01-31 20:14:30 +01:00
|
|
|
#include "stdint.hpp"
|
2022-11-29 13:00:11 +01:00
|
|
|
#include "atomic_counter.hpp"
|
2010-01-31 20:14:30 +01:00
|
|
|
|
|
|
|
namespace zmq
|
|
|
|
{
|
2010-08-28 13:26:19 +02:00
|
|
|
class trie_t
|
2018-02-01 11:46:09 +01:00
|
|
|
{
|
|
|
|
public:
|
2010-08-28 13:26:19 +02:00
|
|
|
trie_t ();
|
|
|
|
~trie_t ();
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2011-05-30 10:07:34 +02:00
|
|
|
// Add key to the trie. Returns true if this is a new item in the trie
|
|
|
|
// rather than a duplicate.
|
|
|
|
bool add (unsigned char *prefix_, size_t size_);
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2011-05-30 10:07:34 +02:00
|
|
|
// Remove key from the trie. Returns true if the item is actually
|
|
|
|
// removed from the trie.
|
2010-01-31 20:14:30 +01:00
|
|
|
bool rm (unsigned char *prefix_, size_t size_);
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2011-05-30 10:07:34 +02:00
|
|
|
// Check whether particular key is in the trie.
|
2020-02-04 12:37:31 +01:00
|
|
|
bool check (const unsigned char *data_, size_t size_) const;
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2011-05-30 10:07:34 +02:00
|
|
|
// Apply the function supplied to each subscription in the trie.
|
|
|
|
void apply (void (*func_) (unsigned char *data_, size_t size_, void *arg_),
|
|
|
|
void *arg_);
|
2018-02-01 11:46:09 +01:00
|
|
|
|
|
|
|
private:
|
2011-05-30 10:07:34 +02:00
|
|
|
void apply_helper (unsigned char **buff_,
|
|
|
|
size_t buffsize_,
|
|
|
|
size_t maxbuffsize_,
|
|
|
|
void (*func_) (unsigned char *data_,
|
|
|
|
size_t size_,
|
|
|
|
void *arg_),
|
2018-08-09 17:30:17 +02:00
|
|
|
void *arg_) const;
|
2012-01-03 16:24:44 +01:00
|
|
|
bool is_redundant () const;
|
2018-02-01 11:46:09 +01:00
|
|
|
|
2018-05-27 11:10:39 +02:00
|
|
|
uint32_t _refcnt;
|
|
|
|
unsigned char _min;
|
|
|
|
unsigned short _count;
|
|
|
|
unsigned short _live_nodes;
|
2018-02-01 11:46:09 +01:00
|
|
|
union
|
2010-01-31 20:14:30 +01:00
|
|
|
{
|
2010-08-28 13:26:19 +02:00
|
|
|
class trie_t *node;
|
2010-01-31 20:14:30 +01:00
|
|
|
class trie_t **table;
|
2018-05-27 11:10:39 +02:00
|
|
|
} _next;
|
2010-01-31 20:14:30 +01:00
|
|
|
|
2019-12-08 19:22:04 +01:00
|
|
|
ZMQ_NON_COPYABLE_NOR_MOVABLE (trie_t)
|
2018-02-01 11:46:09 +01:00
|
|
|
};
|
2022-11-29 13:00:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
// lightweight wrapper around trie_t adding tracking of total number of prefixes
|
|
|
|
class trie_with_size_t
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
trie_with_size_t () {}
|
|
|
|
~trie_with_size_t () {}
|
|
|
|
|
|
|
|
bool add (unsigned char *prefix_, size_t size_)
|
|
|
|
{
|
|
|
|
if (_trie.add (prefix_, size_)) {
|
|
|
|
_num_prefixes.add (1);
|
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool rm (unsigned char *prefix_, size_t size_)
|
|
|
|
{
|
|
|
|
if (_trie.rm (prefix_, size_)) {
|
|
|
|
_num_prefixes.sub (1);
|
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool check (const unsigned char *data_, size_t size_) const
|
|
|
|
{
|
|
|
|
return _trie.check (data_, size_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void apply (void (*func_) (unsigned char *data_, size_t size_, void *arg_),
|
|
|
|
void *arg_)
|
|
|
|
{
|
|
|
|
_trie.apply (func_, arg_);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the number of prefixes stored in this trie (added - removed)
|
|
|
|
// Note this is a multithread safe function.
|
|
|
|
uint32_t num_prefixes () const { return _num_prefixes.get (); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
atomic_counter_t _num_prefixes;
|
|
|
|
trie_t _trie;
|
|
|
|
};
|
|
|
|
|
2010-01-31 20:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|