|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Naive compressed suffix tree (didactic implementation). More...
#include <Suffix_Structures.H>
Classes | |
| struct | Node |
| One tree node. More... | |
Public Member Functions | |
| Naive_Suffix_Tree (const std::string_view text="") | |
| Construct a naive suffix tree. | |
| Naive_Suffix_Tree (const Naive_Suffix_Tree &other)=default | |
| Copy the text and the nodes of another tree. | |
| Naive_Suffix_Tree (Naive_Suffix_Tree &&other) | |
| Take the text and the nodes of another tree, which is left as the tree of the empty text, as built by default. | |
| Naive_Suffix_Tree & | operator= (const Naive_Suffix_Tree &other)=default |
| Copy the text and the nodes of another tree. | |
| Naive_Suffix_Tree & | operator= (Naive_Suffix_Tree &&other) noexcept |
| Exchange this tree with another, which is left with the previous text and nodes of this one. | |
| void | swap (Naive_Suffix_Tree &other) noexcept |
| Exchange in O(1) the text and the nodes of this tree and another. | |
| void | build (const std::string_view text) |
Build or rebuild the suffix tree for text. | |
| bool | contains (const std::string_view pattern) const |
| Return true if the pattern appears in the text. | |
| Array< size_t > | find_all (const std::string_view pattern) const |
| Return all occurrences of a pattern. | |
| size_t | node_count () const noexcept |
| Return the number of nodes in the tree. | |
| size_t | text_size () const noexcept |
| Return the text size (without terminal marker). | |
| const Array< Node > & | nodes () const noexcept |
| Return the internal node array for inspection/debugging. | |
Static Public Attributes | |
| static constexpr size_t | npos = std::numeric_limits<size_t>::max() |
Private Member Functions | |
| size_t | create_node (const size_t start, const size_t end, const size_t parent, const size_t suffix_index) |
| size_t | find_child_by_first_char (const size_t node, const char c) const |
| void | replace_child (const size_t parent, const size_t old_child, const size_t new_child) |
| void | insert_suffix (const size_t suffix_start) |
| void | collect_leaf_suffixes (const size_t node, Array< size_t > &out) const |
Static Private Member Functions | |
| static char | choose_terminal (const std::string_view s) |
Private Attributes | |
| std::string | text_ |
| size_t | original_size_ = 0 |
| Array< Node > | nodes_ |
Naive compressed suffix tree (didactic implementation).
This is intentionally simple and educational (O(n^2) construction), not Ukkonen's linear-time algorithm.
std::vector copies these objects instead of moving them: their move constructor is not noexcept, because it leaves the source empty and usable, which allocates, and std::vector moves only what cannot throw. The containers of Aleph do not copy them to grow: Array moves its items, and DynList and DynArray never relocate them. Definition at line 248 of file Suffix_Structures.H.
|
inlineexplicit |
Construct a naive suffix tree.
| [in] | text | The text to build the tree from. If empty, the tree will only contain the terminal character. |
| std::domain_error | If all 256 byte values are present in text, leaving no unique sentinel character available. |
Definition at line 393 of file Suffix_Structures.H.
References build().
|
default |
Copy the text and the nodes of another tree.
| [in] | other | tree to copy. |
| std::bad_alloc | if the text or the nodes cannot be allocated. |
|
inline |
Take the text and the nodes of another tree, which is left as the tree of the empty text, as built by default.
The implicit move took the text and the nodes, the root among them, but copied the text size: the moved-from tree then reported the size of a text that it no longer had, and its searches read a missing root.
| [in,out] | other | tree to move from. |
| std::bad_alloc | if the tree left to other cannot be allocated; other is then unchanged. |
other. Definition at line 416 of file Suffix_Structures.H.
References swap().
|
inline |
Build or rebuild the suffix tree for text.
| [in] | text | The text to build the tree from. |
| std::domain_error | If all 256 byte values are present in text, leaving no unique sentinel character available. |
Definition at line 457 of file Suffix_Structures.H.
References choose_terminal(), insert_suffix(), nodes_, original_size_, and text_.
Referenced by Naive_Suffix_Tree(), and Moved_From_Case< Naive_Suffix_Tree >::use().
|
inlinestaticprivate |
Definition at line 271 of file Suffix_Structures.H.
References ah_domain_error.
Referenced by build().
|
inlineprivate |
Definition at line 364 of file Suffix_Structures.H.
References Aleph::Array< T >::append(), Aleph::Array< T >::is_empty(), nodes_, npos, original_size_, out, and Aleph::Array< T >::remove_last().
Referenced by find_all().
|
inline |
Return true if the pattern appears in the text.
| [in] | pattern | The substring pattern to search for. |
pattern is found in the text, false otherwise. If pattern is empty, returns true. | None. |
Definition at line 481 of file Suffix_Structures.H.
References Aleph::and, find_child_by_first_char(), k, nodes_, npos, and text_.
Referenced by TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Naive_Suffix_Tree >::use().
|
inlineprivate |
Definition at line 288 of file Suffix_Structures.H.
References Aleph::Naive_Suffix_Tree::Node::end, nodes_, Aleph::Naive_Suffix_Tree::Node::parent, Aleph::Naive_Suffix_Tree::Node::start, and Aleph::Naive_Suffix_Tree::Node::suffix_index.
Referenced by insert_suffix().
|
inline |
Return all occurrences of a pattern.
| [in] | pattern | The substring pattern to search for. |
pattern in the text, sorted in ascending order. Returns an empty array if no occurrences are found. pattern is empty, all positions [0, text.size()] are returned. | None. |
Definition at line 532 of file Suffix_Structures.H.
References Aleph::suffix_structures_detail::all_positions(), Aleph::and, collect_leaf_suffixes(), find_child_by_first_char(), k, nodes_, npos, original_size_, Aleph::suffix_structures_detail::sort_matches(), and text_.
Referenced by Moved_From_Case< Naive_Suffix_Tree >::is_empty(), main(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Naive_Suffix_Tree >::use().
|
inlineprivate |
Definition at line 299 of file Suffix_Structures.H.
References nodes_, npos, Aleph::Array< T >::size(), and text_.
Referenced by contains(), find_all(), and insert_suffix().
|
inlineprivate |
Definition at line 319 of file Suffix_Structures.H.
References Aleph::and, create_node(), find_child_by_first_char(), k, nodes_, npos, replace_child(), Aleph::split(), and text_.
Referenced by build().
|
inlinenoexcept |
Return the number of nodes in the tree.
| None. |
Definition at line 582 of file Suffix_Structures.H.
References nodes_.
Referenced by main(), TEST(), and Moved_From_Case< Naive_Suffix_Tree >::use().
Return the internal node array for inspection/debugging.
| None. |
Definition at line 605 of file Suffix_Structures.H.
References nodes_.
Referenced by TEST(), and Moved_From_Case< Naive_Suffix_Tree >::use().
|
default |
Copy the text and the nodes of another tree.
| [in] | other | tree to copy. |
| std::bad_alloc | if the text or the nodes cannot be allocated. |
|
inlinenoexcept |
Exchange this tree with another, which is left with the previous text and nodes of this one.
| [in,out] | other | tree to move from. |
Definition at line 434 of file Suffix_Structures.H.
References swap().
|
inlineprivate |
Definition at line 309 of file Suffix_Structures.H.
References nodes_.
Referenced by insert_suffix().
|
inlinenoexcept |
Exchange in O(1) the text and the nodes of this tree and another.
| [in,out] | other | tree to exchange with. |
Definition at line 443 of file Suffix_Structures.H.
References nodes_, original_size_, and text_.
Referenced by Naive_Suffix_Tree(), and operator=().
|
inlinenoexcept |
Return the text size (without terminal marker).
original_size_ member. | None. |
Definition at line 594 of file Suffix_Structures.H.
References original_size_.
Referenced by Moved_From_Case< Naive_Suffix_Tree >::is_empty(), TEST(), and Moved_From_Case< Naive_Suffix_Tree >::use().
Definition at line 269 of file Suffix_Structures.H.
Referenced by build(), collect_leaf_suffixes(), contains(), create_node(), find_all(), find_child_by_first_char(), insert_suffix(), node_count(), nodes(), replace_child(), and swap().
|
staticconstexpr |
Definition at line 251 of file Suffix_Structures.H.
Referenced by collect_leaf_suffixes(), contains(), find_all(), find_child_by_first_char(), and insert_suffix().
|
private |
Definition at line 268 of file Suffix_Structures.H.
Referenced by build(), collect_leaf_suffixes(), find_all(), swap(), and text_size().
|
private |
Definition at line 267 of file Suffix_Structures.H.
Referenced by build(), contains(), find_all(), find_child_by_first_char(), insert_suffix(), and swap().