libstdc++
|
00001 // <array> -*- C++ -*- 00002 00003 // Copyright (C) 2007-2017 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file include/array 00026 * This is a Standard C++ Library header. 00027 */ 00028 00029 #ifndef _GLIBCXX_ARRAY 00030 #define _GLIBCXX_ARRAY 1 00031 00032 #pragma GCC system_header 00033 00034 #if __cplusplus < 201103L 00035 # include <bits/c++0x_warning.h> 00036 #else 00037 00038 #include <utility> 00039 #include <stdexcept> 00040 #include <bits/stl_algobase.h> 00041 #include <bits/range_access.h> 00042 00043 namespace std _GLIBCXX_VISIBILITY(default) 00044 { 00045 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER 00046 00047 template<typename _Tp, std::size_t _Nm> 00048 struct __array_traits 00049 { 00050 typedef _Tp _Type[_Nm]; 00051 typedef __is_swappable<_Tp> _Is_swappable; 00052 typedef __is_nothrow_swappable<_Tp> _Is_nothrow_swappable; 00053 00054 static constexpr _Tp& 00055 _S_ref(const _Type& __t, std::size_t __n) noexcept 00056 { return const_cast<_Tp&>(__t[__n]); } 00057 00058 static constexpr _Tp* 00059 _S_ptr(const _Type& __t) noexcept 00060 { return const_cast<_Tp*>(__t); } 00061 }; 00062 00063 template<typename _Tp> 00064 struct __array_traits<_Tp, 0> 00065 { 00066 struct _Type { }; 00067 typedef true_type _Is_swappable; 00068 typedef true_type _Is_nothrow_swappable; 00069 00070 static constexpr _Tp& 00071 _S_ref(const _Type&, std::size_t) noexcept 00072 { return *static_cast<_Tp*>(nullptr); } 00073 00074 static constexpr _Tp* 00075 _S_ptr(const _Type&) noexcept 00076 { return nullptr; } 00077 }; 00078 00079 /** 00080 * @brief A standard container for storing a fixed size sequence of elements. 00081 * 00082 * @ingroup sequences 00083 * 00084 * Meets the requirements of a <a href="tables.html#65">container</a>, a 00085 * <a href="tables.html#66">reversible container</a>, and a 00086 * <a href="tables.html#67">sequence</a>. 00087 * 00088 * Sets support random access iterators. 00089 * 00090 * @tparam Tp Type of element. Required to be a complete type. 00091 * @tparam N Number of elements. 00092 */ 00093 template<typename _Tp, std::size_t _Nm> 00094 struct array 00095 { 00096 typedef _Tp value_type; 00097 typedef value_type* pointer; 00098 typedef const value_type* const_pointer; 00099 typedef value_type& reference; 00100 typedef const value_type& const_reference; 00101 typedef value_type* iterator; 00102 typedef const value_type* const_iterator; 00103 typedef std::size_t size_type; 00104 typedef std::ptrdiff_t difference_type; 00105 typedef std::reverse_iterator<iterator> reverse_iterator; 00106 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00107 00108 // Support for zero-sized arrays mandatory. 00109 typedef _GLIBCXX_STD_C::__array_traits<_Tp, _Nm> _AT_Type; 00110 typename _AT_Type::_Type _M_elems; 00111 00112 // No explicit construct/copy/destroy for aggregate type. 00113 00114 // DR 776. 00115 void 00116 fill(const value_type& __u) 00117 { std::fill_n(begin(), size(), __u); } 00118 00119 void 00120 swap(array& __other) 00121 noexcept(_AT_Type::_Is_nothrow_swappable::value) 00122 { std::swap_ranges(begin(), end(), __other.begin()); } 00123 00124 // Iterators. 00125 _GLIBCXX17_CONSTEXPR iterator 00126 begin() noexcept 00127 { return iterator(data()); } 00128 00129 _GLIBCXX17_CONSTEXPR const_iterator 00130 begin() const noexcept 00131 { return const_iterator(data()); } 00132 00133 _GLIBCXX17_CONSTEXPR iterator 00134 end() noexcept 00135 { return iterator(data() + _Nm); } 00136 00137 _GLIBCXX17_CONSTEXPR const_iterator 00138 end() const noexcept 00139 { return const_iterator(data() + _Nm); } 00140 00141 _GLIBCXX17_CONSTEXPR reverse_iterator 00142 rbegin() noexcept 00143 { return reverse_iterator(end()); } 00144 00145 _GLIBCXX17_CONSTEXPR const_reverse_iterator 00146 rbegin() const noexcept 00147 { return const_reverse_iterator(end()); } 00148 00149 _GLIBCXX17_CONSTEXPR reverse_iterator 00150 rend() noexcept 00151 { return reverse_iterator(begin()); } 00152 00153 _GLIBCXX17_CONSTEXPR const_reverse_iterator 00154 rend() const noexcept 00155 { return const_reverse_iterator(begin()); } 00156 00157 _GLIBCXX17_CONSTEXPR const_iterator 00158 cbegin() const noexcept 00159 { return const_iterator(data()); } 00160 00161 _GLIBCXX17_CONSTEXPR const_iterator 00162 cend() const noexcept 00163 { return const_iterator(data() + _Nm); } 00164 00165 _GLIBCXX17_CONSTEXPR const_reverse_iterator 00166 crbegin() const noexcept 00167 { return const_reverse_iterator(end()); } 00168 00169 _GLIBCXX17_CONSTEXPR const_reverse_iterator 00170 crend() const noexcept 00171 { return const_reverse_iterator(begin()); } 00172 00173 // Capacity. 00174 constexpr size_type 00175 size() const noexcept { return _Nm; } 00176 00177 constexpr size_type 00178 max_size() const noexcept { return _Nm; } 00179 00180 constexpr bool 00181 empty() const noexcept { return size() == 0; } 00182 00183 // Element access. 00184 _GLIBCXX17_CONSTEXPR reference 00185 operator[](size_type __n) noexcept 00186 { return _AT_Type::_S_ref(_M_elems, __n); } 00187 00188 constexpr const_reference 00189 operator[](size_type __n) const noexcept 00190 { return _AT_Type::_S_ref(_M_elems, __n); } 00191 00192 _GLIBCXX17_CONSTEXPR reference 00193 at(size_type __n) 00194 { 00195 if (__n >= _Nm) 00196 std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) " 00197 ">= _Nm (which is %zu)"), 00198 __n, _Nm); 00199 return _AT_Type::_S_ref(_M_elems, __n); 00200 } 00201 00202 constexpr const_reference 00203 at(size_type __n) const 00204 { 00205 // Result of conditional expression must be an lvalue so use 00206 // boolean ? lvalue : (throw-expr, lvalue) 00207 return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n) 00208 : (std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) " 00209 ">= _Nm (which is %zu)"), 00210 __n, _Nm), 00211 _AT_Type::_S_ref(_M_elems, 0)); 00212 } 00213 00214 _GLIBCXX17_CONSTEXPR reference 00215 front() noexcept 00216 { return *begin(); } 00217 00218 constexpr const_reference 00219 front() const noexcept 00220 { return _AT_Type::_S_ref(_M_elems, 0); } 00221 00222 _GLIBCXX17_CONSTEXPR reference 00223 back() noexcept 00224 { return _Nm ? *(end() - 1) : *end(); } 00225 00226 constexpr const_reference 00227 back() const noexcept 00228 { 00229 return _Nm ? _AT_Type::_S_ref(_M_elems, _Nm - 1) 00230 : _AT_Type::_S_ref(_M_elems, 0); 00231 } 00232 00233 _GLIBCXX17_CONSTEXPR pointer 00234 data() noexcept 00235 { return _AT_Type::_S_ptr(_M_elems); } 00236 00237 _GLIBCXX17_CONSTEXPR const_pointer 00238 data() const noexcept 00239 { return _AT_Type::_S_ptr(_M_elems); } 00240 }; 00241 00242 #if __cpp_deduction_guides >= 201606 00243 template<typename _Tp, typename... _Up> 00244 array(_Tp, _Up...) 00245 -> array<enable_if_t<(is_same_v<_Tp, _Up> && ...), _Tp>, 00246 1 + sizeof...(_Up)>; 00247 #endif 00248 00249 // Array comparisons. 00250 template<typename _Tp, std::size_t _Nm> 00251 inline bool 00252 operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) 00253 { return std::equal(__one.begin(), __one.end(), __two.begin()); } 00254 00255 template<typename _Tp, std::size_t _Nm> 00256 inline bool 00257 operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) 00258 { return !(__one == __two); } 00259 00260 template<typename _Tp, std::size_t _Nm> 00261 inline bool 00262 operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b) 00263 { 00264 return std::lexicographical_compare(__a.begin(), __a.end(), 00265 __b.begin(), __b.end()); 00266 } 00267 00268 template<typename _Tp, std::size_t _Nm> 00269 inline bool 00270 operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) 00271 { return __two < __one; } 00272 00273 template<typename _Tp, std::size_t _Nm> 00274 inline bool 00275 operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) 00276 { return !(__one > __two); } 00277 00278 template<typename _Tp, std::size_t _Nm> 00279 inline bool 00280 operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) 00281 { return !(__one < __two); } 00282 00283 // Specialized algorithms. 00284 template<typename _Tp, std::size_t _Nm> 00285 inline 00286 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 00287 // Constrained free swap overload, see p0185r1 00288 typename enable_if< 00289 _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::_Is_swappable::value 00290 >::type 00291 #else 00292 void 00293 #endif 00294 swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two) 00295 noexcept(noexcept(__one.swap(__two))) 00296 { __one.swap(__two); } 00297 00298 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 00299 template<typename _Tp, std::size_t _Nm> 00300 typename enable_if< 00301 !_GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::_Is_swappable::value>::type 00302 swap(array<_Tp, _Nm>&, array<_Tp, _Nm>&) = delete; 00303 #endif 00304 00305 template<std::size_t _Int, typename _Tp, std::size_t _Nm> 00306 constexpr _Tp& 00307 get(array<_Tp, _Nm>& __arr) noexcept 00308 { 00309 static_assert(_Int < _Nm, "array index is within bounds"); 00310 return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>:: 00311 _S_ref(__arr._M_elems, _Int); 00312 } 00313 00314 template<std::size_t _Int, typename _Tp, std::size_t _Nm> 00315 constexpr _Tp&& 00316 get(array<_Tp, _Nm>&& __arr) noexcept 00317 { 00318 static_assert(_Int < _Nm, "array index is within bounds"); 00319 return std::move(_GLIBCXX_STD_C::get<_Int>(__arr)); 00320 } 00321 00322 template<std::size_t _Int, typename _Tp, std::size_t _Nm> 00323 constexpr const _Tp& 00324 get(const array<_Tp, _Nm>& __arr) noexcept 00325 { 00326 static_assert(_Int < _Nm, "array index is within bounds"); 00327 return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>:: 00328 _S_ref(__arr._M_elems, _Int); 00329 } 00330 00331 _GLIBCXX_END_NAMESPACE_CONTAINER 00332 } // namespace std 00333 00334 namespace std _GLIBCXX_VISIBILITY(default) 00335 { 00336 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00337 00338 // Tuple interface to class template array. 00339 00340 /// tuple_size 00341 template<typename _Tp> 00342 class tuple_size; 00343 00344 /// Partial specialization for std::array 00345 template<typename _Tp, std::size_t _Nm> 00346 struct tuple_size<_GLIBCXX_STD_C::array<_Tp, _Nm>> 00347 : public integral_constant<std::size_t, _Nm> { }; 00348 00349 /// tuple_element 00350 template<std::size_t _Int, typename _Tp> 00351 class tuple_element; 00352 00353 /// Partial specialization for std::array 00354 template<std::size_t _Int, typename _Tp, std::size_t _Nm> 00355 struct tuple_element<_Int, _GLIBCXX_STD_C::array<_Tp, _Nm>> 00356 { 00357 static_assert(_Int < _Nm, "index is out of bounds"); 00358 typedef _Tp type; 00359 }; 00360 00361 template<typename _Tp, std::size_t _Nm> 00362 struct __is_tuple_like_impl<_GLIBCXX_STD_C::array<_Tp, _Nm>> : true_type 00363 { }; 00364 00365 _GLIBCXX_END_NAMESPACE_VERSION 00366 } // namespace std 00367 00368 #ifdef _GLIBCXX_DEBUG 00369 # include <debug/array> 00370 #endif 00371 00372 #ifdef _GLIBCXX_PROFILE 00373 # include <profile/array> 00374 #endif 00375 00376 #endif // C++11 00377 00378 #endif // _GLIBCXX_ARRAY