6#include <AH/STL/iterator>
7#include <AH/STL/type_traits>
14 return a < b ? b - a : a - b;
20template <
class T,
size_t N,
bool Reverse,
bool Const>
31template <
class T,
size_t N>
48 ERROR(F(
"Index out of bounds: ") << index << F(
" ≥ ") << N, 0xEDED);
65 ERROR(F(
"Index out of bounds: ") << index << F(
" ≥ ") << N, 0xEDED);
100 for (
size_t i = 0; i < N; i++)
101 if ((*
this)[i] != rhs[i])
126 template <
size_t Start = 0,
size_t End = N - 1>
133 template <
size_t Start = 0,
size_t End = N - 1>
140 template <
size_t Start = 0,
size_t End = N - 1>
144 return This->template slice<Start, End>();
163template <
class T,
size_t N,
bool Reverse = false,
bool Const = true>
166 typename std::conditional<Const, const T &, T &>::type;
168 typename std::conditional<Const, const T *, T *>::type;
179 for (
size_t i = 0; i < N; ++i)
180 slice[i] = (*
this)[i];
185 Reverse, std::reverse_iterator<ElementPtrType>,
ElementPtrType>::type;
198 ERROR(F(
"Index out of bounds: ") << index << F(
" ≥ ") << N, 0xEDEF);
202 return *(
array - index);
204 return *(
array + index);
221 template <
size_t Start,
size_t End>
229template <
class T,
size_t N>
230template <
size_t Start,
size_t End>
233 static_assert(Start < N,
"");
234 static_assert(End < N,
"");
235 return &(*this)[Start];
238template <
class T,
size_t N>
239template <
size_t Start,
size_t End>
242 static_assert(Start < N,
"");
243 static_assert(End < N,
"");
244 return &(*this)[Start];
247template <
class T,
size_t N,
bool Reverse,
bool Const>
248template <
size_t Start,
size_t End>
251 static_assert(Start < N,
"");
252 static_assert(End < N,
"");
253 return &(*this)[Start];
257template <
class T,
size_t N,
bool Reverse,
bool Const>
268template <
class T1,
class T2,
size_t N1,
size_t N2,
bool Reverse1,
269 bool Reverse2,
bool Const1,
bool Const2>
272 static_assert(N1 == N2,
"Error: sizes do not match");
273 for (
size_t i = 0; i < N1; ++i)
281template <
class T1,
class T2,
size_t N1,
size_t N2,
bool Reverse2,
bool Const2>
284 return a.
slice() == b;
289template <
class T1,
class T2,
size_t N1,
size_t N2,
bool Reverse1,
bool Const1>
292 return a == b.
slice();
299template <
class T1,
class T2,
size_t N1,
size_t N2,
bool Reverse1,
300 bool Reverse2,
bool Const1,
bool Const2>
308template <
class T1,
class T2,
size_t N1,
size_t N2,
bool Reverse2,
bool Const2>
311 return a.
slice() != b;
316template <
class T1,
class T2,
size_t N1,
size_t N2,
bool Reverse1,
bool Const1>
319 return a != b.
slice();
326template <
class T1,
class T2,
size_t N1,
size_t N2,
bool Reverse1,
327 bool Reverse2,
bool Const1,
bool Const2>
328Array<
decltype(T1 {} + T2 {}), N1>
331 static_assert(N1 == N2,
"Error: sizes do not match");
332 Array<
decltype(T1 {} + T2 {}), N1> result = {{}};
333 for (
size_t i = 0; i < N1; ++i)
334 result[i] = a[i] + b[i];
340template <
class T1,
class T2,
size_t N1,
size_t N2>
348template <
class T1,
class T2,
size_t N1,
size_t N2,
bool Reverse1,
349 bool Reverse2,
bool Const1,
bool Const2>
353 static_assert(N1 == N2,
"Error: sizes do not match");
354 for (
size_t i = 0; i < N1; ++i)
361template <
class T1,
class T2,
size_t N1,
size_t N2>
371template <
class T1,
class T2,
size_t N1,
size_t N2,
bool Reverse1,
372 bool Reverse2,
bool Const1,
bool Const2>
373Array<
decltype(T1 {} - T2 {}), N1>
376 static_assert(N1 == N2,
"Error: sizes do not match");
377 Array<
decltype(T1 {} - T2 {}), N1> result = {{}};
378 for (
size_t i = 0; i < N1; ++i)
379 result[i] = a[i] - b[i];
385template <
class T1,
class T2,
size_t N1,
size_t N2>
393template <
class T1,
class T2,
size_t N1,
size_t N2,
bool Reverse1,
394 bool Reverse2,
bool Const1,
bool Const2>
398 static_assert(N1 == N2,
"Error: sizes do not match");
399 for (
size_t i = 0; i < N1; ++i)
406template <
class T1,
class T2,
size_t N1,
size_t N2>
416template <
class T1,
class T2,
size_t N1,
bool Reverse1,
bool Const1>
417Array<
decltype(T1 {} * T2 {}), N1>
419 Array<
decltype(T1 {} * T2 {}), N1> result = {{}};
420 for (
size_t i = 0; i < N1; ++i)
421 result[i] = a[i] * b;
427template <
class T1,
class T2,
size_t N1>
429 return a.
slice() * b;
434template <
class T1,
class T2,
size_t N2,
bool Reverse2,
bool Const2>
435Array<
decltype(T1 {} * T2 {}), N2>
437 Array<
decltype(T1 {} * T2 {}), N2> result = {{}};
438 for (
size_t i = 0; i < N2; ++i)
439 result[i] = a * b[i];
445template <
class T1,
class T2,
size_t N2>
447 return a * b.
slice();
452template <
class T1,
class T2,
size_t N1,
bool Reverse1,
bool Const1>
455 for (
size_t i = 0; i < N1; ++i)
462template <
class T1,
class T2,
size_t N1>
472template <
class T1,
class T2,
size_t N1,
bool Reverse1,
bool Const1>
473Array<
decltype(T1 {} / T2 {}), N1>
475 Array<
decltype(T1 {} / T2 {}), N1> result = {{}};
476 for (
size_t i = 0; i < N1; ++i)
477 result[i] = a[i] / b;
483template <
class T1,
class T2,
size_t N1>
485 return a.
slice() / b;
490template <
class T1,
class T2,
size_t N1,
bool Reverse1,
bool Const1>
493 for (
size_t i = 0; i < N1; ++i)
500template <
class T1,
class T2,
size_t N1>
510template <
class T,
size_t N,
bool Reverse,
bool Const>
512 Array<
decltype(-T {}), N> result = {{}};
513 for (
size_t i = 0; i < N; ++i)
520template <
class T,
size_t N>
528template <
class T,
size_t NumRows,
size_t NumCols>
constexpr T abs_diff(const T &a, const T &b)
#define BEGIN_AH_NAMESPACE
Class for a view on a slice of an array.
ArraySlice(ElementPtrType array)
Constructor.
typename std::conditional< Const, const T &, T & >::type ElementRefType
typename std::conditional< Reverse, std::reverse_iterator< ElementPtrType >, ElementPtrType >::type iterator
typename std::conditional< Const, const T *, T * >::type ElementPtrType
ElementRefType operator[](size_t index) const
Get the element at the given index.
Array< T, N > asArray() const
ArraySlice< T, abs_diff(End, Start)+1, Reverse ^(End< Start), Const > slice() const
Array< T1, N1 > & operator*=(Array< T1, N1 > &a, T2 b)
Array *= Scalar.
bool operator!=(ArraySlice< T1, N1, Reverse1, Const1 > a, ArraySlice< T2, N2, Reverse2, Const2 > b)
Slice != Slice.
const ArraySlice< T1, N1, Reverse1, Const1 > & operator+=(const ArraySlice< T1, N1, Reverse1, Const1 > &a, const ArraySlice< T2, N2, Reverse2, Const2 > &b)
Slice += Slice.
const ArraySlice< T1, N1, Reverse1, Const1 > & operator*=(const ArraySlice< T1, N1, Reverse1, Const1 > &a, T2 b)
Slice *= Scalar.
const ArraySlice< T1, N1, Reverse1, Const1 > & operator/=(const ArraySlice< T1, N1, Reverse1, Const1 > &a, T2 b)
Slice /= Scalar.
bool operator!=(const Array< T1, N1 > &a, ArraySlice< T2, N2, Reverse2, Const2 > b)
Array != Slice.
bool operator==(const Array< T1, N1 > &a, ArraySlice< T2, N2, Reverse2, Const2 > b)
Array == Slice.
Array< T1, N1 > & operator+=(Array< T1, N1 > &a, const Array< T2, N2 > &b)
Array += Array.
bool operator==(ArraySlice< T1, N1, Reverse1, Const1 > a, ArraySlice< T2, N2, Reverse2, Const2 > b)
Slice == Slice.
bool operator==(ArraySlice< T1, N1, Reverse1, Const1 > a, const Array< T2, N2 > &b)
Slice == Array.
const ArraySlice< T1, N1, Reverse1, Const1 > & operator-=(const ArraySlice< T1, N1, Reverse1, Const1 > &a, const ArraySlice< T2, N2, Reverse2, Const2 > &b)
Slice -= Slice.
Array< T1, N1 > & operator/=(Array< T1, N1 > &a, T2 b)
Array /= Scalar.
bool operator!=(ArraySlice< T1, N1, Reverse1, Const1 > a, const Array< T2, N2 > &b)
Slice != Array.
Array< T1, N1 > & operator-=(Array< T1, N1 > &a, const Array< T2, N2 > &b)
Array -= Array.
ArraySlice< T, N, Reverse, Const >::iterator operator+(typename ArraySlice< T, N, Reverse, Const >::iterator::difference_type n, typename ArraySlice< T, N, Reverse, Const >::iterator a)
<T, N, Reverse, Const>::iterator
#define ERROR(msg, errc)
Print the error message and error code, and stop the execution if FATAL_ERRORS are enabled.
An array wrapper for easy copying, comparing, and iterating.
static constexpr size_t length
const T * end() const
Get a pointer to the memory beyond the array.
T & operator[](size_t index)
Get the element at the given index.
bool operator==(const Array< T, N > &rhs) const
Check the equality of all elements in two arrays.
bool operator!=(const Array< T, N > &rhs) const
Check the inequality of all elements in two arrays.
T * begin()
Get a pointer to the first element.
T * end()
Get a pointer to the memory beyond the array.
ArraySlice< T, abs_diff(Start, End)+1,(End< Start), true > cslice() const
Get a read-only view on a slice of the Array.
ArraySlice< T, abs_diff(Start, End)+1,(End< Start), true > slice() const
Get a read-only view on a slice of the Array.
ArraySlice< T, abs_diff(Start, End)+1,(End< Start), false > slice()
Get a view on a slice of the Array.
const T * begin() const
Get a pointer to the first element.
const T & operator[](size_t index) const
Get the element at the given index.