OSDN Git Service

d355285f56e818739029c30bacc6654680ccdda9
[pf3gnuchains/gcc-fork.git] / 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 #ifndef _ARRAY
31 #define _ARRAY 1
32
33 #include <new>
34 #include <iterator>
35 #include <algorithm>
36 #include <bits/functexcept.h>
37
38 //namespace std::tr1
39 namespace std
40 {
41 namespace tr1
42 {
43   // [6.2.2] Class template array template
44   // Requires complete type _Tp.
45   template<typename _Tp, size_t _Nm = 1>
46     struct array
47     {
48       typedef _Tp                               value_type;
49       typedef value_type&                       reference;
50       typedef const value_type&                 const_reference;
51       typedef value_type*                       iterator;
52       typedef const value_type*                 const_iterator;
53       typedef size_t                            size_type;
54       typedef ptrdiff_t                         difference_type;
55       typedef std::reverse_iterator<iterator>   reverse_iterator;
56       typedef std::reverse_iterator<const_iterator>     const_reverse_iterator;
57
58       // Compile time constant without other dependencies.
59       enum { _S_index = _Nm };
60
61       // Support for zero-sized arrays mandatory.
62       value_type _M_instance[_Nm ? _Nm : 1];
63
64       // No explicit construct/copy/destroy for aggregate type.
65
66       void 
67       assign(const value_type& u); 
68
69       void 
70       swap(array&);
71
72       // Iterators.
73       iterator 
74       begin()
75       { return reinterpret_cast<iterator>(&_M_instance[0]); }
76
77       const_iterator 
78       begin() const 
79       { return reinterpret_cast<const_iterator>(&_M_instance[0]); }
80
81       iterator 
82       end() 
83       { return reinterpret_cast<iterator>(&_M_instance[_Nm]); }
84
85       const_iterator 
86       end() const
87       { return reinterpret_cast<const_iterator>(&_M_instance[_Nm]); }
88
89       reverse_iterator 
90       rbegin()
91       { return reverse_iterator(this->end()); }
92
93       const_reverse_iterator 
94       rbegin() const
95       { return const_reverse_iterator(this->end()); }
96
97       reverse_iterator 
98       rend()
99       { return reverse_iterator(this->begin()); }
100
101       const_reverse_iterator 
102       rend() const
103       { return const_reverse_iterator(this->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 reinterpret_cast<reference>(_M_instance[__n]); }
119
120       const_reference 
121       operator[](size_type __n) const
122       { return reinterpret_cast<const_reference>(_M_instance[__n]); }
123
124       const_reference 
125       at(size_type __n) const
126       { 
127         if (__builtin_expect(__n > _Nm, false))
128           std::__throw_out_of_range("array::at");
129         return reinterpret_cast<const_reference>(_M_instance[__n]); 
130       }
131
132       reference 
133       at(size_type __n)
134       { 
135         if (__builtin_expect(__n > _Nm, false))
136           std::__throw_out_of_range("array::at");
137         return reinterpret_cast<reference>(_M_instance[__n]); 
138       }
139
140       reference 
141       front(); 
142
143       const_reference 
144       front() const; 
145
146       reference 
147       back(); 
148
149       const_reference 
150       back() const; 
151
152       _Tp* 
153       data(); 
154
155       const _Tp* 
156       data() const;
157     };
158
159   // Array comparisons.
160  template<typename _Tp, size_t _Nm>
161    bool 
162    operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
163    { return std::equal(__one.begin(), __one.end(), __two.begin()); }
164
165  template<typename _Tp, size_t _Nm>
166    bool 
167    operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
168    { return !(__one == __two); }
169
170  template<typename _Tp, size_t _Nm>
171    bool 
172    operator<(const array<_Tp, _Nm>& a, const array<_Tp, _Nm>& b)
173    { 
174      return std::lexicographical_compare(a.begin(), a.end(), 
175                                          b.begin(), b.end()); 
176    }
177
178  template<typename _Tp, size_t _Nm>
179    bool 
180    operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
181    { return __two < __one; }
182
183  template<typename _Tp, size_t _Nm>
184    bool 
185    operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
186    { return !(__one > __two); }
187
188  template<typename _Tp, size_t _Nm>
189    bool 
190    operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
191    { return !(__one < __two); }
192
193   // [6.2.2.2] Specialized algorithms.
194  template<typename _Tp, size_t _Nm>
195    void
196    swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
197    { swap_ranges(__one.begin(), __one.end(), __two.begin()); }
198 } // namespace std::tr1
199 }
200
201 #endif