Import of virgin gcc 4.0.0 distribution.
[dragonfly.git] / contrib / gcc-4.0 / libstdc++-v3 / include / tr1 / array
1 // class template array -*- C++ -*-
2
3 // Copyright (C) 2004 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 <bits/functexcept.h>
41
42 //namespace std::tr1
43 namespace std
44 {
45 namespace tr1
46 {
47   /// @brief  struct array [6.2.2].
48   /// NB: Requires complete type _Tp.
49   template<typename _Tp, size_t _Nm = 1>
50     struct array
51     {
52       typedef _Tp                               value_type;
53       typedef value_type&                       reference;
54       typedef const value_type&                 const_reference;
55       typedef value_type*                       iterator;
56       typedef const value_type*                 const_iterator;
57       typedef size_t                            size_type;
58       typedef ptrdiff_t                         difference_type;
59       typedef std::reverse_iterator<iterator>   reverse_iterator;
60       typedef std::reverse_iterator<const_iterator>     const_reverse_iterator;
61
62       // Compile time constant without other dependencies.
63       enum { _S_index = _Nm };
64
65       // Support for zero-sized arrays mandatory.
66       value_type _M_instance[_Nm ? _Nm : 1];
67
68       // No explicit construct/copy/destroy for aggregate type.
69
70       void 
71       assign(const value_type& u); 
72
73       void 
74       swap(array&);
75
76       // Iterators.
77       iterator 
78       begin()
79       { return reinterpret_cast<iterator>(&_M_instance[0]); }
80
81       const_iterator 
82       begin() const 
83       { return reinterpret_cast<const_iterator>(&_M_instance[0]); }
84
85       iterator 
86       end() 
87       { return reinterpret_cast<iterator>(&_M_instance[_Nm]); }
88
89       const_iterator 
90       end() const
91       { return reinterpret_cast<const_iterator>(&_M_instance[_Nm]); }
92
93       reverse_iterator 
94       rbegin()
95       { return reverse_iterator(this->end()); }
96
97       const_reverse_iterator 
98       rbegin() const
99       { return const_reverse_iterator(this->end()); }
100
101       reverse_iterator 
102       rend()
103       { return reverse_iterator(this->begin()); }
104
105       const_reverse_iterator 
106       rend() const
107       { return const_reverse_iterator(this->begin()); }
108
109       // Capacity.
110       size_type 
111       size() const { return _Nm; }
112
113       size_type 
114       max_size() const { return _Nm; }
115
116       bool 
117       empty() const { return size() == 0; }
118
119       // Element access.
120       reference 
121       operator[](size_type __n)
122       { return reinterpret_cast<reference>(_M_instance[__n]); }
123
124       const_reference 
125       operator[](size_type __n) const
126       { return reinterpret_cast<const_reference>(_M_instance[__n]); }
127
128       const_reference 
129       at(size_type __n) const
130       { 
131         if (__builtin_expect(__n > _Nm, false))
132           std::__throw_out_of_range("array::at");
133         return reinterpret_cast<const_reference>(_M_instance[__n]); 
134       }
135
136       reference 
137       at(size_type __n)
138       { 
139         if (__builtin_expect(__n > _Nm, false))
140           std::__throw_out_of_range("array::at");
141         return reinterpret_cast<reference>(_M_instance[__n]); 
142       }
143
144       reference 
145       front(); 
146
147       const_reference 
148       front() const; 
149
150       reference 
151       back(); 
152
153       const_reference 
154       back() const; 
155
156       _Tp* 
157       data(); 
158
159       const _Tp* 
160       data() const;
161     };
162
163   // Array comparisons.
164  template<typename _Tp, size_t _Nm>
165    bool 
166    operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
167    { return std::equal(__one.begin(), __one.end(), __two.begin()); }
168
169  template<typename _Tp, size_t _Nm>
170    bool 
171    operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
172    { return !(__one == __two); }
173
174  template<typename _Tp, size_t _Nm>
175    bool 
176    operator<(const array<_Tp, _Nm>& a, const array<_Tp, _Nm>& b)
177    { 
178      return std::lexicographical_compare(a.begin(), a.end(), 
179                                          b.begin(), b.end()); 
180    }
181
182  template<typename _Tp, size_t _Nm>
183    bool 
184    operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
185    { return __two < __one; }
186
187  template<typename _Tp, size_t _Nm>
188    bool 
189    operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
190    { return !(__one > __two); }
191
192  template<typename _Tp, size_t _Nm>
193    bool 
194    operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
195    { return !(__one < __two); }
196
197   // Specialized algorithms [6.2.2.2].
198  template<typename _Tp, size_t _Nm>
199    void
200    swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
201    { swap_ranges(__one.begin(), __one.end(), __two.begin()); }
202 } // namespace std::tr1
203 }
204
205 #endif