Stratax
Scientific computing containers and operations
Loading...
Searching...
No Matches
stratax::core::Buffer< T, Alignment > Class Template Reference

Owns contiguous dynamically allocated storage. More...

#include <Buffer.hpp>

Classes

struct  uninitialized_t

Public Member Functions

 Buffer () noexcept
 Creates an empty buffer.
 Buffer (std::size_t size)
 Creates a buffer with default-initialized elements.
 Buffer (std::size_t size, uninitialized_t uninitialized)
 Creates a buffer with allocated but uninitialized storage.
 Buffer (std::size_t size, const T &value)
 Creates a buffer and fills every element with a value.
 Buffer (std::initializer_list< T > list)
 Creates a buffer from an initializer list.
 Buffer (const Buffer &other)
 Creates a copy of another buffer.
 Buffer (Buffer &&other) noexcept
 Transfers ownership from another buffer.
Bufferoperator= (const Buffer &other)
 Replaces this buffer with a copy of another buffer.
Bufferoperator= (Buffer &&other) noexcept
 Replaces this buffer by taking ownership from another buffer.
 ~Buffer ()
 Releases the owned storage.
T & operator[] (std::size_t index) noexcept
 Returns a mutable element reference without bounds checking.
const T & operator[] (std::size_t index) const noexcept
 Returns a const element reference without bounds checking.
T & front ()
 Returns the first element.
const T & front () const
 Returns the first element as a const reference.
T & back ()
 Returns the last element.
const T & back () const
 Returns the last element as a const reference.
T * data () noexcept
 Returns the raw data pointer.
const T * data () const noexcept
 Returns the raw data pointer as a const pointer.
T * begin () noexcept
 Returns an iterator to the first element.
const T * begin () const noexcept
 Returns a const iterator to the first element.
const T * cbegin () const noexcept
 Returns a const iterator to the first element.
T * end () noexcept
 Returns an iterator one past the last element.
const T * end () const noexcept
 Returns a const iterator one past the last element.
const T * cend () const noexcept
 Returns a const iterator one past the last element.
std::reverse_iterator< T * > rbegin () noexcept
 Returns a reverse iterator to the last element.
std::reverse_iterator< const T * > rbegin () const noexcept
 Returns a const reverse iterator to the last element.
std::reverse_iterator< const T * > crbegin () const noexcept
 Returns a const reverse iterator to the last element.
std::reverse_iterator< T * > rend () noexcept
 Returns a reverse iterator before the first element.
std::reverse_iterator< const T * > rend () const noexcept
 Returns a const reverse iterator before the first element.
std::reverse_iterator< const T * > crend () const noexcept
 Returns a const reverse iterator before the first element.
std::size_t size () const noexcept
 Returns the number of stored elements.
bool empty () const noexcept
 Returns whether the buffer contains no elements.
void fill (const T &value)
 Fills every element with the same value.
void swap (Buffer &other) noexcept
 Swaps the contents of two buffers.

Static Public Attributes

static constexpr uninitialized_t uninitialized {}

Detailed Description

template<typename T, std::size_t Alignment = config::default_alignment>
class stratax::core::Buffer< T, Alignment >

Owns contiguous dynamically allocated storage.

Buffer is the low-level memory container used by Stratax arrays. It manages allocation, destruction, and flat element access, while higher-level types such as Vector, Matrix, and Tensor handle shape semantics and mathematical operations.

The stored elements are always contiguous in memory, and the buffer follows RAII so ownership is released automatically when the object is destroyed.

Template Parameters
TElement type.
AlignmentAlignment used for allocation, defaulting to 64.
Note
Buffer provides storage only. It does not validate shapes or perform multidimensional indexing.

Definition at line 38 of file Buffer.hpp.

Constructor & Destructor Documentation

◆ Buffer() [1/7]

template<typename T, std::size_t Alignment = config::default_alignment>
stratax::core::Buffer< T, Alignment >::Buffer ( )
inlinenoexcept

Creates an empty buffer.

The buffer owns no storage and contains zero elements.

Definition at line 48 of file Buffer.hpp.

Referenced by Buffer(), Buffer(), operator=(), operator=(), and swap().

◆ Buffer() [2/7]

template<typename T, std::size_t Alignment = config::default_alignment>
stratax::core::Buffer< T, Alignment >::Buffer ( std::size_t size)
inlineexplicit

Creates a buffer with default-initialized elements.

The buffer allocates storage for the requested number of elements and default-constructs each one in place.

Parameters
sizeNumber of elements to allocate.

Definition at line 58 of file Buffer.hpp.

References size().

◆ Buffer() [3/7]

template<typename T, std::size_t Alignment = config::default_alignment>
stratax::core::Buffer< T, Alignment >::Buffer ( std::size_t size,
uninitialized_t uninitialized )
inline

Creates a buffer with allocated but uninitialized storage.

Use this overload when you intend to construct the elements later and want to avoid default initialization work.

Parameters
sizeNumber of elements to allocate.
uninitializedTag selecting uninitialized storage.
Warning
The element type must be trivially destructible.

Definition at line 74 of file Buffer.hpp.

References size(), and uninitialized.

◆ Buffer() [4/7]

template<typename T, std::size_t Alignment = config::default_alignment>
stratax::core::Buffer< T, Alignment >::Buffer ( std::size_t size,
const T & value )
inline

Creates a buffer and fills every element with a value.

Each element is copy-constructed from the supplied value.

Parameters
sizeNumber of elements to allocate.
valueValue to copy into each element.

Definition at line 91 of file Buffer.hpp.

References size().

◆ Buffer() [5/7]

template<typename T, std::size_t Alignment = config::default_alignment>
stratax::core::Buffer< T, Alignment >::Buffer ( std::initializer_list< T > list)
inline

Creates a buffer from an initializer list.

The elements are copied in list order.

Parameters
listSource values to copy into the buffer.

Definition at line 103 of file Buffer.hpp.

References size().

◆ Buffer() [6/7]

template<typename T, std::size_t Alignment = config::default_alignment>
stratax::core::Buffer< T, Alignment >::Buffer ( const Buffer< T, Alignment > & other)
inline

Creates a copy of another buffer.

The new buffer owns its own storage and duplicates the source elements.

Parameters
otherBuffer to copy from.

Definition at line 127 of file Buffer.hpp.

References Buffer().

◆ Buffer() [7/7]

template<typename T, std::size_t Alignment = config::default_alignment>
stratax::core::Buffer< T, Alignment >::Buffer ( Buffer< T, Alignment > && other)
inlinenoexcept

Transfers ownership from another buffer.

The source buffer is left empty after the move.

Parameters
otherBuffer to move from.

Definition at line 157 of file Buffer.hpp.

References Buffer().

◆ ~Buffer()

template<typename T, std::size_t Alignment = config::default_alignment>
stratax::core::Buffer< T, Alignment >::~Buffer ( )
inline

Releases the owned storage.

Destroying a buffer also destroys any constructed elements before the memory is returned to the allocator.

Definition at line 217 of file Buffer.hpp.

Member Function Documentation

◆ back() [1/2]

template<typename T, std::size_t Alignment = config::default_alignment>
T & stratax::core::Buffer< T, Alignment >::back ( )
inline

Returns the last element.

Returns
Mutable reference to the last stored element.
Exceptions
Exceptions::IndexErrorIf the buffer is empty.

Definition at line 290 of file Buffer.hpp.

References empty().

◆ back() [2/2]

template<typename T, std::size_t Alignment = config::default_alignment>
const T & stratax::core::Buffer< T, Alignment >::back ( ) const
inline

Returns the last element as a const reference.

Returns
Const reference to the last stored element.
Exceptions
Exceptions::IndexErrorIf the buffer is empty.

Definition at line 306 of file Buffer.hpp.

References empty().

◆ begin() [1/2]

template<typename T, std::size_t Alignment = config::default_alignment>
const T * stratax::core::Buffer< T, Alignment >::begin ( ) const
inlinenodiscardnoexcept

Returns a const iterator to the first element.

Returns
Const iterator to the beginning of the buffer.

Definition at line 350 of file Buffer.hpp.

◆ begin() [2/2]

template<typename T, std::size_t Alignment = config::default_alignment>
T * stratax::core::Buffer< T, Alignment >::begin ( )
inlinenodiscardnoexcept

Returns an iterator to the first element.

Returns
Iterator to the beginning of the buffer.

Definition at line 340 of file Buffer.hpp.

Referenced by rend(), and rend().

◆ cbegin()

template<typename T, std::size_t Alignment = config::default_alignment>
const T * stratax::core::Buffer< T, Alignment >::cbegin ( ) const
inlinenodiscardnoexcept

Returns a const iterator to the first element.

Returns
Const iterator to the beginning of the buffer.

Definition at line 360 of file Buffer.hpp.

Referenced by crend().

◆ cend()

template<typename T, std::size_t Alignment = config::default_alignment>
const T * stratax::core::Buffer< T, Alignment >::cend ( ) const
inlinenodiscardnoexcept

Returns a const iterator one past the last element.

Returns
Const iterator to the end of the buffer.

Definition at line 390 of file Buffer.hpp.

Referenced by crbegin().

◆ crbegin()

template<typename T, std::size_t Alignment = config::default_alignment>
std::reverse_iterator< const T * > stratax::core::Buffer< T, Alignment >::crbegin ( ) const
inlinenodiscardnoexcept

Returns a const reverse iterator to the last element.

Returns
Const reverse iterator starting at the last element.

Definition at line 420 of file Buffer.hpp.

References cend().

◆ crend()

template<typename T, std::size_t Alignment = config::default_alignment>
std::reverse_iterator< const T * > stratax::core::Buffer< T, Alignment >::crend ( ) const
inlinenodiscardnoexcept

Returns a const reverse iterator before the first element.

Returns
Const reverse iterator representing the end sentinel.

Definition at line 450 of file Buffer.hpp.

References cbegin().

◆ data() [1/2]

template<typename T, std::size_t Alignment = config::default_alignment>
const T * stratax::core::Buffer< T, Alignment >::data ( ) const
inlinenodiscardnoexcept

Returns the raw data pointer as a const pointer.

Returns
Pointer to the first stored element, or nullptr when empty.

Definition at line 330 of file Buffer.hpp.

◆ data() [2/2]

template<typename T, std::size_t Alignment = config::default_alignment>
T * stratax::core::Buffer< T, Alignment >::data ( )
inlinenodiscardnoexcept

Returns the raw data pointer.

Returns
Pointer to the first stored element, or nullptr when empty.

Definition at line 320 of file Buffer.hpp.

◆ empty()

template<typename T, std::size_t Alignment = config::default_alignment>
bool stratax::core::Buffer< T, Alignment >::empty ( ) const
inlinenodiscardnoexcept

Returns whether the buffer contains no elements.

Returns
true when the buffer is empty; otherwise false.

Definition at line 470 of file Buffer.hpp.

Referenced by back(), back(), front(), and front().

◆ end() [1/2]

template<typename T, std::size_t Alignment = config::default_alignment>
const T * stratax::core::Buffer< T, Alignment >::end ( ) const
inlinenodiscardnoexcept

Returns a const iterator one past the last element.

Returns
Const iterator to the end of the buffer.

Definition at line 380 of file Buffer.hpp.

◆ end() [2/2]

template<typename T, std::size_t Alignment = config::default_alignment>
T * stratax::core::Buffer< T, Alignment >::end ( )
inlinenodiscardnoexcept

Returns an iterator one past the last element.

Returns
Iterator to the end of the buffer.

Definition at line 370 of file Buffer.hpp.

Referenced by rbegin(), and rbegin().

◆ fill()

template<typename T, std::size_t Alignment = config::default_alignment>
void stratax::core::Buffer< T, Alignment >::fill ( const T & value)
inline

Fills every element with the same value.

Parameters
valueValue to assign to each element.

Definition at line 480 of file Buffer.hpp.

◆ front() [1/2]

template<typename T, std::size_t Alignment = config::default_alignment>
T & stratax::core::Buffer< T, Alignment >::front ( )
inline

Returns the first element.

Returns
Mutable reference to the first stored element.
Exceptions
Exceptions::IndexErrorIf the buffer is empty.

Definition at line 258 of file Buffer.hpp.

References empty().

◆ front() [2/2]

template<typename T, std::size_t Alignment = config::default_alignment>
const T & stratax::core::Buffer< T, Alignment >::front ( ) const
inline

Returns the first element as a const reference.

Returns
Const reference to the first stored element.
Exceptions
Exceptions::IndexErrorIf the buffer is empty.

Definition at line 274 of file Buffer.hpp.

References empty().

◆ operator=() [1/2]

template<typename T, std::size_t Alignment = config::default_alignment>
Buffer & stratax::core::Buffer< T, Alignment >::operator= ( Buffer< T, Alignment > && other)
inlinenoexcept

Replaces this buffer by taking ownership from another buffer.

The current contents are destroyed before the source storage is adopted.

Parameters
otherBuffer to move from.
Returns
Reference to this buffer.

Definition at line 194 of file Buffer.hpp.

References Buffer().

◆ operator=() [2/2]

template<typename T, std::size_t Alignment = config::default_alignment>
Buffer & stratax::core::Buffer< T, Alignment >::operator= ( const Buffer< T, Alignment > & other)
inline

Replaces this buffer with a copy of another buffer.

This assignment provides the strong exception guarantee by copying into a temporary buffer first.

Parameters
otherBuffer to copy from.
Returns
Reference to this buffer.

Definition at line 174 of file Buffer.hpp.

References Buffer(), and swap().

◆ operator[]() [1/2]

template<typename T, std::size_t Alignment = config::default_alignment>
const T & stratax::core::Buffer< T, Alignment >::operator[] ( std::size_t index) const
inlinenoexcept

Returns a const element reference without bounds checking.

Parameters
indexFlat element index.
Returns
Const reference to the indexed element.
Warning
The caller must ensure that the index is in range.

Definition at line 246 of file Buffer.hpp.

◆ operator[]() [2/2]

template<typename T, std::size_t Alignment = config::default_alignment>
T & stratax::core::Buffer< T, Alignment >::operator[] ( std::size_t index)
inlinenoexcept

Returns a mutable element reference without bounds checking.

Parameters
indexFlat element index.
Returns
Reference to the indexed element.
Warning
The caller must ensure that the index is in range.

Definition at line 232 of file Buffer.hpp.

◆ rbegin() [1/2]

template<typename T, std::size_t Alignment = config::default_alignment>
std::reverse_iterator< const T * > stratax::core::Buffer< T, Alignment >::rbegin ( ) const
inlinenodiscardnoexcept

Returns a const reverse iterator to the last element.

Returns
Const reverse iterator starting at the last element.

Definition at line 410 of file Buffer.hpp.

References end().

◆ rbegin() [2/2]

template<typename T, std::size_t Alignment = config::default_alignment>
std::reverse_iterator< T * > stratax::core::Buffer< T, Alignment >::rbegin ( )
inlinenodiscardnoexcept

Returns a reverse iterator to the last element.

Returns
Reverse iterator starting at the last element.

Definition at line 400 of file Buffer.hpp.

References end().

◆ rend() [1/2]

template<typename T, std::size_t Alignment = config::default_alignment>
std::reverse_iterator< const T * > stratax::core::Buffer< T, Alignment >::rend ( ) const
inlinenodiscardnoexcept

Returns a const reverse iterator before the first element.

Returns
Const reverse iterator representing the end sentinel.

Definition at line 440 of file Buffer.hpp.

References begin().

◆ rend() [2/2]

template<typename T, std::size_t Alignment = config::default_alignment>
std::reverse_iterator< T * > stratax::core::Buffer< T, Alignment >::rend ( )
inlinenodiscardnoexcept

Returns a reverse iterator before the first element.

Returns
Reverse iterator representing the end sentinel.

Definition at line 430 of file Buffer.hpp.

References begin().

◆ size()

template<typename T, std::size_t Alignment = config::default_alignment>
std::size_t stratax::core::Buffer< T, Alignment >::size ( ) const
inlinenodiscardnoexcept

Returns the number of stored elements.

Returns
Number of elements currently owned by the buffer.

Definition at line 460 of file Buffer.hpp.

Referenced by Buffer(), Buffer(), Buffer(), and Buffer().

◆ swap()

template<typename T, std::size_t Alignment = config::default_alignment>
void stratax::core::Buffer< T, Alignment >::swap ( Buffer< T, Alignment > & other)
inlinenoexcept

Swaps the contents of two buffers.

Parameters
otherBuffer to exchange state with.

Definition at line 490 of file Buffer.hpp.

References Buffer().

Referenced by operator=().

Member Data Documentation

◆ uninitialized

template<typename T, std::size_t Alignment = config::default_alignment>
uninitialized_t stratax::core::Buffer< T, Alignment >::uninitialized {}
staticconstexpr

Definition at line 41 of file Buffer.hpp.

Referenced by Buffer().


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