1 // class template array -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
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
8 // Free Software Foundation; either version 2, or (at your option)
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.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 /** @file tr1_impl/array
31 * This is an internal header file, included by other library headers.
32 * You should not attempt to use it directly.
37 _GLIBCXX_BEGIN_NAMESPACE_TR1
40 * @brief A standard container for storing a fixed size sequence of elements.
45 * Meets the requirements of a <a href="tables.html#65">container</a>, a
46 * <a href="tables.html#66">reversible container</a>, and a
47 * <a href="tables.html#67">sequence</a>.
49 * Sets support random access iterators.
51 * @param Tp Type of element. Required to be a complete type.
52 * @param N Number of elements.
54 template<typename _Tp, std::size_t _Nm>
57 typedef _Tp value_type;
58 typedef value_type& reference;
59 typedef const value_type& const_reference;
60 typedef value_type* iterator;
61 typedef const value_type* const_iterator;
62 typedef std::size_t size_type;
63 typedef std::ptrdiff_t difference_type;
64 typedef std::reverse_iterator<iterator> reverse_iterator;
65 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
67 // Support for zero-sized arrays mandatory.
68 value_type _M_instance[_Nm ? _Nm : 1];
70 // No explicit construct/copy/destroy for aggregate type.
73 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
75 fill(const value_type& __u)
77 assign(const value_type& __u)
79 { std::fill_n(begin(), size(), __u); }
83 { std::swap_ranges(begin(), end(), __other.begin()); }
88 { return iterator(&_M_instance[0]); }
92 { return const_iterator(&_M_instance[0]); }
96 { return iterator(&_M_instance[_Nm]); }
100 { return const_iterator(&_M_instance[_Nm]); }
104 { return reverse_iterator(end()); }
106 const_reverse_iterator
108 { return const_reverse_iterator(end()); }
112 { return reverse_iterator(begin()); }
114 const_reverse_iterator
116 { return const_reverse_iterator(begin()); }
118 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
121 { return const_iterator(&_M_instance[0]); }
125 { return const_iterator(&_M_instance[_Nm]); }
127 const_reverse_iterator
129 { return const_reverse_iterator(end()); }
131 const_reverse_iterator
133 { return const_reverse_iterator(begin()); }
138 size() const { return _Nm; }
141 max_size() const { return _Nm; }
144 empty() const { return size() == 0; }
148 operator[](size_type __n)
149 { return _M_instance[__n]; }
152 operator[](size_type __n) const
153 { return _M_instance[__n]; }
158 if (__builtin_expect(__n >= _Nm, false))
159 std::__throw_out_of_range(__N("array::at"));
160 return _M_instance[__n];
164 at(size_type __n) const
166 if (__builtin_expect(__n >= _Nm, false))
167 std::__throw_out_of_range(__N("array::at"));
168 return _M_instance[__n];
181 { return _Nm ? *(end() - 1) : *end(); }
185 { return _Nm ? *(end() - 1) : *end(); }
189 { return &_M_instance[0]; }
193 { return &_M_instance[0]; }
196 // Array comparisons.
197 template<typename _Tp, std::size_t _Nm>
199 operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
200 { return std::equal(__one.begin(), __one.end(), __two.begin()); }
202 template<typename _Tp, std::size_t _Nm>
204 operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
205 { return !(__one == __two); }
207 template<typename _Tp, std::size_t _Nm>
209 operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
211 return std::lexicographical_compare(__a.begin(), __a.end(),
212 __b.begin(), __b.end());
215 template<typename _Tp, std::size_t _Nm>
217 operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
218 { return __two < __one; }
220 template<typename _Tp, std::size_t _Nm>
222 operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
223 { return !(__one > __two); }
225 template<typename _Tp, std::size_t _Nm>
227 operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
228 { return !(__one < __two); }
230 // Specialized algorithms [6.2.2.2].
231 template<typename _Tp, std::size_t _Nm>
233 swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
234 { std::swap_ranges(__one.begin(), __one.end(), __two.begin()); }
236 // Tuple interface to class template array [6.2.2.5].
239 template<typename _Tp>
243 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
244 template<std::size_t _Int, typename _Tp>
246 template<int _Int, typename _Tp>
250 template<typename _Tp, std::size_t _Nm>
251 struct tuple_size<array<_Tp, _Nm> >
252 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
253 { static const std::size_t value = _Nm; };
255 { static const int value = _Nm; };
258 template<typename _Tp, std::size_t _Nm>
259 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
264 tuple_size<array<_Tp, _Nm> >::value;
266 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
267 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
269 template<int _Int, typename _Tp, std::size_t _Nm>
271 struct tuple_element<_Int, array<_Tp, _Nm> >
272 { typedef _Tp type; };
274 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
275 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
277 template<int _Int, typename _Tp, std::size_t _Nm>
280 get(array<_Tp, _Nm>& __arr)
281 { return __arr[_Int]; }
283 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
284 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
286 template<int _Int, typename _Tp, std::size_t _Nm>
289 get(const array<_Tp, _Nm>& __arr)
290 { return __arr[_Int]; }
292 _GLIBCXX_END_NAMESPACE_TR1