made the prefix trie pull nodes from allocation pool
This commit is contained in:
parent
83319b7f31
commit
4ab4f9fb80
2 changed files with 19 additions and 13 deletions
|
@ -222,8 +222,8 @@ std::shared_ptr<typename CPrefixTrie<TKey, TData>::Node>& CPrefixTrie<TKey, TDat
|
||||||
}
|
}
|
||||||
if (count == 0) {
|
if (count == 0) {
|
||||||
++size;
|
++size;
|
||||||
it = children.emplace(key, std::make_shared<Node>()).first;
|
it = children.emplace(key, std::allocate_shared<Node>(nodePool)).first;
|
||||||
it->second->data = std::make_shared<TData>();
|
it->second->data = std::allocate_shared<TData>(dataPool);
|
||||||
return it->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
if (count < it->first.size()) {
|
if (count < it->first.size()) {
|
||||||
|
@ -232,9 +232,9 @@ std::shared_ptr<typename CPrefixTrie<TKey, TData>::Node>& CPrefixTrie<TKey, TDat
|
||||||
auto nodes = std::move(it->second);
|
auto nodes = std::move(it->second);
|
||||||
children.erase(it);
|
children.erase(it);
|
||||||
++size;
|
++size;
|
||||||
it = children.emplace(prefix, std::make_shared<Node>()).first;
|
it = children.emplace(prefix, std::allocate_shared<Node>(nodePool)).first;
|
||||||
it->second->children.emplace(postfix, std::move(nodes));
|
it->second->children.emplace(postfix, std::move(nodes));
|
||||||
it->second->data = std::make_shared<TData>();
|
it->second->data = std::allocate_shared<TData>(dataPool);
|
||||||
if (key.size() == count)
|
if (key.size() == count)
|
||||||
return it->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
|
@ -253,7 +253,7 @@ void CPrefixTrie<TKey, TData>::erase(const TKey& key, std::shared_ptr<Node>& nod
|
||||||
if (!find(key, node, cb))
|
if (!find(key, node, cb))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
nodes.back().second->data = std::make_shared<TData>();
|
nodes.back().second->data = std::allocate_shared<TData>(dataPool);
|
||||||
for (; nodes.size() > 1; nodes.pop_back()) {
|
for (; nodes.size() > 1; nodes.pop_back()) {
|
||||||
// if we have only one child and no data ourselves, bring them up to our level
|
// if we have only one child and no data ourselves, bring them up to our level
|
||||||
auto& cNode = nodes.back().second;
|
auto& cNode = nodes.back().second;
|
||||||
|
@ -284,9 +284,9 @@ void CPrefixTrie<TKey, TData>::erase(const TKey& key, std::shared_ptr<Node>& nod
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TKey, typename TData>
|
template <typename TKey, typename TData>
|
||||||
CPrefixTrie<TKey, TData>::CPrefixTrie() : size(0), root(std::make_shared<Node>())
|
CPrefixTrie<TKey, TData>::CPrefixTrie() : size(0), root(std::allocate_shared<Node>(nodePool))
|
||||||
{
|
{
|
||||||
root->data = std::make_shared<TData>();
|
root->data = std::allocate_shared<TData>(dataPool);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename TKey, typename TData>
|
template <typename TKey, typename TData>
|
||||||
|
@ -294,7 +294,7 @@ template <typename TDataUni>
|
||||||
typename CPrefixTrie<TKey, TData>::iterator CPrefixTrie<TKey, TData>::insert(const TKey& key, TDataUni&& data)
|
typename CPrefixTrie<TKey, TData>::iterator CPrefixTrie<TKey, TData>::insert(const TKey& key, TDataUni&& data)
|
||||||
{
|
{
|
||||||
auto& node = key.empty() ? root : insert(key, root);
|
auto& node = key.empty() ? root : insert(key, root);
|
||||||
node->data = std::make_shared<TData>(std::forward<TDataUni>(data));
|
node->data = std::allocate_shared<TData>(dataPool, std::forward<TDataUni>(data));
|
||||||
return key.empty() ? begin() : iterator{key, node};
|
return key.empty() ? begin() : iterator{key, node};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -320,7 +320,7 @@ typename CPrefixTrie<TKey, TData>::iterator CPrefixTrie<TKey, TData>::insert(CPr
|
||||||
auto& node = insert(key, shared);
|
auto& node = insert(key, shared);
|
||||||
copy = iterator{name, node};
|
copy = iterator{name, node};
|
||||||
}
|
}
|
||||||
copy.node.lock()->data = std::make_shared<TData>(std::forward<TDataUni>(data));
|
copy.node.lock()->data = std::allocate_shared<TData>(dataPool, std::forward<TDataUni>(data));
|
||||||
return copy;
|
return copy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -393,7 +393,7 @@ bool CPrefixTrie<TKey, TData>::erase(const TKey& key)
|
||||||
{
|
{
|
||||||
auto size_was = height();
|
auto size_was = height();
|
||||||
if (key.empty()) {
|
if (key.empty()) {
|
||||||
root->data = std::make_shared<TData>();
|
root->data = std::allocate_shared<TData>(dataPool);
|
||||||
} else {
|
} else {
|
||||||
erase(key, root);
|
erase(key, root);
|
||||||
}
|
}
|
||||||
|
@ -404,7 +404,7 @@ template <typename TKey, typename TData>
|
||||||
void CPrefixTrie<TKey, TData>::clear()
|
void CPrefixTrie<TKey, TData>::clear()
|
||||||
{
|
{
|
||||||
size = 0;
|
size = 0;
|
||||||
root->data = std::make_shared<TData>();
|
root->data = std::allocate_shared<TData>(dataPool);
|
||||||
root->children.clear();
|
root->children.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,18 +9,21 @@
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include <boost/pool/pool_alloc.hpp>
|
||||||
|
|
||||||
template <typename TKey, typename TData>
|
template <typename TKey, typename TData>
|
||||||
class CPrefixTrie
|
class CPrefixTrie
|
||||||
{
|
{
|
||||||
class Node
|
class Node
|
||||||
{
|
{
|
||||||
|
static boost::pool_allocator<std::pair<const TKey, std::shared_ptr<Node>>> selfPool;
|
||||||
template <bool>
|
template <bool>
|
||||||
friend class Iterator;
|
friend class Iterator;
|
||||||
friend class CPrefixTrie<TKey, TData>;
|
friend class CPrefixTrie<TKey, TData>;
|
||||||
std::map<TKey, std::shared_ptr<Node>> children;
|
std::map<TKey, std::shared_ptr<Node>, std::less<TKey>, decltype(selfPool)> children;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Node() = default;
|
Node() : children(selfPool) {}
|
||||||
Node(const Node&) = delete;
|
Node(const Node&) = delete;
|
||||||
Node(Node&& o) noexcept = default;
|
Node(Node&& o) noexcept = default;
|
||||||
Node& operator=(Node&& o) noexcept = default;
|
Node& operator=(Node&& o) noexcept = default;
|
||||||
|
@ -102,6 +105,9 @@ class CPrefixTrie
|
||||||
size_t size;
|
size_t size;
|
||||||
std::shared_ptr<Node> root;
|
std::shared_ptr<Node> root;
|
||||||
|
|
||||||
|
static boost::pool_allocator<Node> nodePool;
|
||||||
|
static boost::pool_allocator<TData> dataPool;
|
||||||
|
|
||||||
template <typename TNode>
|
template <typename TNode>
|
||||||
using callback = std::function<void(const TKey&, TNode)>;
|
using callback = std::function<void(const TKey&, TNode)>;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue