libstdc++
|
00001 // class template array -*- C++ -*- 00002 00003 // Copyright (C) 2007, 2008, 2009 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 tr1_impl/array 00026 * This is an internal header file, included by other library headers. 00027 * You should not attempt to use it directly. 00028 */ 00029 00030 namespace std 00031 { 00032 _GLIBCXX_BEGIN_NAMESPACE_TR1 00033 00034 /** 00035 * @brief A standard container for storing a fixed size sequence of elements. 00036 * 00037 * @ingroup sequences 00038 * 00039 * Meets the requirements of a <a href="tables.html#65">container</a>, a 00040 * <a href="tables.html#66">reversible container</a>, and a 00041 * <a href="tables.html#67">sequence</a>. 00042 * 00043 * Sets support random access iterators. 00044 * 00045 * @param Tp Type of element. Required to be a complete type. 00046 * @param N Number of elements. 00047 */ 00048 template<typename _Tp, std::size_t _Nm> 00049 struct array 00050 { 00051 typedef _Tp value_type; 00052 typedef value_type& reference; 00053 typedef const value_type& const_reference; 00054 typedef value_type* iterator; 00055 typedef const value_type* const_iterator; 00056 typedef std::size_t size_type; 00057 typedef std::ptrdiff_t difference_type; 00058 typedef std::reverse_iterator<iterator> reverse_iterator; 00059 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00060 00061 // Support for zero-sized arrays mandatory. 00062 value_type _M_instance[_Nm ? _Nm : 1]; 00063 00064 // No explicit construct/copy/destroy for aggregate type. 00065 00066 void 00067 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X 00068 // DR 776. 00069 fill(const value_type& __u) 00070 #else 00071 assign(const value_type& __u) 00072 #endif 00073 { std::fill_n(begin(), size(), __u); } 00074 00075 void 00076 swap(array& __other) 00077 { std::swap_ranges(begin(), end(), __other.begin()); } 00078 00079 // Iterators. 00080 iterator 00081 begin() 00082 { return iterator(&_M_instance[0]); } 00083 00084 const_iterator 00085 begin() const 00086 { return const_iterator(&_M_instance[0]); } 00087 00088 iterator 00089 end() 00090 { return iterator(&_M_instance[_Nm]); } 00091 00092 const_iterator 00093 end() const 00094 { return const_iterator(&_M_instance[_Nm]); } 00095 00096 reverse_iterator 00097 rbegin() 00098 { return reverse_iterator(end()); } 00099 00100 const_reverse_iterator 00101 rbegin() const 00102 { return const_reverse_iterator(end()); } 00103 00104 reverse_iterator 00105 rend() 00106 { return reverse_iterator(begin()); } 00107 00108 const_reverse_iterator 00109 rend() const 00110 { return const_reverse_iterator(begin()); } 00111 00112 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X 00113 const_iterator 00114 cbegin() const 00115 { return const_iterator(&_M_instance[0]); } 00116 00117 const_iterator 00118 cend() const 00119 { return const_iterator(&_M_instance[_Nm]); } 00120 00121 const_reverse_iterator 00122 crbegin() const 00123 { return const_reverse_iterator(end()); } 00124 00125 const_reverse_iterator 00126 crend() const 00127 { return const_reverse_iterator(begin()); } 00128 #endif 00129 00130 // Capacity. 00131 size_type 00132 size() const { return _Nm; } 00133 00134 size_type 00135 max_size() const { return _Nm; } 00136 00137 bool 00138 empty() const { return size() == 0; } 00139 00140 // Element access. 00141 reference 00142 operator[](size_type __n) 00143 { return _M_instance[__n]; } 00144 00145 const_reference 00146 operator[](size_type __n) const 00147 { return _M_instance[__n]; } 00148 00149 reference 00150 at(size_type __n) 00151 { 00152 if (__builtin_expect(__n >= _Nm, false)) 00153 std::__throw_out_of_range(__N("array::at")); 00154 return _M_instance[__n]; 00155 } 00156 00157 const_reference 00158 at(size_type __n) const 00159 { 00160 if (__builtin_expect(__n >= _Nm, false)) 00161 std::__throw_out_of_range(__N("array::at")); 00162 return _M_instance[__n]; 00163 } 00164 00165 reference 00166 front() 00167 { return *begin(); } 00168 00169 const_reference 00170 front() const 00171 { return *begin(); } 00172 00173 reference 00174 back() 00175 { return _Nm ? *(end() - 1) : *end(); } 00176 00177 const_reference 00178 back() const 00179 { return _Nm ? *(end() - 1) : *end(); } 00180 00181 _Tp* 00182 data() 00183 { return &_M_instance[0]; } 00184 00185 const _Tp* 00186 data() const 00187 { return &_M_instance[0]; } 00188 }; 00189 00190 // Array comparisons. 00191 template<typename _Tp, std::size_t _Nm> 00192 inline bool 00193 operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) 00194 { return std::equal(__one.begin(), __one.end(), __two.begin()); } 00195 00196 template<typename _Tp, std::size_t _Nm> 00197 inline bool 00198 operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) 00199 { return !(__one == __two); } 00200 00201 template<typename _Tp, std::size_t _Nm> 00202 inline bool 00203 operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b) 00204 { 00205 return std::lexicographical_compare(__a.begin(), __a.end(), 00206 __b.begin(), __b.end()); 00207 } 00208 00209 template<typename _Tp, std::size_t _Nm> 00210 inline bool 00211 operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) 00212 { return __two < __one; } 00213 00214 template<typename _Tp, std::size_t _Nm> 00215 inline bool 00216 operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) 00217 { return !(__one > __two); } 00218 00219 template<typename _Tp, std::size_t _Nm> 00220 inline bool 00221 operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) 00222 { return !(__one < __two); } 00223 00224 // Specialized algorithms [6.2.2.2]. 00225 template<typename _Tp, std::size_t _Nm> 00226 inline void 00227 swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two) 00228 { std::swap_ranges(__one.begin(), __one.end(), __two.begin()); } 00229 00230 // Tuple interface to class template array [6.2.2.5]. 00231 00232 /// tuple_size 00233 template<typename _Tp> 00234 class tuple_size; 00235 00236 /// tuple_element 00237 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X 00238 template<std::size_t _Int, typename _Tp> 00239 #else 00240 template<int _Int, typename _Tp> 00241 #endif 00242 class tuple_element; 00243 00244 template<typename _Tp, std::size_t _Nm> 00245 struct tuple_size<array<_Tp, _Nm> > 00246 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X 00247 { static const std::size_t value = _Nm; }; 00248 #else 00249 { static const int value = _Nm; }; 00250 #endif 00251 00252 template<typename _Tp, std::size_t _Nm> 00253 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X 00254 const std::size_t 00255 #else 00256 const int 00257 #endif 00258 tuple_size<array<_Tp, _Nm> >::value; 00259 00260 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X 00261 template<std::size_t _Int, typename _Tp, std::size_t _Nm> 00262 #else 00263 template<int _Int, typename _Tp, std::size_t _Nm> 00264 #endif 00265 struct tuple_element<_Int, array<_Tp, _Nm> > 00266 { typedef _Tp type; }; 00267 00268 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X 00269 template<std::size_t _Int, typename _Tp, std::size_t _Nm> 00270 #else 00271 template<int _Int, typename _Tp, std::size_t _Nm> 00272 #endif 00273 inline _Tp& 00274 get(array<_Tp, _Nm>& __arr) 00275 { return __arr[_Int]; } 00276 00277 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X 00278 template<std::size_t _Int, typename _Tp, std::size_t _Nm> 00279 #else 00280 template<int _Int, typename _Tp, std::size_t _Nm> 00281 #endif 00282 inline const _Tp& 00283 get(const array<_Tp, _Nm>& __arr) 00284 { return __arr[_Int]; } 00285 00286 _GLIBCXX_END_NAMESPACE_TR1 00287 }