Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===---------------------------- array -----------------------------------===// |
| 3 | // |
Howard Hinnant | c566dc3 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // |
Howard Hinnant | ee11c31 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 6 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 7 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP_ARRAY |
| 12 | #define _LIBCPP_ARRAY |
| 13 | |
| 14 | /* |
| 15 | array synopsis |
| 16 | |
| 17 | namespace std |
| 18 | { |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 19 | template <class T, size_t N > |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 20 | struct array |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 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; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 30 | typedef T* pointer; |
| 31 | typedef const T* const_pointer; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 32 | typedef std::reverse_iterator<iterator> reverse_iterator; |
| 33 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 34 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 35 | // No explicit construct/copy/destroy for aggregate type |
| 36 | void fill(const T& u); |
Eric Fiselier | 6bfed25 | 2016-04-21 23:38:59 +0000 | [diff] [blame] | 37 | void swap(array& a) noexcept(is_nothrow_swappable_v<T>); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 38 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 39 | // iterators: |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 40 | iterator begin() noexcept; |
| 41 | const_iterator begin() const noexcept; |
| 42 | iterator end() noexcept; |
| 43 | const_iterator end() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 44 | |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 45 | reverse_iterator rbegin() noexcept; |
| 46 | const_reverse_iterator rbegin() const noexcept; |
| 47 | reverse_iterator rend() noexcept; |
| 48 | const_reverse_iterator rend() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 49 | |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 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; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 54 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 55 | // capacity: |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 56 | constexpr size_type size() const noexcept; |
| 57 | constexpr size_type max_size() const noexcept; |
Howard Hinnant | 12ff42f | 2012-07-20 19:20:49 +0000 | [diff] [blame] | 58 | constexpr bool empty() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 59 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 60 | // element access: |
| 61 | reference operator[](size_type n); |
Marshall Clow | 0abb104 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 62 | const_reference operator[](size_type n) const; // constexpr in C++14 |
| 63 | const_reference at(size_type n) const; // constexpr in C++14 |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 64 | reference at(size_type n); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 65 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 66 | reference front(); |
Marshall Clow | 0abb104 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 67 | const_reference front() const; // constexpr in C++14 |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 68 | reference back(); |
Marshall Clow | 0abb104 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 69 | const_reference back() const; // constexpr in C++14 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 70 | |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 71 | T* data() noexcept; |
| 72 | const T* data() const noexcept; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 75 | template <class T, size_t N> |
| 76 | bool operator==(const array<T,N>& x, const array<T,N>& y); |
| 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); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 87 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 88 | template <class T, size_t N > |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 89 | void swap(array<T,N>& x, array<T,N>& y) noexcept(noexcept(x.swap(y))); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 90 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 91 | template <class T> class tuple_size; |
Marshall Clow | 291a351 | 2015-11-19 19:41:04 +0000 | [diff] [blame] | 92 | template <size_t I, class T> class tuple_element; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 93 | template <class T, size_t N> struct tuple_size<array<T, N>>; |
Marshall Clow | 291a351 | 2015-11-19 19:41:04 +0000 | [diff] [blame] | 94 | template <size_t I, class T, size_t N> struct tuple_element<I, array<T, N>>; |
| 95 | template <size_t I, class T, size_t N> T& get(array<T, N>&) noexcept; // constexpr in C++14 |
| 96 | template <size_t I, class T, size_t N> const T& get(const array<T, N>&) noexcept; // constexpr in C++14 |
| 97 | template <size_t I, class T, size_t N> T&& get(array<T, N>&&) noexcept; // constexpr in C++14 |
Eric Fiselier | 6dea809 | 2015-12-18 00:36:55 +0000 | [diff] [blame] | 98 | template <size_t I, class T, size_t N> const T&& get(const array<T, N>&&) noexcept; // constexpr in C++14 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 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> |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 111 | #include <cstdlib> // for _LIBCPP_UNREACHABLE |
| 112 | #include <__debug> |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 113 | |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 114 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 115 | #pragma GCC system_header |
Howard Hinnant | aaaa52b | 2011-10-17 20:05:10 +0000 | [diff] [blame] | 116 | #endif |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 117 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 118 | |
| 119 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 120 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 121 | |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 122 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 123 | template <class _Tp, size_t _Size> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 124 | struct _LIBCPP_TEMPLATE_VIS array |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 125 | { |
| 126 | // types: |
| 127 | typedef array __self; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 128 | typedef _Tp value_type; |
| 129 | typedef value_type& reference; |
| 130 | typedef const value_type& const_reference; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 131 | typedef value_type* iterator; |
| 132 | typedef const value_type* const_iterator; |
| 133 | typedef value_type* pointer; |
| 134 | typedef const value_type* const_pointer; |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 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; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 139 | |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 140 | _Tp __elems_[_Size]; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 141 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 142 | // No explicit construct/copy/destroy for aggregate type |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 143 | _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __u) { |
| 144 | _VSTD::fill_n(__elems_, _Size, __u); |
| 145 | } |
Eric Fiselier | 55659e4 | 2018-02-04 01:03:08 +0000 | [diff] [blame] | 146 | |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 147 | _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 148 | void swap(array& __a) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) { |
| 149 | std::swap_ranges(__elems_, __elems_ + _Size, __a.__elems_); |
| 150 | } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 151 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 152 | // iterators: |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 153 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 154 | iterator begin() _NOEXCEPT {return iterator(data());} |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 155 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 156 | const_iterator begin() const _NOEXCEPT {return const_iterator(data());} |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 157 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 158 | iterator end() _NOEXCEPT {return iterator(data() + _Size);} |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 159 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 160 | const_iterator end() const _NOEXCEPT {return const_iterator(data() + _Size);} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 161 | |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 162 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 163 | reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());} |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 164 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 165 | const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());} |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 166 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 167 | reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());} |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 168 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 169 | const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 170 | |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 171 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 172 | const_iterator cbegin() const _NOEXCEPT {return begin();} |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 173 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 174 | const_iterator cend() const _NOEXCEPT {return end();} |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 175 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 176 | const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();} |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 177 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 178 | const_reverse_iterator crend() const _NOEXCEPT {return rend();} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 179 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 180 | // capacity: |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 181 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 12ff42f | 2012-07-20 19:20:49 +0000 | [diff] [blame] | 182 | _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return _Size;} |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 183 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 12ff42f | 2012-07-20 19:20:49 +0000 | [diff] [blame] | 184 | _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return _Size;} |
Marshall Clow | 425f575 | 2017-11-15 05:51:26 +0000 | [diff] [blame] | 185 | _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 186 | _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return false; } |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 187 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 188 | // element access: |
Marshall Clow | ac63f41 | 2017-01-16 03:02:10 +0000 | [diff] [blame] | 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); |
Marshall Clow | 0abb104 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 195 | _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference at(size_type __n) const; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 196 | |
Marshall Clow | ac63f41 | 2017-01-16 03:02:10 +0000 | [diff] [blame] | 197 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference front() {return __elems_[0];} |
Marshall Clow | 0abb104 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 198 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference front() const {return __elems_[0];} |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 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];} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 201 | |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 202 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Nirav Dave | 129fb5c | 2018-02-06 03:03:37 +0000 | [diff] [blame] | 203 | value_type* data() _NOEXCEPT {return __elems_;} |
Marshall Clow | 99ba002 | 2017-01-04 17:58:17 +0000 | [diff] [blame] | 204 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Nirav Dave | 129fb5c | 2018-02-06 03:03:37 +0000 | [diff] [blame] | 205 | const value_type* data() const _NOEXCEPT {return __elems_;} |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 206 | }; |
| 207 | |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 208 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 209 | template <class _Tp, size_t _Size> |
Marshall Clow | ac63f41 | 2017-01-16 03:02:10 +0000 | [diff] [blame] | 210 | _LIBCPP_CONSTEXPR_AFTER_CXX14 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 211 | typename array<_Tp, _Size>::reference |
| 212 | array<_Tp, _Size>::at(size_type __n) |
| 213 | { |
| 214 | if (__n >= _Size) |
Marshall Clow | 8fea161 | 2016-08-25 15:09:01 +0000 | [diff] [blame] | 215 | __throw_out_of_range("array::at"); |
| 216 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 217 | return __elems_[__n]; |
| 218 | } |
| 219 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 220 | template <class _Tp, size_t _Size> |
Marshall Clow | 0abb104 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 221 | _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 222 | typename array<_Tp, _Size>::const_reference |
| 223 | array<_Tp, _Size>::at(size_type __n) const |
| 224 | { |
| 225 | if (__n >= _Size) |
Marshall Clow | 8fea161 | 2016-08-25 15:09:01 +0000 | [diff] [blame] | 226 | __throw_out_of_range("array::at"); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 227 | return __elems_[__n]; |
| 228 | } |
| 229 | |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 230 | template <class _Tp> |
| 231 | struct _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 | |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 247 | typedef typename conditional<is_const<_Tp>::value, const char, |
| 248 | char>::type _CharType; |
Eric Fiselier | 6a53758 | 2018-02-07 23:50:25 +0000 | [diff] [blame] | 249 | |
| 250 | struct _ArrayInStructT { _Tp __data_[1]; }; |
| 251 | _ALIGNAS_TYPE(_ArrayInStructT) _CharType __elems_[sizeof(_ArrayInStructT)]; |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 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 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 357 | template <class _Tp, size_t _Size> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 358 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 359 | bool |
| 360 | operator==(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) |
| 361 | { |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 362 | return _VSTD::equal(__x.begin(), __x.end(), __y.begin()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 365 | template <class _Tp, size_t _Size> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 366 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 367 | bool |
| 368 | operator!=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) |
| 369 | { |
| 370 | return !(__x == __y); |
| 371 | } |
| 372 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 373 | template <class _Tp, size_t _Size> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 374 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 375 | bool |
| 376 | operator<(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) |
| 377 | { |
Eric Fiselier | 5037a3b | 2018-02-07 21:06:13 +0000 | [diff] [blame] | 378 | return _VSTD::lexicographical_compare(__x.begin(), __x.end(), |
| 379 | __y.begin(), __y.end()); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 382 | template <class _Tp, size_t _Size> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 383 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 384 | bool |
| 385 | operator>(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) |
| 386 | { |
| 387 | return __y < __x; |
| 388 | } |
| 389 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 390 | template <class _Tp, size_t _Size> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 391 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 392 | bool |
| 393 | operator<=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) |
| 394 | { |
| 395 | return !(__y < __x); |
| 396 | } |
| 397 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 398 | template <class _Tp, size_t _Size> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 399 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 400 | bool |
| 401 | operator>=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) |
| 402 | { |
| 403 | return !(__x < __y); |
| 404 | } |
| 405 | |
Howard Hinnant | 3b6579a | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 406 | template <class _Tp, size_t _Size> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 407 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 4426724 | 2011-06-01 19:59:32 +0000 | [diff] [blame] | 408 | typename enable_if |
| 409 | < |
Eric Fiselier | 6bfed25 | 2016-04-21 23:38:59 +0000 | [diff] [blame] | 410 | _Size == 0 || |
Howard Hinnant | 4426724 | 2011-06-01 19:59:32 +0000 | [diff] [blame] | 411 | __is_swappable<_Tp>::value, |
| 412 | void |
| 413 | >::type |
Marshall Clow | ff1aa3d | 2016-03-07 21:57:10 +0000 | [diff] [blame] | 414 | swap(array<_Tp, _Size>& __x, array<_Tp, _Size>& __y) |
Eric Fiselier | 6bfed25 | 2016-04-21 23:38:59 +0000 | [diff] [blame] | 415 | _NOEXCEPT_(noexcept(__x.swap(__y))) |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 416 | { |
| 417 | __x.swap(__y); |
| 418 | } |
| 419 | |
| 420 | template <class _Tp, size_t _Size> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 421 | class _LIBCPP_TEMPLATE_VIS tuple_size<array<_Tp, _Size> > |
Howard Hinnant | 8e29cda | 2010-09-21 20:16:37 +0000 | [diff] [blame] | 422 | : public integral_constant<size_t, _Size> {}; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 423 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 424 | template <size_t _Ip, class _Tp, size_t _Size> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 425 | class _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, array<_Tp, _Size> > |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 426 | { |
Marshall Clow | 6f77fa1 | 2017-06-12 14:41:37 +0000 | [diff] [blame] | 427 | static_assert(_Ip < _Size, "Index out of bounds in std::tuple_element<> (std::array)"); |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 428 | public: |
| 429 | typedef _Tp type; |
| 430 | }; |
| 431 | |
| 432 | template <size_t _Ip, class _Tp, size_t _Size> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 433 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 434 | _Tp& |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 435 | get(array<_Tp, _Size>& __a) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 436 | { |
Marshall Clow | 66c49ca | 2012-12-18 16:46:30 +0000 | [diff] [blame] | 437 | static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array)"); |
Marshall Clow | 0abb104 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 438 | return __a.__elems_[_Ip]; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | template <size_t _Ip, class _Tp, size_t _Size> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 442 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 443 | const _Tp& |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 444 | get(const array<_Tp, _Size>& __a) _NOEXCEPT |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 445 | { |
Marshall Clow | 66c49ca | 2012-12-18 16:46:30 +0000 | [diff] [blame] | 446 | static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array)"); |
Marshall Clow | 0abb104 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 447 | return __a.__elems_[_Ip]; |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Eric Fiselier | 73abed7 | 2017-04-16 02:50:40 +0000 | [diff] [blame] | 450 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 22e9724 | 2010-11-17 19:52:17 +0000 | [diff] [blame] | 451 | |
| 452 | template <size_t _Ip, class _Tp, size_t _Size> |
Howard Hinnant | b8a8ca2 | 2013-10-04 22:09:00 +0000 | [diff] [blame] | 453 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Howard Hinnant | 22e9724 | 2010-11-17 19:52:17 +0000 | [diff] [blame] | 454 | _Tp&& |
Howard Hinnant | 32477e8 | 2011-05-31 21:06:33 +0000 | [diff] [blame] | 455 | get(array<_Tp, _Size>&& __a) _NOEXCEPT |
Howard Hinnant | 22e9724 | 2010-11-17 19:52:17 +0000 | [diff] [blame] | 456 | { |
Marshall Clow | 66c49ca | 2012-12-18 16:46:30 +0000 | [diff] [blame] | 457 | static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array &&)"); |
Marshall Clow | 0abb104 | 2013-07-17 18:25:36 +0000 | [diff] [blame] | 458 | return _VSTD::move(__a.__elems_[_Ip]); |
Howard Hinnant | 22e9724 | 2010-11-17 19:52:17 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Eric Fiselier | 6dea809 | 2015-12-18 00:36:55 +0000 | [diff] [blame] | 461 | template <size_t _Ip, class _Tp, size_t _Size> |
| 462 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 463 | const _Tp&& |
| 464 | get(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 | |
Eric Fiselier | 73abed7 | 2017-04-16 02:50:40 +0000 | [diff] [blame] | 470 | #endif // !_LIBCPP_CXX03_LANG |
Howard Hinnant | 22e9724 | 2010-11-17 19:52:17 +0000 | [diff] [blame] | 471 | |
Howard Hinnant | c51e102 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 472 | _LIBCPP_END_NAMESPACE_STD |
| 473 | |
| 474 | #endif // _LIBCPP_ARRAY |