]>
Commit | Line | Data |
---|---|---|
af13a7a6 BK |
1 | // <array> -*- C++ -*- |
2 | ||
9771644a PC |
3 | // Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 |
4 | // Free Software Foundation, Inc. | |
af13a7a6 BK |
5 | // |
6 | // This file is part of the GNU ISO C++ Library. This library is free | |
7 | // software; you can redistribute it and/or modify it under the | |
8 | // terms of the GNU General Public License as published by the | |
748086b7 | 9 | // Free Software Foundation; either version 3, or (at your option) |
af13a7a6 BK |
10 | // any later version. |
11 | ||
12 | // This library is distributed in the hope that it will be useful, | |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | // GNU General Public License for more details. | |
16 | ||
748086b7 JJ |
17 | // Under Section 7 of GPL version 3, you are granted additional |
18 | // permissions described in the GCC Runtime Library Exception, version | |
19 | // 3.1, as published by the Free Software Foundation. | |
af13a7a6 | 20 | |
748086b7 JJ |
21 | // You should have received a copy of the GNU General Public License and |
22 | // a copy of the GCC Runtime Library Exception along with this program; | |
23 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
24 | // <http://www.gnu.org/licenses/>. | |
af13a7a6 BK |
25 | |
26 | /** @file include/array | |
27 | * This is a Standard C++ Library header. | |
28 | */ | |
29 | ||
4514bed6 BK |
30 | #ifndef _GLIBCXX_ARRAY |
31 | #define _GLIBCXX_ARRAY 1 | |
af13a7a6 BK |
32 | |
33 | #pragma GCC system_header | |
34 | ||
e133ace8 | 35 | #ifndef __GXX_EXPERIMENTAL_CXX0X__ |
ab65a4c7 | 36 | # include <bits/c++0x_warning.h> |
57317d2a | 37 | #else |
af13a7a6 | 38 | |
bfef3a71 | 39 | #include <stdexcept> |
e133ace8 | 40 | #include <bits/stl_algobase.h> |
f67a9881 | 41 | #include <bits/range_access.h> |
e133ace8 | 42 | |
12ffa228 BK |
43 | namespace std _GLIBCXX_VISIBILITY(default) |
44 | { | |
45 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
53dc5044 PC |
46 | |
47 | /** | |
48 | * @brief A standard container for storing a fixed size sequence of elements. | |
49 | * | |
50 | * @ingroup sequences | |
51 | * | |
52 | * Meets the requirements of a <a href="tables.html#65">container</a>, a | |
53 | * <a href="tables.html#66">reversible container</a>, and a | |
54 | * <a href="tables.html#67">sequence</a>. | |
55 | * | |
56 | * Sets support random access iterators. | |
57 | * | |
58 | * @param Tp Type of element. Required to be a complete type. | |
59 | * @param N Number of elements. | |
60 | */ | |
61 | template<typename _Tp, std::size_t _Nm> | |
62 | struct array | |
63 | { | |
64 | typedef _Tp value_type; | |
a7d0c94e BK |
65 | typedef value_type* pointer; |
66 | typedef const value_type* const_pointer; | |
53dc5044 PC |
67 | typedef value_type& reference; |
68 | typedef const value_type& const_reference; | |
69 | typedef value_type* iterator; | |
70 | typedef const value_type* const_iterator; | |
71 | typedef std::size_t size_type; | |
72 | typedef std::ptrdiff_t difference_type; | |
73 | typedef std::reverse_iterator<iterator> reverse_iterator; | |
74 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; | |
75 | ||
76 | // Support for zero-sized arrays mandatory. | |
77 | value_type _M_instance[_Nm ? _Nm : 1]; | |
78 | ||
79 | // No explicit construct/copy/destroy for aggregate type. | |
80 | ||
81 | // DR 776. | |
82 | void | |
83 | fill(const value_type& __u) | |
84 | { std::fill_n(begin(), size(), __u); } | |
85 | ||
86 | void | |
87 | swap(array& __other) | |
18eeaec4 | 88 | noexcept(noexcept(swap(std::declval<_Tp&>(), std::declval<_Tp&>()))) |
53dc5044 PC |
89 | { std::swap_ranges(begin(), end(), __other.begin()); } |
90 | ||
91 | // Iterators. | |
92 | iterator | |
18eeaec4 | 93 | begin() noexcept |
a7d0c94e | 94 | { return iterator(data()); } |
53dc5044 PC |
95 | |
96 | const_iterator | |
18eeaec4 | 97 | begin() const noexcept |
a7d0c94e | 98 | { return const_iterator(data()); } |
53dc5044 PC |
99 | |
100 | iterator | |
18eeaec4 | 101 | end() noexcept |
a7d0c94e | 102 | { return iterator(data() + _Nm); } |
53dc5044 PC |
103 | |
104 | const_iterator | |
18eeaec4 | 105 | end() const noexcept |
a7d0c94e | 106 | { return const_iterator(data() + _Nm); } |
53dc5044 PC |
107 | |
108 | reverse_iterator | |
18eeaec4 | 109 | rbegin() noexcept |
53dc5044 PC |
110 | { return reverse_iterator(end()); } |
111 | ||
112 | const_reverse_iterator | |
18eeaec4 | 113 | rbegin() const noexcept |
53dc5044 PC |
114 | { return const_reverse_iterator(end()); } |
115 | ||
116 | reverse_iterator | |
18eeaec4 | 117 | rend() noexcept |
53dc5044 PC |
118 | { return reverse_iterator(begin()); } |
119 | ||
120 | const_reverse_iterator | |
18eeaec4 | 121 | rend() const noexcept |
53dc5044 PC |
122 | { return const_reverse_iterator(begin()); } |
123 | ||
124 | const_iterator | |
18eeaec4 | 125 | cbegin() const noexcept |
53dc5044 PC |
126 | { return const_iterator(std::__addressof(_M_instance[0])); } |
127 | ||
128 | const_iterator | |
18eeaec4 | 129 | cend() const noexcept |
53dc5044 PC |
130 | { return const_iterator(std::__addressof(_M_instance[_Nm])); } |
131 | ||
132 | const_reverse_iterator | |
18eeaec4 | 133 | crbegin() const noexcept |
53dc5044 PC |
134 | { return const_reverse_iterator(end()); } |
135 | ||
136 | const_reverse_iterator | |
18eeaec4 | 137 | crend() const noexcept |
53dc5044 PC |
138 | { return const_reverse_iterator(begin()); } |
139 | ||
140 | // Capacity. | |
141 | constexpr size_type | |
18eeaec4 | 142 | size() const noexcept { return _Nm; } |
53dc5044 PC |
143 | |
144 | constexpr size_type | |
18eeaec4 | 145 | max_size() const noexcept { return _Nm; } |
53dc5044 PC |
146 | |
147 | constexpr bool | |
18eeaec4 | 148 | empty() const noexcept { return size() == 0; } |
53dc5044 PC |
149 | |
150 | // Element access. | |
151 | reference | |
152 | operator[](size_type __n) | |
153 | { return _M_instance[__n]; } | |
154 | ||
bfef3a71 BK |
155 | constexpr const_reference |
156 | operator[](size_type __n) const noexcept | |
53dc5044 PC |
157 | { return _M_instance[__n]; } |
158 | ||
159 | reference | |
160 | at(size_type __n) | |
161 | { | |
162 | if (__n >= _Nm) | |
163 | std::__throw_out_of_range(__N("array::at")); | |
164 | return _M_instance[__n]; | |
165 | } | |
166 | ||
9bc13c23 | 167 | #ifdef __EXCEPTIONS |
bfef3a71 | 168 | constexpr const_reference |
53dc5044 PC |
169 | at(size_type __n) const |
170 | { | |
9bc13c23 BK |
171 | return __n < _Nm ? |
172 | _M_instance[__n] : throw out_of_range(__N("array::at")); | |
173 | } | |
bfef3a71 | 174 | #else |
9bc13c23 BK |
175 | const_reference |
176 | at(size_type __n) const | |
177 | { | |
4b74f2b4 PC |
178 | if (__n >= _Nm) |
179 | std::__throw_out_of_range(__N("array::at")); | |
180 | return _M_instance[__n]; | |
53dc5044 | 181 | } |
9bc13c23 | 182 | #endif |
53dc5044 PC |
183 | |
184 | reference | |
185 | front() | |
186 | { return *begin(); } | |
187 | ||
188 | const_reference | |
189 | front() const | |
190 | { return *begin(); } | |
191 | ||
192 | reference | |
193 | back() | |
194 | { return _Nm ? *(end() - 1) : *end(); } | |
195 | ||
196 | const_reference | |
197 | back() const | |
198 | { return _Nm ? *(end() - 1) : *end(); } | |
199 | ||
a7d0c94e | 200 | pointer |
18eeaec4 | 201 | data() noexcept |
53dc5044 PC |
202 | { return std::__addressof(_M_instance[0]); } |
203 | ||
a7d0c94e | 204 | const_pointer |
18eeaec4 | 205 | data() const noexcept |
53dc5044 PC |
206 | { return std::__addressof(_M_instance[0]); } |
207 | }; | |
208 | ||
209 | // Array comparisons. | |
210 | template<typename _Tp, std::size_t _Nm> | |
211 | inline bool | |
212 | operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) | |
213 | { return std::equal(__one.begin(), __one.end(), __two.begin()); } | |
214 | ||
215 | template<typename _Tp, std::size_t _Nm> | |
216 | inline bool | |
217 | operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) | |
218 | { return !(__one == __two); } | |
219 | ||
220 | template<typename _Tp, std::size_t _Nm> | |
221 | inline bool | |
222 | operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b) | |
223 | { | |
224 | return std::lexicographical_compare(__a.begin(), __a.end(), | |
225 | __b.begin(), __b.end()); | |
226 | } | |
227 | ||
228 | template<typename _Tp, std::size_t _Nm> | |
229 | inline bool | |
230 | operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) | |
231 | { return __two < __one; } | |
232 | ||
233 | template<typename _Tp, std::size_t _Nm> | |
234 | inline bool | |
235 | operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) | |
236 | { return !(__one > __two); } | |
237 | ||
238 | template<typename _Tp, std::size_t _Nm> | |
239 | inline bool | |
240 | operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) | |
241 | { return !(__one < __two); } | |
242 | ||
18eeaec4 | 243 | // Specialized algorithms. |
53dc5044 PC |
244 | template<typename _Tp, std::size_t _Nm> |
245 | inline void | |
246 | swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two) | |
18eeaec4 | 247 | noexcept(noexcept(__one.swap(__two))) |
53dc5044 PC |
248 | { __one.swap(__two); } |
249 | ||
18eeaec4 | 250 | // Tuple interface to class template array. |
53dc5044 PC |
251 | |
252 | /// tuple_size | |
253 | template<typename _Tp> | |
254 | class tuple_size; | |
255 | ||
664e12c1 PC |
256 | template<typename _Tp, std::size_t _Nm> |
257 | struct tuple_size<array<_Tp, _Nm>> | |
258 | : public integral_constant<std::size_t, _Nm> { }; | |
259 | ||
53dc5044 PC |
260 | /// tuple_element |
261 | template<std::size_t _Int, typename _Tp> | |
262 | class tuple_element; | |
263 | ||
53dc5044 | 264 | template<std::size_t _Int, typename _Tp, std::size_t _Nm> |
9771644a PC |
265 | struct tuple_element<_Int, array<_Tp, _Nm>> |
266 | { | |
267 | static_assert(_Int < _Nm, "index is out of bounds"); | |
268 | typedef _Tp type; | |
269 | }; | |
53dc5044 PC |
270 | |
271 | template<std::size_t _Int, typename _Tp, std::size_t _Nm> | |
9480716c | 272 | constexpr _Tp& |
18eeaec4 | 273 | get(array<_Tp, _Nm>& __arr) noexcept |
9771644a PC |
274 | { |
275 | static_assert(_Int < _Nm, "index is out of bounds"); | |
276 | return __arr._M_instance[_Int]; | |
277 | } | |
53dc5044 | 278 | |
18eeaec4 | 279 | template<std::size_t _Int, typename _Tp, std::size_t _Nm> |
9480716c | 280 | constexpr _Tp&& |
18eeaec4 | 281 | get(array<_Tp, _Nm>&& __arr) noexcept |
9771644a PC |
282 | { |
283 | static_assert(_Int < _Nm, "index is out of bounds"); | |
284 | return std::move(get<_Int>(__arr)); | |
285 | } | |
18eeaec4 | 286 | |
53dc5044 | 287 | template<std::size_t _Int, typename _Tp, std::size_t _Nm> |
9480716c | 288 | constexpr const _Tp& |
18eeaec4 | 289 | get(const array<_Tp, _Nm>& __arr) noexcept |
9771644a PC |
290 | { |
291 | static_assert(_Int < _Nm, "index is out of bounds"); | |
292 | return __arr._M_instance[_Int]; | |
293 | } | |
53dc5044 | 294 | |
12ffa228 BK |
295 | _GLIBCXX_END_NAMESPACE_VERSION |
296 | } // namespace | |
af13a7a6 | 297 | |
57317d2a PC |
298 | #endif // __GXX_EXPERIMENTAL_CXX0X__ |
299 | ||
4514bed6 | 300 | #endif // _GLIBCXX_ARRAY |