3 // Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
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
9 // Free Software Foundation; either version 3, or (at your option)
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.
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.
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/>.
26 /** @file include/array
27 * This is a Standard C++ Library header.
30 #ifndef _GLIBCXX_ARRAY
31 #define _GLIBCXX_ARRAY 1
33 #pragma GCC system_header
35 #ifndef __GXX_EXPERIMENTAL_CXX0X__
36 # include <bits/c++0x_warning.h>
40 #include <bits/stl_algobase.h>
41 #include <bits/range_access.h>
43 namespace std _GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
48 * @brief A standard container for storing a fixed size sequence of elements.
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>.
56 * Sets support random access iterators.
58 * @param Tp Type of element. Required to be a complete type.
59 * @param N Number of elements.
61 template<typename _Tp, std::size_t _Nm>
64 typedef _Tp value_type;
65 typedef value_type* pointer;
66 typedef const value_type* const_pointer;
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;
76 // Support for zero-sized arrays mandatory.
77 value_type _M_instance[_Nm ? _Nm : 1];
79 // No explicit construct/copy/destroy for aggregate type.
83 fill(const value_type& __u)
84 { std::fill_n(begin(), size(), __u); }
88 noexcept(noexcept(swap(std::declval<_Tp&>(), std::declval<_Tp&>())))
89 { std::swap_ranges(begin(), end(), __other.begin()); }
94 { return iterator(data()); }
97 begin() const noexcept
98 { return const_iterator(data()); }
102 { return iterator(data() + _Nm); }
106 { return const_iterator(data() + _Nm); }
110 { return reverse_iterator(end()); }
112 const_reverse_iterator
113 rbegin() const noexcept
114 { return const_reverse_iterator(end()); }
118 { return reverse_iterator(begin()); }
120 const_reverse_iterator
121 rend() const noexcept
122 { return const_reverse_iterator(begin()); }
125 cbegin() const noexcept
126 { return const_iterator(std::__addressof(_M_instance[0])); }
129 cend() const noexcept
130 { return const_iterator(std::__addressof(_M_instance[_Nm])); }
132 const_reverse_iterator
133 crbegin() const noexcept
134 { return const_reverse_iterator(end()); }
136 const_reverse_iterator
137 crend() const noexcept
138 { return const_reverse_iterator(begin()); }
142 size() const noexcept { return _Nm; }
145 max_size() const noexcept { return _Nm; }
148 empty() const noexcept { return size() == 0; }
152 operator[](size_type __n)
153 { return _M_instance[__n]; }
155 constexpr const_reference
156 operator[](size_type __n) const noexcept
157 { return _M_instance[__n]; }
163 std::__throw_out_of_range(__N("array::at"));
164 return _M_instance[__n];
168 constexpr const_reference
169 at(size_type __n) const
172 _M_instance[__n] : throw out_of_range(__N("array::at"));
176 at(size_type __n) const
179 std::__throw_out_of_range(__N("array::at"));
180 return _M_instance[__n];
194 { return _Nm ? *(end() - 1) : *end(); }
198 { return _Nm ? *(end() - 1) : *end(); }
202 { return std::__addressof(_M_instance[0]); }
205 data() const noexcept
206 { return std::__addressof(_M_instance[0]); }
209 // Array comparisons.
210 template<typename _Tp, std::size_t _Nm>
212 operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
213 { return std::equal(__one.begin(), __one.end(), __two.begin()); }
215 template<typename _Tp, std::size_t _Nm>
217 operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
218 { return !(__one == __two); }
220 template<typename _Tp, std::size_t _Nm>
222 operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
224 return std::lexicographical_compare(__a.begin(), __a.end(),
225 __b.begin(), __b.end());
228 template<typename _Tp, std::size_t _Nm>
230 operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
231 { return __two < __one; }
233 template<typename _Tp, std::size_t _Nm>
235 operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
236 { return !(__one > __two); }
238 template<typename _Tp, std::size_t _Nm>
240 operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
241 { return !(__one < __two); }
243 // Specialized algorithms.
244 template<typename _Tp, std::size_t _Nm>
246 swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
247 noexcept(noexcept(__one.swap(__two)))
248 { __one.swap(__two); }
250 // Tuple interface to class template array.
253 template<typename _Tp>
256 template<typename _Tp, std::size_t _Nm>
257 struct tuple_size<array<_Tp, _Nm>>
258 : public integral_constant<std::size_t, _Nm> { };
261 template<std::size_t _Int, typename _Tp>
264 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
265 struct tuple_element<_Int, array<_Tp, _Nm> >
266 { typedef _Tp type; };
268 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
270 get(array<_Tp, _Nm>& __arr) noexcept
271 { return __arr._M_instance[_Int]; }
273 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
275 get(array<_Tp, _Nm>&& __arr) noexcept
276 { return std::move(get<_Int>(__arr)); }
278 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
280 get(const array<_Tp, _Nm>& __arr) noexcept
281 { return __arr._M_instance[_Int]; }
283 _GLIBCXX_END_NAMESPACE_VERSION
286 #endif // __GXX_EXPERIMENTAL_CXX0X__
288 #endif // _GLIBCXX_ARRAY