REAL
Regular Expression Algorithmic Library — constexpr C++20 regex
Loading...
Searching...
No Matches
real::regex_set Class Reference

Multi-pattern set: which patterns match the subject at least once. More...

#include <regex_set.hpp>

Classes

struct  fused_state
 The fused scan's state: the DFA, its subset maps, and what the deferred build counts. More...
 

Public Member Functions

 regex_set (std::span< const std::string_view > patterns, flags compile_flags=flags::none)
 Compiles every pattern in patterns (construction order = bitset order).
 
 regex_set (const std::string_view *patterns, std::size_t n, flags compile_flags=flags::none)
 Convenience: compile from a contiguous array of string views.
 
 regex_set (std::initializer_list< std::string_view > patterns, flags compile_flags=flags::none)
 Brace-init: regex_set{"a", "b", R"(\\d+)"} .
 
 regex_set (std::span< const std::string > patterns, flags compile_flags=flags::none)
 Compile from owning strings (e.g. std::vector<std::string>).
 
std::size_t size () const noexcept
 Number of patterns in the set (bitset length).
 
bool empty () const noexcept
 True if the set has no patterns.
 
flags compile_flags () const noexcept
 Compilation flags shared by every member.
 
bool uses_fused () const noexcept
 True when a fused single-pass DFA is active: built at construction, or since, once the set walked fused_deferred_bytes.
 
std::size_t eligible_count () const noexcept
 How many members the fused DFA holds, or 0 when uses_fused is false.
 
bool is_match (std::string_view text, std::size_t pos=0, std::size_t endpos=npos) const
 True if any pattern matches the subject at least once.
 
std::vector< bool > matches (std::string_view text, std::size_t pos=0, std::size_t endpos=npos) const
 Which patterns match at least once (construction-order bitset).
 
std::vector< std::size_t > which (std::string_view text, std::size_t pos=0, std::size_t endpos=npos) const
 Indices of patterns that match (construction order, ascending).
 
const regex & operator[] (std::size_t i) const
 Access the compiled pattern at construction index i.
 

Static Public Attributes

static constexpr std::size_t fused_min_eligible {56}
 Eligible-member count at which the set builds its fused single-pass DFA at construction.
 
static constexpr std::size_t fused_deferred_min_eligible {24}
 Eligible-member count from which a set builds its fused DFA once it has walked fused_deferred_bytes, rather than at construction.
 
static constexpr std::size_t fused_deferred_bytes {std::size_t {1} << 20U}
 Whole-subject bytes a set of fused_deferred_min_eligible to fused_min_eligible members walks before it builds its fused DFA.
 

Private Member Functions

void build_from_views (std::span< const std::string_view > patterns)
 Compiles every pattern, then splits them into the DFA-eligible subset (fused when it reaches the threshold) and the ineligible remainder each query searches individually.
 
void partition (fused_state &st) const
 Splits the members into the DFA-eligible subset and the rest (a single-pattern munch DFA decides eligibility).
 
void build_fused (fused_state &st) const
 Builds the fused which-matched DFA over the eligible members.
 
void build_deferred (fused_state &st) const
 The deferred build: partitions if construction did not, then builds the fused DFA when enough members are eligible. A fused DFA past its bounds leaves the set on walks for good, as does too small an eligible subset: matches must not throw what construction did not.
 
const fused_state * ready_fused () const noexcept
 The fused state when its DFA is built, without counting or building anything.
 
const fused_state * fused_for (std::string_view text) const
 The fused state for a whole-subject matches over text: counts text toward the deferred build, and runs the build once the count reaches fused_deferred_bytes.
 
void arm_byte_filter ()
 Classifies members into a SPARSE partition one scan can serve, and arms the filter.
 
bool is_sparse (std::size_t i) const noexcept
 One bit, no search: is member i served by the byte filter?
 
bool filter_excludes (std::string_view text, std::size_t pos, std::size_t endpos) const
 True when the filter PROVES no member it serves can match in the region.
 

Private Attributes

std::vector< regex > members_
 Every compiled pattern, in construction order.
 
flags flags_ {flags::none}
 Flags shared by every member.
 
std::shared_ptr< fused_state > fused_state_
 Null when the set can never fuse.
 
std::vector< std::uint64_t > sparse_bits_
 Bit i set when the byte filter serves member i.
 
std::array< std::uint8_t, 8 > filter_bytes_ {}
 The served members' first-byte union, in the mask load's layout.
 
std::uint8_t filter_count_ {0}
 Valid entries in filter_bytes_; 0 = filter off.
 

Static Private Attributes

static constexpr std::uint8_t filter_union_max {8}
 Widest first-byte union the byte filter carries: one 16-byte masked block's capacity.
 

Detailed Description

Multi-pattern set: which patterns match the subject at least once.

Construction compiles every pattern (same flags as regex). If any pattern is invalid or unsupported, the constructor throws regex_error — there is no silent skip. Capture groups are not reported by the set; re-run the individual regex if groups are needed.

Bitset order is the construction order: index 0 is the first pattern, etc.

Constructor & Destructor Documentation

◆ regex_set() [1/4]

real::regex_set::regex_set ( std::span< const std::string_view >  patterns,
flags  compile_flags = flags::none 
)
inlineexplicit

Compiles every pattern in patterns (construction order = bitset order).

Parameters
[in]patternsPattern texts; an empty set is allowed.
[in]compile_flagsFlags applied to every pattern (same as regex).
Exceptions
regex_errorif any pattern fails to compile.

◆ regex_set() [2/4]

real::regex_set::regex_set ( const std::string_view *  patterns,
std::size_t  n,
flags  compile_flags = flags::none 
)
inline

Convenience: compile from a contiguous array of string views.

Parameters
[in]patternsPointer to the first pattern.
[in]nPattern count.
[in]compile_flagsFlags shared by every member.

◆ regex_set() [3/4]

real::regex_set::regex_set ( std::initializer_list< std::string_view >  patterns,
flags  compile_flags = flags::none 
)
inline

Brace-init: regex_set{"a", "b", R"(\\d+)"} .

Parameters
[in]patternsThe patterns to compile.
[in]compile_flagsFlags shared by every member.

◆ regex_set() [4/4]

real::regex_set::regex_set ( std::span< const std::string >  patterns,
flags  compile_flags = flags::none 
)
inlineexplicit

Compile from owning strings (e.g. std::vector<std::string>).

Parameters
[in]patternsThe patterns to compile; only borrowed for the duration of the call.
[in]compile_flagsFlags shared by every member.

Member Function Documentation

◆ arm_byte_filter()

void real::regex_set::arm_byte_filter ( )
inlineprivate

Classifies members into a SPARSE partition one scan can serve, and arms the filter.

Zero work per call is the constraint: classifying per query charges every call, including an any-match walk whose first member hits at once. Everything happens here, once; the walks test one bit.

The classification is the engine's own: first_bytes_valid plus single_first or a small_set_size of 2..8. The hint builder sets small_set only when all 256 bytes of first_bytes count 2..8, so the array is the COMPLETE leading set, which is what makes skipping a member sound. A nullable pattern has first_bytes_valid false and stays wide.

The union is capped in construction order (narrowest-first reorders which members are served, for no gain), so several sparse members cannot form a dense union. A wide member never joins: one \w-leading pattern costs the others nothing.

◆ build_deferred()

void real::regex_set::build_deferred ( fused_state &  st) const
inlineprivate

The deferred build: partitions if construction did not, then builds the fused DFA when enough members are eligible. A fused DFA past its bounds leaves the set on walks for good, as does too small an eligible subset: matches must not throw what construction did not.

Parameters
[in,out]stThe state to complete and publish.

◆ build_from_views()

void real::regex_set::build_from_views ( std::span< const std::string_view >  patterns)
inlineprivate

Compiles every pattern, then splits them into the DFA-eligible subset (fused when it reaches the threshold) and the ineligible remainder each query searches individually.

Parameters
[in]patternsThe patterns to compile, in construction order.
Exceptions
regex_errorif any pattern fails to compile.

◆ build_fused()

void real::regex_set::build_fused ( fused_state &  st) const
inlineprivate

Builds the fused which-matched DFA over the eligible members.

Parameters
[in,out]stThe partitioned state.
Exceptions
dfa_errorwhen the fused automaton exceeds its state or work bound.

◆ compile_flags()

flags real::regex_set::compile_flags ( ) const
inlinenoexcept

Compilation flags shared by every member.

Returns
The flag set every member was compiled with.

◆ eligible_count()

std::size_t real::regex_set::eligible_count ( ) const
inlinenoexcept

How many members the fused DFA holds, or 0 when uses_fused is false.

Until the fused DFA is active every member walks individually, so this is 0 even for patterns a DFA would accept.

Returns
The fused subset size, or 0.

◆ empty()

bool real::regex_set::empty ( ) const
inlinenoexcept

True if the set has no patterns.

Returns
Whether the set is empty.

◆ filter_excludes()

bool real::regex_set::filter_excludes ( std::string_view  text,
std::size_t  pos,
std::size_t  endpos 
) const
inlineprivate

True when the filter PROVES no member it serves can match in the region.

Parameters
[in]textSubject.
[in]posStart offset, as regex::search takes it.
[in]endposRegion end, as regex::search takes it (truncates the view).
Returns
Whether every served member can be skipped.

◆ fused_for()

const fused_state * real::regex_set::fused_for ( std::string_view  text) const
inlineprivate

The fused state for a whole-subject matches over text: counts text toward the deferred build, and runs the build once the count reaches fused_deferred_bytes.

Parameters
[in]textThe subject about to be searched.
Returns
The state when its DFA is built, or null while the set walks.

◆ is_match()

bool real::regex_set::is_match ( std::string_view  text,
std::size_t  pos = 0,
std::size_t  endpos = npos 
) const
inline

True if any pattern matches the subject at least once.

Stops at the first matching pattern (any-match early exit). Region semantics match regex::search — endpos truncates the view; pos is the start offset (not a slice).

Parameters
[in]textSubject.
[in]posByte offset the search starts at.
[in]endposByte offset the region ends at; defaults to the end of text.
Returns
Whether at least one pattern matches.

◆ is_sparse()

bool real::regex_set::is_sparse ( std::size_t  i) const
inlineprivatenoexcept

One bit, no search: is member i served by the byte filter?

Parameters
[in]iConstruction index of the member.
Returns
Whether the filter's union covers every byte this member can start with.

◆ matches()

std::vector< bool > real::regex_set::matches ( std::string_view  text,
std::size_t  pos = 0,
std::size_t  endpos = npos 
) const
inline

Which patterns match at least once (construction-order bitset).

Index i is true iff pattern i matched. Order is always construction order, whether the set walks members individually or through a fused scan.

Parameters
[in]textSubject.
[in]posByte offset the search starts at.
[in]endposByte offset the region ends at; defaults to the end of text.
Returns
One bool per member, in construction order.

◆ operator[]()

const regex & real::regex_set::operator[] ( std::size_t  i) const
inline

Access the compiled pattern at construction index i.

Parameters
[in]iConstruction index.
Returns
A reference to the compiled member.
Exceptions
std::out_of_rangewhen i is past the last member.

◆ partition()

void real::regex_set::partition ( fused_state &  st) const
inlineprivate

Splits the members into the DFA-eligible subset and the rest (a single-pattern munch DFA decides eligibility).

Parameters
[in,out]stThe state receiving both lists.

◆ ready_fused()

const fused_state * real::regex_set::ready_fused ( ) const
inlineprivatenoexcept

The fused state when its DFA is built, without counting or building anything.

Returns
The state, or null while the set walks.

◆ size()

std::size_t real::regex_set::size ( ) const
inlinenoexcept

Number of patterns in the set (bitset length).

Returns
The member count.

◆ uses_fused()

bool real::regex_set::uses_fused ( ) const
inlinenoexcept

True when a fused single-pass DFA is active: built at construction, or since, once the set walked fused_deferred_bytes.

Returns
Whether the eligible members share one DFA rather than being searched individually.

◆ which()

std::vector< std::size_t > real::regex_set::which ( std::string_view  text,
std::size_t  pos = 0,
std::size_t  endpos = npos 
) const
inline

Indices of patterns that match (construction order, ascending).

Parameters
[in]textSubject.
[in]posByte offset the search starts at.
[in]endposByte offset the region ends at; defaults to the end of text.
Returns
The matching members' construction indices, ascending.

Member Data Documentation

◆ filter_union_max

constexpr std::uint8_t real::regex_set::filter_union_max {8}
staticconstexprprivate

Widest first-byte union the byte filter carries: one 16-byte masked block's capacity.

Private, unlike fused_min_eligible, as a scan width rather than a contract; its rationale names detail:: symbols a public reference page cannot link.

◆ fused_deferred_min_eligible

constexpr std::size_t real::regex_set::fused_deferred_min_eligible {24}
staticconstexpr

Eligible-member count from which a set builds its fused DFA once it has walked fused_deferred_bytes, rather than at construction.

From about this many members one fused scan costs less than the walks, but the build costs milliseconds, more than a set built for one short subject gets back. So the set walks until its whole-subject matches calls total fused_deferred_bytes, about the build's cost, which bounds the total at about twice the cheaper choice in hindsight. Below this count the walks win outright.

◆ fused_min_eligible

constexpr std::size_t real::regex_set::fused_min_eligible {56}
staticconstexpr

Eligible-member count at which the set builds its fused single-pass DFA at construction.

One fused automaton costs a build and a full scan; N walks cost N searches that can each stop early. From this count the fused scan pays on the first subject; between fused_deferred_min_eligible and here it pays only once enough text has been searched.


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