35template <
typename T,
size_t NumValues>
class Array {
67 template<
typename... Args>
Array(Args... args) : d{static_cast<T>(args)...} {}
76 size_t size()
const {
return d.size(); }
90 static_assert(std::is_convertible_v<T, T>,
"Incompatible types");
91 static_assert(OtherNumValues <= NumValues,
"Incompatible sizes");
92 for(
size_t i = 0; i < NumValues; ++i) {
93 d[i] = i < OtherNumValues ? other[i] : T{};
107 static_assert(std::is_assignable_v<T&, U>,
"Incompatible types");
108 for (
size_t i = 0; i < NumValues; ++i) {
109 d[i] =
static_cast<T
>(other[i]);
127 static_assert(std::is_assignable_v<T&, U>,
"Incompatible types");
128 static_assert(OtherNumValues <= NumValues,
"Incompatible sizes");
129 for (
size_t i = 0; i < NumValues; ++i) {
130 d[i] = i < OtherNumValues ? static_cast<T>(other[i]) : T{};
145 static_assert(std::is_assignable_v<T&, U>,
"Incompatible types");
146 for (
size_t i = 0; i < NumValues; ++i) {
147 d[i] =
static_cast<T
>(
value);
182 for (
size_t i = 0; i < NumValues; ++i) d[i] += other[i];
192 for (
size_t i = 0; i < NumValues; ++i) d[i] -= other[i];
202 for (
size_t i = 0; i < NumValues; ++i) d[i] *= other[i];
212 for (
size_t i = 0; i < NumValues; ++i) d[i] /= other[i];
222 for (
size_t i = 0; i < NumValues; ++i) d[i] += scalar;
232 for (
size_t i = 0; i < NumValues; ++i) d[i] -= scalar;
242 for (
size_t i = 0; i < NumValues; ++i) d[i] *= scalar;
252 for (
size_t i = 0; i < NumValues; ++i) d[i] /= scalar;
262 for(
size_t i = 0; i < NumValues; ++i) ret += d[i];
272 for(
size_t i = 0; i < NumValues; ++i) val += static_cast<double>(d[i]);
273 val /=
static_cast<double>(NumValues);
298 for(
size_t i = 0; i < NumValues; i++) {
299 if(d[i] != 0)
return false;
312 for(
size_t i = 0; i < d.size(); ++i) ret[i] = ((1.0 - v) * d[i]) + (v * other.d[i]);
324 for(
size_t i = 0; i < d.size(); ++i) {
325 if(d[i] < min[d]) ret[i] = min[d];
326 else if(d[i] > max[d]) ret[i] = max[d];
340 for(
size_t i = 0; i < d.size(); ++i) {
341 if(d[i] < min[i] || d[i] > max[i])
return false;
396 for(
size_t i = 0; i < NumValues; ++i)
if(lhs[i] != rhs[i])
return false;
402 return !(lhs == rhs);
Fixed-size array container wrapping std::array.
Definition array.h:35
T & operator[](size_t index)
Returns a mutable reference to the element at index.
Definition array.h:160
Array< T, NumValues > clamp(const Array< T, NumValues > &min, const Array< T, NumValues > &max) const
Returns a new array with each element clamped to the given range.
Definition array.h:322
bool isZero() const
Returns true if all elements are zero.
Definition array.h:297
Array< T, NumValues > & operator=(const Array< U, NumValues > &other)
Assigns from another Array of the same size but potentially different type.
Definition array.h:106
Array(const DataType &val)
Constructs from a std::array lvalue reference.
Definition array.h:54
Array< T, NumValues > & operator-=(const T &scalar)
Subtracts a scalar value from all elements.
Definition array.h:231
const T * data() const
Returns a const pointer to the underlying contiguous storage.
Definition array.h:289
friend bool operator==(const Array< T, NumValues > &lhs, const Array< T, NumValues > &rhs)
Returns true if all elements of both arrays are equal.
Definition array.h:395
size_t size() const
Returns the number of elements in the array.
Definition array.h:76
friend Array< T, NumValues > operator-(Array< T, NumValues > lhs, const T &scalar)
Returns a new array with a scalar subtracted from each element.
Definition array.h:377
const T & operator[](size_t index) const
Returns a const reference to the element at index.
Definition array.h:172
friend Array< T, NumValues > operator-(Array< T, NumValues > lhs, const Array< T, NumValues > &rhs)
Returns the element-wise difference of two arrays.
Definition array.h:353
Array< T, NumValues > & operator-=(const Array< T, NumValues > &other)
Subtracts another array element-wise from this one.
Definition array.h:191
Array< T, NumValues > & operator*=(const Array< T, NumValues > &other)
Multiplies this array element-wise by another.
Definition array.h:201
~Array()
Destructor.
Definition array.h:70
friend Array< T, NumValues > operator/(Array< T, NumValues > lhs, const T &scalar)
Returns a new array with each element divided by a scalar.
Definition array.h:389
friend Array< T, NumValues > operator/(Array< T, NumValues > lhs, const Array< T, NumValues > &rhs)
Returns the element-wise quotient of two arrays.
Definition array.h:365
bool isBetween(const Array< T, NumValues > &min, const Array< T, NumValues > &max) const
Returns true if all elements fall within the given range.
Definition array.h:338
Array< T, NumValues > & operator=(U value)
Assigns a scalar value to all elements.
Definition array.h:144
double mean() const
Returns the arithmetic mean of all elements.
Definition array.h:270
Array< T, NumValues > & operator+=(const T &scalar)
Adds a scalar value to all elements.
Definition array.h:221
friend Array< T, NumValues > operator+(Array< T, NumValues > lhs, const Array< T, NumValues > &rhs)
Returns the element-wise sum of two arrays.
Definition array.h:347
Array(Args... args)
Constructs from a variadic argument list.
Definition array.h:67
Array< T, NumValues > & operator/=(const Array< T, NumValues > &other)
Divides this array element-wise by another.
Definition array.h:211
Array(const Array< U, OtherNumValues > &other)
Constructs from another Array of a potentially different size.
Definition array.h:89
Array(const DataType &&val)
Constructs from a std::array rvalue reference.
Definition array.h:60
Array()
Default constructor. Value-initializes all elements to zero/default.
Definition array.h:48
T * data()
Returns a mutable pointer to the underlying contiguous storage.
Definition array.h:281
friend Array< T, NumValues > operator*(Array< T, NumValues > lhs, const T &scalar)
Returns a new array with each element multiplied by a scalar.
Definition array.h:383
Array< T, NumValues > lerp(const Array< T, NumValues > &other, double v) const
Returns a linearly interpolated array between this one and another.
Definition array.h:310
friend Array< T, NumValues > operator*(Array< T, NumValues > lhs, const Array< T, NumValues > &rhs)
Returns the element-wise product of two arrays.
Definition array.h:359
Array< T, NumValues > & operator=(const Array< U, OtherNumValues > &other)
Assigns from another Array of a different type and size.
Definition array.h:126
friend bool operator!=(const Array< T, NumValues > &lhs, const Array< T, NumValues > &rhs)
Returns true if any element differs between the two arrays.
Definition array.h:401
Array< T, NumValues > & operator/=(const T &scalar)
Divides all elements by a scalar value.
Definition array.h:251
Array< T, NumValues > & operator*=(const T &scalar)
Multiplies all elements by a scalar value.
Definition array.h:241
friend Array< T, NumValues > operator+(Array< T, NumValues > lhs, const T &scalar)
Returns a new array with a scalar added to each element.
Definition array.h:371
Array< T, NumValues > & operator+=(const Array< T, NumValues > &other)
Adds another array element-wise to this one.
Definition array.h:181
T sum() const
Returns the sum of all elements.
Definition array.h:260
std::array< T, NumValues > DataType
Underlying std::array storage type.
Definition array.h:42
A smart pointer class with reference counting and optional copy-on-write semantics.
Definition sharedptr.h:252
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19
const T & value(const Result< T > &r)
Returns the value from a Result.
Definition result.h:56
#define PROMEKI_SHARED_FINAL(TYPE)
Macro for non-polymorphic native shared objects.
Definition sharedptr.h:88