libwordring
公開型 | 公開メンバ関数 | 限定公開型 | 限定公開メンバ関数 | 限定公開変数類 | 静的限定公開変数類 | フレンド | 全メンバ一覧
wordring::detail::trie_heap< Allocator > クラステンプレート

ダブル・アレイによるTrie実装のメモリー管理を行う [詳解]

#include <trie_heap.hpp>

公開型

using label_type = std::uint8_t
 
using allocator_type = Allocator
 
using serialize_iterator = trie_heap_serialize_iterator< container const >
 

公開メンバ関数

allocator_type get_allocator () const
 コンテナに関連付けられているアロケータを返す [詳解]
 
template<typename InputIterator , typename std::enable_if_t< std::is_integral_v< typename std::iterator_traits< InputIterator >::value_type >, std::nullptr_t > = nullptr>
void assign (InputIterator first, InputIterator last)
 直列化データから割り当てる [詳解]
 
serialize_iterator ibegin () const
 直列化用のイテレータを返す [詳解]
 
serialize_iterator iend () const
 直列化用のイテレータを返す [詳解]
 
void clear () noexcept
 すべての要素を削除する [詳解]
 
void swap (trie_heap &other)
 

限定公開型

using index_type = typename trie_node::index_type
 
using node_type = trie_node
 
using container = std::vector< trie_node, Allocator >
 
using label_vector = static_vector< std::uint16_t, 257 >
 

限定公開メンバ関数

 trie_heap (allocator_type const &alloc)
 
 trie_heap (std::initializer_list< trie_node > il, allocator_type const &alloc=allocator_type())
 初期化子リストから構築する [詳解]
 
index_type limit () const
 
void reserve (std::size_t n, index_type before=0)
 
void allocate (index_type idx, index_type before=0)
 
void allocate (index_type base, label_vector const &labels, index_type before=0)
 
index_type relocate (index_type parent, index_type from, label_vector const &labels)
 
index_type free (index_type idx, index_type before=0)
 
void free (index_type base, label_vector const &labels, index_type before=0)
 
index_type locate (label_vector const &labels, index_type &before) const
 
bool is_free (index_type base, label_vector const &labels) const
 
bool is_free (index_type parent, index_type base, label_vector const &labels) const
 
bool is_tail (index_type idx) const
 
bool has_child (index_type parent) const
 
bool has_null (index_type parent) const
 
bool has_sibling (index_type idx) const
 
bool has_sibling (index_type parent, index_type idx) const
 
index_type at (index_type parent, std::uint16_t label) const
 
index_type add (index_type parent, std::uint16_t label)
 
index_type add (index_type parent, label_vector const &labels)
 

限定公開変数類

container m_c
 

静的限定公開変数類

static constexpr std::uint16_t null_value = 256u
 

フレンド

template<typename Allocator1 >
std::ostream & operator<< (std::ostream &, trie_heap< Allocator1 > const &)
 
template<typename Allocator1 >
std::istream & operator>> (std::istream &, trie_heap< Allocator1 > &)
 

詳解

template<typename Allocator>
class wordring::detail::trie_heap< Allocator >

ダブル・アレイによるTrie実装のメモリー管理を行う

根は常にINDEX1となる。 INDEX0は使用されないため、0は末尾を示す値としても使用される。

配列の利用法

ダブル・アレイの添字は1から始まるため、INDEX0は使用されない。 そのため、以下の用途で使う。

ダブル・アレイのBASEは子の配置基準INDEXを示すため、1未満の値は格納されない。 および、葉は子を持たないため、BASEを使用しない。 そのため、以下の用途で使う。

ダブル・アレイのCHECKは親のINDEXを示すため、1未満の値は格納されない。 そのため、以下の用途に使う。

配列のイメージ

構築子と解体子

◆ trie_heap()

template<typename Allocator >
wordring::detail::trie_heap< Allocator >::trie_heap ( std::initializer_list< trie_node il,
allocator_type const &  alloc = allocator_type() 
)
inlineprotected

初期化子リストから構築する

引数
[in]ilノード・データの初期化子リスト
[in]allocアロケータ

関数詳解

◆ get_allocator()

template<typename Allocator >
allocator_type wordring::detail::trie_heap< Allocator >::get_allocator ( ) const
inline

コンテナに関連付けられているアロケータを返す

戻り値
コンテナに関連付けられているアロケータ

◆ assign()

template<typename Allocator >
template<typename InputIterator , typename std::enable_if_t< std::is_integral_v< typename std::iterator_traits< InputIterator >::value_type >, std::nullptr_t > = nullptr>
void wordring::detail::trie_heap< Allocator >::assign ( InputIterator  first,
InputIterator  last 
)
inline

直列化データから割り当てる

引数
[in]first直列化データの先頭を指すイテレータ
[in]last直列化データの終端を指すイテレータ

直列化データは[ibegin(), iend())から作成された整数値のリストを想定する。 整数の型(ビットの大きさ)は指定されず、適切に処理される。 [ibegin(), iend())から返される32ビット整数のコンテナでも、ファイルから読み込まれた8ビット整数のコンテナでも構わない。

参照
ibegin() const
iend() const

※trie_heapは単独で構築できないため、サンプルはtrieを使用。

// 元となるTrieを作成
std::vector<std::string> v1{ "a", "ac", "b", "cab", "cd" };
auto t1 = trie<char>(v1.begin(), v1.end());
// 直列化データを作成
std::vector<std::int32_t> v2{ t1.ibegin(), t1.iend() };
// 直列化データから割り当て
trie<char> t2;
t2.assign(v2.begin(), v2.end());

◆ ibegin()

template<typename Allocator >
serialize_iterator wordring::detail::trie_heap< Allocator >::ibegin ( ) const
inline

直列化用のイテレータを返す

戻り値
直列化データの先頭を示すイテレータ
参照
assign(InputIterator first, InputIterator last)
trie_heap::iend() const
wordring::serialize_iterator

このイテレータは逆参照すると32ビット整数値を返すが、 wordring::serialize_iterator を使用してバイト値を返すイテレータに変換できる。 一般に、ファイルへ保存する場合にバイトへ直列化する。 いずれの場合も、逆参照時に最小限の計算によって値を返すため、軽量である。

バイト直列化イテレータを使用する例

※trie_heapは単独で構築できないため、サンプルはtrieを使用。

// 元となるTrieを作成
std::vector<std::string> v1{ "a", "ac", "b", "cab", "cd" };
auto t1 = trie<char>(v1.begin(), v1.end());
// バイト直列化イテレータを作成
auto it1 = serialize_iterator(t1.ibegin());
auto it2 = serialize_iterator(t1.iend());
// バイト直列化イテレータから構築
auto t2 = trie<char>(it1, it2);
バイト列に保存する例

※trie_heapは単独で構築できないため、サンプルはtrieを使用。

// 元となるTrieを作成
std::vector<std::string> v1{ "a", "ac", "b", "cab", "cd" };
auto t1 = trie<char>(v1.begin(), v1.end());
// バイト直列化イテレータを作成
auto it1 = serialize_iterator(t1.ibegin());
auto it2 = serialize_iterator(t1.iend());
// バイト列に保存
auto v2 = std::vector<char>(it1, it2);
// バイト列から構築
auto t2 = trie<char>(v2.begin(), v2.end());

◆ iend()

template<typename Allocator >
serialize_iterator wordring::detail::trie_heap< Allocator >::iend ( ) const
inline

直列化用のイテレータを返す

戻り値
直列化データの終端を示すイテレータ
参照
trie_heap::ibegin() const

◆ clear()

template<typename Allocator >
void wordring::detail::trie_heap< Allocator >::clear ( )
inlinenoexcept

すべての要素を削除する

ただし、根は削除されない。

◆ allocate() [1/2]

template<typename Allocator >
void wordring::detail::trie_heap< Allocator >::allocate ( index_type  idx,
index_type  before = 0 
)
inlineprotected

idxで指定される空きノードを使用可能にする

◆ allocate() [2/2]

template<typename Allocator >
void wordring::detail::trie_heap< Allocator >::allocate ( index_type  base,
label_vector const &  labels,
index_type  before = 0 
)
inlineprotected

base + labelsを使用可能にする

◆ relocate()

template<typename Allocator >
index_type wordring::detail::trie_heap< Allocator >::relocate ( index_type  parent,
index_type  from,
label_vector const &  labels 
)
inlineprotected

ノードを移動させる

◆ free() [1/2]

template<typename Allocator >
index_type wordring::detail::trie_heap< Allocator >::free ( index_type  idx,
index_type  before = 0 
)
inlineprotected

idxを解放して未使用リンクにつなげる

  • before を返す。
  • これが次のfree呼び出しのヒントとして使える。

◆ free() [2/2]

template<typename Allocator >
void wordring::detail::trie_heap< Allocator >::free ( index_type  base,
label_vector const &  labels,
index_type  before = 0 
)
inlineprotected

base + labelsを解放して未使用リンクにつなげる

◆ locate()

template<typename Allocator >
index_type wordring::detail::trie_heap< Allocator >::locate ( label_vector const &  labels,
index_type &  before 
) const
inlineprotected

未使用ノードを検索する

  • labelsが配置される最初のノードの直前の空きノードをbeforeで返す。
  • これがallocate呼び出しのヒントとして使える。
  • INDEX1に子は配置されない。

◆ is_free() [1/2]

template<typename Allocator >
bool wordring::detail::trie_heap< Allocator >::is_free ( index_type  base,
label_vector const &  labels 
) const
inlineprotected

base + labelsがすべて未使用である場合、trueを返す

◆ is_free() [2/2]

template<typename Allocator >
bool wordring::detail::trie_heap< Allocator >::is_free ( index_type  parent,
index_type  base,
label_vector const &  labels 
) const
inlineprotected

base + labelsすべてについて、未使用あるいはparentからの遷移である場合、trueを返す

◆ is_tail()

template<typename Allocator >
bool wordring::detail::trie_heap< Allocator >::is_tail ( index_type  idx) const
inlineprotected

idxが遷移終端に達しているか、あるいはnull_valueによる遷移を持つ場合、trueを返す

  • has_childとhas_nullの組み合わせで表現できるが、検索高速化のため独立して用意した。

◆ has_child()

template<typename Allocator >
bool wordring::detail::trie_heap< Allocator >::has_child ( index_type  parent) const
inlineprotected

parentからのラベルによる遷移がある場合、trueを返す

◆ has_null()

template<typename Allocator >
bool wordring::detail::trie_heap< Allocator >::has_null ( index_type  parent) const
inlineprotected

parentからの空遷移がある場合、trueを返す

◆ has_sibling() [1/2]

template<typename Allocator >
bool wordring::detail::trie_heap< Allocator >::has_sibling ( index_type  idx) const
inlineprotected

idxにnull_value以外の兄弟がある場合、trueを返す

◆ has_sibling() [2/2]

template<typename Allocator >
bool wordring::detail::trie_heap< Allocator >::has_sibling ( index_type  parent,
index_type  idx 
) const
inlineprotected

idxにnull_value以外の兄弟がある場合、trueを返す

◆ at()

template<typename Allocator >
index_type wordring::detail::trie_heap< Allocator >::at ( index_type  parent,
std::uint16_t  label 
) const
inlineprotected

parentからlabelで遷移したINDEXを返す

  • 遷移先が無ければ0を返す。

◆ add()

template<typename Allocator >
index_type wordring::detail::trie_heap< Allocator >::add ( index_type  parent,
label_vector const &  labels 
)
inlineprotected

parentの子としてラベル集合labels内の各ラベルによる遷移を挿入する

  • 配置起点を返す。

このクラス詳解は次のファイルから抽出されました: