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 #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 123 template <class _Tp, size_t _Size> 124 struct _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 209 template <class _Tp, size_t _Size> 210 _LIBCPP_CONSTEXPR_AFTER_CXX14 211 typename array<_Tp, _Size>::reference 212 array<_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 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 __throw_out_of_range("array::at"); 227 return __elems_[__n]; 228 } 229 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 247 248 typedef typename conditional<is_const<_Tp>::value, const char, 249 char>::type _CharType; 250 _ALIGNAS(alignment_of<_Tp[1]>::value) _CharType __elems_[sizeof(_Tp[1])]; 251 252 // No explicit construct/copy/destroy for aggregate type 253 _LIBCPP_INLINE_VISIBILITY void fill(const value_type&) { 254 static_assert(!is_const<_Tp>::value, 255 "cannot fill zero-sized array of type 'const T'"); 256 } 257 258 _LIBCPP_INLINE_VISIBILITY 259 void swap(array&) _NOEXCEPT { 260 static_assert(!is_const<_Tp>::value, 261 "cannot swap zero-sized array of type 'const T'"); 262 } 263 264 // iterators: 265 _LIBCPP_INLINE_VISIBILITY 266 iterator begin() _NOEXCEPT {return iterator(data());} 267 _LIBCPP_INLINE_VISIBILITY 268 const_iterator begin() const _NOEXCEPT {return const_iterator(data());} 269 _LIBCPP_INLINE_VISIBILITY 270 iterator end() _NOEXCEPT {return iterator(data());} 271 _LIBCPP_INLINE_VISIBILITY 272 const_iterator end() const _NOEXCEPT {return const_iterator(data());} 273 274 _LIBCPP_INLINE_VISIBILITY 275 reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());} 276 _LIBCPP_INLINE_VISIBILITY 277 const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());} 278 _LIBCPP_INLINE_VISIBILITY 279 reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());} 280 _LIBCPP_INLINE_VISIBILITY 281 const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());} 282 283 _LIBCPP_INLINE_VISIBILITY 284 const_iterator cbegin() const _NOEXCEPT {return begin();} 285 _LIBCPP_INLINE_VISIBILITY 286 const_iterator cend() const _NOEXCEPT {return end();} 287 _LIBCPP_INLINE_VISIBILITY 288 const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();} 289 _LIBCPP_INLINE_VISIBILITY 290 const_reverse_iterator crend() const _NOEXCEPT {return rend();} 291 292 // capacity: 293 _LIBCPP_INLINE_VISIBILITY 294 _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return 0; } 295 _LIBCPP_INLINE_VISIBILITY 296 _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return 0;} 297 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY 298 _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return true;} 299 300 // element access: 301 _LIBCPP_INLINE_VISIBILITY 302 reference operator[](size_type) { 303 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array"); 304 _LIBCPP_UNREACHABLE(); 305 } 306 307 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 308 const_reference operator[](size_type) const { 309 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array"); 310 _LIBCPP_UNREACHABLE(); 311 } 312 313 _LIBCPP_INLINE_VISIBILITY 314 reference at(size_type) { 315 __throw_out_of_range("array<T, 0>::at"); 316 _LIBCPP_UNREACHABLE(); 317 } 318 319 _LIBCPP_INLINE_VISIBILITY 320 const_reference at(size_type) const { 321 __throw_out_of_range("array<T, 0>::at"); 322 _LIBCPP_UNREACHABLE(); 323 } 324 325 _LIBCPP_INLINE_VISIBILITY 326 reference front() { 327 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array"); 328 _LIBCPP_UNREACHABLE(); 329 } 330 331 _LIBCPP_INLINE_VISIBILITY 332 const_reference front() const { 333 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array"); 334 _LIBCPP_UNREACHABLE(); 335 } 336 337 _LIBCPP_INLINE_VISIBILITY 338 reference back() { 339 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array"); 340 _LIBCPP_UNREACHABLE(); 341 } 342 343 _LIBCPP_INLINE_VISIBILITY 344 const_reference back() const { 345 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array"); 346 _LIBCPP_UNREACHABLE(); 347 } 348 349 _LIBCPP_INLINE_VISIBILITY 350 value_type* data() _NOEXCEPT {return reinterpret_cast<value_type*>(__elems_);} 351 _LIBCPP_INLINE_VISIBILITY 352 const value_type* data() const _NOEXCEPT {return reinterpret_cast<const value_type*>(__elems_);} 353 }; 354 355 356 template <class _Tp, size_t _Size> 357 inline _LIBCPP_INLINE_VISIBILITY 358 bool 359 operator==(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) 360 { 361 return _VSTD::equal(__x.begin(), __x.end(), __y.begin()); 362 } 363 364 template <class _Tp, size_t _Size> 365 inline _LIBCPP_INLINE_VISIBILITY 366 bool 367 operator!=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) 368 { 369 return !(__x == __y); 370 } 371 372 template <class _Tp, size_t _Size> 373 inline _LIBCPP_INLINE_VISIBILITY 374 bool 375 operator<(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) 376 { 377 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), 378 __y.begin(), __y.end()); 379 } 380 381 template <class _Tp, size_t _Size> 382 inline _LIBCPP_INLINE_VISIBILITY 383 bool 384 operator>(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) 385 { 386 return __y < __x; 387 } 388 389 template <class _Tp, size_t _Size> 390 inline _LIBCPP_INLINE_VISIBILITY 391 bool 392 operator<=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) 393 { 394 return !(__y < __x); 395 } 396 397 template <class _Tp, size_t _Size> 398 inline _LIBCPP_INLINE_VISIBILITY 399 bool 400 operator>=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y) 401 { 402 return !(__x < __y); 403 } 404 405 template <class _Tp, size_t _Size> 406 inline _LIBCPP_INLINE_VISIBILITY 407 typename enable_if 408 < 409 _Size == 0 || 410 __is_swappable<_Tp>::value, 411 void 412 >::type 413 swap(array<_Tp, _Size>& __x, array<_Tp, _Size>& __y) 414 _NOEXCEPT_(noexcept(__x.swap(__y))) 415 { 416 __x.swap(__y); 417 } 418 419 template <class _Tp, size_t _Size> 420 class _LIBCPP_TEMPLATE_VIS tuple_size<array<_Tp, _Size> > 421 : public integral_constant<size_t, _Size> {}; 422 423 template <size_t _Ip, class _Tp, size_t _Size> 424 class _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, array<_Tp, _Size> > 425 { 426 static_assert(_Ip < _Size, "Index out of bounds in std::tuple_element<> (std::array)"); 427 public: 428 typedef _Tp type; 429 }; 430 431 template <size_t _Ip, class _Tp, size_t _Size> 432 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 433 _Tp& 434 get(array<_Tp, _Size>& __a) _NOEXCEPT 435 { 436 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array)"); 437 return __a.__elems_[_Ip]; 438 } 439 440 template <size_t _Ip, class _Tp, size_t _Size> 441 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 442 const _Tp& 443 get(const array<_Tp, _Size>& __a) _NOEXCEPT 444 { 445 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array)"); 446 return __a.__elems_[_Ip]; 447 } 448 449 #ifndef _LIBCPP_CXX03_LANG 450 451 template <size_t _Ip, class _Tp, size_t _Size> 452 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 453 _Tp&& 454 get(array<_Tp, _Size>&& __a) _NOEXCEPT 455 { 456 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array &&)"); 457 return _VSTD::move(__a.__elems_[_Ip]); 458 } 459 460 template <size_t _Ip, class _Tp, size_t _Size> 461 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 462 const _Tp&& 463 get(const array<_Tp, _Size>&& __a) _NOEXCEPT 464 { 465 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array &&)"); 466 return _VSTD::move(__a.__elems_[_Ip]); 467 } 468 469 #endif // !_LIBCPP_CXX03_LANG 470 471 _LIBCPP_END_NAMESPACE_STD 472 473 #endif // _LIBCPP_ARRAY 474