]>
Commit | Line | Data |
---|---|---|
3febde35 BK |
1 | // class template array -*- C++ -*- |
2 | ||
8f13c4d6 | 3 | // Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc. |
3febde35 BK |
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 | |
83f51799 | 18 | // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
3febde35 BK |
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 | ||
00aca6e8 BK |
30 | /** @file |
31 | * This is a TR1 C++ Library header. | |
32 | */ | |
33 | ||
928341b2 BK |
34 | #ifndef _TR1_ARRAY |
35 | #define _TR1_ARRAY 1 | |
3febde35 BK |
36 | |
37 | #include <new> | |
38 | #include <iterator> | |
aed305a9 | 39 | #include <algorithm> |
5a2ab2c3 | 40 | #include <cstddef> |
aed305a9 | 41 | #include <bits/functexcept.h> |
e7457c3e | 42 | #include <ext/type_traits.h> |
3febde35 BK |
43 | |
44 | //namespace std::tr1 | |
45 | namespace std | |
46 | { | |
3cbc7af0 BK |
47 | _GLIBCXX_BEGIN_NAMESPACE(tr1) |
48 | ||
00aca6e8 BK |
49 | /// @brief struct array [6.2.2]. |
50 | /// NB: Requires complete type _Tp. | |
e7457c3e | 51 | template<typename _Tp, std::size_t _Nm> |
3febde35 BK |
52 | struct array |
53 | { | |
ffe7d885 PC |
54 | typedef _Tp value_type; |
55 | typedef value_type& reference; | |
56 | typedef const value_type& const_reference; | |
57 | typedef value_type* iterator; | |
58 | typedef const value_type* const_iterator; | |
59 | typedef std::size_t size_type; | |
60 | typedef std::ptrdiff_t difference_type; | |
61 | typedef std::reverse_iterator<iterator> reverse_iterator; | |
62 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; | |
3febde35 | 63 | |
aed305a9 | 64 | // Support for zero-sized arrays mandatory. |
8f13c4d6 | 65 | value_type _M_instance[_Nm ? _Nm : 1] __attribute__((__aligned__)); |
3febde35 BK |
66 | |
67 | // No explicit construct/copy/destroy for aggregate type. | |
68 | ||
69 | void | |
ffe7d885 PC |
70 | assign(const value_type& __u) |
71 | { std::fill_n(begin(), size(), __u); } | |
3febde35 BK |
72 | |
73 | void | |
ffe7d885 PC |
74 | swap(array& __other) |
75 | { std::swap_ranges(begin(), end(), __other.begin()); } | |
3febde35 BK |
76 | |
77 | // Iterators. | |
e182017e | 78 | iterator |
3febde35 | 79 | begin() |
e182017e | 80 | { return iterator(&_M_instance[0]); } |
3febde35 | 81 | |
e182017e | 82 | const_iterator |
3febde35 | 83 | begin() const |
e182017e | 84 | { return const_iterator(&_M_instance[0]); } |
3febde35 | 85 | |
e182017e | 86 | iterator |
3febde35 | 87 | end() |
e182017e | 88 | { return iterator(&_M_instance[_Nm]); } |
3febde35 | 89 | |
e182017e | 90 | const_iterator |
3febde35 | 91 | end() const |
e182017e | 92 | { return const_iterator(&_M_instance[_Nm]); } |
3febde35 BK |
93 | |
94 | reverse_iterator | |
95 | rbegin() | |
5a2ab2c3 | 96 | { return reverse_iterator(end()); } |
3febde35 BK |
97 | |
98 | const_reverse_iterator | |
99 | rbegin() const | |
5a2ab2c3 | 100 | { return const_reverse_iterator(end()); } |
3febde35 BK |
101 | |
102 | reverse_iterator | |
103 | rend() | |
5a2ab2c3 | 104 | { return reverse_iterator(begin()); } |
3febde35 BK |
105 | |
106 | const_reverse_iterator | |
107 | rend() const | |
5a2ab2c3 | 108 | { return const_reverse_iterator(begin()); } |
3febde35 BK |
109 | |
110 | // Capacity. | |
111 | size_type | |
aed305a9 | 112 | size() const { return _Nm; } |
3febde35 BK |
113 | |
114 | size_type | |
aed305a9 | 115 | max_size() const { return _Nm; } |
3febde35 BK |
116 | |
117 | bool | |
aed305a9 | 118 | empty() const { return size() == 0; } |
3febde35 BK |
119 | |
120 | // Element access. | |
e7457c3e | 121 | reference |
3febde35 | 122 | operator[](size_type __n) |
5a2ab2c3 | 123 | { return _M_instance[__n]; } |
3febde35 | 124 | |
e7457c3e | 125 | const_reference |
3febde35 | 126 | operator[](size_type __n) const |
5a2ab2c3 | 127 | { return _M_instance[__n]; } |
3febde35 | 128 | |
e7457c3e | 129 | reference |
3febde35 | 130 | at(size_type __n) |
e7457c3e PC |
131 | { return _M_at<_Nm>(__n); } |
132 | ||
133 | const_reference | |
134 | at(size_type __n) const | |
135 | { return _M_at<_Nm>(__n); } | |
3febde35 BK |
136 | |
137 | reference | |
5a2ab2c3 PC |
138 | front() |
139 | { return *begin(); } | |
3febde35 BK |
140 | |
141 | const_reference | |
5a2ab2c3 PC |
142 | front() const |
143 | { return *begin(); } | |
3febde35 BK |
144 | |
145 | reference | |
5a2ab2c3 | 146 | back() |
e7457c3e | 147 | { return _Nm ? *(end() - 1) : *end(); } |
3febde35 BK |
148 | |
149 | const_reference | |
5a2ab2c3 | 150 | back() const |
e7457c3e | 151 | { return _Nm ? *(end() - 1) : *end(); } |
3febde35 BK |
152 | |
153 | _Tp* | |
5a2ab2c3 PC |
154 | data() |
155 | { return &_M_instance[0]; } | |
3febde35 BK |
156 | |
157 | const _Tp* | |
5a2ab2c3 PC |
158 | data() const |
159 | { return &_M_instance[0]; } | |
e7457c3e PC |
160 | |
161 | private: | |
162 | template<std::size_t _Mm> | |
163 | typename __gnu_cxx::__enable_if<_Mm, reference>::__type | |
164 | _M_at(size_type __n) | |
165 | { | |
166 | if (__builtin_expect(__n >= _Mm, false)) | |
c927b11c | 167 | std::__throw_out_of_range(__N("array::_M_at")); |
e7457c3e PC |
168 | return _M_instance[__n]; |
169 | } | |
170 | ||
171 | // Avoid "unsigned comparison with zero" warnings. | |
172 | template<std::size_t _Mm> | |
173 | typename __gnu_cxx::__enable_if<!_Mm, reference>::__type | |
174 | _M_at(size_type) | |
175 | { | |
c927b11c | 176 | std::__throw_out_of_range(__N("array::_M_at")); |
e7457c3e PC |
177 | return _M_instance[0]; |
178 | } | |
179 | ||
180 | template<std::size_t _Mm> | |
181 | typename __gnu_cxx::__enable_if<_Mm, const_reference>::__type | |
182 | _M_at(size_type __n) const | |
183 | { | |
184 | if (__builtin_expect(__n >= _Mm, false)) | |
c927b11c | 185 | std::__throw_out_of_range(__N("array::_M_at")); |
e7457c3e PC |
186 | return _M_instance[__n]; |
187 | } | |
188 | ||
189 | template<std::size_t _Mm> | |
190 | typename __gnu_cxx::__enable_if<!_Mm, const_reference>::__type | |
191 | _M_at(size_type) const | |
192 | { | |
c927b11c | 193 | std::__throw_out_of_range(__N("array::_M_at")); |
e7457c3e PC |
194 | return _M_instance[0]; |
195 | } | |
3febde35 BK |
196 | }; |
197 | ||
198 | // Array comparisons. | |
5a2ab2c3 | 199 | template<typename _Tp, std::size_t _Nm> |
ffe7d885 | 200 | inline bool |
5a2ab2c3 PC |
201 | operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) |
202 | { return std::equal(__one.begin(), __one.end(), __two.begin()); } | |
203 | ||
204 | template<typename _Tp, std::size_t _Nm> | |
ffe7d885 | 205 | inline bool |
5a2ab2c3 PC |
206 | operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) |
207 | { return !(__one == __two); } | |
208 | ||
209 | template<typename _Tp, std::size_t _Nm> | |
ffe7d885 | 210 | inline bool |
5a2ab2c3 PC |
211 | operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b) |
212 | { | |
213 | return std::lexicographical_compare(__a.begin(), __a.end(), | |
214 | __b.begin(), __b.end()); | |
215 | } | |
216 | ||
217 | template<typename _Tp, std::size_t _Nm> | |
ffe7d885 | 218 | inline bool |
5a2ab2c3 PC |
219 | operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) |
220 | { return __two < __one; } | |
221 | ||
222 | template<typename _Tp, std::size_t _Nm> | |
ffe7d885 | 223 | inline bool |
5a2ab2c3 PC |
224 | operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) |
225 | { return !(__one > __two); } | |
226 | ||
227 | template<typename _Tp, std::size_t _Nm> | |
ffe7d885 | 228 | inline bool |
5a2ab2c3 PC |
229 | operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) |
230 | { return !(__one < __two); } | |
3febde35 | 231 | |
00aca6e8 | 232 | // Specialized algorithms [6.2.2.2]. |
5a2ab2c3 | 233 | template<typename _Tp, std::size_t _Nm> |
ffe7d885 | 234 | inline void |
5a2ab2c3 | 235 | swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two) |
ffe7d885 | 236 | { std::swap_ranges(__one.begin(), __one.end(), __two.begin()); } |
5a2ab2c3 PC |
237 | |
238 | // Tuple interface to class template array [6.2.2.5]. | |
239 | template<typename _Tp> class tuple_size; | |
240 | template<int _Int, typename _Tp> class tuple_element; | |
e7457c3e | 241 | |
5a2ab2c3 PC |
242 | template<typename _Tp, std::size_t _Nm> |
243 | struct tuple_size<array<_Tp, _Nm> > | |
244 | { static const int value = _Nm; }; | |
e7457c3e | 245 | |
c8bf5b7c PC |
246 | template<typename _Tp, std::size_t _Nm> |
247 | const int tuple_size<array<_Tp, _Nm> >::value; | |
248 | ||
5a2ab2c3 PC |
249 | template<int _Int, typename _Tp, std::size_t _Nm> |
250 | struct tuple_element<_Int, array<_Tp, _Nm> > | |
251 | { typedef _Tp type; }; | |
252 | ||
253 | template<int _Int, typename _Tp, std::size_t _Nm> | |
ffe7d885 | 254 | inline _Tp& |
5a2ab2c3 PC |
255 | get(array<_Tp, _Nm>& __arr) |
256 | { return __arr[_Int]; } | |
257 | ||
258 | template<int _Int, typename _Tp, std::size_t _Nm> | |
ffe7d885 | 259 | inline const _Tp& |
5a2ab2c3 PC |
260 | get(const array<_Tp, _Nm>& __arr) |
261 | { return __arr[_Int]; } | |
3cbc7af0 BK |
262 | |
263 | _GLIBCXX_END_NAMESPACE | |
3febde35 BK |
264 | } |
265 | ||
266 | #endif |