37# include <gtest/gtest.h>
106 for (
int i = 0; i < 256; ++i)
131 EXPECT_EQ(
sam.longest_common_substring(
"abcabxabcd"),
"abxa");
137 const std::string
text =
"mississippi";
143 for (
size_t i = 1; i < sa.size(); ++i)
146 text.size() - sa[i - 1]);
148 text.size() - sa[i]);
211 for (
int i = 0; i < 5000; ++i)
212 text.push_back(
'a' + (i * 7 + 3) % 26);
214 const auto start = std::chrono::steady_clock::now();
216 const auto end = std::chrono::steady_clock::now();
217 const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
221 if (std::getenv(
"ENABLE_PERF_TESTS"))
222 EXPECT_LT(elapsed, 500) <<
"Performance regression: suffix_array(5000) took " << elapsed <<
"ms";
223 else if (elapsed >= 500)
224 std::cout <<
"[ PERF ] Warning: suffix_array(5000) took " << elapsed <<
"ms\n";
228 for (
size_t i = 1; i < sa.size(); ++i)
231 text.size() - sa[i - 1]);
233 text.size() - sa[i]);
241 for (
int i = 0; i < 256; ++i)
242 text.push_back(
static_cast<char>(i));
248 for (
int i = 0; i < 256; ++i)
250 std::string
pat(1,
static_cast<char>(i));
266 const auto & states =
sam.states();
267 for (
size_t i = 1; i < states.size(); ++i)
268 EXPECT_GE(states[i].link, 0) <<
"state " << i <<
" has link == -1";
272 EXPECT_EQ(
sam.longest_common_substring(
"aacca"),
"aacca");
Suffix structures: suffix array/LCP, suffix tree, suffix automaton.
Simple dynamic array with automatic resizing and functional operations.
constexpr bool is_empty() const noexcept
Checks if the container is empty.
T & append(const T &data)
Append a copy of data
Naive compressed suffix tree (didactic implementation).
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).
bool contains(const std::string_view pattern) const
Return true if the pattern appears in the text.
const Array< Node > & nodes() const noexcept
Return the internal node array for inspection/debugging.
Suffix automaton (SAM) over byte alphabet.
void build(const std::string_view text)
Build the SAM from an entire string.
void expect_array_eq(const Array< size_t > &a, std::initializer_list< size_t > expected)
Assert that an Aleph::Array contains exactly the elements in an initializer_list.
Main namespace for Aleph-w library functions.
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.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
std::string longest_common_substring_sam(const std::string_view a, const std::string_view b)
Convenience function: LCS via suffix automaton.
Array< size_t > suffix_array(const std::string_view text)
Build suffix array with doubling algorithm.
Shared helper functions for Aleph-w unit tests.