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