Merge from mainline
[official-gcc.git] / libstdc++-v3 / include / tr1 / array
blob5f504851847d5cdef5c41f232a59d0ce23a332f5
1 // class template array -*- C++ -*-
3 // Copyright (C) 2004, 2005, 2006 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.
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 // 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.
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.
30 /** @file 
31  *  This is a TR1 C++ Library header. 
32  */
34 #ifndef _ARRAY
35 #define _ARRAY 1
37 #include <new>
38 #include <iterator>
39 #include <algorithm>
40 #include <cstddef>
41 #include <bits/functexcept.h>
43 //namespace std::tr1
44 namespace std
46 _GLIBCXX_BEGIN_NAMESPACE(tr1)
48   /// @brief  struct array [6.2.2].
49   /// NB: Requires complete type _Tp.
50   template<typename _Tp, std::size_t _Nm = 1>
51     struct array
52     {
53       typedef _Tp                               value_type;
54       typedef value_type&                       reference;
55       typedef const value_type&                 const_reference;
56       typedef value_type*                       iterator;
57       typedef const value_type*                 const_iterator;
58       typedef std::size_t                       size_type;
59       typedef std::ptrdiff_t                    difference_type;
60       typedef std::reverse_iterator<iterator>   reverse_iterator;
61       typedef std::reverse_iterator<const_iterator>     const_reverse_iterator;
63       // Compile time constant without other dependencies.
64       enum { _S_index = _Nm };
66       // Support for zero-sized arrays mandatory.
67       value_type _M_instance[_Nm ? _Nm : 1] __attribute__((__aligned__));
69       // No explicit construct/copy/destroy for aggregate type.
71       void 
72       assign(const value_type& u); 
74       void 
75       swap(array&);
77       // Iterators.
78       iterator
79       begin()
80       { return iterator(&_M_instance[0]); }
82       const_iterator
83       begin() const 
84       { return const_iterator(&_M_instance[0]); }
86       iterator
87       end() 
88       { return iterator(&_M_instance[_Nm]); }
90       const_iterator
91       end() const
92       { return const_iterator(&_M_instance[_Nm]); }
94       reverse_iterator 
95       rbegin()
96       { return reverse_iterator(end()); }
98       const_reverse_iterator 
99       rbegin() const
100       { return const_reverse_iterator(end()); }
102       reverse_iterator 
103       rend()
104       { return reverse_iterator(begin()); }
106       const_reverse_iterator 
107       rend() const
108       { return const_reverse_iterator(begin()); }
110       // Capacity.
111       size_type 
112       size() const { return _Nm; }
114       size_type 
115       max_size() const { return _Nm; }
117       bool 
118       empty() const { return size() == 0; }
120       // Element access.
121       reference 
122       operator[](size_type __n)
123       { return _M_instance[__n]; }
125       const_reference 
126       operator[](size_type __n) const
127       { return _M_instance[__n]; }
129       const_reference 
130       at(size_type __n) const
131       { 
132         if (__builtin_expect(__n > _Nm, false))
133           std::__throw_out_of_range("array::at");
134         return _M_instance[__n]; 
135       }
137       reference 
138       at(size_type __n)
139       { 
140         if (__builtin_expect(__n > _Nm, false))
141           std::__throw_out_of_range("array::at");
142         return _M_instance[__n]; 
143       }
145       reference 
146       front()
147       { return *begin(); }
149       const_reference 
150       front() const
151       { return *begin(); }
153       reference 
154       back()
155       { return *(end() - 1); }
157       const_reference 
158       back() const
159       { return *(end() - 1); }
161       _Tp* 
162       data()
163       { return &_M_instance[0]; }
165       const _Tp* 
166       data() const
167       { return &_M_instance[0]; }
168     };
170   // Array comparisons.
171   template<typename _Tp, std::size_t _Nm>
172     bool 
173     operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
174     { return std::equal(__one.begin(), __one.end(), __two.begin()); }
176   template<typename _Tp, std::size_t _Nm>
177     bool 
178     operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
179     { return !(__one == __two); }
181   template<typename _Tp, std::size_t _Nm>
182     bool 
183     operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
184     { 
185       return std::lexicographical_compare(__a.begin(), __a.end(),
186                                           __b.begin(), __b.end()); 
187     }
189   template<typename _Tp, std::size_t _Nm>
190     bool 
191     operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
192     { return __two < __one; }
194   template<typename _Tp, std::size_t _Nm>
195     bool 
196     operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
197     { return !(__one > __two); }
199   template<typename _Tp, std::size_t _Nm>
200     bool 
201     operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
202     { return !(__one < __two); }
204   // Specialized algorithms [6.2.2.2].
205   template<typename _Tp, std::size_t _Nm>
206     void
207     swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
208     { swap_ranges(__one.begin(), __one.end(), __two.begin()); }
210   // Tuple interface to class template array [6.2.2.5].
211   template<typename _Tp> class tuple_size;
212   template<int _Int, typename _Tp> class tuple_element;
213   
214   template<typename _Tp, std::size_t _Nm>
215     struct tuple_size<array<_Tp, _Nm> >
216     { static const int value = _Nm; };
218   template<int _Int, typename _Tp, std::size_t _Nm>
219     struct tuple_element<_Int, array<_Tp, _Nm> >
220     { typedef _Tp type; };
222   template<int _Int, typename _Tp, std::size_t _Nm>
223     _Tp&
224     get(array<_Tp, _Nm>& __arr)
225     { return __arr[_Int]; }
227   template<int _Int, typename _Tp, std::size_t _Nm>
228     const _Tp&
229     get(const array<_Tp, _Nm>& __arr)
230     { return __arr[_Int]; }
232 _GLIBCXX_END_NAMESPACE
235 #endif