libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
array.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <cstddef>
11#include <array>
14
16
35template <typename T, size_t NumValues> class Array {
37 public:
40
42 using DataType = std::array<T, NumValues>;
43
48 Array() : d{} {}
49
54 Array(const DataType &val) : d(val) {}
55
60 Array(const DataType &&val) : d(std::move(val)) {}
61
67 template<typename... Args> Array(Args... args) : d{static_cast<T>(args)...} {}
68
70 ~Array() {}
71
76 size_t size() const { return d.size(); }
77
89 template <typename U, size_t OtherNumValues> Array(const Array<U, OtherNumValues>& other) {
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{};
94 }
95 }
96
106 template <typename U> Array<T, NumValues>& operator=(const Array<U, NumValues>& other) {
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]);
110 }
111 return *this;
112 }
113
126 template <typename U, size_t OtherNumValues> Array<T, NumValues>& operator=(const Array<U, OtherNumValues>& other) {
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{};
131 }
132 return *this;
133 }
134
144 template <typename U> Array<T, NumValues>& operator=(U value) {
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);
148 }
149 return *this;
150 }
151
160 T& operator[](size_t index) {
161 return d[index];
162 }
163
172 const T& operator[](size_t index) const {
173 return d[index];
174 }
175
182 for (size_t i = 0; i < NumValues; ++i) d[i] += other[i];
183 return *this;
184 }
185
192 for (size_t i = 0; i < NumValues; ++i) d[i] -= other[i];
193 return *this;
194 }
195
202 for (size_t i = 0; i < NumValues; ++i) d[i] *= other[i];
203 return *this;
204 }
205
212 for (size_t i = 0; i < NumValues; ++i) d[i] /= other[i];
213 return *this;
214 }
215
221 Array<T, NumValues>& operator+=(const T &scalar) {
222 for (size_t i = 0; i < NumValues; ++i) d[i] += scalar;
223 return *this;
224 }
225
231 Array<T, NumValues>& operator-=(const T &scalar) {
232 for (size_t i = 0; i < NumValues; ++i) d[i] -= scalar;
233 return *this;
234 }
235
241 Array<T, NumValues>& operator*=(const T &scalar) {
242 for (size_t i = 0; i < NumValues; ++i) d[i] *= scalar;
243 return *this;
244 }
245
251 Array<T, NumValues>& operator/=(const T &scalar) {
252 for (size_t i = 0; i < NumValues; ++i) d[i] /= scalar;
253 return *this;
254 }
255
260 T sum() const {
261 T ret{};
262 for(size_t i = 0; i < NumValues; ++i) ret += d[i];
263 return ret;
264 }
265
270 double mean() const {
271 double val = 0.0;
272 for(size_t i = 0; i < NumValues; ++i) val += static_cast<double>(d[i]);
273 val /= static_cast<double>(NumValues);
274 return val;
275 }
276
281 T *data() {
282 return d.data();
283 }
284
289 const T *data() const {
290 return d.data();
291 }
292
297 bool isZero() const {
298 for(size_t i = 0; i < NumValues; i++) {
299 if(d[i] != 0) return false;
300 }
301 return true;
302 }
303
310 Array<T, NumValues> lerp(const Array<T, NumValues> &other, double v) const {
312 for(size_t i = 0; i < d.size(); ++i) ret[i] = ((1.0 - v) * d[i]) + (v * other.d[i]);
313 return ret;
314 }
315
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];
327 else ret[i] = d[i];
328 }
329 return ret;
330 }
331
338 bool isBetween(const Array<T, NumValues> &min, const Array<T, NumValues> &max) const {
340 for(size_t i = 0; i < d.size(); ++i) {
341 if(d[i] < min[i] || d[i] > max[i]) return false;
342 }
343 return true;
344 }
345
348 lhs += rhs;
349 return lhs;
350 }
351
354 lhs -= rhs;
355 return lhs;
356 }
357
360 lhs *= rhs;
361 return lhs;
362 }
363
366 lhs /= rhs;
367 return lhs;
368 }
369
371 friend Array<T, NumValues> operator+(Array<T, NumValues> lhs, const T &scalar) {
372 lhs += scalar;
373 return lhs;
374 }
375
377 friend Array<T, NumValues> operator-(Array<T, NumValues> lhs, const T &scalar) {
378 lhs -= scalar;
379 return lhs;
380 }
381
383 friend Array<T, NumValues> operator*(Array<T, NumValues> lhs, const T &scalar) {
384 lhs *= scalar;
385 return lhs;
386 }
387
389 friend Array<T, NumValues> operator/(Array<T, NumValues> lhs, const T &scalar) {
390 lhs /= scalar;
391 return lhs;
392 }
393
395 friend bool operator==(const Array<T, NumValues> &lhs, const Array<T, NumValues> &rhs) {
396 for(size_t i = 0; i < NumValues; ++i) if(lhs[i] != rhs[i]) return false;
397 return true;
398 }
399
401 friend bool operator!=(const Array<T, NumValues>& lhs, const Array<T, NumValues>& rhs) {
402 return !(lhs == rhs);
403 }
404
405 protected:
406 DataType d;
407};
408
410
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