7#if !defined(TETENGO_TRIE_TRIE_HPP)
8#define TETENGO_TRIE_TRIE_HPP
14#include <initializer_list>
23#include <boost/core/noncopyable.hpp>
52 std::function<void(
const std::string_view& serialized_key)>
adding;
57 std::function<void()>
done;
93 std::vector<std::pair<std::string_view, std::any>> elements,
95 std::size_t double_array_density_factor);
105 std::vector<std::pair<std::string, std::any>> elements,
107 std::size_t double_array_density_factor);
114 explicit trie_impl(std::unique_ptr<storage>&& p_storage);
121 explicit trie_impl(std::unique_ptr<double_array>&& p_double_array);
151 [[nodiscard]] std::size_t
size()
const;
161 [[nodiscard]]
bool contains(
const std::string_view& key)
const;
170 [[nodiscard]]
const std::any*
find(
const std::string_view& key)
const;
194 [[nodiscard]] std::unique_ptr<trie_impl>
subtrie(
const std::string_view& key_prefix)
const;
212 std::unique_ptr<impl> m_p_impl;
223 template <
typename Key,
typename Value,
typename KeySerializer = default_serializer<Key>>
224 class trie :
private boost::noncopyable
277 m_key_serializer{ key_serializer }
289 std::initializer_list<std::pair<key_type, value_type>> elements,
293 m_impl{ serialize_key<true>(std::
begin(elements), std::
end(elements), key_serializer),
294 building_observer_set,
295 double_array_density_factor },
296 m_key_serializer{ key_serializer }
310 template <
typename InputIterator>
317 m_impl{ serialize_key<false>(first, last, key_serializer), building_observer_set, double_array_density_factor },
318 m_key_serializer{ key_serializer }
332 template <
typename InputIterator>
334 std::move_iterator<InputIterator> first,
335 std::move_iterator<InputIterator> last,
339 m_impl{ serialize_key<true>(first, last, key_serializer), building_observer_set, double_array_density_factor },
340 m_key_serializer{ key_serializer }
350 std::unique_ptr<storage>&& p_storage,
352 m_impl{ std::move(p_storage) },
353 m_key_serializer{ key_serializer }
367 return std::empty(m_impl);
375 [[nodiscard]] std::size_t
size()
const
377 return std::size(m_impl);
390 if constexpr (std::is_same_v<key_type, std::string_view> || std::is_same_v<key_type, std::string>)
392 return m_impl.
contains(m_key_serializer(key));
396 const auto serialized_key = m_key_serializer(key);
397 return m_impl.
contains(std::string_view{ std::data(serialized_key), std::size(serialized_key) });
410 const auto*
const p_found = [
this, &key]() {
411 if constexpr (std::is_same_v<key_type, std::string_view> || std::is_same_v<key_type, std::string>)
413 return m_impl.
find(m_key_serializer(key));
417 const auto serialized_key = m_key_serializer(key);
418 return m_impl.
find(std::string_view{ std::data(serialized_key), std::size(serialized_key) });
425 return std::any_cast<value_type>(p_found);
435 return iterator{ std::move(std::begin(m_impl)) };
445 return iterator{ std::move(std::end(m_impl)) };
458 auto p_trie_impl = [
this, &key_prefix]() {
459 if constexpr (std::is_same_v<key_type, std::string_view> || std::is_same_v<key_type, std::string>)
461 return m_impl.
subtrie(m_key_serializer(key_prefix));
465 const auto serialized_key_prefix = m_key_serializer(key_prefix);
467 std::string_view{ std::data(serialized_key_prefix), std::size(serialized_key_prefix) });
474 std::unique_ptr<trie> p_trie{
new trie{ std::move(p_trie_impl), m_key_serializer } };
492 using serialized_element_type = std::pair<
493 std::conditional_t<std::is_same_v<key_type, std::string_view>, std::string_view, std::string>,
499 template <
bool MoveValue,
typename InputIterator>
500 static std::vector<serialized_element_type>
501 serialize_key(InputIterator first, InputIterator last,
const key_serializer_type& key_serializer)
503 std::vector<serialized_element_type> serialized{};
504 if constexpr (std::is_convertible_v<
505 typename std::iterator_traits<InputIterator>::iterator_category,
506 std::forward_iterator_tag>)
508 serialized.reserve(std::distance(first, last));
510 std::transform(first, last, std::back_inserter(serialized), [&key_serializer](
auto&& value) {
511 if constexpr (std::is_same_v<key_type, std::string_view> || std::is_same_v<key_type, std::string>)
513 if constexpr (MoveValue)
515 return serialized_element_type{ key_serializer(value.first), std::move(value.second) };
519 return serialized_element_type{ key_serializer(value.first), value.second };
524 const auto serialized_key_vector = key_serializer(value.first);
525 std::string serialized_key{ std::begin(serialized_key_vector), std::end(serialized_key_vector) };
526 if constexpr (MoveValue)
528 return serialized_element_type{ std::move(serialized_key), std::move(value.second) };
532 return serialized_element_type{ std::move(serialized_key), value.second };
542 const trie_impl m_impl;
550 m_impl{ std::move(*p_impl) },
551 m_key_serializer{ ker_serializer }
A default serializer.
Definition default_serializer.hpp:36
A storage.
Definition storage.hpp:28
An implementation of trie.
Definition trie.hpp:39
std::unique_ptr< trie_impl > subtrie(const std::string_view &key_prefix) const
Returns a subtrie.
static const building_observer_set_type & null_building_observer_set()
Returns the null building observer set.
trie_impl()
Creates an implementation of trie.
bool contains(const std::string_view &key) const
Returns true when the trie contains the given key.
std::size_t size() const
Returns the size of the trie.
trie_iterator_impl end() const
Returns the last iterator.
trie_impl(std::vector< std::pair< std::string, std::any > > elements, const building_observer_set_type &building_observer_set, std::size_t double_array_density_factor)
Creates a trie.
const std::any * find(const std::string_view &key) const
Finds the value object correspoinding the given key.
bool empty() const
Returns true when the trie is empty.
trie_impl(trie_impl &&another) noexcept
Moves a trie.
trie_impl(std::unique_ptr< storage > &&p_storage)
Creates a trie.
const storage & get_storage() const
Returns the storage.
static std::size_t default_double_array_density_factor()
Returns the default double array density factor.
trie_impl(std::vector< std::pair< std::string_view, std::any > > elements, const building_observer_set_type &building_observer_set, std::size_t double_array_density_factor)
Creates a trie.
trie_impl(std::unique_ptr< double_array > &&p_double_array)
Creates a trie.
trie_iterator_impl begin() const
Returns the first iterator.
~trie_impl()
Destroys an implementation of trie.
An implementation of trie iterator.
Definition trie_iterator.hpp:28
A trie iterator.
Definition trie_iterator.hpp:102
A trie.
Definition trie.hpp:225
const storage & get_storage() const
Returns the storage.
Definition trie.hpp:483
trie_impl::building_observer_set_type building_observer_set_type
The building observer set type.
Definition trie.hpp:242
bool contains(const key_type &key) const
Returns true when the trie contains the given key.
Definition trie.hpp:388
std::size_t size() const
Returns the size of the trie.
Definition trie.hpp:375
bool empty() const
Returns true when the trie is empty.
Definition trie.hpp:365
trie(std::unique_ptr< storage > &&p_storage, const key_serializer_type &key_serializer=default_serializer< key_type >{ true })
Creates a trie.
Definition trie.hpp:349
iterator begin() const
Returns the first iterator.
Definition trie.hpp:433
trie(InputIterator first, InputIterator last, const key_serializer_type &key_serializer=default_serializer< key_type >{ true }, const building_observer_set_type &building_observer_set=null_building_observer_set(), std::size_t double_array_density_factor=default_double_array_density_factor())
Creates a trie.
Definition trie.hpp:311
static const building_observer_set_type & null_building_observer_set()
Returns the null building observer set.
Definition trie.hpp:252
Value value_type
The value type.
Definition trie.hpp:233
const value_type * find(const key_type &key) const
Finds the value object correspoinding the given key.
Definition trie.hpp:408
trie(std::initializer_list< std::pair< key_type, value_type > > elements, const key_serializer_type &key_serializer=default_serializer< key_type >{ true }, const building_observer_set_type &building_observer_set=null_building_observer_set(), std::size_t double_array_density_factor=default_double_array_density_factor())
Creates a trie.
Definition trie.hpp:288
iterator end() const
Returns the last iterator.
Definition trie.hpp:443
trie(std::move_iterator< InputIterator > first, std::move_iterator< InputIterator > last, const key_serializer_type &key_serializer=default_serializer< key_type >{ true }, const building_observer_set_type &building_observer_set=null_building_observer_set(), std::size_t double_array_density_factor=default_double_array_density_factor())
Creates a trie.
Definition trie.hpp:333
static std::size_t default_double_array_density_factor()
Returns the default double array density factor.
Definition trie.hpp:262
KeySerializer key_serializer_type
The key serializer_type.
Definition trie.hpp:236
trie(const key_serializer_type &key_serializer=default_serializer< key_type >{ true })
Creates a trie.
Definition trie.hpp:275
Key key_type
The key type.
Definition trie.hpp:230
std::unique_ptr< trie > subtrie(const key_type &key_prefix) const
Returns a subtrie.
Definition trie.hpp:456
The building observer set type.
Definition trie.hpp:45
std::function< void(const std::string_view &serialized_key)> adding
Called when a key is adding.
Definition trie.hpp:52
std::function< void()> done
Called when the building is done.
Definition trie.hpp:57