tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
array.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_ARRAY_ARRAY_HPP
4#define TETL_ARRAY_ARRAY_HPP
5
6#include <etl/_config/all.hpp>
7
17#include <etl/_iterator/end.hpp>
30#include <etl/_utility/move.hpp>
32
33namespace etl {
47template <typename Type, size_t Size>
48struct array {
49 using value_type = Type;
52 using pointer = Type*;
53 using const_pointer = Type const*;
54 using reference = Type&;
55 using const_reference = Type const&;
56 using iterator = Type*;
57 using const_iterator = Type const*;
60
62 [[nodiscard]] constexpr auto operator[](size_type const pos) noexcept -> reference
63 {
64 if constexpr (Size == 0) {
66 } else {
67 TETL_PRECONDITION_SAFE(pos < Size);
68 return _buf[pos];
69 }
70 }
71
73 [[nodiscard]] constexpr auto operator[](size_type const pos) const noexcept -> const_reference
74 {
75 if constexpr (Size == 0) {
77 } else {
78 TETL_PRECONDITION_SAFE(pos < Size);
79 return _buf[pos];
80 }
81 }
82
84 [[nodiscard]] constexpr auto front() noexcept -> reference { return *begin(); }
85
87 [[nodiscard]] constexpr auto front() const noexcept -> const_reference { return *begin(); }
88
90 [[nodiscard]] constexpr auto back() noexcept -> reference { return *etl::prev(end()); }
91
93 [[nodiscard]] constexpr auto back() const noexcept -> const_reference { return *etl::prev(end()); }
94
99 [[nodiscard]] constexpr auto data() noexcept -> pointer { return begin(); }
100
105 [[nodiscard]] constexpr auto data() const noexcept -> const_pointer { return begin(); }
106
108 [[nodiscard]] constexpr auto begin() noexcept -> iterator
109 {
110 if constexpr (Size == 0) {
111 return nullptr;
112 } else {
113 return etl::begin(_buf);
114 }
115 }
116
118 [[nodiscard]] constexpr auto begin() const noexcept -> const_iterator
119 {
120 if constexpr (Size == 0) {
121 return nullptr;
122 } else {
123 return etl::begin(_buf);
124 }
125 }
126
128 [[nodiscard]] constexpr auto cbegin() const noexcept -> const_iterator { return begin(); }
129
131 [[nodiscard]] constexpr auto end() noexcept -> iterator
132 {
133 if constexpr (Size == 0) {
134 return nullptr;
135 } else {
136 return etl::end(_buf);
137 }
138 }
139
141 [[nodiscard]] constexpr auto end() const noexcept -> const_iterator
142 {
143 if constexpr (Size == 0) {
144 return nullptr;
145 } else {
146 return etl::end(_buf);
147 }
148 }
149
151 [[nodiscard]] constexpr auto cend() const noexcept -> const_iterator { return end(); }
152
156 [[nodiscard]] constexpr auto rbegin() noexcept -> reverse_iterator { return reverse_iterator(end()); }
157
161 [[nodiscard]] constexpr auto rbegin() const noexcept -> const_reverse_iterator
162 {
163 return const_reverse_iterator(end());
164 }
165
169 [[nodiscard]] constexpr auto crbegin() const noexcept -> const_reverse_iterator { return rbegin(); }
170
175 [[nodiscard]] constexpr auto rend() noexcept -> reverse_iterator { return reverse_iterator(begin()); }
176
181 [[nodiscard]] constexpr auto rend() const noexcept -> const_reverse_iterator
182 {
184 }
185
190 [[nodiscard]] constexpr auto crend() const noexcept -> const_reverse_iterator { return rend(); }
191
193 [[nodiscard]] constexpr auto empty() const noexcept -> bool { return begin() == end(); }
194
196 [[nodiscard]] constexpr auto size() const noexcept -> size_type { return Size; }
197
204 [[nodiscard]] constexpr auto max_size() const noexcept -> size_type { return Size; }
205
207 constexpr auto fill(const_reference value) -> void { etl::fill_n(begin(), Size, value); }
208
212 constexpr auto swap(array& other) noexcept(is_nothrow_swappable_v<Type>) -> void
213 {
214 etl::swap_ranges(begin(), end(), other.begin());
215 }
216
218 friend constexpr auto swap(array& lhs, array& rhs) noexcept(noexcept(lhs.swap(rhs))) -> void { lhs.swap(rhs); }
219
223 friend constexpr auto operator==(array const& lhs, array const& rhs) -> bool
224 {
225 return etl::equal(lhs.begin(), lhs.end(), rhs.begin());
226 }
227
230 friend constexpr auto operator<(array const& lhs, array const& rhs) -> bool
231 {
232 return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
233 }
234
235 friend constexpr auto operator<=(array const& lhs, array const& rhs) -> bool { return !(rhs < lhs); }
236
237 friend constexpr auto operator>(array const& lhs, array const& rhs) -> bool { return rhs < lhs; }
238
239 friend constexpr auto operator>=(array const& lhs, array const& rhs) -> bool { return !(lhs < rhs); }
240
242 // NOLINTNEXTLINE(readability-identifier-naming)
243 TETL_NO_UNIQUE_ADDRESS conditional_t<Size == 0, empty_c_array, c_array<Type, Size + size_t{Size == 0}>> _buf;
244};
245
251template <typename T, typename... U>
252array(T, U...) -> array<T, 1 + sizeof...(U)>;
253
257template <typename T, size_t N>
258struct tuple_size<array<T, N>> : integral_constant<size_t, N> { };
259
261template <typename T, etl::size_t Size>
262inline constexpr auto is_tuple_like<etl::array<T, Size>> = true;
263
266template <size_t I, typename T>
268
269template <size_t I, typename T, size_t N>
270struct tuple_element<I, array<T, N>> {
271 using type = T;
272};
273
278template <size_t Index, typename T, size_t Size>
279[[nodiscard]] constexpr auto get(array<T, Size>& array) noexcept -> T&
280{
281 static_assert(Index < Size, "array index out of range");
282 return array[Index];
283}
284
286template <size_t Index, typename T, size_t Size>
287[[nodiscard]] constexpr auto get(array<T, Size> const& array) noexcept -> T const&
288{
289 static_assert(Index < Size, "array index out of range");
290 return array[Index];
291}
292
294template <size_t Index, typename T, size_t Size>
295[[nodiscard]] constexpr auto get(array<T, Size>&& array) noexcept -> T&&
296{
297 static_assert(Index < Size, "array index out of range");
298 return etl::move(array[Index]);
299}
300
302template <size_t Index, typename T, size_t Size>
303[[nodiscard]] constexpr auto get(array<T, Size> const&& array) noexcept -> T const&&
304{
305 static_assert(Index < Size, "array index out of range");
306 return etl::move(array[Index]);
307}
308
313template <typename T, size_t N>
314[[nodiscard]] constexpr auto to_array(T (&a)[N]) -> array<remove_cv_t<T>, N>
315{
316 return [&]<etl::size_t... I>(etl::index_sequence<I...> /*i*/) {
317 return etl::array<etl::remove_cv_t<T>, N>{{a[I]...}};
319}
320
322template <typename T, size_t N>
323[[nodiscard]] constexpr auto to_array(T (&&a)[N])
324{
325 return [&]<etl::size_t... I>(etl::index_sequence<I...> /*i*/) {
326 return etl::array<etl::remove_cv_t<T>, N>{{etl::move(a[I])...}};
328}
329
330template <typename T>
331inline constexpr auto is_etl_array = false;
332
333template <typename T, size_t Size>
334inline constexpr auto is_etl_array<array<T, Size>> = true;
335
336} // namespace etl
337
338#endif // TETL_ARRAY_ARRAY_HPP
#define TETL_NO_UNIQUE_ADDRESS
Definition attributes.hpp:41
#define TETL_PRECONDITION_SAFE(...)
Definition check.hpp:17
constexpr auto move(InputIt first, InputIt last, OutputIt destination) -> OutputIt
Moves the elements in the range [first, last), to another range beginning at destination,...
Definition move.hpp:26
constexpr auto swap_ranges(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2) -> ForwardIt2
Exchanges elements between range [first1 ,last1) and another range starting at first2.
Definition swap_ranges.hpp:24
constexpr auto lexicographical_compare(InputIt1 f1, InputIt1 l1, InputIt2 f2, InputIt2 l2, Compare comp) -> bool
Checks if the first range [f1, l1) is lexicographically less than the second range [f2,...
Definition lexicographical_compare.hpp:17
constexpr auto equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, Predicate p) -> bool
Returns true if the range [first1, last1) is equal to the range [first2, first2 + (last1 - first1)),...
Definition equal.hpp:18
constexpr auto fill_n(OutputIt first, Size count, T const &value) -> OutputIt
Assigns the given value to the first count elements in the range beginning at first if count > 0....
Definition fill_n.hpp:16
ValueType[Size] c_array
Definition c_array.hpp:12
constexpr auto prev(BidirIt it, typename iterator_traits< BidirIt >::difference_type n=1) -> BidirIt
Return the nth predecessor of iterator it.
Definition prev.hpp:14
constexpr auto end(C &c) -> decltype(c.end())
Returns an iterator to the end (i.e. the element after the last element) of the given container c or ...
Definition end.hpp:14
constexpr auto begin(C &c) -> decltype(c.begin())
Returns an iterator to the beginning of the given container c or array array. These templates rely on...
Definition begin.hpp:20
Definition adjacent_find.hpp:8
typename conditional< B, T, F >::type conditional_t
Definition conditional.hpp:21
constexpr auto is_tuple_like
Definition is_tuple_like.hpp:9
etl::make_integer_sequence< etl::size_t, Size > make_index_sequence
Definition index_sequence.hpp:15
etl::integer_sequence< etl::size_t, Ints... > index_sequence
Definition index_sequence.hpp:12
constexpr auto is_etl_array
Definition array.hpp:331
TETL_BUILTIN_PTRDIFF ptrdiff_t
etl::ptrdiff_t is the signed integer type of the result of subtracting two pointers.
Definition ptrdiff_t.hpp:14
auto unreachable() -> void
Definition unreachable.hpp:10
TETL_BUILTIN_SIZET size_t
etl::size_t is the unsigned integer type of the result of the sizeof operator.
Definition size_t.hpp:14
constexpr bool is_nothrow_swappable_v
Definition is_nothrow_swappable.hpp:19
A container that encapsulates fixed size arrays.
Definition array.hpp:48
Type & reference
Definition array.hpp:54
Type const * const_iterator
Definition array.hpp:57
constexpr auto fill(const_reference value) -> void
Assigns the given value value to all elements in the container.
Definition array.hpp:207
constexpr auto to_array(T(&&a)[N])
Definition array.hpp:323
Type const * const_pointer
Definition array.hpp:53
constexpr auto front() const noexcept -> const_reference
Accesses the first item.
Definition array.hpp:87
Type * pointer
Definition array.hpp:52
constexpr auto get(array< T, Size > &&array) noexcept -> T &&
Definition array.hpp:295
constexpr auto crbegin() const noexcept -> const_reverse_iterator
Returns a reverse iterator to the first element of the reversed array. It corresponds to the last ele...
Definition array.hpp:169
array(T, U...) -> array< T, 1+sizeof...(U)>
One deduction guide is provided for array to provide an equivalent of experimental::make_array for co...
constexpr auto rbegin() const noexcept -> const_reverse_iterator
Returns a reverse iterator to the first element of the reversed array. It corresponds to the last ele...
Definition array.hpp:161
constexpr auto get(array< T, Size > const &&array) noexcept -> T const &&
Definition array.hpp:303
friend constexpr auto operator<(array const &lhs, array const &rhs) -> bool
Compares the contents of lhs and rhs lexicographically. The comparison is performed by a function equ...
Definition array.hpp:230
typename etl::reverse_iterator< const_iterator > const_reverse_iterator
Definition array.hpp:59
constexpr auto operator[](size_type const pos) noexcept -> reference
Accesses the specified item with range checking.
Definition array.hpp:62
constexpr auto operator[](size_type const pos) const noexcept -> const_reference
Accesses the specified item with range checking.
Definition array.hpp:73
constexpr auto back() noexcept -> reference
Accesses the last item.
Definition array.hpp:90
TETL_NO_UNIQUE_ADDRESS conditional_t< Size==0, empty_c_array, c_array< callback_t, Size+size_t{Size==0}> > _buf
Definition array.hpp:243
constexpr auto rend() noexcept -> reverse_iterator
Returns a reverse iterator to the element following the last element of the reversed array....
Definition array.hpp:175
constexpr auto get(array< T, Size > &array) noexcept -> T &
Extracts the Ith element element from the array. I must be an integer value in range [0,...
Definition array.hpp:279
constexpr auto begin() noexcept -> iterator
Returns an iterator to the beginning.
Definition array.hpp:108
Type * iterator
Definition array.hpp:56
constexpr auto end() noexcept -> iterator
Returns an iterator to the end.
Definition array.hpp:131
constexpr auto begin() const noexcept -> const_iterator
Returns an iterator to the beginning.
Definition array.hpp:118
constexpr auto cbegin() const noexcept -> const_iterator
Returns an const iterator to the beginning.
Definition array.hpp:128
size_t size_type
Definition array.hpp:50
friend constexpr auto operator==(array const &lhs, array const &rhs) -> bool
Checks if the contents of lhs and rhs are equal, that is, they have the same number of elements and e...
Definition array.hpp:223
constexpr auto empty() const noexcept -> bool
Checks if the container has no elements, i.e. whether begin() == end().
Definition array.hpp:193
constexpr auto data() noexcept -> pointer
Returns pointer to the underlying array serving as element storage. The pointer is such that range [d...
Definition array.hpp:99
constexpr auto back() const noexcept -> const_reference
Accesses the last item.
Definition array.hpp:93
constexpr auto front() noexcept -> reference
Accesses the first item.
Definition array.hpp:84
friend constexpr auto operator>(array const &lhs, array const &rhs) -> bool
Definition array.hpp:237
friend constexpr auto swap(array &lhs, array &rhs) noexcept(noexcept(lhs.swap(rhs))) -> void
Specializes the swap algorithm for array. Swaps the contents of lhs and rhs.
Definition array.hpp:218
constexpr auto to_array(T(&a)[N]) -> array< remove_cv_t< T >, N >
Creates a array from the one dimensional built-in array a. The elements of the array are copy-initial...
Definition array.hpp:314
constexpr auto get(array< T, Size > const &array) noexcept -> T const &
Definition array.hpp:287
constexpr auto max_size() const noexcept -> size_type
Returns the maximum number of elements the container is able to hold due to system or library impleme...
Definition array.hpp:204
constexpr auto end() const noexcept -> const_iterator
Returns an iterator to the end.
Definition array.hpp:141
Type const & const_reference
Definition array.hpp:55
constexpr auto crend() const noexcept -> const_reverse_iterator
Returns a reverse iterator to the element following the last element of the reversed array....
Definition array.hpp:190
friend constexpr auto operator>=(array const &lhs, array const &rhs) -> bool
Definition array.hpp:239
friend constexpr auto operator<=(array const &lhs, array const &rhs) -> bool
Definition array.hpp:235
constexpr auto swap(array &other) noexcept(is_nothrow_swappable_v< Type >) -> void
Exchanges the contents of the container with those of other. Does not cause iterators and references ...
Definition array.hpp:212
constexpr auto data() const noexcept -> const_pointer
Returns pointer to the underlying array serving as element storage. The pointer is such that range [d...
Definition array.hpp:105
constexpr auto rend() const noexcept -> const_reverse_iterator
Returns a reverse iterator to the element following the last element of the reversed array....
Definition array.hpp:181
typename etl::reverse_iterator< iterator > reverse_iterator
Definition array.hpp:58
ptrdiff_t difference_type
Definition array.hpp:51
constexpr auto size() const noexcept -> size_type
Returns the number of elements in the container, i.e. distance(begin(), end()).
Definition array.hpp:196
constexpr auto cend() const noexcept -> const_iterator
Returns an const iterator to the end.
Definition array.hpp:151
Type value_type
Definition array.hpp:49
constexpr auto rbegin() noexcept -> reverse_iterator
Returns a reverse iterator to the first element of the reversed array. It corresponds to the last ele...
Definition array.hpp:156
Definition c_array.hpp:15
Definition integral_constant.hpp:9
reverse_iterator is an iterator adaptor that reverses the direction of a given iterator....
Definition reverse_iterator.hpp:22
T type
Definition array.hpp:271
Provides compile-time indexed access to the type of the elements of the array using tuple-like interf...
Definition array.hpp:267
Definition tuple_size.hpp:15