3 #include <wordring/trie/stable_trie_base_iterator.hpp>
4 #include <wordring/trie/trie_heap.hpp>
15 #include <type_traits>
63 template <
typename Allocator = std::allocator<trie_node>>
66 template <
typename Allocator1>
69 template <
typename Allocator1>
75 using typename base_type::container;
77 using typename base_type::index_type;
80 using base_type::null_value;
83 using label_type =
typename base_type::label_type;
84 using value_type = std::uint32_t;
85 using size_type =
typename container::size_type;
86 using allocator_type = Allocator;
100 using base_type::limit;
105 using base_type::add;
107 using base_type::m_c;
157 template <typename InputIterator, typename std::enable_if_t<std::is_integral_v<typename std::iterator_traits<InputIterator>::value_type>, std::nullptr_t> =
nullptr>
158 stable_trie_base(InputIterator first, InputIterator last, allocator_type
const& alloc = allocator_type())
180 template <typename InputIterator, typename std::enable_if_t<std::negation_v<std::is_integral<typename std::iterator_traits<InputIterator>::value_type>>, std::nullptr_t> =
nullptr>
181 stable_trie_base(InputIterator first, InputIterator last, allocator_type
const& alloc = allocator_type())
199 stable_trie_base(std::initializer_list<trie_node> il, allocator_type
const& alloc = allocator_type())
227 template <typename InputIterator, typename std::enable_if_t<std::is_integral_v<typename std::iterator_traits<InputIterator>::value_type>, std::nullptr_t> =
nullptr>
228 void assign(InputIterator first, InputIterator last)
249 template <typename InputIterator, typename std::enable_if_t<std::negation_v<std::is_integral<typename std::iterator_traits<InputIterator>::value_type>>, std::nullptr_t> =
nullptr>
250 void assign(InputIterator first, InputIterator last)
253 while (first != last)
283 idx = (d + pos.m_index)->m_base + null_value;
284 assert(1 < idx && idx < limit());
334 template <
typename InputIterator>
337 auto it =
find(first, last);
338 if (it ==
cend())
throw std::out_of_range(
"");
352 template <
typename InputIterator>
353 const_reference
at(InputIterator first, InputIterator last)
const
370 template <
typename Key>
373 return at(std::begin(key), std::end(key));
386 template <
typename Key>
387 const_reference
at(Key
const& key)
const
389 return at(std::begin(key), std::end(key));
402 template <
typename Key>
409 index_type idx = (d + it.m_index)->m_base + null_value;
410 assert(1 < idx && idx < limit());
463 size_type
size() const noexcept {
return m_c.front().m_base; }
471 return std::numeric_limits<std::int32_t>::max() /
sizeof(
node_type);
490 template <
typename InputIterator>
493 assert(value <=
static_cast<value_type
>(std::numeric_limits<int32_t>::max()));
495 if (first == last)
return cend();
497 index_type parent = 1;
500 for (index_type idx =
at(parent,
static_cast<std::uint8_t
>(*first)); idx != 0; idx =
at(parent,
static_cast<std::uint8_t
>(*first)))
504 if (first == last)
break;
507 if (!
has_null(parent) || first != last)
510 while (first != last)
512 std::uint16_t label =
static_cast<std::uint8_t
>(*first++);
513 index_type base = add(parent, label);
514 parent = base + label;
519 index_type idx = add(parent, null_value) + null_value;
520 (m_c.data() + idx)->m_base = -
static_cast<index_type
>(value);
522 ++m_c.front().m_base;
539 template <
typename Key>
542 return insert(std::begin(key), std::end(key), value);
553 index_type idx = pos.m_index;
554 assert(0 <= idx && idx < limit());
556 if (idx <= 1 || !
has_null(idx))
return;
559 index_type term = (m_c.data() + idx)->m_base + null_value;
560 assert((m_c.data() + term)->m_check == idx);
565 index_type parent = (m_c.data() + idx)->m_check;
570 --m_c.front().m_base;
581 template <
typename InputIterator>
582 void erase(InputIterator first, InputIterator last)
593 template <
typename Key>
596 erase(std::begin(key), std::end(key));
601 base_type::swap(other);
621 template <
typename InputIterator>
622 auto lookup(InputIterator first, InputIterator last, std::uint32_t& i)
const
626 index_type parent = 1;
628 while (first != last)
630 std::uint16_t ch =
static_cast<std::uint8_t
>(*first);
631 index_type idx =
at(parent, ch);
636 assert(1 <= parent && parent < limit());
652 template <
typename InputIterator>
653 auto lookup(InputIterator first, InputIterator last)
const
656 return lookup(first, last, i);
667 template <
typename Key>
670 auto it1 = std::begin(key);
671 auto it2 = std::end(key);
673 auto pair =
lookup(it1, it2);
675 return (pair.second == it2)
689 template <
typename InputIterator>
692 auto pair =
lookup(first, last);
694 auto it = pair.second;
695 index_type idx = pair.first.m_index;
697 return (it == last &&
has_null(idx))
710 template <
typename Key>
713 return find(std::begin(key), std::end(key));
723 template <
typename InputIterator>
724 bool contains(InputIterator first, InputIterator last)
const
726 auto pair =
lookup(first, last);
728 auto it = pair.second;
729 index_type idx = pair.first.m_index;
731 return !(it != last || !
has_null(idx));
740 template <
typename Key>
743 return contains(std::begin(key), std::end(key));
751 template <
typename Allocator1>
762 template <
typename Allocator1>
const_iterator cbegin() const noexcept
根を指すイテレータを返す
Definition: stable_trie_base.hpp:431
reference at(Key const &key)
葉の値への参照を返す
Definition: stable_trie_base.hpp:371
stable_trie_base(std::initializer_list< trie_node > il, allocator_type const &alloc=allocator_type())
初期化子リストからの構築
Definition: stable_trie_base.hpp:199
bool has_null(index_type parent) const
Definition: trie_heap.hpp:690
void assign(InputIterator first, InputIterator last)
直列化データからの割り当て
Definition: stable_trie_base.hpp:228
index_type free(index_type idx, index_type before=0)
Definition: trie_heap.hpp:526
const_reference at(const_iterator pos) const
葉の値への参照を返す
Definition: stable_trie_base.hpp:316
auto lookup(InputIterator first, InputIterator last, std::uint32_t &i) const
部分一致検索
Definition: stable_trie_base.hpp:622
friend std::ostream & operator<<(std::ostream &, stable_trie_base< Allocator1 > const &)
ストリームへ出力する
Definition: stable_trie_base.hpp:752
bool has_child(index_type parent) const
Definition: trie_heap.hpp:664
auto lookup(InputIterator first, InputIterator last) const
部分一致検索
Definition: stable_trie_base.hpp:653
void erase(const_iterator pos)
キー文字列を削除する
Definition: stable_trie_base.hpp:551
reference at(const_iterator pos, index_type &idx)
葉の値への参照を返す
Definition: stable_trie_base.hpp:279
basic_trie< Label, detail::trie_base< Allocator > > trie
メモリー使用量削減を目標とする汎用Trie
Definition: trie.hpp:1026
static constexpr size_type max_size() noexcept
キー文字列の格納可能な最大数を調べる
Definition: stable_trie_base.hpp:469
stable_trie_base()
空のコンテナを構築する
Definition: stable_trie_base.hpp:117
const_iterator end() const noexcept
根の終端を指すイテレータを返す
Definition: stable_trie_base.hpp:440
std::ostream & operator<<(std::ostream &os, stable_trie_base< Allocator1 > const &trie)
ストリームへ出力する
Definition: stable_trie_base.hpp:752
Definition: stable_trie_base_iterator.hpp:8
reference at(InputIterator first, InputIterator last)
葉の値への参照を返す
Definition: stable_trie_base.hpp:335
void assign(InputIterator first, InputIterator last)
直列化データから割り当てる
Definition: trie_heap.hpp:260
const_iterator find(Key const &key) const
完全一致検索
Definition: stable_trie_base.hpp:711
void clear() noexcept
すべての要素を削除する
Definition: trie_heap.hpp:364
const_iterator search(Key const &key) const
前方一致検索
Definition: stable_trie_base.hpp:668
friend std::istream & operator>>(std::istream &, stable_trie_base< Allocator1 > &)
ストリームから入力する
Definition: stable_trie_base.hpp:763
void erase(Key const &key)
キー文字列を削除する
Definition: stable_trie_base.hpp:594
const_iterator insert(Key const &key, value_type value=0)
キー文字列を挿入する
Definition: stable_trie_base.hpp:540
任意の整数型をラベルとして用いることが出来る汎用Trie
Definition: trie.hpp:130
allocator_type get_allocator() const
コンテナに関連付けられているアロケータを返す
Definition: trie_heap.hpp:226
Definition: trie_heap.hpp:23
void erase(InputIterator first, InputIterator last)
キー文字列を削除する
Definition: stable_trie_base.hpp:582
bool contains(InputIterator first, InputIterator last) const
キー文字列が格納されているか調べる
Definition: stable_trie_base.hpp:724
const_iterator insert(InputIterator first, InputIterator last, value_type value=0)
キー文字列を挿入する
Definition: stable_trie_base.hpp:491
serialize_iterator ibegin() const
直列化用のイテレータを返す
Definition: trie_heap.hpp:342
stable_trie_base(InputIterator first, InputIterator last, allocator_type const &alloc=allocator_type())
直列化データからの構築
Definition: stable_trie_base.hpp:158
bool contains(Key const &key) const
キー文字列が格納されているか調べる
Definition: stable_trie_base.hpp:741
reference operator[](Key const &key)
葉の値への参照を返す
Definition: stable_trie_base.hpp:403
ダブル・アレイによるTrie実装のメモリー管理を行う
Definition: trie_heap.hpp:200
index_type at(index_type parent, std::uint16_t label) const
Definition: trie_heap.hpp:766
Definition: trie_heap.hpp:40
const_iterator cend() const noexcept
根の終端を指すイテレータを返す
Definition: stable_trie_base.hpp:449
const_reference at(InputIterator first, InputIterator last) const
葉の値への参照を返す
Definition: stable_trie_base.hpp:353
stable_trie_base(allocator_type const &alloc)
アロケータを指定して空のコンテナを構築する
Definition: stable_trie_base.hpp:131
serialize_iterator iend() const
直列化用のイテレータを返す
Definition: trie_heap.hpp:353
bool empty() const noexcept
キー文字列を格納していないことを調べる
Definition: stable_trie_base.hpp:457
size_type size() const noexcept
格納しているキー文字列数を調べる
Definition: stable_trie_base.hpp:463
const_iterator begin() const noexcept
根を指すイテレータを返す
Definition: stable_trie_base.hpp:423
挿入や削除によって葉を指すイテレータが無効とならない安定なTrie木の実装
Definition: stable_trie_base.hpp:64
std::istream & operator>>(std::istream &is, stable_trie_base< Allocator1 > &trie)
ストリームから入力する
Definition: stable_trie_base.hpp:763
const_reference at(Key const &key) const
葉の値への参照を返す
Definition: stable_trie_base.hpp:387
Definition: trie_heap.hpp:78
reference at(const_iterator pos)
葉の値への参照を返す
Definition: stable_trie_base.hpp:302
const_iterator find(InputIterator first, InputIterator last) const
完全一致検索
Definition: stable_trie_base.hpp:690