1 // class template array -*- C++ -*-
3 // Copyright (C) 2007, 2008 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 /// NB: Requires complete type _Tp.
41 template<typename _Tp, std::size_t _Nm>
44 typedef _Tp value_type;
45 typedef value_type& reference;
46 typedef const value_type& const_reference;
47 typedef value_type* iterator;
48 typedef const value_type* const_iterator;
49 typedef std::size_t size_type;
50 typedef std::ptrdiff_t difference_type;
51 typedef std::reverse_iterator<iterator> reverse_iterator;
52 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
54 // Support for zero-sized arrays mandatory.
55 value_type _M_instance[_Nm ? _Nm : 1];
57 // No explicit construct/copy/destroy for aggregate type.
60 assign(const value_type& __u)
61 { std::fill_n(begin(), size(), __u); }
65 { std::swap_ranges(begin(), end(), __other.begin()); }
70 { return iterator(&_M_instance[0]); }
74 { return const_iterator(&_M_instance[0]); }
78 { return iterator(&_M_instance[_Nm]); }
82 { return const_iterator(&_M_instance[_Nm]); }
86 { return reverse_iterator(end()); }
88 const_reverse_iterator
90 { return const_reverse_iterator(end()); }
94 { return reverse_iterator(begin()); }
96 const_reverse_iterator
98 { return const_reverse_iterator(begin()); }
100 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
103 { return const_iterator(&_M_instance[0]); }
107 { return const_iterator(&_M_instance[_Nm]); }
109 const_reverse_iterator
111 { return const_reverse_iterator(end()); }
113 const_reverse_iterator
115 { return const_reverse_iterator(begin()); }
120 size() const { return _Nm; }
123 max_size() const { return _Nm; }
126 empty() const { return size() == 0; }
130 operator[](size_type __n)
131 { return _M_instance[__n]; }
134 operator[](size_type __n) const
135 { return _M_instance[__n]; }
140 if (__builtin_expect(__n >= _Nm, false))
141 std::__throw_out_of_range(__N("array::at"));
142 return _M_instance[__n];
146 at(size_type __n) const
148 if (__builtin_expect(__n >= _Nm, false))
149 std::__throw_out_of_range(__N("array::at"));
150 return _M_instance[__n];
163 { return _Nm ? *(end() - 1) : *end(); }
167 { return _Nm ? *(end() - 1) : *end(); }
171 { return &_M_instance[0]; }
175 { return &_M_instance[0]; }
178 // Array comparisons.
179 template<typename _Tp, std::size_t _Nm>
181 operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
182 { return std::equal(__one.begin(), __one.end(), __two.begin()); }
184 template<typename _Tp, std::size_t _Nm>
186 operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
187 { return !(__one == __two); }
189 template<typename _Tp, std::size_t _Nm>
191 operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
193 return std::lexicographical_compare(__a.begin(), __a.end(),
194 __b.begin(), __b.end());
197 template<typename _Tp, std::size_t _Nm>
199 operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
200 { return __two < __one; }
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>& __one, const array<_Tp, _Nm>& __two)
210 { return !(__one < __two); }
212 // Specialized algorithms [6.2.2.2].
213 template<typename _Tp, std::size_t _Nm>
215 swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
216 { std::swap_ranges(__one.begin(), __one.end(), __two.begin()); }
218 // Tuple interface to class template array [6.2.2.5].
221 template<typename _Tp>
225 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
226 template<std::size_t _Int, typename _Tp>
228 template<int _Int, typename _Tp>
232 template<typename _Tp, std::size_t _Nm>
233 struct tuple_size<array<_Tp, _Nm> >
234 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
235 { static const std::size_t value = _Nm; };
237 { static const int value = _Nm; };
240 template<typename _Tp, std::size_t _Nm>
241 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
246 tuple_size<array<_Tp, _Nm> >::value;
248 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
249 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
251 template<int _Int, typename _Tp, std::size_t _Nm>
253 struct tuple_element<_Int, array<_Tp, _Nm> >
254 { typedef _Tp type; };
256 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
257 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
259 template<int _Int, typename _Tp, std::size_t _Nm>
262 get(array<_Tp, _Nm>& __arr)
263 { return __arr[_Int]; }
265 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
266 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
268 template<int _Int, typename _Tp, std::size_t _Nm>
271 get(const array<_Tp, _Nm>& __arr)
272 { return __arr[_Int]; }
274 _GLIBCXX_END_NAMESPACE_TR1