OSDN Git Service

2007-10-15 Paolo Carlini <pcarlini@suse.de>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / tr1_impl / array
1 // class template array -*- C++ -*-
2
3 // Copyright (C) 2007 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   /// @brief  struct array.
40   /// NB: Requires complete type _Tp.
41   template<typename _Tp, std::size_t _Nm>
42     struct array
43     {
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;
53
54       // Support for zero-sized arrays mandatory.
55       value_type _M_instance[_Nm ? _Nm : 1] __attribute__((__aligned__));
56
57       // No explicit construct/copy/destroy for aggregate type.
58
59       void 
60       assign(const value_type& __u)
61       { std::fill_n(begin(), size(), __u); }
62
63       void 
64       swap(array& __other)
65       { std::swap_ranges(begin(), end(), __other.begin()); }
66
67       // Iterators.
68       iterator
69       begin()
70       { return iterator(&_M_instance[0]); }
71
72       const_iterator
73       begin() const 
74       { return const_iterator(&_M_instance[0]); }
75
76       iterator
77       end() 
78       { return iterator(&_M_instance[_Nm]); }
79
80       const_iterator
81       end() const
82       { return const_iterator(&_M_instance[_Nm]); }
83
84       reverse_iterator 
85       rbegin()
86       { return reverse_iterator(end()); }
87
88       const_reverse_iterator 
89       rbegin() const
90       { return const_reverse_iterator(end()); }
91
92       reverse_iterator 
93       rend()
94       { return reverse_iterator(begin()); }
95
96       const_reverse_iterator 
97       rend() const
98       { return const_reverse_iterator(begin()); }
99
100 #ifdef __GXX_EXPERIMENTAL_CXX0X__
101       const_iterator
102       cbegin() const 
103       { return const_iterator(&_M_instance[0]); }
104
105       const_iterator
106       cend() const
107       { return const_iterator(&_M_instance[_Nm]); }
108
109       const_reverse_iterator 
110       crbegin() const
111       { return const_reverse_iterator(end()); }
112
113       const_reverse_iterator 
114       crend() const
115       { return const_reverse_iterator(begin()); }
116 #endif
117
118       // Capacity.
119       size_type 
120       size() const { return _Nm; }
121
122       size_type 
123       max_size() const { return _Nm; }
124
125       bool 
126       empty() const { return size() == 0; }
127
128       // Element access.
129       reference
130       operator[](size_type __n)
131       { return _M_instance[__n]; }
132
133       const_reference
134       operator[](size_type __n) const
135       { return _M_instance[__n]; }
136
137       reference
138       at(size_type __n)
139       { 
140         _M_check<_Nm>(__n);
141         return _M_instance[__n];
142       }
143
144       const_reference
145       at(size_type __n) const
146       {
147         _M_check<_Nm>(__n);
148         return _M_instance[__n];
149       }
150
151       reference 
152       front()
153       { return *begin(); }
154
155       const_reference 
156       front() const
157       { return *begin(); }
158
159       reference 
160       back()
161       { return _Nm ? *(end() - 1) : *end(); }
162
163       const_reference 
164       back() const
165       { return _Nm ? *(end() - 1) : *end(); }
166
167       _Tp* 
168       data()
169       { return &_M_instance[0]; }
170
171       const _Tp* 
172       data() const
173       { return &_M_instance[0]; }
174
175     private:
176       template<std::size_t _Mm>
177         typename __gnu_cxx::__enable_if<_Mm, void>::__type
178         _M_check(size_type __n) const
179         {
180           if (__builtin_expect(__n >= _Mm, false))
181             std::__throw_out_of_range(__N("array::_M_check"));
182         }
183
184       // Avoid "unsigned comparison with zero" warnings.
185       template<std::size_t _Mm>
186         typename __gnu_cxx::__enable_if<!_Mm, void>::__type
187         _M_check(size_type) const
188         { std::__throw_out_of_range(__N("array::_M_check")); }
189     };
190
191   // Array comparisons.
192   template<typename _Tp, std::size_t _Nm>
193     inline bool 
194     operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
195     { return std::equal(__one.begin(), __one.end(), __two.begin()); }
196
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 !(__one == __two); }
201
202   template<typename _Tp, std::size_t _Nm>
203     inline bool
204     operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
205     { 
206       return std::lexicographical_compare(__a.begin(), __a.end(),
207                                           __b.begin(), __b.end()); 
208     }
209
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 __two < __one; }
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>& __one, const array<_Tp, _Nm>& __two)
223     { return !(__one < __two); }
224
225   // Specialized algorithms [6.2.2.2].
226   template<typename _Tp, std::size_t _Nm>
227     inline void
228     swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
229     { std::swap_ranges(__one.begin(), __one.end(), __two.begin()); }
230
231   // Tuple interface to class template array [6.2.2.5].
232   template<typename _Tp> class tuple_size;
233   template<int _Int, typename _Tp> class tuple_element;
234
235   template<typename _Tp, std::size_t _Nm>
236     struct tuple_size<array<_Tp, _Nm> >
237     { static const int value = _Nm; };
238
239   template<typename _Tp, std::size_t _Nm>
240     const int tuple_size<array<_Tp, _Nm> >::value;
241
242   template<int _Int, typename _Tp, std::size_t _Nm>
243     struct tuple_element<_Int, array<_Tp, _Nm> >
244     { typedef _Tp type; };
245
246   template<int _Int, typename _Tp, std::size_t _Nm>
247     inline _Tp&
248     get(array<_Tp, _Nm>& __arr)
249     { return __arr[_Int]; }
250
251   template<int _Int, typename _Tp, std::size_t _Nm>
252     inline const _Tp&
253     get(const array<_Tp, _Nm>& __arr)
254     { return __arr[_Int]; }
255
256 _GLIBCXX_END_NAMESPACE_TR1
257 }