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>
35 constexpr static size_t length = N;
48 ERROR(F(
"Index out of bounds: ") << index << F(
" ≥ ") << N, 0xEDED);
65 ERROR(F(
"Index out of bounds: ") << index << F(
" ≥ ") << N, 0xEDED);
79 const T *
begin()
const {
return &data[0]; }
84 T *
end() {
return &data[N]; }
89 const T *
end()
const {
return &data[N]; }
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>
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];
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>
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();
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();
328Array<
decltype(
T1 {} + T2 {}), N1>
331 static_assert(
N1 ==
N2,
"Error: sizes do not match");
333 for (
size_t i = 0;
i <
N1; ++
i)
340template <
class T1,
class T2,
size_t N1,
size_t N2>
341Array<
decltype(
T1 {} +
T2 {}), N1>
operator+(
const Array<T1, N1> &a,
342 const Array<T2, N2> &b) {
343 return a.
slice() + b.slice();
348template <
class T1,
class T2,
size_t N1,
size_t N2,
bool Reverse1,
349 bool Reverse2,
bool Const1,
bool Const2>
350const ArraySlice<T1, N1, Reverse1, Const1> &
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>
363 a.slice() += b.slice();
373Array<
decltype(
T1 {} - T2 {}), N1>
376 static_assert(
N1 ==
N2,
"Error: sizes do not match");
378 for (
size_t i = 0;
i <
N1; ++
i)
385template <
class T1,
class T2,
size_t N1,
size_t N2>
386Array<
decltype(
T1 {} -
T2 {}), N1>
operator-(
const Array<T1, N1> &a,
387 const Array<T2, N2> &b) {
388 return a.
slice() - b.slice();
393template <
class T1,
class T2,
size_t N1,
size_t N2,
bool Reverse1,
394 bool Reverse2,
bool Const1,
bool Const2>
395const ArraySlice<T1, N1, Reverse1, Const1> &
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>
408 a.slice() -= b.slice();
416template <
class T1,
class T2,
size_t N1,
bool Reverse1,
bool Const1>
417Array<
decltype(
T1 {} * T2 {}), N1>
420 for (
size_t i = 0;
i <
N1; ++
i)
427template <
class T1,
class T2,
size_t N1>
428Array<
decltype(
T1 {} *
T2 {}), N1>
operator*(
const Array<T1, N1> &a, T2 b) {
429 return a.
slice() * b;
434template <
class T1,
class T2,
size_t N2,
bool Reverse2,
bool Const2>
435Array<
decltype(T1 {} * T2 {}), N2>
438 for (
size_t i = 0;
i <
N2; ++
i)
445template <
class T1,
class T2,
size_t N2>
446Array<
decltype(
T1 {} *
T2 {}), N2>
operator*(T1 a,
const Array<T2, N2> &b) {
447 return a * b.
slice();
452template <
class T1,
class T2,
size_t N1,
bool Reverse1,
bool Const1>
453const ArraySlice<T1, N1, Reverse1, 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>
476 for (
size_t i = 0;
i <
N1; ++
i)
483template <
class T1,
class T2,
size_t N1>
484Array<
decltype(
T1 {} /
T2 {}), N1>
operator/(
const Array<T1, N1> &a, T2 b) {
485 return a.
slice() / b;
490template <
class T1,
class T2,
size_t N1,
bool Reverse1,
bool Const1>
491const ArraySlice<T1, N1, Reverse1, 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>
#define BEGIN_AH_NAMESPACE
@ Start
Start System Real-Time message.
Class for a view on a slice of an array.
typename std::conditional< Const, const T *, T * >::type ElementPtrType
typename std::conditional< Reverse, std::reverse_iterator< ElementPtrType >, ElementPtrType >::type iterator
ArraySlice(ElementPtrType array)
Constructor.
typename std::conditional< Const, const T &, T & >::type ElementRefType
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
A class for serial-in/parallel-out shift registers, like the 74HC595 that are connected to the SPI bu...
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
constexpr T abs_diff(const T &a, const T &b)
An array wrapper for easy copying, comparing, and iterating.
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.