xref: /llvm-project-15.0.7/libcxx/include/array (revision a3b5f71e)
1// -*- C++ -*-
2//===---------------------------- array -----------------------------------===//
3//
4//                     The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_ARRAY
12#define _LIBCPP_ARRAY
13
14/*
15    array synopsis
16
17namespace std
18{
19template <class T, size_t N >
20struct array
21{
22    // types:
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;
29    typedef T value_type;
30    typedef T* pointer;
31    typedef const T* const_pointer;
32    typedef std::reverse_iterator<iterator> reverse_iterator;
33    typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
34
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>);
38
39    // iterators:
40    iterator begin() noexcept;
41    const_iterator begin() const noexcept;
42    iterator end() noexcept;
43    const_iterator end() const noexcept;
44
45    reverse_iterator rbegin() noexcept;
46    const_reverse_iterator rbegin() const noexcept;
47    reverse_iterator rend() noexcept;
48    const_reverse_iterator rend() const noexcept;
49
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;
54
55    // capacity:
56    constexpr size_type size() const noexcept;
57    constexpr size_type max_size() const noexcept;
58    constexpr bool empty() const noexcept;
59
60    // element access:
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);
65
66    reference front();
67    const_reference front() const; // constexpr in C++14
68    reference back();
69    const_reference back() const; // constexpr in C++14
70
71    T* data() noexcept;
72    const T* data() const noexcept;
73};
74
75template <class T, size_t N>
76  bool operator==(const array<T,N>& x, const array<T,N>& y);
77template <class T, size_t N>
78  bool operator!=(const array<T,N>& x, const array<T,N>& y);
79template <class T, size_t N>
80  bool operator<(const array<T,N>& x, const array<T,N>& y);
81template <class T, size_t N>
82  bool operator>(const array<T,N>& x, const array<T,N>& y);
83template <class T, size_t N>
84  bool operator<=(const array<T,N>& x, const array<T,N>& y);
85template <class T, size_t N>
86  bool operator>=(const array<T,N>& x, const array<T,N>& y);
87
88template <class T, size_t N >
89  void swap(array<T,N>& x, array<T,N>& y) noexcept(noexcept(x.swap(y)));
90
91template <class T> class tuple_size;
92template <size_t I, class T> class tuple_element;
93template <class T, size_t N> struct tuple_size<array<T, N>>;
94template <size_t I, class T, size_t N> struct tuple_element<I, array<T, N>>;
95template <size_t I, class T, size_t N> T& get(array<T, N>&) noexcept; // constexpr in C++14
96template <size_t I, class T, size_t N> const T& get(const array<T, N>&) noexcept; // constexpr in C++14
97template <size_t I, class T, size_t N> T&& get(array<T, N>&&) noexcept; // constexpr in C++14
98template <size_t I, class T, size_t N> const T&& get(const array<T, N>&&) noexcept; // constexpr in C++14
99
100}  // std
101
102*/
103
104#include <__config>
105#include <__tuple>
106#include <type_traits>
107#include <utility>
108#include <iterator>
109#include <algorithm>
110#include <stdexcept>
111#include <cstdlib> // for _LIBCPP_UNREACHABLE
112#include <__debug>
113
114#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
115#pragma GCC system_header
116#endif
117
118
119
120_LIBCPP_BEGIN_NAMESPACE_STD
121
122
123template <class _Tp, size_t _Size>
124struct _LIBCPP_TEMPLATE_VIS array
125{
126    // types:
127    typedef array __self;
128    typedef _Tp                                   value_type;
129    typedef value_type&                           reference;
130    typedef const value_type&                     const_reference;
131    typedef value_type*                           iterator;
132    typedef const value_type*                     const_iterator;
133    typedef value_type*                           pointer;
134    typedef const value_type*                     const_pointer;
135    typedef size_t                                size_type;
136    typedef ptrdiff_t                             difference_type;
137    typedef std::reverse_iterator<iterator>       reverse_iterator;
138    typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
139
140    _Tp __elems_[_Size];
141
142    // No explicit construct/copy/destroy for aggregate type
143    _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __u) {
144      _VSTD::fill_n(__elems_, _Size, __u);
145    }
146
147    _LIBCPP_INLINE_VISIBILITY
148    void swap(array& __a) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) {
149      std::swap_ranges(__elems_, __elems_ + _Size, __a.__elems_);
150    }
151
152    // iterators:
153    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
154    iterator begin() _NOEXCEPT {return iterator(data());}
155    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
156    const_iterator begin() const _NOEXCEPT {return const_iterator(data());}
157    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
158    iterator end() _NOEXCEPT {return iterator(data() + _Size);}
159    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
160    const_iterator end() const _NOEXCEPT {return const_iterator(data() + _Size);}
161
162    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
163    reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
164    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
165    const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());}
166    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
167    reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
168    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
169    const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());}
170
171    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
172    const_iterator cbegin() const _NOEXCEPT {return begin();}
173    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
174    const_iterator cend() const _NOEXCEPT {return end();}
175    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
176    const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
177    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
178    const_reverse_iterator crend() const _NOEXCEPT {return rend();}
179
180    // capacity:
181    _LIBCPP_INLINE_VISIBILITY
182    _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return _Size;}
183    _LIBCPP_INLINE_VISIBILITY
184    _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return _Size;}
185    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
186    _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return false; }
187
188    // element access:
189    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
190    reference operator[](size_type __n)             {return __elems_[__n];}
191    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
192    const_reference operator[](size_type __n) const {return __elems_[__n];}
193
194    _LIBCPP_CONSTEXPR_AFTER_CXX14       reference at(size_type __n);
195    _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference at(size_type __n) const;
196
197    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference front()             {return __elems_[0];}
198    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference front() const {return __elems_[0];}
199    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference back()              {return __elems_[_Size - 1];}
200    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference back() const  {return __elems_[_Size - 1];}
201
202    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
203    value_type* data() _NOEXCEPT {return __elems_;}
204    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
205    const value_type* data() const _NOEXCEPT {return __elems_;}
206};
207
208
209template <class _Tp, size_t _Size>
210_LIBCPP_CONSTEXPR_AFTER_CXX14
211typename array<_Tp, _Size>::reference
212array<_Tp, _Size>::at(size_type __n)
213{
214    if (__n >= _Size)
215        __throw_out_of_range("array::at");
216
217    return __elems_[__n];
218}
219
220template <class _Tp, size_t _Size>
221_LIBCPP_CONSTEXPR_AFTER_CXX11
222typename array<_Tp, _Size>::const_reference
223array<_Tp, _Size>::at(size_type __n) const
224{
225    if (__n >= _Size)
226        __throw_out_of_range("array::at");
227    return __elems_[__n];
228}
229
230template <class _Tp>
231struct _LIBCPP_TEMPLATE_VIS array<_Tp, 0>
232{
233    // types:
234    typedef array __self;
235    typedef _Tp                                   value_type;
236    typedef value_type&                           reference;
237    typedef const value_type&                     const_reference;
238    typedef value_type*                           iterator;
239    typedef const value_type*                     const_iterator;
240    typedef value_type*                           pointer;
241    typedef const value_type*                     const_pointer;
242    typedef size_t                                size_type;
243    typedef ptrdiff_t                             difference_type;
244    typedef std::reverse_iterator<iterator>       reverse_iterator;
245    typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
246
247    typedef typename conditional<is_const<_Tp>::value, const char,
248                                char>::type _CharType;
249
250    struct  _ArrayInStructT { _Tp __data_[1]; };
251    _ALIGNAS_TYPE(_ArrayInStructT) _CharType __elems_[sizeof(_ArrayInStructT)];
252
253    // No explicit construct/copy/destroy for aggregate type
254    _LIBCPP_INLINE_VISIBILITY void fill(const value_type&) {
255      static_assert(!is_const<_Tp>::value,
256                    "cannot fill zero-sized array of type 'const T'");
257    }
258
259    _LIBCPP_INLINE_VISIBILITY
260    void swap(array&) _NOEXCEPT {
261      static_assert(!is_const<_Tp>::value,
262                    "cannot swap zero-sized array of type 'const T'");
263    }
264
265    // iterators:
266    _LIBCPP_INLINE_VISIBILITY
267    iterator begin() _NOEXCEPT {return iterator(data());}
268    _LIBCPP_INLINE_VISIBILITY
269    const_iterator begin() const _NOEXCEPT {return const_iterator(data());}
270    _LIBCPP_INLINE_VISIBILITY
271    iterator end() _NOEXCEPT {return iterator(data());}
272    _LIBCPP_INLINE_VISIBILITY
273    const_iterator end() const _NOEXCEPT {return const_iterator(data());}
274
275    _LIBCPP_INLINE_VISIBILITY
276    reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
277    _LIBCPP_INLINE_VISIBILITY
278    const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());}
279    _LIBCPP_INLINE_VISIBILITY
280    reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
281    _LIBCPP_INLINE_VISIBILITY
282    const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());}
283
284    _LIBCPP_INLINE_VISIBILITY
285    const_iterator cbegin() const _NOEXCEPT {return begin();}
286    _LIBCPP_INLINE_VISIBILITY
287    const_iterator cend() const _NOEXCEPT {return end();}
288    _LIBCPP_INLINE_VISIBILITY
289    const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
290    _LIBCPP_INLINE_VISIBILITY
291    const_reverse_iterator crend() const _NOEXCEPT {return rend();}
292
293    // capacity:
294    _LIBCPP_INLINE_VISIBILITY
295    _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return 0; }
296    _LIBCPP_INLINE_VISIBILITY
297    _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return 0;}
298    _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
299    _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return true;}
300
301    // element access:
302    _LIBCPP_INLINE_VISIBILITY
303    reference operator[](size_type) {
304      _LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
305      _LIBCPP_UNREACHABLE();
306    }
307
308    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
309    const_reference operator[](size_type) const {
310      _LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
311      _LIBCPP_UNREACHABLE();
312    }
313
314    _LIBCPP_INLINE_VISIBILITY
315    reference at(size_type) {
316      __throw_out_of_range("array<T, 0>::at");
317      _LIBCPP_UNREACHABLE();
318    }
319
320    _LIBCPP_INLINE_VISIBILITY
321    const_reference at(size_type) const {
322      __throw_out_of_range("array<T, 0>::at");
323      _LIBCPP_UNREACHABLE();
324    }
325
326    _LIBCPP_INLINE_VISIBILITY
327    reference front() {
328      _LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array");
329      _LIBCPP_UNREACHABLE();
330    }
331
332    _LIBCPP_INLINE_VISIBILITY
333    const_reference front() const {
334      _LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array");
335      _LIBCPP_UNREACHABLE();
336    }
337
338    _LIBCPP_INLINE_VISIBILITY
339    reference back() {
340      _LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array");
341      _LIBCPP_UNREACHABLE();
342    }
343
344    _LIBCPP_INLINE_VISIBILITY
345    const_reference back() const {
346      _LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array");
347      _LIBCPP_UNREACHABLE();
348    }
349
350    _LIBCPP_INLINE_VISIBILITY
351    value_type* data() _NOEXCEPT {return reinterpret_cast<value_type*>(__elems_);}
352    _LIBCPP_INLINE_VISIBILITY
353    const value_type* data() const _NOEXCEPT {return reinterpret_cast<const value_type*>(__elems_);}
354};
355
356
357template <class _Tp, size_t _Size>
358inline _LIBCPP_INLINE_VISIBILITY
359bool
360operator==(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
361{
362    return _VSTD::equal(__x.begin(), __x.end(), __y.begin());
363}
364
365template <class _Tp, size_t _Size>
366inline _LIBCPP_INLINE_VISIBILITY
367bool
368operator!=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
369{
370    return !(__x == __y);
371}
372
373template <class _Tp, size_t _Size>
374inline _LIBCPP_INLINE_VISIBILITY
375bool
376operator<(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
377{
378    return _VSTD::lexicographical_compare(__x.begin(), __x.end(),
379                                          __y.begin(), __y.end());
380}
381
382template <class _Tp, size_t _Size>
383inline _LIBCPP_INLINE_VISIBILITY
384bool
385operator>(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
386{
387    return __y < __x;
388}
389
390template <class _Tp, size_t _Size>
391inline _LIBCPP_INLINE_VISIBILITY
392bool
393operator<=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
394{
395    return !(__y < __x);
396}
397
398template <class _Tp, size_t _Size>
399inline _LIBCPP_INLINE_VISIBILITY
400bool
401operator>=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
402{
403    return !(__x < __y);
404}
405
406template <class _Tp, size_t _Size>
407inline _LIBCPP_INLINE_VISIBILITY
408typename enable_if
409<
410    _Size == 0 ||
411    __is_swappable<_Tp>::value,
412    void
413>::type
414swap(array<_Tp, _Size>& __x, array<_Tp, _Size>& __y)
415                                  _NOEXCEPT_(noexcept(__x.swap(__y)))
416{
417    __x.swap(__y);
418}
419
420template <class _Tp, size_t _Size>
421class _LIBCPP_TEMPLATE_VIS tuple_size<array<_Tp, _Size> >
422    : public integral_constant<size_t, _Size> {};
423
424template <size_t _Ip, class _Tp, size_t _Size>
425class _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, array<_Tp, _Size> >
426{
427    static_assert(_Ip < _Size, "Index out of bounds in std::tuple_element<> (std::array)");
428public:
429    typedef _Tp type;
430};
431
432template <size_t _Ip, class _Tp, size_t _Size>
433inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
434_Tp&
435get(array<_Tp, _Size>& __a) _NOEXCEPT
436{
437    static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array)");
438    return __a.__elems_[_Ip];
439}
440
441template <size_t _Ip, class _Tp, size_t _Size>
442inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
443const _Tp&
444get(const array<_Tp, _Size>& __a) _NOEXCEPT
445{
446    static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array)");
447    return __a.__elems_[_Ip];
448}
449
450#ifndef _LIBCPP_CXX03_LANG
451
452template <size_t _Ip, class _Tp, size_t _Size>
453inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
454_Tp&&
455get(array<_Tp, _Size>&& __a) _NOEXCEPT
456{
457    static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array &&)");
458    return _VSTD::move(__a.__elems_[_Ip]);
459}
460
461template <size_t _Ip, class _Tp, size_t _Size>
462inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
463const _Tp&&
464get(const array<_Tp, _Size>&& __a) _NOEXCEPT
465{
466    static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array &&)");
467    return _VSTD::move(__a.__elems_[_Ip]);
468}
469
470#endif  // !_LIBCPP_CXX03_LANG
471
472_LIBCPP_END_NAMESPACE_STD
473
474#endif  // _LIBCPP_ARRAY
475