Import a stripped down version of gcc-4.1.1
[dragonfly.git] / contrib / gcc-4.1 / libstdc++-v3 / include / tr1 / array
1 // class template array -*- C++ -*-
2
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.
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 
31  *  This is a TR1 C++ Library header. 
32  */
33
34 #ifndef _ARRAY
35 #define _ARRAY 1
36
37 #include <new>
38 #include <iterator>
39 #include <algorithm>
40 #include <cstddef>
41 #include <bits/functexcept.h>
42
43 //namespace std::tr1
44 namespace std
45 {
46 namespace tr1
47 {
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;
62
63       // Compile time constant without other dependencies.
64       enum { _S_index = _Nm };
65
66       // Support for zero-sized arrays mandatory.
67       value_type _M_instance[_Nm ? _Nm : 1] __attribute__((__aligned__));
68
69       // No explicit construct/copy/destroy for aggregate type.
70
71       void 
72       assign(const value_type& __u)
73       { std::fill_n(begin(), size(), __u); }
74
75       void 
76       swap(array& __other)
77       { std::swap_ranges(begin(), end(), __other.begin()); }
78
79       // Iterators.
80       iterator
81       begin()
82       { return iterator(&_M_instance[0]); }
83
84       const_iterator
85       begin() const 
86       { return const_iterator(&_M_instance[0]); }
87
88       iterator
89       end() 
90       { return iterator(&_M_instance[_Nm]); }
91
92       const_iterator
93       end() const
94       { return const_iterator(&_M_instance[_Nm]); }
95
96       reverse_iterator 
97       rbegin()
98       { return reverse_iterator(end()); }
99
100       const_reverse_iterator 
101       rbegin() const
102       { return const_reverse_iterator(end()); }
103
104       reverse_iterator 
105       rend()
106       { return reverse_iterator(begin()); }
107
108       const_reverse_iterator 
109       rend() const
110       { return const_reverse_iterator(begin()); }
111
112       // Capacity.
113       size_type 
114       size() const { return _Nm; }
115
116       size_type 
117       max_size() const { return _Nm; }
118
119       bool 
120       empty() const { return size() == 0; }
121
122       // Element access.
123       reference 
124       operator[](size_type __n)
125       { return _M_instance[__n]; }
126
127       const_reference 
128       operator[](size_type __n) const
129       { return _M_instance[__n]; }
130
131       const_reference 
132       at(size_type __n) const
133       { 
134         if (__builtin_expect(__n > _Nm, false))
135           std::__throw_out_of_range("array::at");
136         return _M_instance[__n]; 
137       }
138
139       reference 
140       at(size_type __n)
141       { 
142         if (__builtin_expect(__n > _Nm, false))
143           std::__throw_out_of_range("array::at");
144         return _M_instance[__n]; 
145       }
146
147       reference 
148       front()
149       { return *begin(); }
150
151       const_reference 
152       front() const
153       { return *begin(); }
154
155       reference 
156       back()
157       { return *(end() - 1); }
158
159       const_reference 
160       back() const
161       { return *(end() - 1); }
162
163       _Tp* 
164       data()
165       { return &_M_instance[0]; }
166
167       const _Tp* 
168       data() const
169       { return &_M_instance[0]; }
170     };
171
172   // Array comparisons.
173   template<typename _Tp, std::size_t _Nm>
174     inline bool 
175     operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
176     { return std::equal(__one.begin(), __one.end(), __two.begin()); }
177
178   template<typename _Tp, std::size_t _Nm>
179     inline bool
180     operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
181     { return !(__one == __two); }
182
183   template<typename _Tp, std::size_t _Nm>
184     inline bool
185     operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
186     { 
187       return std::lexicographical_compare(__a.begin(), __a.end(),
188                                           __b.begin(), __b.end()); 
189     }
190
191   template<typename _Tp, std::size_t _Nm>
192     inline bool
193     operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
194     { return __two < __one; }
195
196   template<typename _Tp, std::size_t _Nm>
197     inline bool
198     operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
199     { return !(__one > __two); }
200
201   template<typename _Tp, std::size_t _Nm>
202     inline bool
203     operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
204     { return !(__one < __two); }
205
206   // Specialized algorithms [6.2.2.2].
207   template<typename _Tp, std::size_t _Nm>
208     inline void
209     swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
210     { std::swap_ranges(__one.begin(), __one.end(), __two.begin()); }
211
212   // Tuple interface to class template array [6.2.2.5].
213   template<typename _Tp> class tuple_size;
214   template<int _Int, typename _Tp> class tuple_element;
215   
216   template<typename _Tp, std::size_t _Nm>
217     struct tuple_size<array<_Tp, _Nm> >
218     { static const int value = _Nm; };
219  
220   template<int _Int, typename _Tp, std::size_t _Nm>
221     struct tuple_element<_Int, array<_Tp, _Nm> >
222     { typedef _Tp type; };
223
224   template<int _Int, typename _Tp, std::size_t _Nm>
225     inline _Tp&
226     get(array<_Tp, _Nm>& __arr)
227     { return __arr[_Int]; }
228
229   template<int _Int, typename _Tp, std::size_t _Nm>
230     inline const _Tp&
231     get(const array<_Tp, _Nm>& __arr)
232     { return __arr[_Int]; }
233 } // namespace std::tr1
234 }
235
236 #endif