1 // Debugging array implementation -*- C++ -*-
3 // Copyright (C) 2012-2015 Free Software Foundation, Inc.
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 3, or (at your option)
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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
26 * This is a Standard C++ Library header.
29 #ifndef _GLIBCXX_DEBUG_ARRAY
30 #define _GLIBCXX_DEBUG_ARRAY 1
32 #pragma GCC system_header
34 #include <debug/safe_sequence.h>
36 namespace std _GLIBCXX_VISIBILITY(default)
40 template<typename _Tp, std::size_t _Nm>
43 typedef _Tp value_type;
44 typedef value_type* pointer;
45 typedef const value_type* const_pointer;
46 typedef value_type& reference;
47 typedef const value_type& const_reference;
48 typedef value_type* iterator;
49 typedef const value_type* const_iterator;
50 typedef std::size_t size_type;
51 typedef std::ptrdiff_t difference_type;
52 typedef std::reverse_iterator<iterator> reverse_iterator;
53 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
55 // Support for zero-sized arrays mandatory.
56 typedef _GLIBCXX_STD_C::__array_traits<_Tp, _Nm> _AT_Type;
57 typename _AT_Type::_Type _M_elems;
59 template<std::size_t _Size>
60 struct _Array_check_subscript
62 std::size_t size() { return _Size; }
64 _Array_check_subscript(std::size_t __index)
65 { __glibcxx_check_subscript(__index); }
68 template<std::size_t _Size>
69 struct _Array_check_nonempty
71 bool empty() { return _Size == 0; }
73 _Array_check_nonempty()
74 { __glibcxx_check_nonempty(); }
77 // No explicit construct/copy/destroy for aggregate type.
81 fill(const value_type& __u)
82 { std::fill_n(begin(), size(), __u); }
86 noexcept(noexcept(swap(std::declval<_Tp&>(), std::declval<_Tp&>())))
87 { std::swap_ranges(begin(), end(), __other.begin()); }
92 { return iterator(data()); }
95 begin() const noexcept
96 { return const_iterator(data()); }
100 { return iterator(data() + _Nm); }
104 { return const_iterator(data() + _Nm); }
108 { return reverse_iterator(end()); }
110 const_reverse_iterator
111 rbegin() const noexcept
112 { return const_reverse_iterator(end()); }
116 { return reverse_iterator(begin()); }
118 const_reverse_iterator
119 rend() const noexcept
120 { return const_reverse_iterator(begin()); }
123 cbegin() const noexcept
124 { return const_iterator(data()); }
127 cend() const noexcept
128 { return const_iterator(data() + _Nm); }
130 const_reverse_iterator
131 crbegin() const noexcept
132 { return const_reverse_iterator(end()); }
134 const_reverse_iterator
135 crend() const noexcept
136 { return const_reverse_iterator(begin()); }
140 size() const noexcept { return _Nm; }
143 max_size() const noexcept { return _Nm; }
146 empty() const noexcept { return size() == 0; }
150 operator[](size_type __n) noexcept
152 __glibcxx_check_subscript(__n);
153 return _AT_Type::_S_ref(_M_elems, __n);
156 constexpr const_reference
157 operator[](size_type __n) const noexcept
159 return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n)
160 : (_GLIBCXX_THROW_OR_ABORT(_Array_check_subscript<_Nm>(__n)),
161 _AT_Type::_S_ref(_M_elems, 0));
168 std::__throw_out_of_range_fmt(__N("array::at: __n "
169 "(which is %zu) >= _Nm "
172 return _AT_Type::_S_ref(_M_elems, __n);
175 constexpr const_reference
176 at(size_type __n) const
178 // Result of conditional expression must be an lvalue so use
179 // boolean ? lvalue : (throw-expr, lvalue)
180 return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n)
181 : (std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
182 ">= _Nm (which is %zu)"),
184 _AT_Type::_S_ref(_M_elems, 0));
190 __glibcxx_check_nonempty();
194 constexpr const_reference
195 front() const noexcept
197 return _Nm ? _AT_Type::_S_ref(_M_elems, 0)
198 : (_GLIBCXX_THROW_OR_ABORT(_Array_check_nonempty<_Nm>()),
199 _AT_Type::_S_ref(_M_elems, 0));
205 __glibcxx_check_nonempty();
206 return _Nm ? *(end() - 1) : *end();
209 constexpr const_reference
210 back() const noexcept
212 return _Nm ? _AT_Type::_S_ref(_M_elems, _Nm - 1)
213 : (_GLIBCXX_THROW_OR_ABORT(_Array_check_nonempty<_Nm>()),
214 _AT_Type::_S_ref(_M_elems, 0));
219 { return _AT_Type::_S_ptr(_M_elems); }
222 data() const noexcept
223 { return _AT_Type::_S_ptr(_M_elems); }
226 // Array comparisons.
227 template<typename _Tp, std::size_t _Nm>
229 operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
230 { return std::equal(__one.begin(), __one.end(), __two.begin()); }
232 template<typename _Tp, std::size_t _Nm>
234 operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
235 { return !(__one == __two); }
237 template<typename _Tp, std::size_t _Nm>
239 operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
241 return std::lexicographical_compare(__a.begin(), __a.end(),
242 __b.begin(), __b.end());
245 template<typename _Tp, std::size_t _Nm>
247 operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
248 { return __two < __one; }
250 template<typename _Tp, std::size_t _Nm>
252 operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
253 { return !(__one > __two); }
255 template<typename _Tp, std::size_t _Nm>
257 operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
258 { return !(__one < __two); }
260 // Specialized algorithms.
261 template<typename _Tp, std::size_t _Nm>
263 swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
264 noexcept(noexcept(__one.swap(__two)))
265 { __one.swap(__two); }
267 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
269 get(array<_Tp, _Nm>& __arr) noexcept
271 static_assert(_Int < _Nm, "index is out of bounds");
272 return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
273 _S_ref(__arr._M_elems, _Int);
276 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
278 get(array<_Tp, _Nm>&& __arr) noexcept
280 static_assert(_Int < _Nm, "index is out of bounds");
281 return std::move(__debug::get<_Int>(__arr));
284 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
286 get(const array<_Tp, _Nm>& __arr) noexcept
288 static_assert(_Int < _Nm, "index is out of bounds");
289 return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
290 _S_ref(__arr._M_elems, _Int);
292 } // namespace __debug
294 // Tuple interface to class template array.
297 template<typename _Tp, std::size_t _Nm>
298 struct tuple_size<__debug::array<_Tp, _Nm>>
299 : public integral_constant<std::size_t, _Nm> { };
302 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
303 struct tuple_element<_Int, __debug::array<_Tp, _Nm>>
305 static_assert(_Int < _Nm, "index is out of bounds");
310 #endif // _GLIBCXX_DEBUG_ARRAY