tesseract++ 0.0.1
N-dimensional tensor library for embedded systems
Loading...
Searching...
No Matches
array.h
Go to the documentation of this file.
1#pragma once
2
3#include "config.h" // for my_size_t
4
19template <typename T, my_size_t N>
20struct Array
21{
22 // ========================================================================
23 // DATA
24 // ========================================================================
25
26 T data[N];
27
28 // ========================================================================
29 // ELEMENT ACCESS
30 // ========================================================================
31
32 FORCE_INLINE constexpr T &operator[](my_size_t i) noexcept
33 {
34 return data[i];
35 }
36
37 FORCE_INLINE constexpr const T &operator[](my_size_t i) const noexcept
38 {
39 return data[i];
40 }
41
42 constexpr T &at(my_size_t i)
43 {
44 if (i >= N)
45 MyErrorHandler::error("Array::at: index out of bounds");
46 return data[i];
47 }
48
49 constexpr const T &at(my_size_t i) const
50 {
51 if (i >= N)
52 MyErrorHandler::error("Array::at: index out of bounds");
53 return data[i];
54 }
55
56 FORCE_INLINE constexpr T &front() noexcept { return data[0]; }
57 FORCE_INLINE constexpr const T &front() const noexcept { return data[0]; }
58
59 FORCE_INLINE constexpr T &back() noexcept { return data[N - 1]; }
60 FORCE_INLINE constexpr const T &back() const noexcept { return data[N - 1]; }
61
62 // ========================================================================
63 // CAPACITY
64 // ========================================================================
65
66 FORCE_INLINE static constexpr my_size_t size() noexcept { return N; }
67 FORCE_INLINE static constexpr bool empty() noexcept { return N == 0; }
68
69 // ========================================================================
70 // ITERATORS
71 // ========================================================================
72
73 FORCE_INLINE constexpr T *begin() noexcept { return data; }
74 FORCE_INLINE constexpr const T *begin() const noexcept { return data; }
75
76 FORCE_INLINE constexpr T *end() noexcept { return data + N; }
77 FORCE_INLINE constexpr const T *end() const noexcept { return data + N; }
78
79 // ========================================================================
80 // OPERATIONS
81 // ========================================================================
82
83 constexpr void fill(const T &value) noexcept
84 {
85 for (my_size_t i = 0; i < N; ++i)
86 data[i] = value;
87 }
88
89 constexpr void swap(Array &other) noexcept
90 {
91 for (my_size_t i = 0; i < N; ++i)
92 {
93 T tmp = data[i];
94 data[i] = other.data[i];
95 other.data[i] = tmp;
96 }
97 }
98};
99
100// ============================================================================
101// SPECIALIZATION FOR EMPTY ARRAY (N=0)
102// ============================================================================
103
110template <typename T>
111struct Array<T, 0>
112{
113 // No data member — zero-size C arrays are non-standard
114
115 // ========================================================================
116 // ELEMENT ACCESS (limited - no elements exist)
117 // ========================================================================
118
119#ifdef TESSERACT_BOUNDS_CHECK
120 constexpr T &at(my_size_t)
121 {
122 MyErrorHandler::error("Array<T,0>::at: empty array has no elements");
123 }
124
125 constexpr const T &at(my_size_t) const
126 {
127 MyErrorHandler::error("Array<T,0>::at: empty array has no elements");
128 }
129#endif
130
131 // operator[], front(), back() intentionally omitted — undefined for empty array
132
133 // ========================================================================
134 // CAPACITY
135 // ========================================================================
136
137 FORCE_INLINE static constexpr my_size_t size() noexcept { return 0; }
138 FORCE_INLINE static constexpr bool empty() noexcept { return true; }
139
140 // ========================================================================
141 // ITERATORS
142 // ========================================================================
143
144 FORCE_INLINE constexpr T *begin() noexcept { return nullptr; }
145 FORCE_INLINE constexpr const T *begin() const noexcept { return nullptr; }
146
147 FORCE_INLINE constexpr T *end() noexcept { return nullptr; }
148 FORCE_INLINE constexpr const T *end() const noexcept { return nullptr; }
149
150 // ========================================================================
151 // OPERATIONS
152 // ========================================================================
153
154 constexpr void fill(const T &) noexcept { /* nothing to fill */ }
155 constexpr void swap(Array &) noexcept { /* nothing to swap */ }
156};
157
158// ============================================================================
159// DEDUCTION GUIDE (C++17)
160// ============================================================================
161
169template <typename T, typename... U>
170Array(T, U...) -> Array<T, 1 + sizeof...(U)>;
static void error(const T &msg)
Definition error_handler.h:30
Global configuration for the tesseract tensor library.
#define my_size_t
Size/index type used throughout the library.
Definition config.h:126
#define FORCE_INLINE
Hint the compiler to always inline a function.
Definition config.h:26
static FORCE_INLINE constexpr my_size_t size() noexcept
Definition array.h:137
FORCE_INLINE constexpr T * begin() noexcept
Definition array.h:144
static FORCE_INLINE constexpr bool empty() noexcept
Definition array.h:138
constexpr void fill(const T &) noexcept
Definition array.h:154
FORCE_INLINE constexpr T * end() noexcept
Definition array.h:147
FORCE_INLINE constexpr const T * end() const noexcept
Definition array.h:148
constexpr void swap(Array &) noexcept
Definition array.h:155
FORCE_INLINE constexpr const T * begin() const noexcept
Definition array.h:145
Fixed-size array container for embedded systems.
Definition array.h:21
constexpr void fill(const T &value) noexcept
Definition array.h:83
FORCE_INLINE constexpr const T & back() const noexcept
Definition array.h:60
FORCE_INLINE constexpr T & front() noexcept
Definition array.h:56
FORCE_INLINE constexpr T & back() noexcept
Definition array.h:59
constexpr const T & at(my_size_t i) const
Definition array.h:49
FORCE_INLINE constexpr T * end() noexcept
Definition array.h:76
static FORCE_INLINE constexpr my_size_t size() noexcept
Definition array.h:66
constexpr void swap(Array &other) noexcept
Definition array.h:89
T data[N]
Definition array.h:26
FORCE_INLINE constexpr const T & operator[](my_size_t i) const noexcept
Definition array.h:37
FORCE_INLINE constexpr const T * end() const noexcept
Definition array.h:77
static FORCE_INLINE constexpr bool empty() noexcept
Definition array.h:67
FORCE_INLINE constexpr T * begin() noexcept
Definition array.h:73
FORCE_INLINE constexpr const T * begin() const noexcept
Definition array.h:74
FORCE_INLINE constexpr const T & front() const noexcept
Definition array.h:57
FORCE_INLINE constexpr T & operator[](my_size_t i) noexcept
Definition array.h:32
constexpr T & at(my_size_t i)
Definition array.h:42