OSDN Git Service

2009-02-18 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / tr1_impl / array
1 // class template array -*- C++ -*-
2
3 // Copyright (C) 2007, 2008, 2009 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 2, or (at your option)
9 // any later version.
10
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.
15
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,
19 // USA.
20
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.
29
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.
33  */
34
35 namespace std
36 {
37 _GLIBCXX_BEGIN_NAMESPACE_TR1
38
39   /**
40    *  @brief A standard container for storing a fixed size sequence of elements.
41    *
42    *  @ingroup containers
43    *  @ingroup sequences
44    *
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>.
48    *
49    *  Sets support random access iterators.
50    *
51    *  @param  Tp  Type of element. Required to be a complete type.
52    *  @param  N  Number of elements.
53   */
54   template<typename _Tp, std::size_t _Nm>
55     struct array
56     {
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;
66
67       // Support for zero-sized arrays mandatory.
68       value_type _M_instance[_Nm ? _Nm : 1];
69
70       // No explicit construct/copy/destroy for aggregate type.
71
72       void
73 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
74       // DR 776.
75       fill(const value_type& __u)
76 #else
77       assign(const value_type& __u)
78 #endif
79       { std::fill_n(begin(), size(), __u); }
80
81       void
82       swap(array& __other)
83       { std::swap_ranges(begin(), end(), __other.begin()); }
84
85       // Iterators.
86       iterator
87       begin()
88       { return iterator(&_M_instance[0]); }
89
90       const_iterator
91       begin() const 
92       { return const_iterator(&_M_instance[0]); }
93
94       iterator
95       end()
96       { return iterator(&_M_instance[_Nm]); }
97
98       const_iterator
99       end() const
100       { return const_iterator(&_M_instance[_Nm]); }
101
102       reverse_iterator 
103       rbegin()
104       { return reverse_iterator(end()); }
105
106       const_reverse_iterator 
107       rbegin() const
108       { return const_reverse_iterator(end()); }
109
110       reverse_iterator 
111       rend()
112       { return reverse_iterator(begin()); }
113
114       const_reverse_iterator 
115       rend() const
116       { return const_reverse_iterator(begin()); }
117
118 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
119       const_iterator
120       cbegin() const 
121       { return const_iterator(&_M_instance[0]); }
122
123       const_iterator
124       cend() const
125       { return const_iterator(&_M_instance[_Nm]); }
126
127       const_reverse_iterator 
128       crbegin() const
129       { return const_reverse_iterator(end()); }
130
131       const_reverse_iterator 
132       crend() const
133       { return const_reverse_iterator(begin()); }
134 #endif
135
136       // Capacity.
137       size_type 
138       size() const { return _Nm; }
139
140       size_type 
141       max_size() const { return _Nm; }
142
143       bool 
144       empty() const { return size() == 0; }
145
146       // Element access.
147       reference
148       operator[](size_type __n)
149       { return _M_instance[__n]; }
150
151       const_reference
152       operator[](size_type __n) const
153       { return _M_instance[__n]; }
154
155       reference
156       at(size_type __n)
157       {
158         if (__builtin_expect(__n >= _Nm, false))
159           std::__throw_out_of_range(__N("array::at"));
160         return _M_instance[__n];
161       }
162
163       const_reference
164       at(size_type __n) const
165       {
166         if (__builtin_expect(__n >= _Nm, false))
167           std::__throw_out_of_range(__N("array::at"));
168         return _M_instance[__n];
169       }
170
171       reference 
172       front()
173       { return *begin(); }
174
175       const_reference 
176       front() const
177       { return *begin(); }
178
179       reference 
180       back()
181       { return _Nm ? *(end() - 1) : *end(); }
182
183       const_reference 
184       back() const
185       { return _Nm ? *(end() - 1) : *end(); }
186
187       _Tp* 
188       data()
189       { return &_M_instance[0]; }
190
191       const _Tp* 
192       data() const
193       { return &_M_instance[0]; }
194     };
195
196   // Array comparisons.
197   template<typename _Tp, std::size_t _Nm>
198     inline bool 
199     operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
200     { return std::equal(__one.begin(), __one.end(), __two.begin()); }
201
202   template<typename _Tp, std::size_t _Nm>
203     inline bool
204     operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
205     { return !(__one == __two); }
206
207   template<typename _Tp, std::size_t _Nm>
208     inline bool
209     operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
210     { 
211       return std::lexicographical_compare(__a.begin(), __a.end(),
212                                           __b.begin(), __b.end()); 
213     }
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 __two < __one; }
219
220   template<typename _Tp, std::size_t _Nm>
221     inline bool
222     operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
223     { return !(__one > __two); }
224
225   template<typename _Tp, std::size_t _Nm>
226     inline bool
227     operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
228     { return !(__one < __two); }
229
230   // Specialized algorithms [6.2.2.2].
231   template<typename _Tp, std::size_t _Nm>
232     inline void
233     swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
234     { std::swap_ranges(__one.begin(), __one.end(), __two.begin()); }
235
236   // Tuple interface to class template array [6.2.2.5].
237
238   /// tuple_size
239   template<typename _Tp> 
240     class tuple_size;
241
242   /// tuple_element
243 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
244   template<std::size_t _Int, typename _Tp>
245 #else
246   template<int _Int, typename _Tp>
247 #endif
248     class tuple_element;
249
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; };
254 #else
255     { static const int value = _Nm; };
256 #endif
257
258   template<typename _Tp, std::size_t _Nm>
259 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
260     const std::size_t
261 #else
262     const int
263 #endif
264     tuple_size<array<_Tp, _Nm> >::value;  
265
266 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
267   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
268 #else
269   template<int _Int, typename _Tp, std::size_t _Nm>
270 #endif
271     struct tuple_element<_Int, array<_Tp, _Nm> >
272     { typedef _Tp type; };
273
274 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
275   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
276 #else
277   template<int _Int, typename _Tp, std::size_t _Nm>
278 #endif
279     inline _Tp&
280     get(array<_Tp, _Nm>& __arr)
281     { return __arr[_Int]; }
282
283 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
284   template<std::size_t _Int, typename _Tp, std::size_t _Nm>
285 #else
286   template<int _Int, typename _Tp, std::size_t _Nm>
287 #endif
288     inline const _Tp&
289     get(const array<_Tp, _Nm>& __arr)
290     { return __arr[_Int]; }
291
292 _GLIBCXX_END_NAMESPACE_TR1
293 }