2 //===---------------------------- array -----------------------------------===//
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
8 //===----------------------------------------------------------------------===//
18 template <class T, size_t N >
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;
30 typedef const T* const_pointer;
31 typedef std::reverse_iterator<iterator> reverse_iterator;
32 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
34 // No explicit construct/copy/destroy for aggregate type
35 void fill(const T& u);
36 void swap(array& a) noexcept(is_nothrow_swappable_v<T>);
39 iterator begin() noexcept;
40 const_iterator begin() const noexcept;
41 iterator end() noexcept;
42 const_iterator end() const noexcept;
44 reverse_iterator rbegin() noexcept;
45 const_reverse_iterator rbegin() const noexcept;
46 reverse_iterator rend() noexcept;
47 const_reverse_iterator rend() const noexcept;
49 const_iterator cbegin() const noexcept;
50 const_iterator cend() const noexcept;
51 const_reverse_iterator crbegin() const noexcept;
52 const_reverse_iterator crend() const noexcept;
55 constexpr size_type size() const noexcept;
56 constexpr size_type max_size() const noexcept;
57 constexpr bool empty() const noexcept;
60 reference operator[](size_type n);
61 const_reference operator[](size_type n) const; // constexpr in C++14
62 const_reference at(size_type n) const; // constexpr in C++14
63 reference at(size_type n);
66 const_reference front() const; // constexpr in C++14
68 const_reference back() const; // constexpr in C++14
71 const T* data() const noexcept;
74 template <class T, class... U>
75 array(T, U...) -> array<T, 1 + sizeof...(U)>;
77 template <class T, size_t N>
78 bool operator==(const array<T,N>& x, const array<T,N>& y);
79 template <class T, size_t N>
80 bool operator!=(const array<T,N>& x, const array<T,N>& y);
81 template <class T, size_t N>
82 bool operator<(const array<T,N>& x, const array<T,N>& y);
83 template <class T, size_t N>
84 bool operator>(const array<T,N>& x, const array<T,N>& y);
85 template <class T, size_t N>
86 bool operator<=(const array<T,N>& x, const array<T,N>& y);
87 template <class T, size_t N>
88 bool operator>=(const array<T,N>& x, const array<T,N>& y);
90 template <class T, size_t N >
91 void swap(array<T,N>& x, array<T,N>& y) noexcept(noexcept(x.swap(y))); // C++17
93 template <class T> struct tuple_size;
94 template <size_t I, class T> struct tuple_element;
95 template <class T, size_t N> struct tuple_size<array<T, N>>;
96 template <size_t I, class T, size_t N> struct tuple_element<I, array<T, N>>;
97 template <size_t I, class T, size_t N> T& get(array<T, N>&) noexcept; // constexpr in C++14
98 template <size_t I, class T, size_t N> const T& get(const array<T, N>&) noexcept; // constexpr in C++14
99 template <size_t I, class T, size_t N> T&& get(array<T, N>&&) noexcept; // constexpr in C++14
100 template <size_t I, class T, size_t N> const T&& get(const array<T, N>&&) noexcept; // constexpr in C++14
108 #include <type_traits>
113 #include <cstdlib> // for _LIBCPP_UNREACHABLE
117 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
118 #pragma GCC system_header
123 _LIBCPP_BEGIN_NAMESPACE_STD
126 template <class _Tp, size_t _Size>
127 struct _LIBCPP_TEMPLATE_VIS array
130 typedef array __self;
131 typedef _Tp value_type;
132 typedef value_type& reference;
133 typedef const value_type& const_reference;
134 typedef value_type* iterator;
135 typedef const value_type* const_iterator;
136 typedef value_type* pointer;
137 typedef const value_type* const_pointer;
138 typedef size_t size_type;
139 typedef ptrdiff_t difference_type;
140 typedef std::reverse_iterator<iterator> reverse_iterator;
141 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
145 // No explicit construct/copy/destroy for aggregate type
146 _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __u) {
147 _VSTD::fill_n(__elems_, _Size, __u);
150 _LIBCPP_INLINE_VISIBILITY
151 void swap(array& __a) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) {
152 std::swap_ranges(__elems_, __elems_ + _Size, __a.__elems_);
156 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
157 iterator begin() _NOEXCEPT {return iterator(data());}
158 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
159 const_iterator begin() const _NOEXCEPT {return const_iterator(data());}
160 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
161 iterator end() _NOEXCEPT {return iterator(data() + _Size);}
162 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
163 const_iterator end() const _NOEXCEPT {return const_iterator(data() + _Size);}
165 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
166 reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
167 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
168 const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());}
169 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
170 reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
171 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
172 const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());}
174 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
175 const_iterator cbegin() const _NOEXCEPT {return begin();}
176 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
177 const_iterator cend() const _NOEXCEPT {return end();}
178 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
179 const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
180 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
181 const_reverse_iterator crend() const _NOEXCEPT {return rend();}
184 _LIBCPP_INLINE_VISIBILITY
185 _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return _Size;}
186 _LIBCPP_INLINE_VISIBILITY
187 _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return _Size;}
188 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
189 _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return false; }
192 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
193 reference operator[](size_type __n) _NOEXCEPT {return __elems_[__n];}
194 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
195 const_reference operator[](size_type __n) const _NOEXCEPT {return __elems_[__n];}
197 _LIBCPP_CONSTEXPR_AFTER_CXX14 reference at(size_type __n);
198 _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference at(size_type __n) const;
200 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference front() _NOEXCEPT {return __elems_[0];}
201 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference front() const _NOEXCEPT {return __elems_[0];}
202 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference back() _NOEXCEPT {return __elems_[_Size - 1];}
203 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference back() const _NOEXCEPT {return __elems_[_Size - 1];}
205 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
206 value_type* data() _NOEXCEPT {return __elems_;}
207 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
208 const value_type* data() const _NOEXCEPT {return __elems_;}
212 template <class _Tp, size_t _Size>
213 _LIBCPP_CONSTEXPR_AFTER_CXX14
214 typename array<_Tp, _Size>::reference
215 array<_Tp, _Size>::at(size_type __n)
218 __throw_out_of_range("array::at");
220 return __elems_[__n];
223 template <class _Tp, size_t _Size>
224 _LIBCPP_CONSTEXPR_AFTER_CXX11
225 typename array<_Tp, _Size>::const_reference
226 array<_Tp, _Size>::at(size_type __n) const
229 __throw_out_of_range("array::at");
230 return __elems_[__n];
234 struct _LIBCPP_TEMPLATE_VIS array<_Tp, 0>
237 typedef array __self;
238 typedef _Tp value_type;
239 typedef value_type& reference;
240 typedef const value_type& const_reference;
241 typedef value_type* iterator;
242 typedef const value_type* const_iterator;
243 typedef value_type* pointer;
244 typedef const value_type* const_pointer;
245 typedef size_t size_type;
246 typedef ptrdiff_t difference_type;
247 typedef std::reverse_iterator<iterator> reverse_iterator;
248 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
250 typedef typename conditional<is_const<_Tp>::value, const char,
251 char>::type _CharType;
253 struct _ArrayInStructT { _Tp __data_[1]; };
254 _ALIGNAS_TYPE(_ArrayInStructT) _CharType __elems_[sizeof(_ArrayInStructT)];
256 // No explicit construct/copy/destroy for aggregate type
257 _LIBCPP_INLINE_VISIBILITY void fill(const value_type&) {
258 static_assert(!is_const<_Tp>::value,
259 "cannot fill zero-sized array of type 'const T'");
262 _LIBCPP_INLINE_VISIBILITY
263 void swap(array&) _NOEXCEPT {
264 static_assert(!is_const<_Tp>::value,
265 "cannot swap zero-sized array of type 'const T'");
269 _LIBCPP_INLINE_VISIBILITY
270 iterator begin() _NOEXCEPT {return iterator(data());}
271 _LIBCPP_INLINE_VISIBILITY
272 const_iterator begin() const _NOEXCEPT {return const_iterator(data());}
273 _LIBCPP_INLINE_VISIBILITY
274 iterator end() _NOEXCEPT {return iterator(data());}
275 _LIBCPP_INLINE_VISIBILITY
276 const_iterator end() const _NOEXCEPT {return const_iterator(data());}
278 _LIBCPP_INLINE_VISIBILITY
279 reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
280 _LIBCPP_INLINE_VISIBILITY
281 const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());}
282 _LIBCPP_INLINE_VISIBILITY
283 reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
284 _LIBCPP_INLINE_VISIBILITY
285 const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());}
287 _LIBCPP_INLINE_VISIBILITY
288 const_iterator cbegin() const _NOEXCEPT {return begin();}
289 _LIBCPP_INLINE_VISIBILITY
290 const_iterator cend() const _NOEXCEPT {return end();}
291 _LIBCPP_INLINE_VISIBILITY
292 const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
293 _LIBCPP_INLINE_VISIBILITY
294 const_reverse_iterator crend() const _NOEXCEPT {return rend();}
297 _LIBCPP_INLINE_VISIBILITY
298 _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return 0; }
299 _LIBCPP_INLINE_VISIBILITY
300 _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return 0;}
301 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
302 _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return true;}
305 _LIBCPP_INLINE_VISIBILITY
306 reference operator[](size_type) _NOEXCEPT {
307 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
308 _LIBCPP_UNREACHABLE();
311 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
312 const_reference operator[](size_type) const _NOEXCEPT {
313 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
314 _LIBCPP_UNREACHABLE();
317 _LIBCPP_INLINE_VISIBILITY
318 reference at(size_type) {
319 __throw_out_of_range("array<T, 0>::at");
320 _LIBCPP_UNREACHABLE();
323 _LIBCPP_INLINE_VISIBILITY
324 const_reference at(size_type) const {
325 __throw_out_of_range("array<T, 0>::at");
326 _LIBCPP_UNREACHABLE();
329 _LIBCPP_INLINE_VISIBILITY
330 reference front() _NOEXCEPT {
331 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array");
332 _LIBCPP_UNREACHABLE();
335 _LIBCPP_INLINE_VISIBILITY
336 const_reference front() const _NOEXCEPT {
337 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array");
338 _LIBCPP_UNREACHABLE();
341 _LIBCPP_INLINE_VISIBILITY
342 reference back() _NOEXCEPT {
343 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array");
344 _LIBCPP_UNREACHABLE();
347 _LIBCPP_INLINE_VISIBILITY
348 const_reference back() const _NOEXCEPT {
349 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array");
350 _LIBCPP_UNREACHABLE();
353 _LIBCPP_INLINE_VISIBILITY
354 value_type* data() _NOEXCEPT {return reinterpret_cast<value_type*>(__elems_);}
355 _LIBCPP_INLINE_VISIBILITY
356 const value_type* data() const _NOEXCEPT {return reinterpret_cast<const value_type*>(__elems_);}
360 #ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
361 template<class _Tp, class... _Args,
362 class = typename enable_if<(is_same_v<_Tp, _Args> && ...), void>::type
365 -> array<_Tp, 1 + sizeof...(_Args)>;
368 template <class _Tp, size_t _Size>
369 inline _LIBCPP_INLINE_VISIBILITY
370 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
371 operator==(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
373 return _VSTD::equal(__x.begin(), __x.end(), __y.begin());
376 template <class _Tp, size_t _Size>
377 inline _LIBCPP_INLINE_VISIBILITY
378 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
379 operator!=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
381 return !(__x == __y);
384 template <class _Tp, size_t _Size>
385 inline _LIBCPP_INLINE_VISIBILITY
386 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
387 operator<(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
389 return _VSTD::lexicographical_compare(__x.begin(), __x.end(),
390 __y.begin(), __y.end());
393 template <class _Tp, size_t _Size>
394 inline _LIBCPP_INLINE_VISIBILITY
395 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
396 operator>(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
401 template <class _Tp, size_t _Size>
402 inline _LIBCPP_INLINE_VISIBILITY
403 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
404 operator<=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
409 template <class _Tp, size_t _Size>
410 inline _LIBCPP_INLINE_VISIBILITY
411 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
412 operator>=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
417 template <class _Tp, size_t _Size>
418 inline _LIBCPP_INLINE_VISIBILITY
422 __is_swappable<_Tp>::value,
425 swap(array<_Tp, _Size>& __x, array<_Tp, _Size>& __y)
426 _NOEXCEPT_(noexcept(__x.swap(__y)))
431 template <class _Tp, size_t _Size>
432 struct _LIBCPP_TEMPLATE_VIS tuple_size<array<_Tp, _Size> >
433 : public integral_constant<size_t, _Size> {};
435 template <size_t _Ip, class _Tp, size_t _Size>
436 struct _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, array<_Tp, _Size> >
438 static_assert(_Ip < _Size, "Index out of bounds in std::tuple_element<> (std::array)");
442 template <size_t _Ip, class _Tp, size_t _Size>
443 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
445 get(array<_Tp, _Size>& __a) _NOEXCEPT
447 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array)");
448 return __a.__elems_[_Ip];
451 template <size_t _Ip, class _Tp, size_t _Size>
452 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
454 get(const array<_Tp, _Size>& __a) _NOEXCEPT
456 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array)");
457 return __a.__elems_[_Ip];
460 #ifndef _LIBCPP_CXX03_LANG
462 template <size_t _Ip, class _Tp, size_t _Size>
463 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
465 get(array<_Tp, _Size>&& __a) _NOEXCEPT
467 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array &&)");
468 return _VSTD::move(__a.__elems_[_Ip]);
471 template <size_t _Ip, class _Tp, size_t _Size>
472 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
474 get(const array<_Tp, _Size>&& __a) _NOEXCEPT
476 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array &&)");
477 return _VSTD::move(__a.__elems_[_Ip]);
480 #endif // !_LIBCPP_CXX03_LANG
482 _LIBCPP_END_NAMESPACE_STD
484 #endif // _LIBCPP_ARRAY