Merge branches/gcc-4_9-branch rev 225109.
[official-gcc.git] / gcc-4_9-branch / libstdc++-v3 / include / std / array
blobb32f5105e634081218a4128de172006ffa1ed9fc
1 // <array> -*- C++ -*-
3 // Copyright (C) 2007-2014 Free Software Foundation, Inc.
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
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
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 // 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.
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/>.
25 /** @file include/array
26  *  This is a Standard C++ Library header.
27  */
29 #ifndef _GLIBCXX_ARRAY
30 #define _GLIBCXX_ARRAY 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
35 # include <bits/c++0x_warning.h>
36 #else
38 #include <stdexcept>
39 #include <bits/stl_algobase.h>
40 #include <bits/range_access.h>
42 namespace std _GLIBCXX_VISIBILITY(default)
44 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
46   template<typename _Tp, std::size_t _Nm>
47     struct __array_traits
48     {
49       typedef _Tp _Type[_Nm];
51       static constexpr _Tp&
52       _S_ref(const _Type& __t, std::size_t __n) noexcept
53       { return const_cast<_Tp&>(__t[__n]); }
55       static constexpr _Tp*
56       _S_ptr(const _Type& __t) noexcept
57       { return const_cast<_Tp*>(__t); }
58     };
60  template<typename _Tp>
61    struct __array_traits<_Tp, 0>
62    {
63      struct _Type { };
65      static constexpr _Tp&
66      _S_ref(const _Type&, std::size_t) noexcept
67      { return *static_cast<_Tp*>(nullptr); }
69      static constexpr _Tp*
70      _S_ptr(const _Type&) noexcept
71      { return nullptr; }
72    };
74   /**
75    *  @brief A standard container for storing a fixed size sequence of elements.
76    *
77    *  @ingroup sequences
78    *
79    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
80    *  <a href="tables.html#66">reversible container</a>, and a
81    *  <a href="tables.html#67">sequence</a>.
82    *
83    *  Sets support random access iterators.
84    *
85    *  @tparam  Tp  Type of element. Required to be a complete type.
86    *  @tparam  N  Number of elements.
87   */
88   template<typename _Tp, std::size_t _Nm>
89     struct array
90     {
91       typedef _Tp                                     value_type;
92       typedef value_type*                             pointer;
93       typedef const value_type*                       const_pointer;
94       typedef value_type&                             reference;
95       typedef const value_type&                       const_reference;
96       typedef value_type*                             iterator;
97       typedef const value_type*                       const_iterator;
98       typedef std::size_t                             size_type;
99       typedef std::ptrdiff_t                          difference_type;
100       typedef std::reverse_iterator<iterator>         reverse_iterator;
101       typedef std::reverse_iterator<const_iterator>   const_reverse_iterator;
103       // Support for zero-sized arrays mandatory.
104       typedef _GLIBCXX_STD_C::__array_traits<_Tp, _Nm> _AT_Type;
105       typename _AT_Type::_Type                         _M_elems;
107       // No explicit construct/copy/destroy for aggregate type.
109       // DR 776.
110       void
111       fill(const value_type& __u)
112       { std::fill_n(begin(), size(), __u); }
114       void
115       swap(array& __other)
116       noexcept(noexcept(swap(std::declval<_Tp&>(), std::declval<_Tp&>())))
117       { std::swap_ranges(begin(), end(), __other.begin()); }
119       // Iterators.
120       iterator
121       begin() noexcept
122       { return iterator(data()); }
124       const_iterator
125       begin() const noexcept
126       { return const_iterator(data()); }
128       iterator
129       end() noexcept
130       { return iterator(data() + _Nm); }
132       const_iterator
133       end() const noexcept
134       { return const_iterator(data() + _Nm); }
136       reverse_iterator 
137       rbegin() noexcept
138       { return reverse_iterator(end()); }
140       const_reverse_iterator 
141       rbegin() const noexcept
142       { return const_reverse_iterator(end()); }
144       reverse_iterator 
145       rend() noexcept
146       { return reverse_iterator(begin()); }
148       const_reverse_iterator 
149       rend() const noexcept
150       { return const_reverse_iterator(begin()); }
152       const_iterator
153       cbegin() const noexcept
154       { return const_iterator(data()); }
156       const_iterator
157       cend() const noexcept
158       { return const_iterator(data() + _Nm); }
160       const_reverse_iterator 
161       crbegin() const noexcept
162       { return const_reverse_iterator(end()); }
164       const_reverse_iterator 
165       crend() const noexcept
166       { return const_reverse_iterator(begin()); }
168       // Capacity.
169       constexpr size_type 
170       size() const noexcept { return _Nm; }
172       constexpr size_type 
173       max_size() const noexcept { return _Nm; }
175       constexpr bool 
176       empty() const noexcept { return size() == 0; }
178       // Element access.
179       reference
180       operator[](size_type __n) noexcept
181       { return _AT_Type::_S_ref(_M_elems, __n); }
183       constexpr const_reference
184       operator[](size_type __n) const noexcept
185       { return _AT_Type::_S_ref(_M_elems, __n); }
187       reference
188       at(size_type __n)
189       {
190         if (__n >= _Nm)
191           std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
192                                             ">= _Nm (which is %zu)"),
193                                         __n, _Nm);
194         return _AT_Type::_S_ref(_M_elems, __n);
195       }
197       constexpr const_reference
198       at(size_type __n) const
199       {
200         // Result of conditional expression must be an lvalue so use
201         // boolean ? lvalue : (throw-expr, lvalue)
202         return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n)
203           : (std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
204                                                ">= _Nm (which is %zu)"),
205                                            __n, _Nm),
206              _AT_Type::_S_ref(_M_elems, 0));
207       }
209       reference 
210       front() noexcept
211       { return *begin(); }
213       constexpr const_reference 
214       front() const noexcept
215       { return _AT_Type::_S_ref(_M_elems, 0); }
217       reference 
218       back() noexcept
219       { return _Nm ? *(end() - 1) : *end(); }
221       constexpr const_reference 
222       back() const noexcept
223       { 
224         return _Nm ? _AT_Type::_S_ref(_M_elems, _Nm - 1) 
225                    : _AT_Type::_S_ref(_M_elems, 0);
226       }
228       pointer
229       data() noexcept
230       { return _AT_Type::_S_ptr(_M_elems); }
232       const_pointer
233       data() const noexcept
234       { return _AT_Type::_S_ptr(_M_elems); }
235     };
237   // Array comparisons.
238   template<typename _Tp, std::size_t _Nm>
239     inline bool 
240     operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
241     { return std::equal(__one.begin(), __one.end(), __two.begin()); }
243   template<typename _Tp, std::size_t _Nm>
244     inline bool
245     operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
246     { return !(__one == __two); }
248   template<typename _Tp, std::size_t _Nm>
249     inline bool
250     operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
251     { 
252       return std::lexicographical_compare(__a.begin(), __a.end(),
253                                           __b.begin(), __b.end()); 
254     }
256   template<typename _Tp, std::size_t _Nm>
257     inline bool
258     operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
259     { return __two < __one; }
261   template<typename _Tp, std::size_t _Nm>
262     inline bool
263     operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
264     { return !(__one > __two); }
266   template<typename _Tp, std::size_t _Nm>
267     inline bool
268     operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
269     { return !(__one < __two); }
271   // Specialized algorithms.
272   template<typename _Tp, std::size_t _Nm>
273     inline void
274     swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
275     noexcept(noexcept(__one.swap(__two)))
276     { __one.swap(__two); }
278   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
279     constexpr _Tp&
280     get(array<_Tp, _Nm>& __arr) noexcept
281     {
282       static_assert(_Int < _Nm, "index is out of bounds");
283       return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
284         _S_ref(__arr._M_elems, _Int);
285     }
287   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
288     constexpr _Tp&&
289     get(array<_Tp, _Nm>&& __arr) noexcept
290     {
291       static_assert(_Int < _Nm, "index is out of bounds");
292       return std::move(get<_Int>(__arr));
293     }
295   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
296     constexpr const _Tp&
297     get(const array<_Tp, _Nm>& __arr) noexcept
298     {
299       static_assert(_Int < _Nm, "index is out of bounds");
300       return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
301         _S_ref(__arr._M_elems, _Int);
302     }
304 _GLIBCXX_END_NAMESPACE_CONTAINER
305 } // namespace std
307 namespace std _GLIBCXX_VISIBILITY(default)
309 _GLIBCXX_BEGIN_NAMESPACE_VERSION
311   // Tuple interface to class template array.
313   /// tuple_size
314   template<typename _Tp> 
315     class tuple_size;
317   template<typename _Tp, std::size_t _Nm>
318     struct tuple_size<_GLIBCXX_STD_C::array<_Tp, _Nm>>
319     : public integral_constant<std::size_t, _Nm> { };
321   /// tuple_element
322   template<std::size_t _Int, typename _Tp>
323     class tuple_element;
325   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
326     struct tuple_element<_Int, _GLIBCXX_STD_C::array<_Tp, _Nm>>
327     {
328       static_assert(_Int < _Nm, "index is out of bounds");
329       typedef _Tp type;
330     };
332 _GLIBCXX_END_NAMESPACE_VERSION
333 } // namespace std
335 #ifdef _GLIBCXX_DEBUG
336 # include <debug/array>
337 #endif
339 #ifdef _GLIBCXX_PROFILE
340 # include <profile/array>
341 #endif
343 #endif // C++11
345 #endif // _GLIBCXX_ARRAY