REAL
Regular Expression Algorithmic Library — constexpr C++20 regex
Loading...
Searching...
No Matches
real::detail::small_vec< T, InlineCapacity > Class Template Reference

Small-buffer-optimized vector for the dynamic hot paths. More...

#include <storage.hpp>

Collaboration diagram for real::detail::small_vec< T, InlineCapacity >:
[legend]

Classes

struct  inline_block
 Active member (inline buffer or heap pointer) per is_heap_ state. More...
 
union  Storage
 

Public Types

using value_type = T
 Element type.
 
using size_type_ = std::size_t
 Size type (for std-container API compat).
 

Public Member Functions

constexpr small_vec () noexcept
 Constructs an empty vector in the inline state.
 
constexpr ~small_vec ()
 Destroys elements and frees any heap block.
 
constexpr void push_back (const T &value)
 Appends value, growing to the heap if the inline buffer is full.
 
constexpr void assign (std::size_t count, const T &value)
 Resizes to count copies of value.
 
constexpr std::size_t size () const noexcept
 Returns the number of elements.
 
constexpr bool empty () const noexcept
 Returns true if empty.
 
constexpr T & operator[] (std::size_t i) noexcept
 Returns reference to the element at i.
 
constexpr const T & operator[] (std::size_t i) const noexcept
 Returns const reference to the element at i.
 
constexpr void clear () noexcept
 Removes all elements (capacity and heap state unchanged).
 
constexpr T & back () noexcept
 Returns reference to the last element. Precondition: the vector is non-empty.
 
constexpr const T & back () const noexcept
 Returns const reference to the last element. Precondition: the vector is non-empty.
 
constexpr void pop_back () noexcept
 Removes the last element. Precondition: the vector is non-empty.
 
constexpr void reserve (std::size_t new_capacity)
 Ensures capacity for at least new_capacity elements (heap-backed).
 
constexpr small_vec (small_vec &&other) noexcept
 Move constructor: steals other's heap block or moves inline elements.
 
constexpr small_vecoperator= (small_vec &&other) noexcept
 Move assignment.
 
constexpr small_vec (const small_vec &other)
 Copy constructor (needed for vector<match_result> in find_all).
 
constexpr small_vecoperator= (const small_vec &other)
 Copy assignment.
 

Private Member Functions

constexpr void refresh_data () noexcept
 Refreshes data_ to the active storage base (run time only).
 
constexpr T * inline_data () noexcept
 Returns pointer to the inline buffer.
 
constexpr const T * inline_data () const noexcept
 Returns const pointer to the inline buffer.
 
template<bool Move>
constexpr void transfer_range (const T *src, std::size_t count, T *dest)
 Copies or moves count elements from src to dest.
 
template<bool Move>
constexpr void transfer_inline_from (const small_vec &other)
 Transfers other's inline elements into this vector's inline buffer.
 
constexpr void cleanup () noexcept
 Frees the heap block, if any (run-time only). T is trivially destructible (see the class static_assert), so no element destructors run — and inline storage needs no cleanup at all.
 
void extend_capacity ()
 Doubles the capacity (saturating), spilling to the heap as needed.
 

Private Attributes

std::size_t size_ {}
 Number of elements in use.
 
std::size_t capacity_ {InlineCapacity}
 Current capacity.
 
bool is_heap_ {}
 True once spilled to the heap.
 
union real::detail::small_vec::Storage storage_
 
T * data_ {}
 

Detailed Description

template<typename T, std::size_t InlineCapacity>
class real::detail::small_vec< T, InlineCapacity >

Small-buffer-optimized vector for the dynamic hot paths.

Keeps up to InlineCapacity elements inline (no heap), spilling to the heap beyond that — so the common small-group match avoids allocation entirely. Used for capture slots and working state in the dynamic mode.

Note
T must be trivially destructible (enforced by a static_assert): small_vec runs no element destructors — inline elements in particular are never individually destroyed — which suits its POD-like VM use and keeps the hot path allocation- and bookkeeping-free.
Warning
Run-time invariant — the inline buffer is left UNINITIALIZED (value-initialized only under std::is_constant_evaluated(), in the Storage union constructor). Every element's lifetime is begun by std::construct_at (placement-new) before it is read, and reads stay within [0, size_). Any accessor added here must preserve that write-before-read order, or it reads indeterminate memory — a silent UB the former value-init used to mask. MemorySanitizer is the detector (the CI sanitize leg is ASan/UBSan, which does not catch it); run MSan on the devbox when changing how small_vec accesses its elements.
Template Parameters
TElement type.
InlineCapacityNumber of elements held inline before spilling.

Definition at line 219 of file storage.hpp.

Member Typedef Documentation

◆ size_type_

template<typename T , std::size_t InlineCapacity>
using real::detail::small_vec< T, InlineCapacity >::size_type_ = std::size_t

Size type (for std-container API compat).

Definition at line 398 of file storage.hpp.

◆ value_type

template<typename T , std::size_t InlineCapacity>
using real::detail::small_vec< T, InlineCapacity >::value_type = T

Element type.

Definition at line 397 of file storage.hpp.

Constructor & Destructor Documentation

◆ small_vec() [1/3]

template<typename T , std::size_t InlineCapacity>
constexpr real::detail::small_vec< T, InlineCapacity >::small_vec ( )
inlineconstexprnoexcept

Constructs an empty vector in the inline state.

Definition at line 403 of file storage.hpp.

◆ ~small_vec()

template<typename T , std::size_t InlineCapacity>
constexpr real::detail::small_vec< T, InlineCapacity >::~small_vec ( )
inlineconstexpr

Destroys elements and frees any heap block.

Definition at line 411 of file storage.hpp.

◆ small_vec() [2/3]

template<typename T , std::size_t InlineCapacity>
constexpr real::detail::small_vec< T, InlineCapacity >::small_vec ( small_vec< T, InlineCapacity > &&  other)
inlineconstexprnoexcept

Move constructor: steals other's heap block or moves inline elements.

Definition at line 600 of file storage.hpp.

◆ small_vec() [3/3]

template<typename T , std::size_t InlineCapacity>
constexpr real::detail::small_vec< T, InlineCapacity >::small_vec ( const small_vec< T, InlineCapacity > &  other)
inlineconstexpr

Copy constructor (needed for vector<match_result> in find_all).

Definition at line 650 of file storage.hpp.

Member Function Documentation

◆ assign()

template<typename T , std::size_t InlineCapacity>
constexpr void real::detail::small_vec< T, InlineCapacity >::assign ( std::size_t  count,
const T &  value 
)
inlineconstexpr

Resizes to count copies of value.

Parameters
[in]countNumber of elements.
[in]valueThe value to fill with.
Exceptions
std::bad_allocduring constant evaluation if growth is needed.

Definition at line 455 of file storage.hpp.

◆ back() [1/2]

template<typename T , std::size_t InlineCapacity>
constexpr const T & real::detail::small_vec< T, InlineCapacity >::back ( ) const
inlineconstexprnoexcept

Returns const reference to the last element. Precondition: the vector is non-empty.

Definition at line 551 of file storage.hpp.

◆ back() [2/2]

template<typename T , std::size_t InlineCapacity>
constexpr T & real::detail::small_vec< T, InlineCapacity >::back ( )
inlineconstexprnoexcept

Returns reference to the last element. Precondition: the vector is non-empty.

Definition at line 539 of file storage.hpp.

◆ cleanup()

template<typename T , std::size_t InlineCapacity>
constexpr void real::detail::small_vec< T, InlineCapacity >::cleanup ( )
inlineconstexprprivatenoexcept

Frees the heap block, if any (run-time only). T is trivially destructible (see the class static_assert), so no element destructors run — and inline storage needs no cleanup at all.

Definition at line 375 of file storage.hpp.

◆ clear()

template<typename T , std::size_t InlineCapacity>
constexpr void real::detail::small_vec< T, InlineCapacity >::clear ( )
inlineconstexprnoexcept

Removes all elements (capacity and heap state unchanged).

Definition at line 531 of file storage.hpp.

◆ empty()

template<typename T , std::size_t InlineCapacity>
constexpr bool real::detail::small_vec< T, InlineCapacity >::empty ( ) const
inlineconstexprnoexcept

Returns true if empty.

Definition at line 497 of file storage.hpp.

◆ extend_capacity()

template<typename T , std::size_t InlineCapacity>
void real::detail::small_vec< T, InlineCapacity >::extend_capacity ( )
inlineprivate

Doubles the capacity (saturating), spilling to the heap as needed.

Definition at line 388 of file storage.hpp.

◆ inline_data() [1/2]

template<typename T , std::size_t InlineCapacity>
constexpr const T * real::detail::small_vec< T, InlineCapacity >::inline_data ( ) const
inlineconstexprprivatenoexcept

Returns const pointer to the inline buffer.

Definition at line 315 of file storage.hpp.

◆ inline_data() [2/2]

template<typename T , std::size_t InlineCapacity>
constexpr T * real::detail::small_vec< T, InlineCapacity >::inline_data ( )
inlineconstexprprivatenoexcept

Returns pointer to the inline buffer.

Definition at line 307 of file storage.hpp.

◆ operator=() [1/2]

template<typename T , std::size_t InlineCapacity>
constexpr small_vec & real::detail::small_vec< T, InlineCapacity >::operator= ( const small_vec< T, InlineCapacity > &  other)
inlineconstexpr

Copy assignment.

Parameters
[in]otherSource.
Returns
*this.

Definition at line 674 of file storage.hpp.

◆ operator=() [2/2]

template<typename T , std::size_t InlineCapacity>
constexpr small_vec & real::detail::small_vec< T, InlineCapacity >::operator= ( small_vec< T, InlineCapacity > &&  other)
inlineconstexprnoexcept

Move assignment.

Parameters
[in,out]otherSource (left empty).
Returns
*this.

Definition at line 624 of file storage.hpp.

◆ operator[]() [1/2]

template<typename T , std::size_t InlineCapacity>
constexpr const T & real::detail::small_vec< T, InlineCapacity >::operator[] ( std::size_t  i) const
inlineconstexprnoexcept

Returns const reference to the element at i.

Parameters
[in]iIndex.
Returns
Const reference to the element at i.

Definition at line 520 of file storage.hpp.

◆ operator[]() [2/2]

template<typename T , std::size_t InlineCapacity>
constexpr T & real::detail::small_vec< T, InlineCapacity >::operator[] ( std::size_t  i)
inlineconstexprnoexcept

Returns reference to the element at i.

Parameters
[in]iIndex.
Returns
Reference to the element at i.

Definition at line 507 of file storage.hpp.

◆ pop_back()

template<typename T , std::size_t InlineCapacity>
constexpr void real::detail::small_vec< T, InlineCapacity >::pop_back ( )
inlineconstexprnoexcept

Removes the last element. Precondition: the vector is non-empty.

For VM-internal use (POD types like size_t, eps_entry) explicit destroy is unnecessary; full cleanup happens in the destructor / clear when on the heap.

Definition at line 566 of file storage.hpp.

◆ push_back()

template<typename T , std::size_t InlineCapacity>
constexpr void real::detail::small_vec< T, InlineCapacity >::push_back ( const T &  value)
inlineconstexpr

Appends value, growing to the heap if the inline buffer is full.

Parameters
[in]valueThe element to append.
Exceptions
std::bad_allocduring constant evaluation if growth is needed (constexpr use must stay within InlineCapacity).

Definition at line 424 of file storage.hpp.

◆ refresh_data()

template<typename T , std::size_t InlineCapacity>
constexpr void real::detail::small_vec< T, InlineCapacity >::refresh_data ( )
inlineconstexprprivatenoexcept

Refreshes data_ to the active storage base (run time only).

Definition at line 297 of file storage.hpp.

◆ reserve()

template<typename T , std::size_t InlineCapacity>
constexpr void real::detail::small_vec< T, InlineCapacity >::reserve ( std::size_t  new_capacity)
inlineconstexpr

Ensures capacity for at least new_capacity elements (heap-backed).

Parameters
[in]new_capacityDesired minimum capacity; smaller is a no-op.
Exceptions
std::bad_allocduring constant evaluation (constexpr stays inline).

Definition at line 577 of file storage.hpp.

◆ size()

template<typename T , std::size_t InlineCapacity>
constexpr std::size_t real::detail::small_vec< T, InlineCapacity >::size ( ) const
inlineconstexprnoexcept

Returns the number of elements.

Definition at line 489 of file storage.hpp.

◆ transfer_inline_from()

template<typename T , std::size_t InlineCapacity>
template<bool Move>
constexpr void real::detail::small_vec< T, InlineCapacity >::transfer_inline_from ( const small_vec< T, InlineCapacity > &  other)
inlineconstexprprivate

Transfers other's inline elements into this vector's inline buffer.

The copy/move paths use this when other has not spilled. The count is clamped to InlineCapacity — a no-op on the value, since this runs only when the elements are inline (size_ <= InlineCapacity) — but it lets the optimizer see the inline buffer cannot overflow. Without it, g++ -O3 value-propagates a spilled source's large size_ into this then-dead branch and reports a spurious -Wstringop-overflow on the memcpy in transfer_range.

Template Parameters
MoveIf true, move-construct the elements; otherwise copy-construct.
Parameters
[in]otherThe not-yet-spilled source vector.

Definition at line 364 of file storage.hpp.

◆ transfer_range()

template<typename T , std::size_t InlineCapacity>
template<bool Move>
constexpr void real::detail::small_vec< T, InlineCapacity >::transfer_range ( const T *  src,
std::size_t  count,
T *  dest 
)
inlineconstexprprivate

Copies or moves count elements from src to dest.

Template Parameters
MoveIf true, move-construct; otherwise copy-construct.
Parameters
[in]srcSource range.
[in]countElement count.
[out]destDestination (uninitialized) range.

Definition at line 328 of file storage.hpp.

Member Data Documentation

◆ capacity_

template<typename T , std::size_t InlineCapacity>
std::size_t real::detail::small_vec< T, InlineCapacity >::capacity_ {InlineCapacity}
private

Current capacity.

Definition at line 237 of file storage.hpp.

◆ data_

template<typename T , std::size_t InlineCapacity>
T* real::detail::small_vec< T, InlineCapacity >::data_ {}
private

Definition at line 294 of file storage.hpp.

◆ is_heap_

template<typename T , std::size_t InlineCapacity>
bool real::detail::small_vec< T, InlineCapacity >::is_heap_ {}
private

True once spilled to the heap.

Definition at line 238 of file storage.hpp.

◆ size_

template<typename T , std::size_t InlineCapacity>
std::size_t real::detail::small_vec< T, InlineCapacity >::size_ {}
private

Number of elements in use.

Definition at line 236 of file storage.hpp.

◆ storage_

template<typename T , std::size_t InlineCapacity>
union real::detail::small_vec::Storage real::detail::small_vec< T, InlineCapacity >::storage_
private

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