Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Suffix_Automaton Class Reference

Suffix automaton (SAM) over byte alphabet. More...

#include <Suffix_Structures.H>

Collaboration diagram for Aleph::Suffix_Automaton:
[legend]

Classes

struct  State
 One automaton state. More...
 

Public Member Functions

 Suffix_Automaton ()
 Construct an empty suffix automaton (SAM) with only the root state.
 
 Suffix_Automaton (const Suffix_Automaton &other)=default
 Copy the states of another automaton.
 
 Suffix_Automaton (Suffix_Automaton &&other)
 Take the states of another automaton, which is left empty, with the root state only, as built by default.
 
Suffix_Automaton & operator= (const Suffix_Automaton &other)=default
 Copy the states of another automaton.
 
Suffix_Automaton & operator= (Suffix_Automaton &&other) noexcept
 Exchange this automaton with another, which is left with the previous states of this one.
 
void swap (Suffix_Automaton &other) noexcept
 Exchange in O(1) the states of this automaton and another.
 
void clear ()
 Reset the automaton to its initial empty state (root only).
 
void extend (const char ch)
 Extend the SAM with a single character.
 
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.
 
size_t distinct_substring_count () const
 Count the number of distinct substrings in the built text.
 
std::string longest_common_substring (const std::string_view other) const
 Find the longest common substring between the built SAM text and other.
 
size_t state_count () const noexcept
 Return the number of states in the SAM.
 
const Array< State > & states () const noexcept
 Access the internal states array for testing/inspection.
 

Private Member Functions

void mark_terminals ()
 Traverse suffix links from last and mark all states as terminal.
 

Private Attributes

Array< State > states_
 
int last_ = 0
 

Detailed Description

Suffix automaton (SAM) over byte alphabet.

Supports substring checks, distinct substring counting, and longest common substring against another string.

Note
A growing 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.
Examples
suffix_automaton_example.cc.

Definition at line 622 of file Suffix_Structures.H.

Constructor & Destructor Documentation

◆ Suffix_Automaton() [1/3]

Aleph::Suffix_Automaton::Suffix_Automaton ( )
inline

Construct an empty suffix automaton (SAM) with only the root state.

Exceptions
None.
Complexity
O(1).

Definition at line 663 of file Suffix_Structures.H.

References clear().

◆ Suffix_Automaton() [2/3]

Aleph::Suffix_Automaton::Suffix_Automaton ( const Suffix_Automaton &  other)
default

Copy the states of another automaton.

Parameters
[in]otherautomaton to copy.
Exceptions
std::bad_allocif the states cannot be allocated.

◆ Suffix_Automaton() [3/3]

Aleph::Suffix_Automaton::Suffix_Automaton ( Suffix_Automaton &&  other)
inline

Take the states of another automaton, which is left empty, with the root state only, as built by default.

The implicit move took the states, the root among them, but copied the index of the last state: the moved-from automaton then read states that it no longer had.

Parameters
[in,out]otherautomaton to move from.
Exceptions
std::bad_allocif the root left to other cannot be allocated; other is then unchanged.
Note
Complexity: O(1), but it allocates the empty automaton left to other.

Definition at line 687 of file Suffix_Structures.H.

References swap().

Member Function Documentation

◆ build()

void Aleph::Suffix_Automaton::build ( const std::string_view  text)
inline

Build the SAM from an entire string.

Parameters
[in]textThe input string to process.
Exceptions
None.
Complexity
O(n) where n is text.size().
Examples
suffix_automaton_example.cc.

Definition at line 798 of file Suffix_Structures.H.

References clear(), extend(), and mark_terminals().

Referenced by Aleph::longest_common_substring_sam(), main(), Moved_From_Case< Suffix_Automaton >::make(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Suffix_Automaton >::use().

◆ clear()

void Aleph::Suffix_Automaton::clear ( )
inline

Reset the automaton to its initial empty state (root only).

Exceptions
None.
Complexity
O(1).

Definition at line 725 of file Suffix_Structures.H.

References last_, and states_.

Referenced by Suffix_Automaton(), build(), and Moved_From_Case< Suffix_Automaton >::use().

◆ contains()

bool Aleph::Suffix_Automaton::contains ( const std::string_view  pattern) const
inline

Return true if pattern is a substring of the built text.

Parameters
[in]patternThe substring pattern to search for.
Returns
True if the SAM contains pattern, false otherwise.
Exceptions
None.
Complexity
O(m) where m is pattern.size().
Examples
suffix_automaton_example.cc.

Definition at line 814 of file Suffix_Structures.H.

References Aleph::next(), and states_.

Referenced by main(), TEST(), TEST(), TEST(), TEST(), TEST(), and Moved_From_Case< Suffix_Automaton >::use().

◆ distinct_substring_count()

size_t Aleph::Suffix_Automaton::distinct_substring_count ( ) const
inline

Count the number of distinct substrings in the built text.

Returns
The total count of unique substrings.
Exceptions
None.
Complexity
O(V) where V is the number of states.
Examples
suffix_automaton_example.cc.

Definition at line 833 of file Suffix_Structures.H.

References Aleph::Suffix_Automaton::State::len, Aleph::Suffix_Automaton::State::link, and states_.

Referenced by Moved_From_Case< Suffix_Automaton >::is_empty(), main(), TEST(), TEST(), TEST(), and Moved_From_Case< Suffix_Automaton >::use().

◆ extend()

void Aleph::Suffix_Automaton::extend ( const char  ch)
inline

Extend the SAM with a single character.

Parameters
[in]chThe character to append to the current text representation.
Exceptions
None.
Complexity
Amortized O(1).

Definition at line 742 of file Suffix_Structures.H.

References Aleph::and, last_, Aleph::Suffix_Automaton::State::len, Aleph::next(), and states_.

Referenced by build(), and Moved_From_Case< Suffix_Automaton >::use().

◆ longest_common_substring()

std::string Aleph::Suffix_Automaton::longest_common_substring ( const std::string_view  other) const
inline

Find the longest common substring between the built SAM text and other.

Parameters
[in]otherString to compare against the built SAM text.
Returns
The longest common substring. Returns an empty string if no common substring exists.
Exceptions
None.
Complexity
O(m) where m is other.size().
Examples
suffix_automaton_example.cc.

Definition at line 854 of file Suffix_Structures.H.

References Aleph::and, Aleph::next(), and states_.

Referenced by Aleph::longest_common_substring_sam(), main(), TEST(), TEST(), and Moved_From_Case< Suffix_Automaton >::use().

◆ mark_terminals()

void Aleph::Suffix_Automaton::mark_terminals ( )
inlineprivate

Traverse suffix links from last and mark all states as terminal.

Exceptions
None.

Definition at line 647 of file Suffix_Structures.H.

References last_, and states_.

Referenced by build().

◆ operator=() [1/2]

Suffix_Automaton & Aleph::Suffix_Automaton::operator= ( const Suffix_Automaton &  other)
default

Copy the states of another automaton.

Parameters
[in]otherautomaton to copy.
Returns
this automaton.
Exceptions
std::bad_allocif the states cannot be allocated.

◆ operator=() [2/2]

Suffix_Automaton & Aleph::Suffix_Automaton::operator= ( Suffix_Automaton &&  other)
inlinenoexcept

Exchange this automaton with another, which is left with the previous states of this one.

Parameters
[in,out]otherautomaton to move from.
Returns
this automaton.
Note
Complexity: O(1).

Definition at line 705 of file Suffix_Structures.H.

References swap().

◆ state_count()

size_t Aleph::Suffix_Automaton::state_count ( ) const
inlinenoexcept

Return the number of states in the SAM.

Returns
The size of the internal states array.
Exceptions
None.
Complexity
O(1).
Examples
suffix_automaton_example.cc.

Definition at line 902 of file Suffix_Structures.H.

References states_.

Referenced by Moved_From_Case< Suffix_Automaton >::is_empty(), main(), TEST(), and Moved_From_Case< Suffix_Automaton >::use().

◆ states()

const Array< State > & Aleph::Suffix_Automaton::states ( ) const
inlinenoexcept

Access the internal states array for testing/inspection.

Returns
A constant reference to the underlying Array of states.
Exceptions
None.
Complexity
O(1).

Definition at line 913 of file Suffix_Structures.H.

References states_.

Referenced by TEST(), and Moved_From_Case< Suffix_Automaton >::use().

◆ swap()

void Aleph::Suffix_Automaton::swap ( Suffix_Automaton &  other)
inlinenoexcept

Exchange in O(1) the states of this automaton and another.

Parameters
[in,out]otherautomaton to exchange with.

Definition at line 714 of file Suffix_Structures.H.

References last_, and states_.

Referenced by Suffix_Automaton(), and operator=().

Member Data Documentation

◆ last_

int Aleph::Suffix_Automaton::last_ = 0
private

Definition at line 642 of file Suffix_Structures.H.

Referenced by clear(), extend(), mark_terminals(), and swap().

◆ states_

Array<State> Aleph::Suffix_Automaton::states_
private

The documentation for this class was generated from the following file: