|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Owning prefix tree map from strings to values. More...
#include <prefix-tree.H>
Classes | |
| class | Node |
| Internal trie node storing one character and an optional value. More... | |
Public Types | |
| using | Key = std::string |
| Key type accepted by the map. | |
| using | Value = T |
| Mapped value type stored in terminal nodes. | |
Public Member Functions | |
| Prefix_Tree_Map () | |
| Construct an empty prefix map. | |
| Prefix_Tree_Map (const Prefix_Tree_Map &other) | |
| Construct a deep copy of another prefix map. | |
| Prefix_Tree_Map (Prefix_Tree_Map &&other) noexcept | |
| Move-construct, taking ownership of another map's root. | |
| ~Prefix_Tree_Map () noexcept | |
| Destroy the owned root and all descendants. | |
| Prefix_Tree_Map & | operator= (const Prefix_Tree_Map &other) |
| Replace this map with a deep copy of another map. | |
| Prefix_Tree_Map & | operator= (Prefix_Tree_Map &&other) noexcept |
| Move-assign, taking ownership of another map's root. | |
| bool | insert (const Key &key, const Value &value) |
| Insert key with a copied value if absent. | |
| bool | insert (const Key &key, Value &&value) |
| Insert key with a moved value if absent. | |
| void | insert_or_assign (const Key &key, Value value) |
| Insert key or overwrite its mapped value. | |
| bool | erase (const Key &key) noexcept |
| Remove key if present. | |
| bool | contains (const Key &key) const noexcept |
| Check whether key is stored. | |
| const Value * | find (const Key &key) const noexcept |
| Look up key. | |
| Value * | find (const Key &key) noexcept |
| Mutable lookup overload. | |
| size_t | size () const noexcept |
| Return the number of stored key-value pairs. | |
| size_t | count () const noexcept |
| Return the number of stored key-value pairs. | |
| bool | is_empty () const noexcept |
| Check whether the map has no keys. | |
| void | clear () |
| Remove every key-value pair from the map. | |
| DynArray< Key > | words (const size_t max_word_length=2048) const |
| Get all keys stored in the map. | |
| DynArray< Key > | words_with_prefix (const Key &prefix, const size_t max_word_length=2048) const |
| Get all keys with a given prefix. | |
Private Member Functions | |
| void | ensure_root () |
| Ensure the root node exists after a move. | |
| const Node * | find_node (const std::string &word) const noexcept |
| Find the terminal node for word. | |
| Node * | find_node (const std::string &word) noexcept |
| Find the terminal node for word. | |
| template<typename U > | |
| bool | insert_impl (const std::string &word, U &&value, const bool assign_if_present=false) |
Shared implementation for insert()'s copy/move overloads and for insert_or_assign(): when assign_if_present is true (only insert_or_assign() sets it), an already-present key's value is overwritten in place instead of leaving it untouched – letting insert_or_assign() share this single tree descent instead of calling find() first and then re-descending from the root a second time via this function. | |
Static Private Member Functions | |
| static void | destroy_root (Node *root) noexcept |
| Destroy an owned root and all descendants. | |
Private Attributes | |
| Node * | root_ = nullptr |
| size_t | size_ = 0 |
Owning prefix tree map from strings to values.
Prefix_Tree_Map is the mapped-value counterpart of Prefix_Tree. It uses the same one-character-per-edge trie shape, but terminal nodes store a mapped value instead of only a word-ending flag. It is intentionally a separate type so the legacy Prefix_Tree and Cnode APIs remain source-compatible.
Insert, exact lookup, and prefix lookup are O(k) in the length of the key plus the cost of scanning sibling lists at each level. size() and count() are O(1). words() and words_with_prefix() return independent arrays of copied keys.
Aleph::RadixTree (tpl_radix_tree.H), which compresses chains of single-child nodes into one edge, Prefix_Tree_Map never compresses: a single key of length k always creates (or reuses) k nodes on its own path. Destruction, deep copy, and words()/words_with_prefix() all recurse to that depth, so a single very long key (tens of thousands of characters) can exhaust the stack. For workloads with long individual keys or long shared prefixes, prefer RadixTree.std::string::c_str(), which stops at the first ‘’\0'. Two distinctstd::stringkeys that only differ after an embedded NUL byte (legal forstd::string, unlike C strings) are silently treated as the same key. This limitation is shared with the legacyCnode/Prefix_Tree`.| T | Mapped value type. Move-only values are supported for insertion and lookup; copying the map requires T to be copy-constructible. |
Definition at line 1018 of file prefix-tree.H.
| using Aleph::Prefix_Tree_Map< T >::Key = std::string |
Key type accepted by the map.
Definition at line 1022 of file prefix-tree.H.
Mapped value type stored in terminal nodes.
Definition at line 1025 of file prefix-tree.H.
|
inline |
Construct an empty prefix map.
| std::bad_alloc | If allocating the root node fails. |
Definition at line 1519 of file prefix-tree.H.
|
inline |
Construct a deep copy of another prefix map.
| [in] | other | Map to copy. |
| Whatever | T copying throws, or std::bad_alloc. |
Definition at line 1530 of file prefix-tree.H.
|
inlinenoexcept |
Move-construct, taking ownership of another map's root.
| [in,out] | other | Source map, left empty and usable. |
Definition at line 1542 of file prefix-tree.H.
References Aleph::blossom_maximum_cardinality_matching().
|
inlinenoexcept |
Destroy the owned root and all descendants.
| Nothing. |
Definition at line 1553 of file prefix-tree.H.
References Aleph::Prefix_Tree_Map< T >::destroy_root(), and Aleph::Prefix_Tree_Map< T >::root_.
|
inline |
Remove every key-value pair from the map.
| std::bad_alloc | If recreating the empty root fails. |
Definition at line 1739 of file prefix-tree.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::Prefix_Tree_Map< T >::destroy_root(), Aleph::Prefix_Tree_Map< T >::root_, and Aleph::Prefix_Tree_Map< T >::size_.
Referenced by TEST().
|
inlinenoexcept |
Check whether key is stored.
| [in] | key | Key to search for. |
| Nothing. |
Definition at line 1667 of file prefix-tree.H.
References Aleph::Prefix_Tree_Map< T >::find().
|
inlinenoexcept |
Return the number of stored key-value pairs.
| Nothing. |
Definition at line 1719 of file prefix-tree.H.
References Aleph::Prefix_Tree_Map< T >::size().
Referenced by TEST().
|
inlinestaticprivatenoexcept |
Destroy an owned root and all descendants.
Definition at line 1448 of file prefix-tree.H.
References root().
Referenced by Aleph::Prefix_Tree_Map< T >::~Prefix_Tree_Map(), Aleph::Prefix_Tree_Map< T >::clear(), Aleph::Prefix_Tree_Map< T >::operator=(), and Aleph::Prefix_Tree_Map< T >::operator=().
|
inlineprivate |
Ensure the root node exists after a move.
Definition at line 1458 of file prefix-tree.H.
References Aleph::Prefix_Tree_Map< T >::root_.
Referenced by Aleph::Prefix_Tree_Map< T >::insert_impl().
Remove key if present.
| [in] | key | Key to remove. |
| Nothing. |
Definition at line 1649 of file prefix-tree.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::Prefix_Tree_Map< T >::find_node(), Aleph::Prefix_Tree_Map< T >::Node::has_value(), Aleph::Prefix_Tree_Map< T >::Node::reset_value(), and Aleph::Prefix_Tree_Map< T >::size_.
Referenced by TEST().
Look up key.
| [in] | key | Key to search for. |
| Nothing. |
Definition at line 1680 of file prefix-tree.H.
References Aleph::Prefix_Tree_Map< T >::find_node(), and Aleph::Prefix_Tree_Map< T >::Node::value().
Referenced by Aleph::Prefix_Tree_Map< T >::contains(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
Mutable lookup overload.
| [in] | key | Key to search for. |
| Nothing. |
Definition at line 1694 of file prefix-tree.H.
References Aleph::Prefix_Tree_Map< T >::find_node(), and Aleph::Prefix_Tree_Map< T >::Node::value().
|
inlineprivatenoexcept |
Find the terminal node for word.
Definition at line 1465 of file prefix-tree.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::Prefix_Tree_Map< T >::root_, and Aleph::Prefix_Tree_Map< T >::Node::search_prefix().
Referenced by Aleph::Prefix_Tree_Map< T >::erase(), Aleph::Prefix_Tree_Map< T >::find(), Aleph::Prefix_Tree_Map< T >::find(), and Aleph::Prefix_Tree_Map< T >::find_node().
|
inlineprivatenoexcept |
Find the terminal node for word.
Definition at line 1475 of file prefix-tree.H.
References Aleph::blossom_maximum_cardinality_matching(), and Aleph::Prefix_Tree_Map< T >::find_node().
Insert key with a copied value if absent.
| [in] | key | Key to insert. The empty string is valid. |
| [in] | value | Value to copy. |
| Whatever | T copying throws, or std::bad_alloc. |
Definition at line 1607 of file prefix-tree.H.
References Aleph::Prefix_Tree_Map< T >::insert_impl(), and value.
Referenced by TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
Insert key with a moved value if absent.
| [in] | key | Key to insert. The empty string is valid. |
| [in,out] | value | Value to move from if insertion succeeds. |
| Whatever | T moving throws, or std::bad_alloc. |
Definition at line 1622 of file prefix-tree.H.
References Aleph::Prefix_Tree_Map< T >::insert_impl(), and value.
|
inlineprivate |
Shared implementation for insert()'s copy/move overloads and for insert_or_assign(): when assign_if_present is true (only insert_or_assign() sets it), an already-present key's value is overwritten in place instead of leaving it untouched – letting insert_or_assign() share this single tree descent instead of calling find() first and then re-descending from the root a second time via this function.
Definition at line 1490 of file prefix-tree.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::Prefix_Tree_Map< T >::ensure_root(), Aleph::Prefix_Tree_Map< T >::root_, Aleph::Prefix_Tree_Map< T >::Node::search_prefix(), Aleph::Prefix_Tree_Map< T >::size_, and value.
Referenced by Aleph::Prefix_Tree_Map< T >::insert(), Aleph::Prefix_Tree_Map< T >::insert(), and Aleph::Prefix_Tree_Map< T >::insert_or_assign().
Insert key or overwrite its mapped value.
| [in] | key | Key to insert or update. |
| [in] | value | New value, moved into place. |
| Whatever | T construction or assignment throws, or std::bad_alloc. |
Definition at line 1634 of file prefix-tree.H.
References Aleph::Prefix_Tree_Map< T >::insert_impl(), and value.
|
inlinenoexcept |
Check whether the map has no keys.
| Nothing. |
Definition at line 1730 of file prefix-tree.H.
References Aleph::Prefix_Tree_Map< T >::size_.
|
inline |
Replace this map with a deep copy of another map.
| [in] | other | Map to copy from. |
| Whatever | T copying throws, or std::bad_alloc. |
Definition at line 1568 of file prefix-tree.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::Prefix_Tree_Map< T >::Node::clone(), Aleph::Prefix_Tree_Map< T >::destroy_root(), Aleph::Prefix_Tree_Map< T >::root_, and Aleph::Prefix_Tree_Map< T >::size_.
|
inlinenoexcept |
Move-assign, taking ownership of another map's root.
| [in,out] | other | Source map, left empty and usable. |
Definition at line 1586 of file prefix-tree.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::Prefix_Tree_Map< T >::destroy_root(), Aleph::Prefix_Tree_Map< T >::root_, and Aleph::Prefix_Tree_Map< T >::size_.
|
inlinenoexcept |
Return the number of stored key-value pairs.
| Nothing. |
Definition at line 1706 of file prefix-tree.H.
References Aleph::Prefix_Tree_Map< T >::size_.
Referenced by Aleph::Prefix_Tree_Map< T >::count(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inline |
Get all keys stored in the map.
| [in] | max_word_length | Maximum expected key length. |
| std::bad_alloc | If allocation fails. |
Definition at line 1757 of file prefix-tree.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::DynArray< T >::reserve(), Aleph::Prefix_Tree_Map< T >::root_, and Aleph::Prefix_Tree_Map< T >::Node::words_impl().
|
inline |
Get all keys with a given prefix.
| [in] | prefix | Prefix to search for. |
| [in] | max_word_length | Maximum expected key length. |
| std::bad_alloc | If allocation fails. |
Definition at line 1780 of file prefix-tree.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::prefix(), Aleph::Prefix_Tree_Map< T >::root_, and Aleph::Prefix_Tree_Map< T >::Node::search_prefix().
Referenced by TEST().
|
private |
Definition at line 1444 of file prefix-tree.H.
Referenced by Aleph::Prefix_Tree_Map< T >::~Prefix_Tree_Map(), Aleph::Prefix_Tree_Map< T >::clear(), Aleph::Prefix_Tree_Map< T >::ensure_root(), Aleph::Prefix_Tree_Map< T >::find_node(), Aleph::Prefix_Tree_Map< T >::insert_impl(), Aleph::Prefix_Tree_Map< T >::operator=(), Aleph::Prefix_Tree_Map< T >::operator=(), Aleph::Prefix_Tree_Map< T >::words(), and Aleph::Prefix_Tree_Map< T >::words_with_prefix().
|
private |
Definition at line 1445 of file prefix-tree.H.
Referenced by Aleph::Prefix_Tree_Map< T >::clear(), Aleph::Prefix_Tree_Map< T >::erase(), Aleph::Prefix_Tree_Map< T >::insert_impl(), Aleph::Prefix_Tree_Map< T >::is_empty(), Aleph::Prefix_Tree_Map< T >::operator=(), Aleph::Prefix_Tree_Map< T >::operator=(), and Aleph::Prefix_Tree_Map< T >::size().