53# ifndef STRING_SEARCH_H
54# define STRING_SEARCH_H
59# include <string_view>
66 namespace string_search_detail
76 for (
size_t i = 0; i <= text_size; ++i)
89 const std::string_view b,
92 std::array<bool, 256>
used{};
94 for (
const unsigned char c : a)
97 for (
const unsigned char c : b)
100 for (
size_t i = 0; i < 256; ++i)
118 const std::string_view pattern,
121 for (
size_t i = 0; i < pattern.size(); ++i)
122 if (
text[pos + i] != pattern[i])
151 for (
size_t i = 1; i < pattern.size(); ++i)
153 while (j > 0
and pattern[i] != pattern[j])
156 if (pattern[i] == pattern[j])
179 const std::string_view pattern)
185 if (
text.size() < pattern.size())
191 for (
size_t i = 0; i <
text.size(); ++i)
193 while (j > 0
and text[i] != pattern[j])
196 if (
text[i] == pattern[j])
199 if (j == pattern.size())
234 for (
size_t i = 1; i < s.size(); ++i)
237 z[i] = std::min(
r - i + 1, z[i -
l]);
241 while (i + z[i] < s.
size()
and s[z[i]] == s[i + z[i]])
270 const std::string_view pattern)
275 if (
text.size() < pattern.size())
293 if (z[i] >= pattern.
size())
314 const std::string_view pattern)
321 const size_t m = pattern.
size();
327 for (
size_t i = 0; i < 256; ++i)
330 for (
size_t i = 0; i + 1 <
m; ++i)
331 shift[
static_cast<unsigned char>(pattern[i])] =
m - 1 - i;
337 while (j > 0
and pattern[j - 1] ==
text[i + j - 1])
343 const auto c =
static_cast<unsigned char>(
text[i +
m - 1]);
344 const size_t step = shift[c] == 0 ? 1 : shift[c];
372 const std::string_view pattern,
380 const size_t m = pattern.
size();
386 for (
size_t i = 1; i <
m; ++i)
392 for (
size_t i = 0; i <
m; ++i)
395 +
static_cast<unsigned char>(pattern[i]) + 1ull;
397 +
static_cast<unsigned char>(
text[i]) + 1ull;
404 for (
size_t i =
m; i < n; ++i)
Simple dynamic array with automatic resizing and functional operations.
static Array create(size_t n)
Create an array with n logical elements.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
Key * append(const Key &key)
Alias for insert() (copy version).
constexpr size_t size() const noexcept
Returns the number of entries in the table.
const long double offset[]
Offset values indexed by symbol string length (bounded by MAX_OFFSET_INDEX)
bool equals_at(const std::string_view text, const std::string_view pattern, const size_t pos)
Check if pattern matches text at given position.
bool find_unused_delimiter(const std::string_view a, const std::string_view b, char &delimiter)
Find a character not present in either string.
Array< size_t > all_match_positions(const size_t text_size)
Generate an array of all positions from 0 to text_size.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Array< size_t > z_search(const std::string_view text, const std::string_view pattern)
Find all occurrences of a pattern using the Z-algorithm.
Array< size_t > rabin_karp_search(const std::string_view text, const std::string_view pattern, const uint64_t base=911382323ull)
Find all occurrences using Rabin-Karp with rolling hash.
Array< size_t > z_algorithm(const std::string_view s)
Compute Z-array for a string.
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.
Array< size_t > kmp_search(const std::string_view text, const std::string_view pattern)
Find all occurrences of a pattern using KMP.
Array< size_t > kmp_prefix_function(const std::string_view pattern)
Prefix-function used by KMP.
Array< size_t > boyer_moore_horspool_search(const std::string_view text, const std::string_view pattern)
Find all occurrences using Boyer-Moore-Horspool.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dynamic array container with automatic resizing.