Debian Codegraph @

libcxx/include/__cxx03/array

raw
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef _LIBCPP___CXX03_ARRAY
11 #define _LIBCPP___CXX03_ARRAY
12
13 /*
14 array synopsis
15
16 namespace std
17 {
18 template <class T, size_t N >
19 struct array
20 {
21 // types:
22 typedef T & reference;
23 typedef const T & const_reference;
24 typedef implementation defined iterator;
25 typedef implementation defined const_iterator;
26 typedef size_t size_type;
27 typedef ptrdiff_t difference_type;
28 typedef T value_type;
29 typedef T* pointer;
30 typedef const T* const_pointer;
31 typedef std::reverse_iterator<iterator> reverse_iterator;
32 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
33
34 // No explicit construct/copy/destroy for aggregate type
35 void fill(const T& u); // constexpr in C++20
36 void swap(array& a) noexcept(is_nothrow_swappable_v<T>); // constexpr in C++20
37
38 // iterators:
39 iterator begin() noexcept; // constexpr in C++17
40 const_iterator begin() const noexcept; // constexpr in C++17
41 iterator end() noexcept; // constexpr in C++17
42 const_iterator end() const noexcept; // constexpr in C++17
43
44 reverse_iterator rbegin() noexcept; // constexpr in C++17
45 const_reverse_iterator rbegin() const noexcept; // constexpr in C++17
46 reverse_iterator rend() noexcept; // constexpr in C++17
47 const_reverse_iterator rend() const noexcept; // constexpr in C++17
48
49 const_iterator cbegin() const noexcept; // constexpr in C++17
50 const_iterator cend() const noexcept; // constexpr in C++17
51 const_reverse_iterator crbegin() const noexcept; // constexpr in C++17
52 const_reverse_iterator crend() const noexcept; // constexpr in C++17
53
54 // capacity:
55 constexpr size_type size() const noexcept;
56 constexpr size_type max_size() const noexcept;
57 constexpr bool empty() const noexcept;
58
59 // element access:
60 reference operator[](size_type n); // constexpr in C++17
61 const_reference operator[](size_type n) const; // constexpr in C++14
62 reference at(size_type n); // constexpr in C++17
63 const_reference at(size_type n) const; // constexpr in C++14
64
65 reference front(); // constexpr in C++17
66 const_reference front() const; // constexpr in C++14
67 reference back(); // constexpr in C++17
68 const_reference back() const; // constexpr in C++14
69
70 T* data() noexcept; // constexpr in C++17
71 const T* data() const noexcept; // constexpr in C++17
72 };
73
74 template <class T, class... U>
75 array(T, U...) -> array<T, 1 + sizeof...(U)>; // C++17
76
77 template <class T, size_t N>
78 bool operator==(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20
79 template <class T, size_t N>
80 bool operator!=(const array<T,N>& x, const array<T,N>& y); // removed in C++20
81 template <class T, size_t N>
82 bool operator<(const array<T,N>& x, const array<T,N>& y); // removed in C++20
83 template <class T, size_t N>
84 bool operator>(const array<T,N>& x, const array<T,N>& y); // removed in C++20
85 template <class T, size_t N>
86 bool operator<=(const array<T,N>& x, const array<T,N>& y); // removed in C++20
87 template <class T, size_t N>
88 bool operator>=(const array<T,N>& x, const array<T,N>& y); // removed in C++20
89 template<class T, size_t N>
90 constexpr synth-three-way-result<T>
91 operator<=>(const array<T, N>& x, const array<T, N>& y); // since C++20
92
93 template <class T, size_t N >
94 void swap(array<T,N>& x, array<T,N>& y) noexcept(noexcept(x.swap(y))); // constexpr in C++20
95
96 template <class T, size_t N>
97 constexpr array<remove_cv_t<T>, N> to_array(T (&a)[N]); // C++20
98 template <class T, size_t N>
99 constexpr array<remove_cv_t<T>, N> to_array(T (&&a)[N]); // C++20
100
101 template <class T> struct tuple_size;
102 template <size_t I, class T> struct tuple_element;
103 template <class T, size_t N> struct tuple_size<array<T, N>>;
104 template <size_t I, class T, size_t N> struct tuple_element<I, array<T, N>>;
105 template <size_t I, class T, size_t N> T& get(array<T, N>&) noexcept; // constexpr in C++14
106 template <size_t I, class T, size_t N> const T& get(const array<T, N>&) noexcept; // constexpr in C++14
107 template <size_t I, class T, size_t N> T&& get(array<T, N>&&) noexcept; // constexpr in C++14
108 template <size_t I, class T, size_t N> const T&& get(const array<T, N>&&) noexcept; // constexpr in C++14
109
110 } // std
111
112 */
113
114 #include <__cxx03/__algorithm/equal.h>
115 #include <__cxx03/__algorithm/fill_n.h>
116 #include <__cxx03/__algorithm/lexicographical_compare.h>
117 #include <__cxx03/__algorithm/swap_ranges.h>
118 #include <__cxx03/__assert>
119 #include <__cxx03/__config>
120 #include <__cxx03/__fwd/array.h>
121 #include <__cxx03/__iterator/reverse_iterator.h>
122 #include <__cxx03/__iterator/wrap_iter.h>
123 #include <__cxx03/__tuple/sfinae_helpers.h>
124 #include <__cxx03/__type_traits/conditional.h>
125 #include <__cxx03/__type_traits/conjunction.h>
126 #include <__cxx03/__type_traits/is_array.h>
127 #include <__cxx03/__type_traits/is_const.h>
128 #include <__cxx03/__type_traits/is_constructible.h>
129 #include <__cxx03/__type_traits/is_nothrow_constructible.h>
130 #include <__cxx03/__type_traits/is_same.h>
131 #include <__cxx03/__type_traits/is_swappable.h>
132 #include <__cxx03/__type_traits/is_trivially_relocatable.h>
133 #include <__cxx03/__type_traits/remove_cv.h>
134 #include <__cxx03/__utility/empty.h>
135 #include <__cxx03/__utility/integer_sequence.h>
136 #include <__cxx03/__utility/move.h>
137 #include <__cxx03/__utility/unreachable.h>
138 #include <__cxx03/stdexcept>
139 #include <__cxx03/version>
140
141 // standard-mandated includes
142
143 // [iterator.range]
144 #include <__cxx03/__iterator/access.h>
145
146 // [tuple.helper]
147 #include <__cxx03/__tuple/tuple_element.h>
148 #include <__cxx03/__tuple/tuple_size.h>
149
150 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
151 # pragma GCC system_header
152 #endif
153
154 _LIBCPP_PUSH_MACROS
155 #include <__cxx03/__undef_macros>
156
157 _LIBCPP_BEGIN_NAMESPACE_STD
158
159 template <class _Tp, size_t _Size>
160 struct _LIBCPP_TEMPLATE_VIS array {
161 using __trivially_relocatable = __conditional_t<__libcpp_is_trivially_relocatable<_Tp>::value, array, void>;
162
163 // types:
164 using __self = array;
165 using value_type = _Tp;
166 using reference = value_type&;
167 using const_reference = const value_type&;
168 using pointer = value_type*;
169 using const_pointer = const value_type*;
170 #if defined(_LIBCPP_ABI_USE_WRAP_ITER_IN_STD_ARRAY)
171 using iterator = __wrap_iter<pointer>;
172 using const_iterator = __wrap_iter<const_pointer>;
173 #else
174 using iterator = pointer;
175 using const_iterator = const_pointer;
176 #endif
177 using size_type = size_t;
178 using difference_type = ptrdiff_t;
179 using reverse_iterator = std::reverse_iterator<iterator>;
180 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
181
182 _Tp __elems_[_Size];
183
184 // No explicit construct/copy/destroy for aggregate type
185 _LIBCPP_HIDE_FROM_ABI void fill(const value_type& __u) { std::fill_n(data(), _Size, __u); }
186
187 _LIBCPP_HIDE_FROM_ABI void swap(array& __a) { std::swap_ranges(data(), data() + _Size, __a.data()); }
188
189 // iterators:
190 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return iterator(data()); }
191 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return const_iterator(data()); }
192 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return iterator(data() + _Size); }
193 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return const_iterator(data() + _Size); }
194
195 _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT { return reverse_iterator(end()); }
196 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(end()); }
197 _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT { return reverse_iterator(begin()); }
198 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(begin()); }
199
200 _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT { return begin(); }
201 _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT { return end(); }
202 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT { return rbegin(); }
203 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT { return rend(); }
204
205 // capacity:
206 _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return _Size; }
207 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT { return _Size; }
208 _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return _Size == 0; }
209
210 // element access:
211 _LIBCPP_HIDE_FROM_ABI reference operator[](size_type __n) _NOEXCEPT {
212 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n < _Size, "out-of-bounds access in std::array<T, N>");
213 return __elems_[__n];
214 }
215 _LIBCPP_HIDE_FROM_ABI const_reference operator[](size_type __n) const _NOEXCEPT {
216 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n < _Size, "out-of-bounds access in std::array<T, N>");
217 return __elems_[__n];
218 }
219
220 _LIBCPP_HIDE_FROM_ABI reference at(size_type __n) {
221 if (__n >= _Size)
222 __throw_out_of_range("array::at");
223 return __elems_[__n];
224 }
225
226 _LIBCPP_HIDE_FROM_ABI const_reference at(size_type __n) const {
227 if (__n >= _Size)
228 __throw_out_of_range("array::at");
229 return __elems_[__n];
230 }
231
232 _LIBCPP_HIDE_FROM_ABI reference front() _NOEXCEPT { return (*this)[0]; }
233 _LIBCPP_HIDE_FROM_ABI const_reference front() const _NOEXCEPT { return (*this)[0]; }
234 _LIBCPP_HIDE_FROM_ABI reference back() _NOEXCEPT { return (*this)[_Size - 1]; }
235 _LIBCPP_HIDE_FROM_ABI const_reference back() const _NOEXCEPT { return (*this)[_Size - 1]; }
236
237 _LIBCPP_HIDE_FROM_ABI value_type* data() _NOEXCEPT { return __elems_; }
238 _LIBCPP_HIDE_FROM_ABI const value_type* data() const _NOEXCEPT { return __elems_; }
239 };
240
241 template <class _Tp>
242 struct _LIBCPP_TEMPLATE_VIS array<_Tp, 0> {
243 // types:
244 typedef array __self;
245 typedef _Tp value_type;
246 typedef value_type& reference;
247 typedef const value_type& const_reference;
248 typedef value_type* iterator;
249 typedef const value_type* const_iterator;
250 typedef value_type* pointer;
251 typedef const value_type* const_pointer;
252 typedef size_t size_type;
253 typedef ptrdiff_t difference_type;
254 typedef std::reverse_iterator<iterator> reverse_iterator;
255 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
256
257 typedef __conditional_t<is_const<_Tp>::value, const __empty, __empty> _EmptyType;
258
259 struct _ArrayInStructT {
260 _Tp __data_[1];
261 };
262 _ALIGNAS_TYPE(_ArrayInStructT) _EmptyType __elems_[sizeof(_ArrayInStructT)];
263
264 _LIBCPP_HIDE_FROM_ABI value_type* data() _NOEXCEPT { return nullptr; }
265 _LIBCPP_HIDE_FROM_ABI const value_type* data() const _NOEXCEPT { return nullptr; }
266
267 // No explicit construct/copy/destroy for aggregate type
268 _LIBCPP_HIDE_FROM_ABI void fill(const value_type&) {
269 static_assert(!is_const<_Tp>::value, "cannot fill zero-sized array of type 'const T'");
270 }
271
272 _LIBCPP_HIDE_FROM_ABI void swap(array&) _NOEXCEPT {
273 static_assert(!is_const<_Tp>::value, "cannot swap zero-sized array of type 'const T'");
274 }
275
276 // iterators:
277 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return iterator(data()); }
278 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return const_iterator(data()); }
279 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return iterator(data()); }
280 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return const_iterator(data()); }
281
282 _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT { return reverse_iterator(end()); }
283 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(end()); }
284 _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT { return reverse_iterator(begin()); }
285 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(begin()); }
286
287 _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT { return begin(); }
288 _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT { return end(); }
289 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT { return rbegin(); }
290 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT { return rend(); }
291
292 // capacity:
293 _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return 0; }
294 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT { return 0; }
295 _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT { return true; }
296
297 // element access:
298 _LIBCPP_HIDE_FROM_ABI reference operator[](size_type) _NOEXCEPT {
299 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
300 __libcpp_unreachable();
301 }
302
303 _LIBCPP_HIDE_FROM_ABI const_reference operator[](size_type) const _NOEXCEPT {
304 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
305 __libcpp_unreachable();
306 }
307
308 _LIBCPP_HIDE_FROM_ABI reference at(size_type) {
309 __throw_out_of_range("array<T, 0>::at");
310 __libcpp_unreachable();
311 }
312
313 _LIBCPP_HIDE_FROM_ABI const_reference at(size_type) const {
314 __throw_out_of_range("array<T, 0>::at");
315 __libcpp_unreachable();
316 }
317
318 _LIBCPP_HIDE_FROM_ABI reference front() _NOEXCEPT {
319 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(false, "cannot call array<T, 0>::front() on a zero-sized array");
320 __libcpp_unreachable();
321 }
322
323 _LIBCPP_HIDE_FROM_ABI const_reference front() const _NOEXCEPT {
324 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(false, "cannot call array<T, 0>::front() on a zero-sized array");
325 __libcpp_unreachable();
326 }
327
328 _LIBCPP_HIDE_FROM_ABI reference back() _NOEXCEPT {
329 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(false, "cannot call array<T, 0>::back() on a zero-sized array");
330 __libcpp_unreachable();
331 }
332
333 _LIBCPP_HIDE_FROM_ABI const_reference back() const _NOEXCEPT {
334 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(false, "cannot call array<T, 0>::back() on a zero-sized array");
335 __libcpp_unreachable();
336 }
337 };
338
339 template <class _Tp, size_t _Size>
340 inline _LIBCPP_HIDE_FROM_ABI bool operator==(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) {
341 return std::equal(__x.begin(), __x.end(), __y.begin());
342 }
343
344 template <class _Tp, size_t _Size>
345 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) {
346 return !(__x == __y);
347 }
348
349 template <class _Tp, size_t _Size>
350 inline _LIBCPP_HIDE_FROM_ABI bool operator<(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) {
351 return std::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
352 }
353
354 template <class _Tp, size_t _Size>
355 inline _LIBCPP_HIDE_FROM_ABI bool operator>(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) {
356 return __y < __x;
357 }
358
359 template <class _Tp, size_t _Size>
360 inline _LIBCPP_HIDE_FROM_ABI bool operator<=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) {
361 return !(__y < __x);
362 }
363
364 template <class _Tp, size_t _Size>
365 inline _LIBCPP_HIDE_FROM_ABI bool operator>=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) {
366 return !(__x < __y);
367 }
368
369 template <class _Tp, size_t _Size, __enable_if_t<_Size == 0 || __is_swappable_v<_Tp>, int> = 0>
370 inline _LIBCPP_HIDE_FROM_ABI void swap(array<_Tp, _Size>& __x, array<_Tp, _Size>& __y) {
371 __x.swap(__y);
372 }
373
374 template <class _Tp, size_t _Size>
375 struct _LIBCPP_TEMPLATE_VIS tuple_size<array<_Tp, _Size> > : public integral_constant<size_t, _Size> {};
376
377 template <size_t _Ip, class _Tp, size_t _Size>
378 struct _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, array<_Tp, _Size> > {
379 static_assert(_Ip < _Size, "Index out of bounds in std::tuple_element<> (std::array)");
380 typedef _Tp type;
381 };
382
383 template <size_t _Ip, class _Tp, size_t _Size>
384 inline _LIBCPP_HIDE_FROM_ABI _Tp& get(array<_Tp, _Size>& __a) _NOEXCEPT {
385 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array)");
386 return __a.__elems_[_Ip];
387 }
388
389 template <size_t _Ip, class _Tp, size_t _Size>
390 inline _LIBCPP_HIDE_FROM_ABI const _Tp& get(const array<_Tp, _Size>& __a) _NOEXCEPT {
391 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array)");
392 return __a.__elems_[_Ip];
393 }
394
395 template <size_t _Ip, class _Tp, size_t _Size>
396 inline _LIBCPP_HIDE_FROM_ABI _Tp&& get(array<_Tp, _Size>&& __a) _NOEXCEPT {
397 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array &&)");
398 return std::move(__a.__elems_[_Ip]);
399 }
400
401 template <size_t _Ip, class _Tp, size_t _Size>
402 inline _LIBCPP_HIDE_FROM_ABI const _Tp&& get(const array<_Tp, _Size>&& __a) _NOEXCEPT {
403 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array &&)");
404 return std::move(__a.__elems_[_Ip]);
405 }
406
407 _LIBCPP_END_NAMESPACE_STD
408
409 _LIBCPP_POP_MACROS
410
411 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES)
412 # include <__cxx03/algorithm>
413 # include <__cxx03/cstdlib>
414 # include <__cxx03/iterator>
415 # include <__cxx03/type_traits>
416 # include <__cxx03/utility>
417 #endif
418
419 #endif // _LIBCPP___CXX03_ARRAY