]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/std/array
Implement basic block path solver.
[thirdparty/gcc.git] / libstdc++-v3 / include / std / array
CommitLineData
af13a7a6
BK
1// <array> -*- C++ -*-
2
99dee823 3// Copyright (C) 2007-2021 Free Software Foundation, Inc.
af13a7a6
BK
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
748086b7 8// Free Software Foundation; either version 3, or (at your option)
af13a7a6
BK
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
748086b7
JJ
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
af13a7a6 19
748086b7
JJ
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
af13a7a6
BK
24
25/** @file include/array
26 * This is a Standard C++ Library header.
27 */
28
4514bed6
BK
29#ifndef _GLIBCXX_ARRAY
30#define _GLIBCXX_ARRAY 1
af13a7a6
BK
31
32#pragma GCC system_header
33
734f5023 34#if __cplusplus < 201103L
ab65a4c7 35# include <bits/c++0x_warning.h>
57317d2a 36#else
af13a7a6 37
7d17de7f 38#include <utility>
beb0086f 39#include <bits/functexcept.h>
e133ace8 40#include <bits/stl_algobase.h>
f67a9881 41#include <bits/range_access.h>
6db08247 42#include <debug/assertions.h>
e133ace8 43
12ffa228
BK
44namespace std _GLIBCXX_VISIBILITY(default)
45{
6db08247 46_GLIBCXX_BEGIN_NAMESPACE_VERSION
53dc5044 47
90f9c94e
PC
48 template<typename _Tp, std::size_t _Nm>
49 struct __array_traits
50 {
51 typedef _Tp _Type[_Nm];
6b9539e2
DK
52 typedef __is_swappable<_Tp> _Is_swappable;
53 typedef __is_nothrow_swappable<_Tp> _Is_nothrow_swappable;
90f9c94e
PC
54
55 static constexpr _Tp&
56 _S_ref(const _Type& __t, std::size_t __n) noexcept
57 { return const_cast<_Tp&>(__t[__n]); }
6a344b95
JW
58
59 static constexpr _Tp*
60 _S_ptr(const _Type& __t) noexcept
61 { return const_cast<_Tp*>(__t); }
90f9c94e
PC
62 };
63
64 template<typename _Tp>
65 struct __array_traits<_Tp, 0>
66 {
67 struct _Type { };
6b9539e2
DK
68 typedef true_type _Is_swappable;
69 typedef true_type _Is_nothrow_swappable;
90f9c94e
PC
70
71 static constexpr _Tp&
72 _S_ref(const _Type&, std::size_t) noexcept
73 { return *static_cast<_Tp*>(nullptr); }
6a344b95
JW
74
75 static constexpr _Tp*
76 _S_ptr(const _Type&) noexcept
77 { return nullptr; }
90f9c94e
PC
78 };
79
53dc5044
PC
80 /**
81 * @brief A standard container for storing a fixed size sequence of elements.
82 *
83 * @ingroup sequences
84 *
85 * Meets the requirements of a <a href="tables.html#65">container</a>, a
86 * <a href="tables.html#66">reversible container</a>, and a
87 * <a href="tables.html#67">sequence</a>.
88 *
89 * Sets support random access iterators.
90 *
d632488a 91 * @tparam Tp Type of element. Required to be a complete type.
cb0de9b6 92 * @tparam Nm Number of elements.
53dc5044
PC
93 */
94 template<typename _Tp, std::size_t _Nm>
95 struct array
96 {
97 typedef _Tp value_type;
a7d0c94e
BK
98 typedef value_type* pointer;
99 typedef const value_type* const_pointer;
53dc5044
PC
100 typedef value_type& reference;
101 typedef const value_type& const_reference;
102 typedef value_type* iterator;
103 typedef const value_type* const_iterator;
104 typedef std::size_t size_type;
105 typedef std::ptrdiff_t difference_type;
106 typedef std::reverse_iterator<iterator> reverse_iterator;
107 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
108
109 // Support for zero-sized arrays mandatory.
6db08247 110 typedef __array_traits<_Tp, _Nm> _AT_Type;
0611ce44 111 typename _AT_Type::_Type _M_elems;
53dc5044
PC
112
113 // No explicit construct/copy/destroy for aggregate type.
114
115 // DR 776.
1c09b664 116 _GLIBCXX20_CONSTEXPR void
53dc5044
PC
117 fill(const value_type& __u)
118 { std::fill_n(begin(), size(), __u); }
119
1c09b664 120 _GLIBCXX20_CONSTEXPR void
53dc5044 121 swap(array& __other)
6b9539e2 122 noexcept(_AT_Type::_Is_nothrow_swappable::value)
53dc5044
PC
123 { std::swap_ranges(begin(), end(), __other.begin()); }
124
125 // Iterators.
06db9920 126 _GLIBCXX17_CONSTEXPR iterator
18eeaec4 127 begin() noexcept
a7d0c94e 128 { return iterator(data()); }
53dc5044 129
06db9920 130 _GLIBCXX17_CONSTEXPR const_iterator
18eeaec4 131 begin() const noexcept
a7d0c94e 132 { return const_iterator(data()); }
53dc5044 133
06db9920 134 _GLIBCXX17_CONSTEXPR iterator
18eeaec4 135 end() noexcept
a7d0c94e 136 { return iterator(data() + _Nm); }
53dc5044 137
06db9920 138 _GLIBCXX17_CONSTEXPR const_iterator
18eeaec4 139 end() const noexcept
a7d0c94e 140 { return const_iterator(data() + _Nm); }
53dc5044 141
06db9920 142 _GLIBCXX17_CONSTEXPR reverse_iterator
18eeaec4 143 rbegin() noexcept
53dc5044
PC
144 { return reverse_iterator(end()); }
145
06db9920 146 _GLIBCXX17_CONSTEXPR const_reverse_iterator
18eeaec4 147 rbegin() const noexcept
53dc5044
PC
148 { return const_reverse_iterator(end()); }
149
06db9920 150 _GLIBCXX17_CONSTEXPR reverse_iterator
18eeaec4 151 rend() noexcept
53dc5044
PC
152 { return reverse_iterator(begin()); }
153
06db9920 154 _GLIBCXX17_CONSTEXPR const_reverse_iterator
18eeaec4 155 rend() const noexcept
53dc5044
PC
156 { return const_reverse_iterator(begin()); }
157
06db9920 158 _GLIBCXX17_CONSTEXPR const_iterator
18eeaec4 159 cbegin() const noexcept
90f9c94e 160 { return const_iterator(data()); }
53dc5044 161
06db9920 162 _GLIBCXX17_CONSTEXPR const_iterator
18eeaec4 163 cend() const noexcept
90f9c94e 164 { return const_iterator(data() + _Nm); }
53dc5044 165
06db9920 166 _GLIBCXX17_CONSTEXPR const_reverse_iterator
18eeaec4 167 crbegin() const noexcept
53dc5044
PC
168 { return const_reverse_iterator(end()); }
169
06db9920 170 _GLIBCXX17_CONSTEXPR const_reverse_iterator
18eeaec4 171 crend() const noexcept
53dc5044
PC
172 { return const_reverse_iterator(begin()); }
173
174 // Capacity.
33ac58d5 175 constexpr size_type
18eeaec4 176 size() const noexcept { return _Nm; }
53dc5044 177
33ac58d5 178 constexpr size_type
18eeaec4 179 max_size() const noexcept { return _Nm; }
53dc5044 180
d715f554 181 _GLIBCXX_NODISCARD constexpr bool
18eeaec4 182 empty() const noexcept { return size() == 0; }
53dc5044
PC
183
184 // Element access.
06db9920 185 _GLIBCXX17_CONSTEXPR reference
b4efa80e 186 operator[](size_type __n) noexcept
6db08247
FD
187 {
188 __glibcxx_requires_subscript(__n);
189 return _AT_Type::_S_ref(_M_elems, __n);
190 }
53dc5044 191
bfef3a71
BK
192 constexpr const_reference
193 operator[](size_type __n) const noexcept
6db08247 194 {
91cfacc4 195#if __cplusplus >= 201402L
6db08247 196 __glibcxx_requires_subscript(__n);
91cfacc4 197#endif
6db08247
FD
198 return _AT_Type::_S_ref(_M_elems, __n);
199 }
53dc5044 200
06db9920 201 _GLIBCXX17_CONSTEXPR reference
53dc5044
PC
202 at(size_type __n)
203 {
204 if (__n >= _Nm)
9779c871
PP
205 std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
206 ">= _Nm (which is %zu)"),
207 __n, _Nm);
90f9c94e 208 return _AT_Type::_S_ref(_M_elems, __n);
53dc5044
PC
209 }
210
bfef3a71 211 constexpr const_reference
53dc5044
PC
212 at(size_type __n) const
213 {
885e8121
JW
214 // Result of conditional expression must be an lvalue so use
215 // boolean ? lvalue : (throw-expr, lvalue)
90f9c94e 216 return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n)
9779c871
PP
217 : (std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
218 ">= _Nm (which is %zu)"),
219 __n, _Nm),
90f9c94e 220 _AT_Type::_S_ref(_M_elems, 0));
9bc13c23 221 }
53dc5044 222
06db9920 223 _GLIBCXX17_CONSTEXPR reference
b4efa80e 224 front() noexcept
6db08247
FD
225 {
226 __glibcxx_requires_nonempty();
227 return *begin();
228 }
53dc5044 229
33ac58d5 230 constexpr const_reference
b4efa80e 231 front() const noexcept
6db08247 232 {
91cfacc4 233#if __cplusplus >= 201402L
6db08247 234 __glibcxx_requires_nonempty();
91cfacc4 235#endif
6db08247
FD
236 return _AT_Type::_S_ref(_M_elems, 0);
237 }
53dc5044 238
06db9920 239 _GLIBCXX17_CONSTEXPR reference
b4efa80e 240 back() noexcept
6db08247
FD
241 {
242 __glibcxx_requires_nonempty();
243 return _Nm ? *(end() - 1) : *end();
244 }
53dc5044 245
33ac58d5 246 constexpr const_reference
b4efa80e 247 back() const noexcept
33ac58d5 248 {
91cfacc4 249#if __cplusplus >= 201402L
6db08247 250 __glibcxx_requires_nonempty();
91cfacc4 251#endif
33ac58d5 252 return _Nm ? _AT_Type::_S_ref(_M_elems, _Nm - 1)
0611ce44 253 : _AT_Type::_S_ref(_M_elems, 0);
a9ba8ba5 254 }
53dc5044 255
06db9920 256 _GLIBCXX17_CONSTEXPR pointer
18eeaec4 257 data() noexcept
6a344b95 258 { return _AT_Type::_S_ptr(_M_elems); }
53dc5044 259
06db9920 260 _GLIBCXX17_CONSTEXPR const_pointer
18eeaec4 261 data() const noexcept
6a344b95 262 { return _AT_Type::_S_ptr(_M_elems); }
53dc5044
PC
263 };
264
af181c91
JW
265#if __cpp_deduction_guides >= 201606
266 template<typename _Tp, typename... _Up>
267 array(_Tp, _Up...)
268 -> array<enable_if_t<(is_same_v<_Tp, _Up> && ...), _Tp>,
269 1 + sizeof...(_Up)>;
270#endif
271
53dc5044
PC
272 // Array comparisons.
273 template<typename _Tp, std::size_t _Nm>
3a66e68a 274 _GLIBCXX20_CONSTEXPR
33ac58d5 275 inline bool
53dc5044
PC
276 operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
277 { return std::equal(__one.begin(), __one.end(), __two.begin()); }
278
3a4cc628
JW
279#if __cpp_lib_three_way_comparison && __cpp_lib_concepts
280 template<typename _Tp, size_t _Nm>
281 constexpr __detail::__synth3way_t<_Tp>
282 operator<=>(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
283 {
2f983fa6
JW
284#ifdef __cpp_lib_is_constant_evaluated
285 if constexpr (_Nm && __is_memcmp_ordered<_Tp>::__value)
286 if (!std::is_constant_evaluated())
287 {
288 constexpr size_t __n = _Nm * sizeof(_Tp);
289 return __builtin_memcmp(__a.data(), __b.data(), __n) <=> 0;
290 }
291#endif
292
293 for (size_t __i = 0; __i < _Nm; ++__i)
3a4cc628 294 {
2f983fa6
JW
295 auto __c = __detail::__synth3way(__a[__i], __b[__i]);
296 if (__c != 0)
297 return __c;
3a4cc628
JW
298 }
299 return strong_ordering::equal;
300 }
301#else
53dc5044 302 template<typename _Tp, std::size_t _Nm>
3a66e68a 303 _GLIBCXX20_CONSTEXPR
53dc5044
PC
304 inline bool
305 operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
306 { return !(__one == __two); }
307
308 template<typename _Tp, std::size_t _Nm>
3a66e68a 309 _GLIBCXX20_CONSTEXPR
53dc5044
PC
310 inline bool
311 operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
33ac58d5 312 {
53dc5044 313 return std::lexicographical_compare(__a.begin(), __a.end(),
33ac58d5 314 __b.begin(), __b.end());
53dc5044
PC
315 }
316
317 template<typename _Tp, std::size_t _Nm>
3a66e68a 318 _GLIBCXX20_CONSTEXPR
53dc5044
PC
319 inline bool
320 operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
321 { return __two < __one; }
322
323 template<typename _Tp, std::size_t _Nm>
3a66e68a 324 _GLIBCXX20_CONSTEXPR
53dc5044
PC
325 inline bool
326 operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
327 { return !(__one > __two); }
328
329 template<typename _Tp, std::size_t _Nm>
3a66e68a 330 _GLIBCXX20_CONSTEXPR
53dc5044
PC
331 inline bool
332 operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
333 { return !(__one < __two); }
3a4cc628 334#endif // three_way_comparison && concepts
53dc5044 335
18eeaec4 336 // Specialized algorithms.
53dc5044 337 template<typename _Tp, std::size_t _Nm>
1c09b664 338 _GLIBCXX20_CONSTEXPR
6b9539e2
DK
339 inline
340#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
341 // Constrained free swap overload, see p0185r1
342 typename enable_if<
6db08247 343 __array_traits<_Tp, _Nm>::_Is_swappable::value
6b9539e2
DK
344 >::type
345#else
346 void
347#endif
53dc5044 348 swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
18eeaec4 349 noexcept(noexcept(__one.swap(__two)))
53dc5044
PC
350 { __one.swap(__two); }
351
a2863bde
VV
352#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
353 template<typename _Tp, std::size_t _Nm>
a2863bde 354 typename enable_if<
6db08247 355 !__array_traits<_Tp, _Nm>::_Is_swappable::value>::type
a2863bde
VV
356 swap(array<_Tp, _Nm>&, array<_Tp, _Nm>&) = delete;
357#endif
358
53dc5044 359 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
9480716c 360 constexpr _Tp&
18eeaec4 361 get(array<_Tp, _Nm>& __arr) noexcept
9771644a 362 {
3c040fa4 363 static_assert(_Int < _Nm, "array index is within bounds");
6db08247 364 return __array_traits<_Tp, _Nm>::_S_ref(__arr._M_elems, _Int);
9771644a 365 }
53dc5044 366
18eeaec4 367 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
9480716c 368 constexpr _Tp&&
18eeaec4 369 get(array<_Tp, _Nm>&& __arr) noexcept
9771644a 370 {
3c040fa4 371 static_assert(_Int < _Nm, "array index is within bounds");
6db08247 372 return std::move(std::get<_Int>(__arr));
9771644a 373 }
18eeaec4 374
53dc5044 375 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
9480716c 376 constexpr const _Tp&
18eeaec4 377 get(const array<_Tp, _Nm>& __arr) noexcept
9771644a 378 {
3c040fa4 379 static_assert(_Int < _Nm, "array index is within bounds");
6db08247 380 return __array_traits<_Tp, _Nm>::_S_ref(__arr._M_elems, _Int);
9771644a 381 }
53dc5044 382
ccbbf8df
VV
383 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
384 constexpr const _Tp&&
385 get(const array<_Tp, _Nm>&& __arr) noexcept
386 {
387 static_assert(_Int < _Nm, "array index is within bounds");
6db08247 388 return std::move(std::get<_Int>(__arr));
ccbbf8df
VV
389 }
390
cb0de9b6
JW
391#if __cplusplus > 201703L
392#define __cpp_lib_to_array 201907L
393
394 template<bool _Move = false, typename _Tp, size_t... _Idx>
395 constexpr array<remove_cv_t<_Tp>, sizeof...(_Idx)>
396 __to_array(_Tp (&__a)[sizeof...(_Idx)], index_sequence<_Idx...>)
397 {
398 if constexpr (_Move)
399 return {{std::move(__a[_Idx])...}};
400 else
401 return {{__a[_Idx]...}};
402 }
403
404 template<typename _Tp, size_t _Nm>
405 constexpr array<remove_cv_t<_Tp>, _Nm>
406 to_array(_Tp (&__a)[_Nm])
407 noexcept(is_nothrow_constructible_v<_Tp, _Tp&>)
408 {
409 static_assert(!is_array_v<_Tp>);
410 static_assert(is_constructible_v<_Tp, _Tp&>);
411 if constexpr (is_constructible_v<_Tp, _Tp&>)
6db08247 412 return __to_array(__a, make_index_sequence<_Nm>{});
cb0de9b6
JW
413 __builtin_unreachable(); // FIXME: see PR c++/91388
414 }
415
416 template<typename _Tp, size_t _Nm>
417 constexpr array<remove_cv_t<_Tp>, _Nm>
418 to_array(_Tp (&&__a)[_Nm])
419 noexcept(is_nothrow_move_constructible_v<_Tp>)
420 {
421 static_assert(!is_array_v<_Tp>);
422 static_assert(is_move_constructible_v<_Tp>);
423 if constexpr (is_move_constructible_v<_Tp>)
6db08247 424 return __to_array<1>(__a, make_index_sequence<_Nm>{});
cb0de9b6
JW
425 __builtin_unreachable(); // FIXME: see PR c++/91388
426 }
427#endif // C++20
428
741aacaf
PC
429 // Tuple interface to class template array.
430
431 /// tuple_size
33ac58d5 432 template<typename _Tp>
30e0aed1 433 struct tuple_size;
741aacaf 434
3a004764 435 /// Partial specialization for std::array
741aacaf 436 template<typename _Tp, std::size_t _Nm>
6db08247 437 struct tuple_size<array<_Tp, _Nm>>
741aacaf
PC
438 : public integral_constant<std::size_t, _Nm> { };
439
440 /// tuple_element
441 template<std::size_t _Int, typename _Tp>
30e0aed1 442 struct tuple_element;
741aacaf 443
3a004764 444 /// Partial specialization for std::array
741aacaf 445 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
6db08247 446 struct tuple_element<_Int, array<_Tp, _Nm>>
741aacaf
PC
447 {
448 static_assert(_Int < _Nm, "index is out of bounds");
449 typedef _Tp type;
450 };
451
7d17de7f 452 template<typename _Tp, std::size_t _Nm>
6db08247 453 struct __is_tuple_like_impl<array<_Tp, _Nm>> : true_type
7d17de7f
FD
454 { };
455
741aacaf
PC
456_GLIBCXX_END_NAMESPACE_VERSION
457} // namespace std
458
734f5023 459#endif // C++11
57317d2a 460
4514bed6 461#endif // _GLIBCXX_ARRAY