| 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 | |
| 17 | namespace std |
| 18 | { |
| 19 | template <class T, size_t N > |
| 20 | struct 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 | |
| 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); |
| 87 | |
| 88 | template <class T, size_t N > |
| 89 | void swap(array<T,N>& x, array<T,N>& y) noexcept(noexcept(x.swap(y))); |
| 90 | |
| 91 | template <class T> class tuple_size; |
| 92 | template <size_t I, class T> class tuple_element; |
| 93 | template <class T, size_t N> struct tuple_size<array<T, N>>; |
| 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 |
| 98 | template <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 | #if defined(_LIBCPP_NO_EXCEPTIONS) |
| 112 | #include <cassert> |
| 113 | #endif |
| 114 | |
| 115 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 116 | #pragma GCC system_header |
| 117 | #endif |
| 118 | |
| 119 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 120 | |
| 121 | template <class _Tp, size_t _Size> |
| 122 | struct _LIBCPP_TYPE_VIS_ONLY array |
| 123 | { |
| 124 | // types: |
| 125 | typedef array __self; |
| 126 | typedef _Tp value_type; |
| 127 | typedef value_type& reference; |
| 128 | typedef const value_type& const_reference; |
| 129 | typedef value_type* iterator; |
| 130 | typedef const value_type* const_iterator; |
| 131 | typedef value_type* pointer; |
| 132 | typedef const value_type* const_pointer; |
| 133 | typedef size_t size_type; |
| 134 | typedef ptrdiff_t difference_type; |
| 135 | typedef std::reverse_iterator<iterator> reverse_iterator; |
| 136 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
| 137 | |
| 138 | value_type __elems_[_Size > 0 ? _Size : 1]; |
| 139 | |
| 140 | // No explicit construct/copy/destroy for aggregate type |
| 141 | _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __u) |
| 142 | {_VSTD::fill_n(__elems_, _Size, __u);} |
| 143 | _LIBCPP_INLINE_VISIBILITY |
| 144 | void swap(array& __a) _NOEXCEPT_(_Size == 0 || __is_nothrow_swappable<_Tp>::value) |
| 145 | { __swap_dispatch((std::integral_constant<bool, _Size == 0>()), __a); } |
| 146 | |
| 147 | _LIBCPP_INLINE_VISIBILITY |
| 148 | void __swap_dispatch(std::true_type, array&) {} |
| 149 | |
| 150 | _LIBCPP_INLINE_VISIBILITY |
| 151 | void __swap_dispatch(std::false_type, array& __a) |
| 152 | { _VSTD::swap_ranges(__elems_, __elems_ + _Size, __a.__elems_);} |
| 153 | |
| 154 | // iterators: |
| 155 | _LIBCPP_INLINE_VISIBILITY |
| 156 | iterator begin() _NOEXCEPT {return iterator(__elems_);} |
| 157 | _LIBCPP_INLINE_VISIBILITY |
| 158 | const_iterator begin() const _NOEXCEPT {return const_iterator(__elems_);} |
| 159 | _LIBCPP_INLINE_VISIBILITY |
| 160 | iterator end() _NOEXCEPT {return iterator(__elems_ + _Size);} |
| 161 | _LIBCPP_INLINE_VISIBILITY |
| 162 | const_iterator end() const _NOEXCEPT {return const_iterator(__elems_ + _Size);} |
| 163 | |
| 164 | _LIBCPP_INLINE_VISIBILITY |
| 165 | reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());} |
| 166 | _LIBCPP_INLINE_VISIBILITY |
| 167 | const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());} |
| 168 | _LIBCPP_INLINE_VISIBILITY |
| 169 | reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());} |
| 170 | _LIBCPP_INLINE_VISIBILITY |
| 171 | const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());} |
| 172 | |
| 173 | _LIBCPP_INLINE_VISIBILITY |
| 174 | const_iterator cbegin() const _NOEXCEPT {return begin();} |
| 175 | _LIBCPP_INLINE_VISIBILITY |
| 176 | const_iterator cend() const _NOEXCEPT {return end();} |
| 177 | _LIBCPP_INLINE_VISIBILITY |
| 178 | const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();} |
| 179 | _LIBCPP_INLINE_VISIBILITY |
| 180 | const_reverse_iterator crend() const _NOEXCEPT {return rend();} |
| 181 | |
| 182 | // capacity: |
| 183 | _LIBCPP_INLINE_VISIBILITY |
| 184 | _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return _Size;} |
| 185 | _LIBCPP_INLINE_VISIBILITY |
| 186 | _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return _Size;} |
| 187 | _LIBCPP_INLINE_VISIBILITY |
| 188 | _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return _Size == 0;} |
| 189 | |
| 190 | // element access: |
| 191 | _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __elems_[__n];} |
| 192 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference operator[](size_type __n) const {return __elems_[__n];} |
| 193 | reference at(size_type __n); |
| 194 | _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference at(size_type __n) const; |
| 195 | |
| 196 | _LIBCPP_INLINE_VISIBILITY reference front() {return __elems_[0];} |
| 197 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference front() const {return __elems_[0];} |
| 198 | _LIBCPP_INLINE_VISIBILITY reference back() {return __elems_[_Size > 0 ? _Size-1 : 0];} |
| 199 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference back() const {return __elems_[_Size > 0 ? _Size-1 : 0];} |
| 200 | |
| 201 | _LIBCPP_INLINE_VISIBILITY |
| 202 | value_type* data() _NOEXCEPT {return __elems_;} |
| 203 | _LIBCPP_INLINE_VISIBILITY |
| 204 | const value_type* data() const _NOEXCEPT {return __elems_;} |
| 205 | }; |
| 206 | |
| 207 | template <class _Tp, size_t _Size> |
| 208 | typename array<_Tp, _Size>::reference |
| 209 | array<_Tp, _Size>::at(size_type __n) |
| 210 | { |
| 211 | if (__n >= _Size) |
| 212 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 213 | throw out_of_range("array::at" ); |
| 214 | #else |
| 215 | assert(!"array::at out_of_range" ); |
| 216 | #endif |
| 217 | return __elems_[__n]; |
| 218 | } |
| 219 | |
| 220 | template <class _Tp, size_t _Size> |
| 221 | _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 222 | typename array<_Tp, _Size>::const_reference |
| 223 | array<_Tp, _Size>::at(size_type __n) const |
| 224 | { |
| 225 | if (__n >= _Size) |
| 226 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 227 | throw out_of_range("array::at" ); |
| 228 | #else |
| 229 | assert(!"array::at out_of_range" ); |
| 230 | #endif |
| 231 | return __elems_[__n]; |
| 232 | } |
| 233 | |
| 234 | template <class _Tp, size_t _Size> |
| 235 | inline _LIBCPP_INLINE_VISIBILITY |
| 236 | bool |
| 237 | operator==(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) |
| 238 | { |
| 239 | return _VSTD::equal(__x.__elems_, __x.__elems_ + _Size, __y.__elems_); |
| 240 | } |
| 241 | |
| 242 | template <class _Tp, size_t _Size> |
| 243 | inline _LIBCPP_INLINE_VISIBILITY |
| 244 | bool |
| 245 | operator!=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) |
| 246 | { |
| 247 | return !(__x == __y); |
| 248 | } |
| 249 | |
| 250 | template <class _Tp, size_t _Size> |
| 251 | inline _LIBCPP_INLINE_VISIBILITY |
| 252 | bool |
| 253 | operator<(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) |
| 254 | { |
| 255 | return _VSTD::lexicographical_compare(__x.__elems_, __x.__elems_ + _Size, __y.__elems_, __y.__elems_ + _Size); |
| 256 | } |
| 257 | |
| 258 | template <class _Tp, size_t _Size> |
| 259 | inline _LIBCPP_INLINE_VISIBILITY |
| 260 | bool |
| 261 | operator>(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) |
| 262 | { |
| 263 | return __y < __x; |
| 264 | } |
| 265 | |
| 266 | template <class _Tp, size_t _Size> |
| 267 | inline _LIBCPP_INLINE_VISIBILITY |
| 268 | bool |
| 269 | operator<=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) |
| 270 | { |
| 271 | return !(__y < __x); |
| 272 | } |
| 273 | |
| 274 | template <class _Tp, size_t _Size> |
| 275 | inline _LIBCPP_INLINE_VISIBILITY |
| 276 | bool |
| 277 | operator>=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) |
| 278 | { |
| 279 | return !(__x < __y); |
| 280 | } |
| 281 | |
| 282 | template <class _Tp, size_t _Size> |
| 283 | inline _LIBCPP_INLINE_VISIBILITY |
| 284 | typename enable_if |
| 285 | < |
| 286 | _Size == 0 || |
| 287 | __is_swappable<_Tp>::value, |
| 288 | void |
| 289 | >::type |
| 290 | swap(array<_Tp, _Size>& __x, array<_Tp, _Size>& __y) |
| 291 | _NOEXCEPT_(noexcept(__x.swap(__y))) |
| 292 | { |
| 293 | __x.swap(__y); |
| 294 | } |
| 295 | |
| 296 | template <class _Tp, size_t _Size> |
| 297 | class _LIBCPP_TYPE_VIS_ONLY tuple_size<array<_Tp, _Size> > |
| 298 | : public integral_constant<size_t, _Size> {}; |
| 299 | |
| 300 | template <size_t _Ip, class _Tp, size_t _Size> |
| 301 | class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, array<_Tp, _Size> > |
| 302 | { |
| 303 | public: |
| 304 | typedef _Tp type; |
| 305 | }; |
| 306 | |
| 307 | template <size_t _Ip, class _Tp, size_t _Size> |
| 308 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 309 | _Tp& |
| 310 | get(array<_Tp, _Size>& __a) _NOEXCEPT |
| 311 | { |
| 312 | static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array)" ); |
| 313 | return __a.__elems_[_Ip]; |
| 314 | } |
| 315 | |
| 316 | template <size_t _Ip, class _Tp, size_t _Size> |
| 317 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 318 | const _Tp& |
| 319 | get(const array<_Tp, _Size>& __a) _NOEXCEPT |
| 320 | { |
| 321 | static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array)" ); |
| 322 | return __a.__elems_[_Ip]; |
| 323 | } |
| 324 | |
| 325 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 326 | |
| 327 | template <size_t _Ip, class _Tp, size_t _Size> |
| 328 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 329 | _Tp&& |
| 330 | get(array<_Tp, _Size>&& __a) _NOEXCEPT |
| 331 | { |
| 332 | static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array &&)" ); |
| 333 | return _VSTD::move(__a.__elems_[_Ip]); |
| 334 | } |
| 335 | |
| 336 | template <size_t _Ip, class _Tp, size_t _Size> |
| 337 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 338 | const _Tp&& |
| 339 | get(const array<_Tp, _Size>&& __a) _NOEXCEPT |
| 340 | { |
| 341 | static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array &&)" ); |
| 342 | return _VSTD::move(__a.__elems_[_Ip]); |
| 343 | } |
| 344 | |
| 345 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 346 | |
| 347 | _LIBCPP_END_NAMESPACE_STD |
| 348 | |
| 349 | #endif // _LIBCPP_ARRAY |
| 350 | |