mirror of
https://github.com/zeromq/libzmq.git
synced 2025-03-03 12:58:05 +01:00
Prune redundant nodes in the trie.
Signed-off-by: Staffan Gimåker <staffan@spotify.com>
This commit is contained in:
parent
6fa9ffebe5
commit
19129edc60
23
src/trie.cpp
23
src/trie.cpp
@ -1,6 +1,7 @@
|
||||
/*
|
||||
Copyright (c) 2009-2011 250bpm s.r.o.
|
||||
Copyright (c) 2007-2009 iMatix Corporation
|
||||
Copyright (c) 2011-2012 Spotify AB
|
||||
Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
|
||||
|
||||
This file is part of 0MQ.
|
||||
@ -35,7 +36,8 @@
|
||||
zmq::trie_t::trie_t () :
|
||||
refcnt (0),
|
||||
min (0),
|
||||
count (0)
|
||||
count (0),
|
||||
live_nodes (0)
|
||||
{
|
||||
}
|
||||
|
||||
@ -113,6 +115,7 @@ bool zmq::trie_t::add (unsigned char *prefix_, size_t size_)
|
||||
if (!next.node) {
|
||||
next.node = new (std::nothrow) trie_t;
|
||||
zmq_assert (next.node);
|
||||
++live_nodes;
|
||||
}
|
||||
return next.node->add (prefix_ + 1, size_ - 1);
|
||||
}
|
||||
@ -120,6 +123,7 @@ bool zmq::trie_t::add (unsigned char *prefix_, size_t size_)
|
||||
if (!next.table [c - min]) {
|
||||
next.table [c - min] = new (std::nothrow) trie_t;
|
||||
zmq_assert (next.table [c - min]);
|
||||
++live_nodes;
|
||||
}
|
||||
return next.table [c - min]->add (prefix_ + 1, size_ - 1);
|
||||
}
|
||||
@ -146,7 +150,18 @@ bool zmq::trie_t::rm (unsigned char *prefix_, size_t size_)
|
||||
if (!next_node)
|
||||
return false;
|
||||
|
||||
return next_node->rm (prefix_ + 1, size_ - 1);
|
||||
bool ret = next_node->rm (prefix_ + 1, size_ - 1);
|
||||
|
||||
if (next_node->is_redundant ()) {
|
||||
delete next_node;
|
||||
if (count == 1)
|
||||
next.node = 0;
|
||||
else
|
||||
next.table [c - min] = 0;
|
||||
--live_nodes;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool zmq::trie_t::check (unsigned char *data_, size_t size_)
|
||||
@ -227,3 +242,7 @@ void zmq::trie_t::apply_helper (
|
||||
}
|
||||
}
|
||||
|
||||
bool zmq::trie_t::is_redundant() const
|
||||
{
|
||||
return refcnt == 0 && live_nodes == 0;
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
/*
|
||||
Copyright (c) 2009-2011 250bpm s.r.o.
|
||||
Copyright (c) 2007-2009 iMatix Corporation
|
||||
Copyright (c) 2011-2012 Spotify AB
|
||||
Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
|
||||
|
||||
This file is part of 0MQ.
|
||||
@ -57,10 +58,12 @@ namespace zmq
|
||||
unsigned char **buff_, size_t buffsize_, size_t maxbuffsize_,
|
||||
void (*func_) (unsigned char *data_, size_t size_, void *arg_),
|
||||
void *arg_);
|
||||
bool is_redundant () const;
|
||||
|
||||
uint32_t refcnt;
|
||||
unsigned char min;
|
||||
unsigned short count;
|
||||
unsigned short live_nodes;
|
||||
union {
|
||||
class trie_t *node;
|
||||
class trie_t **table;
|
||||
|
Loading…
x
Reference in New Issue
Block a user