OSDN Git Service

2007-03-12 Paolo Carlini <pcarlini@suse.de>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / tr1 / array
1 // class template array -*- C++ -*-
2
3 // Copyright (C) 2004, 2005, 2006, 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/array
31  *  This is a TR1 C++ Library header. 
32  */
33
34 #ifndef _TR1_ARRAY
35 #define _TR1_ARRAY 1
36
37 #include <bits/stl_algobase.h>
38
39 //namespace std::tr1
40 namespace std
41 {
42 _GLIBCXX_BEGIN_NAMESPACE(_GLIBCXX_TR1)
43
44   /// @brief  struct array [6.2.2].
45   /// NB: Requires complete type _Tp.
46   template<typename _Tp, std::size_t _Nm>
47     struct array
48     {
49       typedef _Tp                                     value_type;
50       typedef value_type&                             reference;
51       typedef const value_type&                       const_reference;
52       typedef value_type*                             iterator;
53       typedef const value_type*                       const_iterator;
54       typedef std::size_t                             size_type;
55       typedef std::ptrdiff_t                          difference_type;
56       typedef std::reverse_iterator<iterator>         reverse_iterator;
57       typedef std::reverse_iterator<const_iterator>   const_reverse_iterator;
58
59       // Support for zero-sized arrays mandatory.
60       value_type _M_instance[_Nm ? _Nm : 1] __attribute__((__aligned__));
61
62       // No explicit construct/copy/destroy for aggregate type.
63
64       void 
65       assign(const value_type& __u)
66       { std::fill_n(begin(), size(), __u); }
67
68       void 
69       swap(array& __other)
70       { std::swap_ranges(begin(), end(), __other.begin()); }
71
72       // Iterators.
73       iterator
74       begin()
75       { return iterator(&_M_instance[0]); }
76
77       const_iterator
78       begin() const 
79       { return const_iterator(&_M_instance[0]); }
80
81       iterator
82       end() 
83       { return iterator(&_M_instance[_Nm]); }
84
85       const_iterator
86       end() const
87       { return const_iterator(&_M_instance[_Nm]); }
88
89       reverse_iterator 
90       rbegin()
91       { return reverse_iterator(end()); }
92
93       const_reverse_iterator 
94       rbegin() const
95       { return const_reverse_iterator(end()); }
96
97       reverse_iterator 
98       rend()
99       { return reverse_iterator(begin()); }
100
101       const_reverse_iterator 
102       rend() const
103       { return const_reverse_iterator(begin()); }
104
105       // Capacity.
106       size_type 
107       size() const { return _Nm; }
108
109       size_type 
110       max_size() const { return _Nm; }
111
112       bool 
113       empty() const { return size() == 0; }
114
115       // Element access.
116       reference
117       operator[](size_type __n)
118       { return _M_instance[__n]; }
119
120       const_reference
121       operator[](size_type __n) const
122       { return _M_instance[__n]; }
123
124       reference
125       at(size_type __n)
126       { 
127         _M_check<_Nm>(__n);
128         return _M_instance[__n];
129       }
130
131       const_reference
132       at(size_type __n) const
133       {
134         _M_check<_Nm>(__n);
135         return _M_instance[__n];
136       }
137
138       reference 
139       front()
140       { return *begin(); }
141
142       const_reference 
143       front() const
144       { return *begin(); }
145
146       reference 
147       back()
148       { return _Nm ? *(end() - 1) : *end(); }
149
150       const_reference 
151       back() const
152       { return _Nm ? *(end() - 1) : *end(); }
153
154       _Tp* 
155       data()
156       { return &_M_instance[0]; }
157
158       const _Tp* 
159       data() const
160       { return &_M_instance[0]; }
161
162     private:
163       template<std::size_t _Mm>
164         typename __gnu_cxx::__enable_if<_Mm, void>::__type
165         _M_check(size_type __n) const
166         {
167           if (__builtin_expect(__n >= _Mm, false))
168             std::__throw_out_of_range(__N("array::_M_check"));
169         }
170
171       // Avoid "unsigned comparison with zero" warnings.
172       template<std::size_t _Mm>
173         typename __gnu_cxx::__enable_if<!_Mm, void>::__type
174         _M_check(size_type) const
175         { std::__throw_out_of_range(__N("array::_M_check")); }
176     };
177
178   // Array comparisons.
179   template<typename _Tp, std::size_t _Nm>
180     inline bool 
181     operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
182     { return std::equal(__one.begin(), __one.end(), __two.begin()); }
183
184   template<typename _Tp, std::size_t _Nm>
185     inline bool
186     operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
187     { return !(__one == __two); }
188
189   template<typename _Tp, std::size_t _Nm>
190     inline bool
191     operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
192     { 
193       return std::lexicographical_compare(__a.begin(), __a.end(),
194                                           __b.begin(), __b.end()); 
195     }
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 __two < __one; }
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>& __one, const array<_Tp, _Nm>& __two)
210     { return !(__one < __two); }
211
212   // Specialized algorithms [6.2.2.2].
213   template<typename _Tp, std::size_t _Nm>
214     inline void
215     swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
216     { std::swap_ranges(__one.begin(), __one.end(), __two.begin()); }
217
218   // Tuple interface to class template array [6.2.2.5].
219   template<typename _Tp> class tuple_size;
220   template<int _Int, typename _Tp> class tuple_element;
221
222   template<typename _Tp, std::size_t _Nm>
223     struct tuple_size<array<_Tp, _Nm> >
224     { static const int value = _Nm; };
225
226   template<typename _Tp, std::size_t _Nm>
227     const int tuple_size<array<_Tp, _Nm> >::value;
228
229   template<int _Int, typename _Tp, std::size_t _Nm>
230     struct tuple_element<_Int, array<_Tp, _Nm> >
231     { typedef _Tp type; };
232
233   template<int _Int, typename _Tp, std::size_t _Nm>
234     inline _Tp&
235     get(array<_Tp, _Nm>& __arr)
236     { return __arr[_Int]; }
237
238   template<int _Int, typename _Tp, std::size_t _Nm>
239     inline const _Tp&
240     get(const array<_Tp, _Nm>& __arr)
241     { return __arr[_Int]; }
242
243 _GLIBCXX_END_NAMESPACE
244 }
245
246 #endif