48#ifndef SUFFIX_STRUCTURES_H
49#define SUFFIX_STRUCTURES_H
63namespace suffix_structures_detail {
72 for (
size_t i = 0; i <= n; ++i)
100 const size_t n = text.
size();
109 for (
size_t i = 0; i < n; ++i)
112 rank[i] =
static_cast<unsigned char>(text[i]);
119 auto key2 = [&](
const size_t idx) ->
int
121 return (idx +
k < n) ? rank[idx +
k] : -1;
124 auto key1 = [&](
const size_t idx) ->
int
132 auto less_idx = [&](
const size_t a,
const size_t b)
134 if (rank[a] != rank[b])
135 return rank[a] < rank[b];
136 const int ra = (a +
k < n) ? rank[a +
k] : -1;
137 const int rb = (b +
k < n) ? rank[b +
k] : -1;
142 for (
size_t i = 1; i < n; ++i)
143 tmp_rank[sa[i]] = tmp_rank[sa[i - 1]] + (less_idx(sa[i - 1], sa[i]) ? 1 : 0);
145 for (
size_t i = 0; i < n; ++i)
146 rank[i] = tmp_rank[i];
148 max_rank = rank[sa[n - 1]];
150 if (max_rank ==
static_cast<int>(n - 1))
185 const size_t n = text.
size();
194 for (
size_t i = 0; i < n; ++i)
198 for (
size_t i = 0; i < n; ++i)
201 <<
"lcp_array_kasai(): suffix array contains invalid index";
203 <<
"lcp_array_kasai(): suffix array contains duplicate index";
208 for (
size_t i = 0; i < n; ++i)
210 const size_t r = rank[i];
217 const size_t j = sa[
r + 1];
218 while (i +
k < n
and j +
k < n
and text[i +
k] == text[j +
k])
251 static constexpr size_t npos = std::numeric_limits<size_t>::max();
273 std::array<bool, 256> used;
275 for (
const unsigned char c : s)
278 for (
size_t i = 0; i < 256; ++i)
280 return static_cast<char>(i);
282 ah_domain_error() <<
"Naive_Suffix_Tree::choose_terminal(): all 256 byte values "
283 <<
"are present in the input; no terminal marker available";
288 size_t create_node(
const size_t start,
const size_t end,
const size_t parent,
const size_t suffix_index)
302 for (
size_t i = 0; i < children.
size(); ++i)
303 if (
const size_t child = children[i];
text_[
nodes_[child].start] == c)
309 void replace_child(
const size_t parent,
const size_t old_child,
const size_t new_child)
322 size_t pos = suffix_start;
324 while (pos <
text_.size())
330 nodes_[current].children.append(leaf);
334 const size_t edge_start =
nodes_[child].start;
335 const size_t edge_end =
nodes_[child].end;
338 while (pos +
k <
text_.size()
and edge_start +
k < edge_end
342 if (edge_start +
k == edge_end)
353 nodes_[child].start = edge_start +
k;
445 text_.swap(other.text_);
447 nodes_.swap(other.nodes_);
457 void build(
const std::string_view text)
460 std::string new_text(text.begin(), text.end());
461 new_text.push_back(terminal);
464 text_ = std::move(new_text);
469 for (
size_t i = 0; i <
text_.size(); ++i)
481 [[nodiscard]]
bool contains(
const std::string_view pattern)
const
489 while (pos < pattern.size())
495 const size_t edge_start =
nodes_[child].start;
496 const size_t edge_end =
nodes_[child].end;
499 while (pos +
k < pattern.size()
and edge_start +
k < edge_end
501 and pattern[pos +
k] ==
text_[edge_start +
k])
504 if (pos +
k == pattern.size())
507 if (edge_start +
k == edge_end)
540 while (pos < pattern.size())
546 const size_t edge_start =
nodes_[child].start;
547 const size_t edge_end =
nodes_[child].end;
550 while (pos +
k < pattern.size()
and edge_start +
k < edge_end
552 and pattern[pos +
k] ==
text_[edge_start +
k])
555 if (pos +
k == pattern.size())
563 if (edge_start +
k == edge_end)
652 states_[
static_cast<size_t>(p)].terminal =
true;
653 p =
states_[
static_cast<size_t>(p)].link;
717 std::swap(
last_, other.last_);
744 const auto c =
static_cast<unsigned char>(ch);
746 const int cur =
static_cast<int>(
states_.size());
749 states_[
static_cast<size_t>(cur)].first_pos =
states_[
static_cast<size_t>(cur)].len - 1;
752 while (p != -1
and states_[
static_cast<size_t>(p)].next[c] == -1)
755 p =
states_[
static_cast<size_t>(p)].link;
759 states_[
static_cast<size_t>(cur)].link = 0;
762 if (
const int q =
states_[
static_cast<size_t>(p)].next[c];
763 states_[
static_cast<size_t>(p)].len + 1 ==
states_[
static_cast<size_t>(q)].len)
764 states_[
static_cast<size_t>(cur)].link = q;
767 const int clone =
static_cast<int>(
states_.size());
770 const State clone_state =
states_[
static_cast<size_t>(q)];
772 states_[
static_cast<size_t>(clone)].len =
states_[
static_cast<size_t>(p)].
len + 1;
773 states_[
static_cast<size_t>(clone)].terminal =
false;
775 if (
states_[
static_cast<size_t>(clone)].link == -1)
776 states_[
static_cast<size_t>(clone)].link = 0;
778 while (p != -1
and states_[
static_cast<size_t>(p)].next[c] == q)
781 p =
states_[
static_cast<size_t>(p)].link;
784 states_[
static_cast<size_t>(q)].link = clone;
785 states_[
static_cast<size_t>(cur)].link = clone;
798 void build(
const std::string_view text)
801 for (
const char c : text)
814 [[nodiscard]]
bool contains(
const std::string_view pattern)
const
817 for (
const unsigned char c : pattern)
819 state =
states_[
static_cast<size_t>(state)].
next[c];
836 for (
size_t i = 1; i <
states_.size(); ++i)
840 total += s.
len - parent.
len;
862 for (
size_t i = 0; i < other.size(); ++i)
864 const auto c =
static_cast<unsigned char>(other[i]);
866 while (state != 0
and states_[
static_cast<size_t>(state)].next[c] == -1)
868 state =
states_[
static_cast<size_t>(state)].link;
869 len =
states_[
static_cast<size_t>(state)].len;
872 if (
states_[
static_cast<size_t>(state)].next[c] != -1)
874 state =
states_[
static_cast<size_t>(state)].
next[c];
893 return std::string(other.substr(best_pos + 1 - best_len, best_len));
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error()
Throws std::domain_error unconditionally.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
size_t size_t int32_t * out
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
constexpr bool is_empty() const noexcept
Checks if the container is empty.
static Array create(const size_t n)
Create an array with n logical elements.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
Naive compressed suffix tree (didactic implementation).
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.
Array< size_t > find_all(const std::string_view pattern) const
Return all occurrences of a pattern.
void replace_child(const size_t parent, const size_t old_child, const size_t new_child)
Naive_Suffix_Tree & operator=(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,...
Naive_Suffix_Tree(const std::string_view text="")
Construct a naive suffix tree.
void swap(Naive_Suffix_Tree &other) noexcept
Exchange in O(1) the text and the nodes of this tree and another.
static constexpr size_t npos
size_t node_count() const noexcept
Return the number of nodes in the tree.
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 build(const std::string_view text)
Build or rebuild the suffix tree for text.
size_t text_size() const noexcept
Return the text size (without terminal marker).
void insert_suffix(const size_t suffix_start)
void collect_leaf_suffixes(const size_t node, Array< size_t > &out) const
bool contains(const std::string_view pattern) const
Return true if the pattern appears in the text.
static char choose_terminal(const std::string_view s)
Naive_Suffix_Tree(const Naive_Suffix_Tree &other)=default
Copy the text and the nodes of another tree.
const Array< Node > & nodes() const noexcept
Return the internal node array for inspection/debugging.
Suffix automaton (SAM) over byte alphabet.
Suffix_Automaton(Suffix_Automaton &&other)
Take the states of another automaton, which is left empty, with the root state only,...
std::string longest_common_substring(const std::string_view other) const
Find the longest common substring between the built SAM text and other.
void build(const std::string_view text)
Build the SAM from an entire string.
bool contains(const std::string_view pattern) const
Return true if pattern is a substring of the built text.
Suffix_Automaton & operator=(Suffix_Automaton &&other) noexcept
Exchange this automaton with another, which is left with the previous states of this one.
const Array< State > & states() const noexcept
Access the internal states array for testing/inspection.
Suffix_Automaton & operator=(const Suffix_Automaton &other)=default
Copy the states of another automaton.
void swap(Suffix_Automaton &other) noexcept
Exchange in O(1) the states of this automaton and another.
void mark_terminals()
Traverse suffix links from last and mark all states as terminal.
size_t distinct_substring_count() const
Count the number of distinct substrings in the built text.
Suffix_Automaton(const Suffix_Automaton &other)=default
Copy the states of another automaton.
Suffix_Automaton()
Construct an empty suffix automaton (SAM) with only the root state.
size_t state_count() const noexcept
Return the number of states in the SAM.
void clear()
Reset the automaton to its initial empty state (root only).
void extend(const char ch)
Extend the SAM with a single character.
Array< size_t > all_positions(const size_t n)
Generate an array of all positions from 0 to n.
void sort_matches(Array< size_t > &a)
Sort matches in place using introsort.
Main namespace for Aleph-w library functions.
and std::convertible_to< std::invoke_result_t< const KeyFn &, size_t >, int > void counting_sort_indices(Array< size_t > &sa, Array< size_t > &tmp, const size_t n, const int min_key, const int max_key, const KeyFn &key_of)
Stable counting sort on an index array by integer keys.
Array< size_t > lcp_array_kasai(const std::string_view text, const Array< size_t > &sa)
Compute LCP array from text and suffix array using Kasai.
std::string longest_common_substring_sam(const std::string_view a, const std::string_view b)
Convenience function: LCS via suffix automaton.
and
Check uniqueness with explicit hash + equality functors.
std::vector< std::string > & split(const std::string &s, const char delim, std::vector< std::string > &elems)
Split a std::string by a single delimiter character.
void next()
Advance all underlying iterators (bounds-checked).
void introsort(T *a, const long l, const long r, const Compare &cmp=Compare())
Sort an array using introsort (introspective sort).
Array< size_t > suffix_array(const std::string_view text)
Build suffix array with doubling algorithm.
size_t parent
Parent node index.
size_t start
Start index in text.
size_t end
End index in text.
Array< size_t > children
Indices of children nodes.
size_t suffix_index
Index of suffix starting at leaf.
bool terminal
true if state is terminal
std::array< int, 256 > next
Transitions for each byte.
size_t first_pos
First occurrence end position.
size_t len
Max length of substring in this state.
Dynamic array container with automatic resizing.
Comprehensive sorting algorithms and search utilities for Aleph-w.