]>
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 | |
e133ace8 | 38 | #include <bits/stl_algobase.h> |
f67a9881 | 39 | #include <bits/range_access.h> |
e133ace8 | 40 | |
12ffa228 BK |
41 | namespace std _GLIBCXX_VISIBILITY(default) |
42 | { | |
43 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
53dc5044 PC |
44 | |
45 | /** | |
46 | * @brief A standard container for storing a fixed size sequence of elements. | |
47 | * | |
48 | * @ingroup sequences | |
49 | * | |
50 | * Meets the requirements of a <a href="tables.html#65">container</a>, a | |
51 | * <a href="tables.html#66">reversible container</a>, and a | |
52 | * <a href="tables.html#67">sequence</a>. | |
53 | * | |
54 | * Sets support random access iterators. | |
55 | * | |
56 | * @param Tp Type of element. Required to be a complete type. | |
57 | * @param N Number of elements. | |
58 | */ | |
59 | template<typename _Tp, std::size_t _Nm> | |
60 | struct array | |
61 | { | |
62 | typedef _Tp value_type; | |
63 | typedef _Tp* pointer; | |
64 | typedef const _Tp* const_pointer; | |
65 | typedef value_type& reference; | |
66 | typedef const value_type& const_reference; | |
67 | typedef value_type* iterator; | |
68 | typedef const value_type* const_iterator; | |
69 | typedef std::size_t size_type; | |
70 | typedef std::ptrdiff_t difference_type; | |
71 | typedef std::reverse_iterator<iterator> reverse_iterator; | |
72 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; | |
73 | ||
74 | // Support for zero-sized arrays mandatory. | |
75 | value_type _M_instance[_Nm ? _Nm : 1]; | |
76 | ||
77 | // No explicit construct/copy/destroy for aggregate type. | |
78 | ||
79 | // DR 776. | |
80 | void | |
81 | fill(const value_type& __u) | |
82 | { std::fill_n(begin(), size(), __u); } | |
83 | ||
84 | void | |
85 | swap(array& __other) | |
18eeaec4 | 86 | noexcept(noexcept(swap(std::declval<_Tp&>(), std::declval<_Tp&>()))) |
53dc5044 PC |
87 | { std::swap_ranges(begin(), end(), __other.begin()); } |
88 | ||
89 | // Iterators. | |
90 | iterator | |
18eeaec4 | 91 | begin() noexcept |
53dc5044 PC |
92 | { return iterator(std::__addressof(_M_instance[0])); } |
93 | ||
94 | const_iterator | |
18eeaec4 | 95 | begin() const noexcept |
53dc5044 PC |
96 | { return const_iterator(std::__addressof(_M_instance[0])); } |
97 | ||
98 | iterator | |
18eeaec4 | 99 | end() noexcept |
53dc5044 PC |
100 | { return iterator(std::__addressof(_M_instance[_Nm])); } |
101 | ||
102 | const_iterator | |
18eeaec4 | 103 | end() const noexcept |
53dc5044 PC |
104 | { return const_iterator(std::__addressof(_M_instance[_Nm])); } |
105 | ||
106 | reverse_iterator | |
18eeaec4 | 107 | rbegin() noexcept |
53dc5044 PC |
108 | { return reverse_iterator(end()); } |
109 | ||
110 | const_reverse_iterator | |
18eeaec4 | 111 | rbegin() const noexcept |
53dc5044 PC |
112 | { return const_reverse_iterator(end()); } |
113 | ||
114 | reverse_iterator | |
18eeaec4 | 115 | rend() noexcept |
53dc5044 PC |
116 | { return reverse_iterator(begin()); } |
117 | ||
118 | const_reverse_iterator | |
18eeaec4 | 119 | rend() const noexcept |
53dc5044 PC |
120 | { return const_reverse_iterator(begin()); } |
121 | ||
122 | const_iterator | |
18eeaec4 | 123 | cbegin() const noexcept |
53dc5044 PC |
124 | { return const_iterator(std::__addressof(_M_instance[0])); } |
125 | ||
126 | const_iterator | |
18eeaec4 | 127 | cend() const noexcept |
53dc5044 PC |
128 | { return const_iterator(std::__addressof(_M_instance[_Nm])); } |
129 | ||
130 | const_reverse_iterator | |
18eeaec4 | 131 | crbegin() const noexcept |
53dc5044 PC |
132 | { return const_reverse_iterator(end()); } |
133 | ||
134 | const_reverse_iterator | |
18eeaec4 | 135 | crend() const noexcept |
53dc5044 PC |
136 | { return const_reverse_iterator(begin()); } |
137 | ||
138 | // Capacity. | |
139 | constexpr size_type | |
18eeaec4 | 140 | size() const noexcept { return _Nm; } |
53dc5044 PC |
141 | |
142 | constexpr size_type | |
18eeaec4 | 143 | max_size() const noexcept { return _Nm; } |
53dc5044 PC |
144 | |
145 | constexpr bool | |
18eeaec4 | 146 | empty() const noexcept { return size() == 0; } |
53dc5044 PC |
147 | |
148 | // Element access. | |
149 | reference | |
150 | operator[](size_type __n) | |
151 | { return _M_instance[__n]; } | |
152 | ||
153 | const_reference | |
154 | operator[](size_type __n) const | |
155 | { return _M_instance[__n]; } | |
156 | ||
157 | reference | |
158 | at(size_type __n) | |
159 | { | |
160 | if (__n >= _Nm) | |
161 | std::__throw_out_of_range(__N("array::at")); | |
162 | return _M_instance[__n]; | |
163 | } | |
164 | ||
165 | const_reference | |
166 | at(size_type __n) const | |
167 | { | |
168 | if (__n >= _Nm) | |
169 | std::__throw_out_of_range(__N("array::at")); | |
170 | return _M_instance[__n]; | |
171 | } | |
172 | ||
173 | reference | |
174 | front() | |
175 | { return *begin(); } | |
176 | ||
177 | const_reference | |
178 | front() const | |
179 | { return *begin(); } | |
180 | ||
181 | reference | |
182 | back() | |
183 | { return _Nm ? *(end() - 1) : *end(); } | |
184 | ||
185 | const_reference | |
186 | back() const | |
187 | { return _Nm ? *(end() - 1) : *end(); } | |
188 | ||
189 | _Tp* | |
18eeaec4 | 190 | data() noexcept |
53dc5044 PC |
191 | { return std::__addressof(_M_instance[0]); } |
192 | ||
193 | const _Tp* | |
18eeaec4 | 194 | data() const noexcept |
53dc5044 PC |
195 | { return std::__addressof(_M_instance[0]); } |
196 | }; | |
197 | ||
198 | // Array comparisons. | |
199 | template<typename _Tp, std::size_t _Nm> | |
200 | inline bool | |
201 | operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) | |
202 | { return std::equal(__one.begin(), __one.end(), __two.begin()); } | |
203 | ||
204 | template<typename _Tp, std::size_t _Nm> | |
205 | inline bool | |
206 | operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) | |
207 | { return !(__one == __two); } | |
208 | ||
209 | template<typename _Tp, std::size_t _Nm> | |
210 | inline bool | |
211 | operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b) | |
212 | { | |
213 | return std::lexicographical_compare(__a.begin(), __a.end(), | |
214 | __b.begin(), __b.end()); | |
215 | } | |
216 | ||
217 | template<typename _Tp, std::size_t _Nm> | |
218 | inline bool | |
219 | operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) | |
220 | { return __two < __one; } | |
221 | ||
222 | template<typename _Tp, std::size_t _Nm> | |
223 | inline bool | |
224 | operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) | |
225 | { return !(__one > __two); } | |
226 | ||
227 | template<typename _Tp, std::size_t _Nm> | |
228 | inline bool | |
229 | operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) | |
230 | { return !(__one < __two); } | |
231 | ||
18eeaec4 | 232 | // Specialized algorithms. |
53dc5044 PC |
233 | template<typename _Tp, std::size_t _Nm> |
234 | inline void | |
235 | swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two) | |
18eeaec4 | 236 | noexcept(noexcept(__one.swap(__two))) |
53dc5044 PC |
237 | { __one.swap(__two); } |
238 | ||
18eeaec4 | 239 | // Tuple interface to class template array. |
53dc5044 PC |
240 | |
241 | /// tuple_size | |
242 | template<typename _Tp> | |
243 | class tuple_size; | |
244 | ||
664e12c1 PC |
245 | template<typename _Tp, std::size_t _Nm> |
246 | struct tuple_size<array<_Tp, _Nm>> | |
247 | : public integral_constant<std::size_t, _Nm> { }; | |
248 | ||
53dc5044 PC |
249 | /// tuple_element |
250 | template<std::size_t _Int, typename _Tp> | |
251 | class tuple_element; | |
252 | ||
53dc5044 PC |
253 | template<std::size_t _Int, typename _Tp, std::size_t _Nm> |
254 | struct tuple_element<_Int, array<_Tp, _Nm> > | |
255 | { typedef _Tp type; }; | |
256 | ||
257 | template<std::size_t _Int, typename _Tp, std::size_t _Nm> | |
258 | inline _Tp& | |
18eeaec4 | 259 | get(array<_Tp, _Nm>& __arr) noexcept |
53dc5044 PC |
260 | { return __arr[_Int]; } |
261 | ||
18eeaec4 PC |
262 | template<std::size_t _Int, typename _Tp, std::size_t _Nm> |
263 | inline _Tp&& | |
264 | get(array<_Tp, _Nm>&& __arr) noexcept | |
265 | { return std::move(get<_Int>(__arr)); } | |
266 | ||
53dc5044 PC |
267 | template<std::size_t _Int, typename _Tp, std::size_t _Nm> |
268 | inline const _Tp& | |
18eeaec4 | 269 | get(const array<_Tp, _Nm>& __arr) noexcept |
53dc5044 PC |
270 | { return __arr[_Int]; } |
271 | ||
12ffa228 BK |
272 | _GLIBCXX_END_NAMESPACE_VERSION |
273 | } // namespace | |
af13a7a6 | 274 | |
57317d2a PC |
275 | #endif // __GXX_EXPERIMENTAL_CXX0X__ |
276 | ||
4514bed6 | 277 | #endif // _GLIBCXX_ARRAY |