2 //===---------------------------- array -----------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
19 template <class T, size_t N >
23 typedef T & reference;
24 typedef const T & const_reference;
25 typedef implementation defined iterator;
26 typedef implementation defined const_iterator;
27 typedef size_t size_type;
28 typedef ptrdiff_t difference_type;
31 typedef const T* const_pointer;
32 typedef std::reverse_iterator<iterator> reverse_iterator;
33 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
35 // No explicit construct/copy/destroy for aggregate type
36 void fill(const T& u);
37 void swap(array& a) noexcept(is_nothrow_swappable_v<T>);
40 iterator begin() noexcept;
41 const_iterator begin() const noexcept;
42 iterator end() noexcept;
43 const_iterator end() const noexcept;
45 reverse_iterator rbegin() noexcept;
46 const_reverse_iterator rbegin() const noexcept;
47 reverse_iterator rend() noexcept;
48 const_reverse_iterator rend() const noexcept;
50 const_iterator cbegin() const noexcept;
51 const_iterator cend() const noexcept;
52 const_reverse_iterator crbegin() const noexcept;
53 const_reverse_iterator crend() const noexcept;
56 constexpr size_type size() const noexcept;
57 constexpr size_type max_size() const noexcept;
58 constexpr bool empty() const noexcept;
61 reference operator[](size_type n);
62 const_reference operator[](size_type n) const; // constexpr in C++14
63 const_reference at(size_type n) const; // constexpr in C++14
64 reference at(size_type n);
67 const_reference front() const; // constexpr in C++14
69 const_reference back() const; // constexpr in C++14
72 const T* data() const noexcept;
75 template <class T, class... U>
76 array(T, U...) -> array<T, 1 + sizeof...(U)>;
78 template <class T, size_t N>
79 bool operator==(const array<T,N>& x, const array<T,N>& y);
80 template <class T, size_t N>
81 bool operator!=(const array<T,N>& x, const array<T,N>& y);
82 template <class T, size_t N>
83 bool operator<(const array<T,N>& x, const array<T,N>& y);
84 template <class T, size_t N>
85 bool operator>(const array<T,N>& x, const array<T,N>& y);
86 template <class T, size_t N>
87 bool operator<=(const array<T,N>& x, const array<T,N>& y);
88 template <class T, size_t N>
89 bool operator>=(const array<T,N>& x, const array<T,N>& y);
91 template <class T, size_t N >
92 void swap(array<T,N>& x, array<T,N>& y) noexcept(noexcept(x.swap(y))); // C++17
94 template <class T> struct tuple_size;
95 template <size_t I, class T> class tuple_element;
96 template <class T, size_t N> struct tuple_size<array<T, N>>;
97 template <size_t I, class T, size_t N> struct tuple_element<I, array<T, N>>;
98 template <size_t I, class T, size_t N> T& get(array<T, N>&) noexcept; // constexpr in C++14
99 template <size_t I, class T, size_t N> const T& get(const array<T, N>&) noexcept; // constexpr in C++14
100 template <size_t I, class T, size_t N> T&& get(array<T, N>&&) noexcept; // constexpr in C++14
101 template <size_t I, class T, size_t N> const T&& get(const array<T, N>&&) noexcept; // constexpr in C++14
109 #include <type_traits>
114 #include <cstdlib> // for _LIBCPP_UNREACHABLE
118 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
119 #pragma GCC system_header
124 _LIBCPP_BEGIN_NAMESPACE_STD
127 template <class _Tp, size_t _Size>
128 struct _LIBCPP_TEMPLATE_VIS array
131 typedef array __self;
132 typedef _Tp value_type;
133 typedef value_type& reference;
134 typedef const value_type& const_reference;
135 typedef value_type* iterator;
136 typedef const value_type* const_iterator;
137 typedef value_type* pointer;
138 typedef const value_type* const_pointer;
139 typedef size_t size_type;
140 typedef ptrdiff_t difference_type;
141 typedef std::reverse_iterator<iterator> reverse_iterator;
142 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
146 // No explicit construct/copy/destroy for aggregate type
147 _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __u) {
148 _VSTD::fill_n(__elems_, _Size, __u);
151 _LIBCPP_INLINE_VISIBILITY
152 void swap(array& __a) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) {
153 std::swap_ranges(__elems_, __elems_ + _Size, __a.__elems_);
157 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
158 iterator begin() _NOEXCEPT {return iterator(data());}
159 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
160 const_iterator begin() const _NOEXCEPT {return const_iterator(data());}
161 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
162 iterator end() _NOEXCEPT {return iterator(data() + _Size);}
163 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
164 const_iterator end() const _NOEXCEPT {return const_iterator(data() + _Size);}
166 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
167 reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
168 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
169 const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());}
170 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
171 reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
172 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
173 const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());}
175 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
176 const_iterator cbegin() const _NOEXCEPT {return begin();}
177 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
178 const_iterator cend() const _NOEXCEPT {return end();}
179 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
180 const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
181 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
182 const_reverse_iterator crend() const _NOEXCEPT {return rend();}
185 _LIBCPP_INLINE_VISIBILITY
186 _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return _Size;}
187 _LIBCPP_INLINE_VISIBILITY
188 _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return _Size;}
189 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
190 _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return false; }
193 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
194 reference operator[](size_type __n) {return __elems_[__n];}
195 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
196 const_reference operator[](size_type __n) const {return __elems_[__n];}
198 _LIBCPP_CONSTEXPR_AFTER_CXX14 reference at(size_type __n);
199 _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference at(size_type __n) const;
201 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference front() {return __elems_[0];}
202 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference front() const {return __elems_[0];}
203 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference back() {return __elems_[_Size - 1];}
204 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference back() const {return __elems_[_Size - 1];}
206 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
207 value_type* data() _NOEXCEPT {return __elems_;}
208 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
209 const value_type* data() const _NOEXCEPT {return __elems_;}
213 template <class _Tp, size_t _Size>
214 _LIBCPP_CONSTEXPR_AFTER_CXX14
215 typename array<_Tp, _Size>::reference
216 array<_Tp, _Size>::at(size_type __n)
219 __throw_out_of_range("array::at");
221 return __elems_[__n];
224 template <class _Tp, size_t _Size>
225 _LIBCPP_CONSTEXPR_AFTER_CXX11
226 typename array<_Tp, _Size>::const_reference
227 array<_Tp, _Size>::at(size_type __n) const
230 __throw_out_of_range("array::at");
231 return __elems_[__n];
235 struct _LIBCPP_TEMPLATE_VIS array<_Tp, 0>
238 typedef array __self;
239 typedef _Tp value_type;
240 typedef value_type& reference;
241 typedef const value_type& const_reference;
242 typedef value_type* iterator;
243 typedef const value_type* const_iterator;
244 typedef value_type* pointer;
245 typedef const value_type* const_pointer;
246 typedef size_t size_type;
247 typedef ptrdiff_t difference_type;
248 typedef std::reverse_iterator<iterator> reverse_iterator;
249 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
251 typedef typename conditional<is_const<_Tp>::value, const char,
252 char>::type _CharType;
254 struct _ArrayInStructT { _Tp __data_[1]; };
255 _ALIGNAS_TYPE(_ArrayInStructT) _CharType __elems_[sizeof(_ArrayInStructT)];
257 // No explicit construct/copy/destroy for aggregate type
258 _LIBCPP_INLINE_VISIBILITY void fill(const value_type&) {
259 static_assert(!is_const<_Tp>::value,
260 "cannot fill zero-sized array of type 'const T'");
263 _LIBCPP_INLINE_VISIBILITY
264 void swap(array&) _NOEXCEPT {
265 static_assert(!is_const<_Tp>::value,
266 "cannot swap zero-sized array of type 'const T'");
270 _LIBCPP_INLINE_VISIBILITY
271 iterator begin() _NOEXCEPT {return iterator(data());}
272 _LIBCPP_INLINE_VISIBILITY
273 const_iterator begin() const _NOEXCEPT {return const_iterator(data());}
274 _LIBCPP_INLINE_VISIBILITY
275 iterator end() _NOEXCEPT {return iterator(data());}
276 _LIBCPP_INLINE_VISIBILITY
277 const_iterator end() const _NOEXCEPT {return const_iterator(data());}
279 _LIBCPP_INLINE_VISIBILITY
280 reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
281 _LIBCPP_INLINE_VISIBILITY
282 const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());}
283 _LIBCPP_INLINE_VISIBILITY
284 reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
285 _LIBCPP_INLINE_VISIBILITY
286 const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());}
288 _LIBCPP_INLINE_VISIBILITY
289 const_iterator cbegin() const _NOEXCEPT {return begin();}
290 _LIBCPP_INLINE_VISIBILITY
291 const_iterator cend() const _NOEXCEPT {return end();}
292 _LIBCPP_INLINE_VISIBILITY
293 const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
294 _LIBCPP_INLINE_VISIBILITY
295 const_reverse_iterator crend() const _NOEXCEPT {return rend();}
298 _LIBCPP_INLINE_VISIBILITY
299 _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return 0; }
300 _LIBCPP_INLINE_VISIBILITY
301 _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return 0;}
302 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
303 _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return true;}
306 _LIBCPP_INLINE_VISIBILITY
307 reference operator[](size_type) {
308 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
309 _LIBCPP_UNREACHABLE();
312 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
313 const_reference operator[](size_type) const {
314 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
315 _LIBCPP_UNREACHABLE();
318 _LIBCPP_INLINE_VISIBILITY
319 reference at(size_type) {
320 __throw_out_of_range("array<T, 0>::at");
321 _LIBCPP_UNREACHABLE();
324 _LIBCPP_INLINE_VISIBILITY
325 const_reference at(size_type) const {
326 __throw_out_of_range("array<T, 0>::at");
327 _LIBCPP_UNREACHABLE();
330 _LIBCPP_INLINE_VISIBILITY
332 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array");
333 _LIBCPP_UNREACHABLE();
336 _LIBCPP_INLINE_VISIBILITY
337 const_reference front() const {
338 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array");
339 _LIBCPP_UNREACHABLE();
342 _LIBCPP_INLINE_VISIBILITY
344 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array");
345 _LIBCPP_UNREACHABLE();
348 _LIBCPP_INLINE_VISIBILITY
349 const_reference back() const {
350 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array");
351 _LIBCPP_UNREACHABLE();
354 _LIBCPP_INLINE_VISIBILITY
355 value_type* data() _NOEXCEPT {return reinterpret_cast<value_type*>(__elems_);}
356 _LIBCPP_INLINE_VISIBILITY
357 const value_type* data() const _NOEXCEPT {return reinterpret_cast<const value_type*>(__elems_);}
361 #ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
362 template<class _Tp, class... _Args,
363 class = typename enable_if<(is_same_v<_Tp, _Args> && ...), void>::type
366 -> array<_Tp, 1 + sizeof...(_Args)>;
369 template <class _Tp, size_t _Size>
370 inline _LIBCPP_INLINE_VISIBILITY
371 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
372 operator==(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
374 return _VSTD::equal(__x.begin(), __x.end(), __y.begin());
377 template <class _Tp, size_t _Size>
378 inline _LIBCPP_INLINE_VISIBILITY
379 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
380 operator!=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
382 return !(__x == __y);
385 template <class _Tp, size_t _Size>
386 inline _LIBCPP_INLINE_VISIBILITY
387 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
388 operator<(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
390 return _VSTD::lexicographical_compare(__x.begin(), __x.end(),
391 __y.begin(), __y.end());
394 template <class _Tp, size_t _Size>
395 inline _LIBCPP_INLINE_VISIBILITY
396 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
397 operator>(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
402 template <class _Tp, size_t _Size>
403 inline _LIBCPP_INLINE_VISIBILITY
404 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
405 operator<=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
410 template <class _Tp, size_t _Size>
411 inline _LIBCPP_INLINE_VISIBILITY
412 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
413 operator>=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
418 template <class _Tp, size_t _Size>
419 inline _LIBCPP_INLINE_VISIBILITY
423 __is_swappable<_Tp>::value,
426 swap(array<_Tp, _Size>& __x, array<_Tp, _Size>& __y)
427 _NOEXCEPT_(noexcept(__x.swap(__y)))
432 template <class _Tp, size_t _Size>
433 struct _LIBCPP_TEMPLATE_VIS tuple_size<array<_Tp, _Size> >
434 : public integral_constant<size_t, _Size> {};
436 template <size_t _Ip, class _Tp, size_t _Size>
437 class _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, array<_Tp, _Size> >
439 static_assert(_Ip < _Size, "Index out of bounds in std::tuple_element<> (std::array)");
444 template <size_t _Ip, class _Tp, size_t _Size>
445 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
447 get(array<_Tp, _Size>& __a) _NOEXCEPT
449 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array)");
450 return __a.__elems_[_Ip];
453 template <size_t _Ip, class _Tp, size_t _Size>
454 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
456 get(const array<_Tp, _Size>& __a) _NOEXCEPT
458 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array)");
459 return __a.__elems_[_Ip];
462 #ifndef _LIBCPP_CXX03_LANG
464 template <size_t _Ip, class _Tp, size_t _Size>
465 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
467 get(array<_Tp, _Size>&& __a) _NOEXCEPT
469 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array &&)");
470 return _VSTD::move(__a.__elems_[_Ip]);
473 template <size_t _Ip, class _Tp, size_t _Size>
474 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
476 get(const array<_Tp, _Size>&& __a) _NOEXCEPT
478 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array &&)");
479 return _VSTD::move(__a.__elems_[_Ip]);
482 #endif // !_LIBCPP_CXX03_LANG
484 _LIBCPP_END_NAMESPACE_STD
486 #endif // _LIBCPP_ARRAY