|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Owning prefix tree wrapper. More...
#include <prefix-tree.H>
Public Member Functions | |
| Prefix_Tree () | |
| Construct an empty prefix tree. | |
| Prefix_Tree (const Prefix_Tree &other) | |
| Construct a deep copy of another prefix tree. | |
| Prefix_Tree (Prefix_Tree &&other) | |
| Move-construct, taking ownership of another tree's contents. | |
| ~Prefix_Tree () noexcept | |
| Destroy the owned root and all descendants. | |
| Prefix_Tree & | operator= (const Prefix_Tree &other) |
| Replace this tree with a deep copy of another tree. | |
| Prefix_Tree & | operator= (Prefix_Tree &&other) |
| Move-assign, taking ownership of another tree's contents. | |
| bool | insert_word (const std::string &word) |
| Insert a word into the tree. | |
| bool | contains (const std::string &word) const noexcept |
| Check whether a word exists in the tree. | |
| DynArray< std::string > | words (const size_t max_word_length=2048) const |
| Get all words stored in the tree. | |
| DynArray< std::string > | words_with_prefix (const std::string &prefix, const size_t max_word_length=2048) const |
| Get all words with a given prefix. | |
| size_t | count () const noexcept |
| Count the words stored in the tree. | |
| size_t | size () const noexcept |
| Return the number of words stored in the tree. | |
| Cnode * | root () noexcept |
| Return the mutable root node. | |
| const Cnode * | root () const noexcept |
| Return the const root node. | |
Private Member Functions | |
| void | swap_state (Prefix_Tree &other) noexcept |
| Swap internal ownership and cached count state. | |
Static Private Member Functions | |
| static void | destroy_root (Cnode *root) noexcept |
| Destroy an owned root and all its descendants. | |
Private Attributes | |
| Cnode * | root_ = nullptr |
| size_t | word_count_ = 0 |
| bool | count_dirty_ = false |
| bool | mutable_root_exposed_ = false |
Owning prefix tree wrapper.
Prefix_Tree owns a root Cnode and destroys all descendant nodes in its destructor. It provides the same common word-level operations as Cnode, while preserving Cnode as the low-level node API for legacy code and manual tree manipulation.
Copies are deep copies. The root returned by root() is non-owning; callers must not delete it or call destroy_tree() on it.
const calls to count() or size(), because those functions may refresh the mutable cached word count after the low-level mutable root() API has been exposed. Definition at line 746 of file prefix-tree.H.
|
inline |
Construct an empty prefix tree.
The root node uses the conventional ‘’\0'` symbol; the symbol is not part of any stored word.
| std::bad_alloc | If allocating the root node fails. |
Definition at line 780 of file prefix-tree.H.
|
inline |
Construct a deep copy of another prefix tree.
| [in] | other | Tree to copy. |
| std::bad_alloc | If allocating any copied node fails. |
Definition at line 791 of file prefix-tree.H.
|
inline |
Move-construct, taking ownership of another tree's contents.
| [in,out] | other | Tree to move from. Left as a valid empty tree. |
| std::bad_alloc | If allocating the replacement empty root for other fails; in that case other is left unchanged. |
Definition at line 805 of file prefix-tree.H.
References Aleph::blossom_maximum_cardinality_matching(), and swap_state().
|
inlinenoexcept |
Destroy the owned root and all descendants.
This destructor does not throw.
Definition at line 814 of file prefix-tree.H.
References destroy_root(), and root_.
Check whether a word exists in the tree.
| [in] | word | Word to search for. |
Definition at line 882 of file prefix-tree.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::Cnode::contains(), and root_.
Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inlinenoexcept |
Count the words stored in the tree.
const method may update the internal mutable cache. It is not safe to call concurrently on the same tree without external synchronization. Definition at line 932 of file prefix-tree.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::Cnode::count(), count_dirty_, mutable_root_exposed_, root_, and word_count_.
Referenced by size(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
Destroy an owned root and all its descendants.
Definition at line 754 of file prefix-tree.H.
References Aleph::Cnode::destroy(), and root().
Referenced by ~Prefix_Tree(), and operator=().
Insert a word into the tree.
| [in] | word | Word to insert. |
| std::bad_alloc | If memory allocation fails. |
Definition at line 869 of file prefix-tree.H.
References Aleph::and, Aleph::blossom_maximum_cardinality_matching(), count_dirty_, Aleph::Cnode::insert_word(), mutable_root_exposed_, root_, and word_count_.
Referenced by demo_basic_operations(), demo_trie_structure(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inline |
Replace this tree with a deep copy of another tree.
| [in] | other | Tree to copy from. |
| std::bad_alloc | If allocating any copied node fails. |
Definition at line 829 of file prefix-tree.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::Cnode::clone(), count_dirty_, destroy_root(), mutable_root_exposed_, root_, and word_count_.
|
inline |
Move-assign, taking ownership of another tree's contents.
| [in,out] | other | Tree to move from. Left as a valid empty tree. |
| std::bad_alloc | If allocating the replacement empty root for other fails; in that case both trees are left unchanged. |
Definition at line 852 of file prefix-tree.H.
References Aleph::blossom_maximum_cardinality_matching(), and swap_state().
Return the const root node.
Definition at line 978 of file prefix-tree.H.
References root_.
|
inlinenoexcept |
Return the mutable root node.
Definition at line 964 of file prefix-tree.H.
References count_dirty_, mutable_root_exposed_, and root_.
Referenced by destroy_root(), TEST(), and TEST().
|
inlinenoexcept |
Return the number of words stored in the tree.
Definition at line 952 of file prefix-tree.H.
References count().
Referenced by demo_command_autocomplete(), demo_performance(), demo_prefix_search(), demo_spell_checker(), TEST(), TEST(), and TEST().
|
inlineprivatenoexcept |
Swap internal ownership and cached count state.
Definition at line 764 of file prefix-tree.H.
References Aleph::blossom_maximum_cardinality_matching(), count_dirty_, mutable_root_exposed_, root_, and word_count_.
Referenced by Prefix_Tree(), and operator=().
|
inline |
Get all words stored in the tree.
| [in] | max_word_length | Maximum expected word length (default 2048). |
| std::bad_alloc | If memory allocation fails. |
max_word_length must be large enough for every stored word when assertions are enabled. Definition at line 897 of file prefix-tree.H.
References Aleph::blossom_maximum_cardinality_matching(), root_, and Aleph::Cnode::words().
|
inline |
Get all words with a given prefix.
| [in] | prefix | Prefix to search for. |
| [in] | max_word_length | Maximum expected word length (default 2048). |
| std::bad_alloc | If memory allocation fails. |
max_word_length must be at least prefix.size() and large enough for every emitted word when assertions are enabled. Definition at line 913 of file prefix-tree.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::prefix(), root_, and Aleph::Cnode::words_with_prefix().
Definition at line 750 of file prefix-tree.H.
Referenced by count(), insert_word(), operator=(), root(), and swap_state().
Definition at line 751 of file prefix-tree.H.
Referenced by count(), insert_word(), operator=(), root(), and swap_state().
|
private |
Definition at line 748 of file prefix-tree.H.
Referenced by ~Prefix_Tree(), contains(), count(), insert_word(), operator=(), root(), root(), swap_state(), words(), and words_with_prefix().
|
mutableprivate |
Definition at line 749 of file prefix-tree.H.
Referenced by count(), insert_word(), operator=(), and swap_state().