1 // class template array -*- C++ -*-
3 // Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
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/>.
27 * This is a TR1 C++ Library header.
30 #ifndef _GLIBCXX_TR1_ARRAY
31 #define _GLIBCXX_TR1_ARRAY 1
33 #pragma GCC system_header
35 #include <bits/stl_algobase.h>
37 namespace std _GLIBCXX_VISIBILITY(default)
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 * @brief A standard container for storing a fixed size sequence of elements.
48 * Meets the requirements of a <a href="tables.html#65">container</a>, a
49 * <a href="tables.html#66">reversible container</a>, and a
50 * <a href="tables.html#67">sequence</a>.
52 * Sets support random access iterators.
54 * @param Tp Type of element. Required to be a complete type.
55 * @param N Number of elements.
57 template<typename _Tp, std::size_t _Nm>
60 typedef _Tp value_type;
61 typedef value_type& reference;
62 typedef const value_type& const_reference;
63 typedef value_type* iterator;
64 typedef const value_type* const_iterator;
65 typedef std::size_t size_type;
66 typedef std::ptrdiff_t difference_type;
67 typedef std::reverse_iterator<iterator> reverse_iterator;
68 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
70 // Support for zero-sized arrays mandatory.
71 value_type _M_instance[_Nm ? _Nm : 1];
73 // No explicit construct/copy/destroy for aggregate type.
76 assign(const value_type& __u)
77 { std::fill_n(begin(), size(), __u); }
81 { std::swap_ranges(begin(), end(), __other.begin()); }
86 { return iterator(std::__addressof(_M_instance[0])); }
90 { return const_iterator(std::__addressof(_M_instance[0])); }
94 { return iterator(std::__addressof(_M_instance[_Nm])); }
98 { return const_iterator(std::__addressof(_M_instance[_Nm])); }
102 { return reverse_iterator(end()); }
104 const_reverse_iterator
106 { return const_reverse_iterator(end()); }
110 { return reverse_iterator(begin()); }
112 const_reverse_iterator
114 { return const_reverse_iterator(begin()); }
118 size() const { return _Nm; }
121 max_size() const { return _Nm; }
124 empty() const { return size() == 0; }
128 operator[](size_type __n)
129 { return _M_instance[__n]; }
132 operator[](size_type __n) const
133 { return _M_instance[__n]; }
139 std::__throw_out_of_range(__N("array::at"));
140 return _M_instance[__n];
144 at(size_type __n) const
147 std::__throw_out_of_range(__N("array::at"));
148 return _M_instance[__n];
161 { return _Nm ? *(end() - 1) : *end(); }
165 { return _Nm ? *(end() - 1) : *end(); }
169 { return std::__addressof(_M_instance[0]); }
173 { return std::__addressof(_M_instance[0]); }
176 // Array comparisons.
177 template<typename _Tp, std::size_t _Nm>
179 operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
180 { return std::equal(__one.begin(), __one.end(), __two.begin()); }
182 template<typename _Tp, std::size_t _Nm>
184 operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
185 { return !(__one == __two); }
187 template<typename _Tp, std::size_t _Nm>
189 operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
191 return std::lexicographical_compare(__a.begin(), __a.end(),
192 __b.begin(), __b.end());
195 template<typename _Tp, std::size_t _Nm>
197 operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
198 { return __two < __one; }
200 template<typename _Tp, std::size_t _Nm>
202 operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
203 { return !(__one > __two); }
205 template<typename _Tp, std::size_t _Nm>
207 operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
208 { return !(__one < __two); }
210 // Specialized algorithms [6.2.2.2].
211 template<typename _Tp, std::size_t _Nm>
213 swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
214 { __one.swap(__two); }
216 // Tuple interface to class template array [6.2.2.5].
219 template<typename _Tp>
223 template<int _Int, typename _Tp>
226 template<typename _Tp, std::size_t _Nm>
227 struct tuple_size<array<_Tp, _Nm> >
228 { static const int value = _Nm; };
230 template<typename _Tp, std::size_t _Nm>
232 tuple_size<array<_Tp, _Nm> >::value;
234 template<int _Int, typename _Tp, std::size_t _Nm>
235 struct tuple_element<_Int, array<_Tp, _Nm> >
236 { typedef _Tp type; };
238 template<int _Int, typename _Tp, std::size_t _Nm>
240 get(array<_Tp, _Nm>& __arr)
241 { return __arr[_Int]; }
243 template<int _Int, typename _Tp, std::size_t _Nm>
245 get(const array<_Tp, _Nm>& __arr)
246 { return __arr[_Int]; }
248 _GLIBCXX_END_NAMESPACE_VERSION
252 #endif // _GLIBCXX_TR1_ARRAY