OSDN Git Service

8cd538808206704797f2ab02452d0c481a8f0cbf
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / std / array
1 // <array> -*- C++ -*-
2
3 // Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
5 //
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)
10 // any later version.
11
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.
16
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.
20
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/>.
25
26 /** @file include/array
27  *  This is a Standard C++ Library header.
28  */
29
30 #ifndef _GLIBCXX_ARRAY
31 #define _GLIBCXX_ARRAY 1
32
33 #pragma GCC system_header
34
35 #ifndef __GXX_EXPERIMENTAL_CXX0X__
36 # include <bits/c++0x_warning.h>
37 #else
38
39 #include <stdexcept>
40 #include <bits/stl_algobase.h>
41 #include <bits/range_access.h>
42
43 namespace std _GLIBCXX_VISIBILITY(default)
44 {
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46
47   /**
48    *  @brief A standard container for storing a fixed size sequence of elements.
49    *
50    *  @ingroup sequences
51    *
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>.
55    *
56    *  Sets support random access iterators.
57    *
58    *  @param  Tp  Type of element. Required to be a complete type.
59    *  @param  N  Number of elements.
60   */
61   template<typename _Tp, std::size_t _Nm>
62     struct array
63     {
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;
75
76       // Support for zero-sized arrays mandatory.
77       value_type _M_instance[_Nm ? _Nm : 1];
78
79       // No explicit construct/copy/destroy for aggregate type.
80
81       // DR 776.
82       void
83       fill(const value_type& __u)
84       { std::fill_n(begin(), size(), __u); }
85
86       void
87       swap(array& __other)
88       noexcept(noexcept(swap(std::declval<_Tp&>(), std::declval<_Tp&>())))
89       { std::swap_ranges(begin(), end(), __other.begin()); }
90
91       // Iterators.
92       iterator
93       begin() noexcept
94       { return iterator(data()); }
95
96       const_iterator
97       begin() const noexcept
98       { return const_iterator(data()); }
99
100       iterator
101       end() noexcept
102       { return iterator(data() + _Nm); }
103
104       const_iterator
105       end() const noexcept
106       { return const_iterator(data() + _Nm); }
107
108       reverse_iterator 
109       rbegin() noexcept
110       { return reverse_iterator(end()); }
111
112       const_reverse_iterator 
113       rbegin() const noexcept
114       { return const_reverse_iterator(end()); }
115
116       reverse_iterator 
117       rend() noexcept
118       { return reverse_iterator(begin()); }
119
120       const_reverse_iterator 
121       rend() const noexcept
122       { return const_reverse_iterator(begin()); }
123
124       const_iterator
125       cbegin() const noexcept
126       { return const_iterator(std::__addressof(_M_instance[0])); }
127
128       const_iterator
129       cend() const noexcept
130       { return const_iterator(std::__addressof(_M_instance[_Nm])); }
131
132       const_reverse_iterator 
133       crbegin() const noexcept
134       { return const_reverse_iterator(end()); }
135
136       const_reverse_iterator 
137       crend() const noexcept
138       { return const_reverse_iterator(begin()); }
139
140       // Capacity.
141       constexpr size_type 
142       size() const noexcept { return _Nm; }
143
144       constexpr size_type 
145       max_size() const noexcept { return _Nm; }
146
147       constexpr bool 
148       empty() const noexcept { return size() == 0; }
149
150       // Element access.
151       reference
152       operator[](size_type __n)
153       { return _M_instance[__n]; }
154
155       constexpr const_reference
156       operator[](size_type __n) const noexcept
157       { return _M_instance[__n]; }
158
159       reference
160       at(size_type __n)
161       {
162         if (__n >= _Nm)
163           std::__throw_out_of_range(__N("array::at"));
164         return _M_instance[__n];
165       }
166
167 #ifdef __EXCEPTIONS
168       constexpr const_reference
169       at(size_type __n) const
170       {
171         return __n < _Nm ? 
172                _M_instance[__n] : throw out_of_range(__N("array::at"));
173       }
174 #else
175       const_reference
176       at(size_type __n) const
177       {
178         if (__n >= _Nm)
179           std::__throw_out_of_range(__N("array::at"));
180         return _M_instance[__n];
181       }
182 #endif
183
184       reference 
185       front()
186       { return *begin(); }
187
188       const_reference 
189       front() const
190       { return *begin(); }
191
192       reference 
193       back()
194       { return _Nm ? *(end() - 1) : *end(); }
195
196       const_reference 
197       back() const
198       { return _Nm ? *(end() - 1) : *end(); }
199
200       pointer
201       data() noexcept
202       { return std::__addressof(_M_instance[0]); }
203
204       const_pointer
205       data() const noexcept
206       { return std::__addressof(_M_instance[0]); }
207     };
208
209   // Array comparisons.
210   template<typename _Tp, std::size_t _Nm>
211     inline bool 
212     operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
213     { return std::equal(__one.begin(), __one.end(), __two.begin()); }
214
215   template<typename _Tp, std::size_t _Nm>
216     inline bool
217     operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
218     { return !(__one == __two); }
219
220   template<typename _Tp, std::size_t _Nm>
221     inline bool
222     operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
223     { 
224       return std::lexicographical_compare(__a.begin(), __a.end(),
225                                           __b.begin(), __b.end()); 
226     }
227
228   template<typename _Tp, std::size_t _Nm>
229     inline bool
230     operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
231     { return __two < __one; }
232
233   template<typename _Tp, std::size_t _Nm>
234     inline bool
235     operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
236     { return !(__one > __two); }
237
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 !(__one < __two); }
242
243   // Specialized algorithms.
244   template<typename _Tp, std::size_t _Nm>
245     inline void
246     swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
247     noexcept(noexcept(__one.swap(__two)))
248     { __one.swap(__two); }
249
250   // Tuple interface to class template array.
251
252   /// tuple_size
253   template<typename _Tp> 
254     class tuple_size;
255
256   template<typename _Tp, std::size_t _Nm>
257     struct tuple_size<array<_Tp, _Nm>>
258     : public integral_constant<std::size_t, _Nm> { };
259
260   /// tuple_element
261   template<std::size_t _Int, typename _Tp>
262     class tuple_element;
263
264   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
265     struct tuple_element<_Int, array<_Tp, _Nm> >
266     { typedef _Tp type; };
267
268   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
269     constexpr _Tp&
270     get(array<_Tp, _Nm>& __arr) noexcept
271     { return __arr._M_instance[_Int]; }
272
273   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
274     constexpr _Tp&&
275     get(array<_Tp, _Nm>&& __arr) noexcept
276     { return std::move(get<_Int>(__arr)); }
277
278   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
279     constexpr const _Tp&
280     get(const array<_Tp, _Nm>& __arr) noexcept
281     { return __arr._M_instance[_Int]; }
282
283 _GLIBCXX_END_NAMESPACE_VERSION
284 } // namespace
285
286 #endif // __GXX_EXPERIMENTAL_CXX0X__
287
288 #endif // _GLIBCXX_ARRAY