libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
Array< T, NumValues > Class Template Reference

Fixed-size array container wrapping std::array. More...

#include <array.h>

Public Types

using Ptr = SharedPtr< Array >
 Shared pointer type for Array.
 
using DataType = std::array< T, NumValues >
 Underlying std::array storage type.
 

Public Member Functions

 Array ()
 Default constructor. Value-initializes all elements to zero/default.
 
 Array (const DataType &val)
 Constructs from a std::array lvalue reference.
 
 Array (const DataType &&val)
 Constructs from a std::array rvalue reference.
 
template<typename... Args>
 Array (Args... args)
 Constructs from a variadic argument list.
 
 ~Array ()
 Destructor.
 
size_t size () const
 Returns the number of elements in the array.
 
template<typename U , size_t OtherNumValues>
 Array (const Array< U, OtherNumValues > &other)
 Constructs from another Array of a potentially different size.
 
template<typename U >
Array< T, NumValues > & operator= (const Array< U, NumValues > &other)
 Assigns from another Array of the same size but potentially different type.
 
template<typename U , size_t OtherNumValues>
Array< T, NumValues > & operator= (const Array< U, OtherNumValues > &other)
 Assigns from another Array of a different type and size.
 
template<typename U >
Array< T, NumValues > & operator= (U value)
 Assigns a scalar value to all elements.
 
Toperator[] (size_t index)
 Returns a mutable reference to the element at index.
 
const Toperator[] (size_t index) const
 Returns a const reference to the element at index.
 
Array< T, NumValues > & operator+= (const Array< T, NumValues > &other)
 Adds another array element-wise to this one.
 
Array< T, NumValues > & operator-= (const Array< T, NumValues > &other)
 Subtracts another array element-wise from this one.
 
Array< T, NumValues > & operator*= (const Array< T, NumValues > &other)
 Multiplies this array element-wise by another.
 
Array< T, NumValues > & operator/= (const Array< T, NumValues > &other)
 Divides this array element-wise by another.
 
Array< T, NumValues > & operator+= (const T &scalar)
 Adds a scalar value to all elements.
 
Array< T, NumValues > & operator-= (const T &scalar)
 Subtracts a scalar value from all elements.
 
Array< T, NumValues > & operator*= (const T &scalar)
 Multiplies all elements by a scalar value.
 
Array< T, NumValues > & operator/= (const T &scalar)
 Divides all elements by a scalar value.
 
T sum () const
 Returns the sum of all elements.
 
double mean () const
 Returns the arithmetic mean of all elements.
 
Tdata ()
 Returns a mutable pointer to the underlying contiguous storage.
 
const Tdata () const
 Returns a const pointer to the underlying contiguous storage.
 
bool isZero () const
 Returns true if all elements are zero.
 
Array< T, NumValueslerp (const Array< T, NumValues > &other, double v) const
 Returns a linearly interpolated array between this one and another.
 
Array< T, NumValuesclamp (const Array< T, NumValues > &min, const Array< T, NumValues > &max) const
 Returns a new array with each element clamped to the given range.
 
bool isBetween (const Array< T, NumValues > &min, const Array< T, NumValues > &max) const
 Returns true if all elements fall within the given range.
 

Protected Attributes

DataType d
 

Friends

Array< T, NumValuesoperator+ (Array< T, NumValues > lhs, const Array< T, NumValues > &rhs)
 Returns the element-wise sum of two arrays.
 
Array< T, NumValuesoperator- (Array< T, NumValues > lhs, const Array< T, NumValues > &rhs)
 Returns the element-wise difference of two arrays.
 
Array< T, NumValuesoperator* (Array< T, NumValues > lhs, const Array< T, NumValues > &rhs)
 Returns the element-wise product of two arrays.
 
Array< T, NumValuesoperator/ (Array< T, NumValues > lhs, const Array< T, NumValues > &rhs)
 Returns the element-wise quotient of two arrays.
 
Array< T, NumValuesoperator+ (Array< T, NumValues > lhs, const T &scalar)
 Returns a new array with a scalar added to each element.
 
Array< T, NumValuesoperator- (Array< T, NumValues > lhs, const T &scalar)
 Returns a new array with a scalar subtracted from each element.
 
Array< T, NumValuesoperator* (Array< T, NumValues > lhs, const T &scalar)
 Returns a new array with each element multiplied by a scalar.
 
Array< T, NumValuesoperator/ (Array< T, NumValues > lhs, const T &scalar)
 Returns a new array with each element divided by a scalar.
 
bool operator== (const Array< T, NumValues > &lhs, const Array< T, NumValues > &rhs)
 Returns true if all elements of both arrays are equal.
 
bool operator!= (const Array< T, NumValues > &lhs, const Array< T, NumValues > &rhs)
 Returns true if any element differs between the two arrays.
 

Detailed Description

template<typename T, size_t NumValues>
class Array< T, NumValues >

Fixed-size array container wrapping std::array.

Example
Array<float, 3> rgb = {0.5f, 0.8f, 1.0f};
float r = rgb[0]; // 0.5
size_t n = rgb.size(); // 3
rgb.fill(0.0f); // all zeros
Dynamic array container wrapping std::vector.
Definition list.h:40
size_t size() const noexcept
Returns the number of elements in the list.
Definition list.h:301
Extends std::array with element-wise arithmetic, interpolation, clamping, and range-checking utilities.
Template Parameters
TElement type.
NumValuesNumber of elements (fixed at compile time).

Constructor & Destructor Documentation

◆ Array() [1/4]

template<typename T , size_t NumValues>
Array< T, NumValues >::Array ( const DataType val)
inline

Constructs from a std::array lvalue reference.

Parameters
valThe std::array to copy.

◆ Array() [2/4]

template<typename T , size_t NumValues>
Array< T, NumValues >::Array ( const DataType &&  val)
inline

Constructs from a std::array rvalue reference.

Parameters
valThe std::array to move from.

◆ Array() [3/4]

template<typename T , size_t NumValues>
template<typename... Args>
Array< T, NumValues >::Array ( Args...  args)
inline

Constructs from a variadic argument list.

Template Parameters
ArgsArgument types (must be convertible to T).
Parameters
argsValues used to initialize each element.

◆ Array() [4/4]

template<typename T , size_t NumValues>
template<typename U , size_t OtherNumValues>
Array< T, NumValues >::Array ( const Array< U, OtherNumValues > &  other)
inline

Constructs from another Array of a potentially different size.

The source array must have a size less than or equal to this array. Extra elements are value-initialized. Fails at compile time if the source array is larger.

Template Parameters
USource element type (must be convertible to T).
OtherNumValuesSource array size.
Parameters
otherThe source array to copy from.

Member Function Documentation

◆ clamp()

template<typename T , size_t NumValues>
Array< T, NumValues > Array< T, NumValues >::clamp ( const Array< T, NumValues > &  min,
const Array< T, NumValues > &  max 
) const
inline

Returns a new array with each element clamped to the given range.

Parameters
minPer-element minimum values.
maxPer-element maximum values.
Returns
A new array with each element clamped to [min, max].

◆ data() [1/2]

template<typename T , size_t NumValues>
T * Array< T, NumValues >::data ( )
inline

Returns a mutable pointer to the underlying contiguous storage.

Returns
Pointer to the first element.

◆ data() [2/2]

template<typename T , size_t NumValues>
const T * Array< T, NumValues >::data ( ) const
inline

Returns a const pointer to the underlying contiguous storage.

Returns
Const pointer to the first element.

◆ isBetween()

template<typename T , size_t NumValues>
bool Array< T, NumValues >::isBetween ( const Array< T, NumValues > &  min,
const Array< T, NumValues > &  max 
) const
inline

Returns true if all elements fall within the given range.

Parameters
minPer-element minimum values (inclusive).
maxPer-element maximum values (inclusive).
Returns
True if every element is between the corresponding min and max.

◆ isZero()

template<typename T , size_t NumValues>
bool Array< T, NumValues >::isZero ( ) const
inline

Returns true if all elements are zero.

Returns
True if every element compares equal to zero.

◆ lerp()

template<typename T , size_t NumValues>
Array< T, NumValues > Array< T, NumValues >::lerp ( const Array< T, NumValues > &  other,
double  v 
) const
inline

Returns a linearly interpolated array between this one and another.

Parameters
otherThe target array to interpolate toward.
vInterpolation factor (0.0 returns this array, 1.0 returns other).
Returns
A new array with each element interpolated.

◆ mean()

template<typename T , size_t NumValues>
double Array< T, NumValues >::mean ( ) const
inline

Returns the arithmetic mean of all elements.

Returns
The mean as a double.

◆ operator*=() [1/2]

template<typename T , size_t NumValues>
Array< T, NumValues > & Array< T, NumValues >::operator*= ( const Array< T, NumValues > &  other)
inline

Multiplies this array element-wise by another.

Parameters
otherThe array to multiply by.
Returns
Reference to this array.

◆ operator*=() [2/2]

template<typename T , size_t NumValues>
Array< T, NumValues > & Array< T, NumValues >::operator*= ( const T scalar)
inline

Multiplies all elements by a scalar value.

Parameters
scalarThe value to multiply by.
Returns
Reference to this array.

◆ operator+=() [1/2]

template<typename T , size_t NumValues>
Array< T, NumValues > & Array< T, NumValues >::operator+= ( const Array< T, NumValues > &  other)
inline

Adds another array element-wise to this one.

Parameters
otherThe array to add.
Returns
Reference to this array.

◆ operator+=() [2/2]

template<typename T , size_t NumValues>
Array< T, NumValues > & Array< T, NumValues >::operator+= ( const T scalar)
inline

Adds a scalar value to all elements.

Parameters
scalarThe value to add.
Returns
Reference to this array.

◆ operator-=() [1/2]

template<typename T , size_t NumValues>
Array< T, NumValues > & Array< T, NumValues >::operator-= ( const Array< T, NumValues > &  other)
inline

Subtracts another array element-wise from this one.

Parameters
otherThe array to subtract.
Returns
Reference to this array.

◆ operator-=() [2/2]

template<typename T , size_t NumValues>
Array< T, NumValues > & Array< T, NumValues >::operator-= ( const T scalar)
inline

Subtracts a scalar value from all elements.

Parameters
scalarThe value to subtract.
Returns
Reference to this array.

◆ operator/=() [1/2]

template<typename T , size_t NumValues>
Array< T, NumValues > & Array< T, NumValues >::operator/= ( const Array< T, NumValues > &  other)
inline

Divides this array element-wise by another.

Parameters
otherThe array to divide by.
Returns
Reference to this array.

◆ operator/=() [2/2]

template<typename T , size_t NumValues>
Array< T, NumValues > & Array< T, NumValues >::operator/= ( const T scalar)
inline

Divides all elements by a scalar value.

Parameters
scalarThe value to divide by.
Returns
Reference to this array.

◆ operator=() [1/3]

template<typename T , size_t NumValues>
template<typename U >
Array< T, NumValues > & Array< T, NumValues >::operator= ( const Array< U, NumValues > &  other)
inline

Assigns from another Array of the same size but potentially different type.

Fails at compile time if types cannot be converted.

Template Parameters
USource element type.
Parameters
otherThe source array to assign from.
Returns
Reference to this array.

◆ operator=() [2/3]

template<typename T , size_t NumValues>
template<typename U , size_t OtherNumValues>
Array< T, NumValues > & Array< T, NumValues >::operator= ( const Array< U, OtherNumValues > &  other)
inline

Assigns from another Array of a different type and size.

The source must not be larger than this array. Extra elements are value-initialized. Fails at compile time if types cannot be converted or the source array is too large.

Template Parameters
USource element type.
OtherNumValuesSource array size.
Parameters
otherThe source array to assign from.
Returns
Reference to this array.

◆ operator=() [3/3]

template<typename T , size_t NumValues>
template<typename U >
Array< T, NumValues > & Array< T, NumValues >::operator= ( U  value)
inline

Assigns a scalar value to all elements.

Fails at compile time if the type cannot be converted.

Template Parameters
UScalar type.
Parameters
valueThe value to assign to every element.
Returns
Reference to this array.

◆ operator[]() [1/2]

template<typename T , size_t NumValues>
T & Array< T, NumValues >::operator[] ( size_t  index)
inline

Returns a mutable reference to the element at index.

No bounds checking is performed.

Parameters
indexZero-based element index.
Returns
Reference to the element.

◆ operator[]() [2/2]

template<typename T , size_t NumValues>
const T & Array< T, NumValues >::operator[] ( size_t  index) const
inline

Returns a const reference to the element at index.

No bounds checking is performed.

Parameters
indexZero-based element index.
Returns
Const reference to the element.

◆ size()

template<typename T , size_t NumValues>
size_t Array< T, NumValues >::size ( ) const
inline

Returns the number of elements in the array.

Returns
The compile-time fixed size NumValues.

◆ sum()

template<typename T , size_t NumValues>
T Array< T, NumValues >::sum ( ) const
inline

Returns the sum of all elements.

Returns
The sum, starting from a value-initialized accumulator.

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