1 // Profile array implementation -*- C++ -*-
3 // Copyright (C) 2012-2014 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/>.
25 /** @file profile/array
26 * This is a Standard C++ Library header.
29 #ifndef _GLIBCXX_PROFILE_ARRAY
30 #define _GLIBCXX_PROFILE_ARRAY 1
32 #pragma GCC system_header
34 namespace std _GLIBCXX_VISIBILITY(default)
38 template<typename _Tp, std::size_t _Nm>
41 typedef _Tp value_type;
42 typedef value_type* pointer;
43 typedef const value_type* const_pointer;
44 typedef value_type& reference;
45 typedef const value_type& const_reference;
46 typedef value_type* iterator;
47 typedef const value_type* const_iterator;
48 typedef std::size_t size_type;
49 typedef std::ptrdiff_t difference_type;
50 typedef std::reverse_iterator<iterator> reverse_iterator;
51 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
53 // Support for zero-sized arrays mandatory.
54 typedef _GLIBCXX_STD_C::__array_traits<_Tp, _Nm> _AT_Type;
55 typename _AT_Type::_Type _M_elems;
57 // No explicit construct/copy/destroy for aggregate type.
61 fill(const value_type& __u)
62 { std::fill_n(begin(), size(), __u); }
66 noexcept(noexcept(swap(std::declval<_Tp&>(), std::declval<_Tp&>())))
67 { std::swap_ranges(begin(), end(), __other.begin()); }
72 { return iterator(data()); }
75 begin() const noexcept
76 { return const_iterator(data()); }
80 { return iterator(data() + _Nm); }
84 { return const_iterator(data() + _Nm); }
88 { return reverse_iterator(end()); }
90 const_reverse_iterator
91 rbegin() const noexcept
92 { return const_reverse_iterator(end()); }
96 { return reverse_iterator(begin()); }
98 const_reverse_iterator
100 { return const_reverse_iterator(begin()); }
103 cbegin() const noexcept
104 { return const_iterator(data()); }
107 cend() const noexcept
108 { return const_iterator(data() + _Nm); }
110 const_reverse_iterator
111 crbegin() const noexcept
112 { return const_reverse_iterator(end()); }
114 const_reverse_iterator
115 crend() const noexcept
116 { return const_reverse_iterator(begin()); }
120 size() const noexcept { return _Nm; }
123 max_size() const noexcept { return _Nm; }
126 empty() const noexcept { return size() == 0; }
130 operator[](size_type __n) noexcept
131 { return _AT_Type::_S_ref(_M_elems, __n); }
133 constexpr const_reference
134 operator[](size_type __n) const noexcept
135 { return _AT_Type::_S_ref(_M_elems, __n); }
141 std::__throw_out_of_range_fmt(__N("array::at: __n "
142 "(which is %zu) >= _Nm "
145 return _AT_Type::_S_ref(_M_elems, __n);
148 constexpr const_reference
149 at(size_type __n) const
151 // Result of conditional expression must be an lvalue so use
152 // boolean ? lvalue : (throw-expr, lvalue)
153 return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n)
154 : (std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
155 ">= _Nm (which is %zu)"),
157 _AT_Type::_S_ref(_M_elems, 0));
164 constexpr const_reference
165 front() const noexcept
166 { return _AT_Type::_S_ref(_M_elems, 0); }
170 { return _Nm ? *(end() - 1) : *end(); }
172 constexpr const_reference
173 back() const noexcept
175 return _Nm ? _AT_Type::_S_ref(_M_elems, _Nm - 1)
176 : _AT_Type::_S_ref(_M_elems, 0);
181 { return std::__addressof(_AT_Type::_S_ref(_M_elems, 0)); }
184 data() const noexcept
185 { return std::__addressof(_AT_Type::_S_ref(_M_elems, 0)); }
188 // Array comparisons.
189 template<typename _Tp, std::size_t _Nm>
191 operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
192 { return std::equal(__one.begin(), __one.end(), __two.begin()); }
194 template<typename _Tp, std::size_t _Nm>
196 operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
197 { return !(__one == __two); }
199 template<typename _Tp, std::size_t _Nm>
201 operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
203 return std::lexicographical_compare(__a.begin(), __a.end(),
204 __b.begin(), __b.end());
207 template<typename _Tp, std::size_t _Nm>
209 operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
210 { return __two < __one; }
212 template<typename _Tp, std::size_t _Nm>
214 operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
215 { return !(__one > __two); }
217 template<typename _Tp, std::size_t _Nm>
219 operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
220 { return !(__one < __two); }
222 // Specialized algorithms.
223 template<typename _Tp, std::size_t _Nm>
225 swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
226 noexcept(noexcept(__one.swap(__two)))
227 { __one.swap(__two); }
229 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
231 get(array<_Tp, _Nm>& __arr) noexcept
233 static_assert(_Int < _Nm, "index is out of bounds");
234 return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
235 _S_ref(__arr._M_elems, _Int);
238 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
240 get(array<_Tp, _Nm>&& __arr) noexcept
242 static_assert(_Int < _Nm, "index is out of bounds");
243 return std::move(get<_Int>(__arr));
246 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
248 get(const array<_Tp, _Nm>& __arr) noexcept
250 static_assert(_Int < _Nm, "index is out of bounds");
251 return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
252 _S_ref(__arr._M_elems, _Int);
254 } // namespace __profile
256 // Tuple interface to class template array.
259 template<typename _Tp, std::size_t _Nm>
260 struct tuple_size<__profile::array<_Tp, _Nm>>
261 : public integral_constant<std::size_t, _Nm> { };
264 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
265 struct tuple_element<_Int, __profile::array<_Tp, _Nm>>
267 static_assert(_Int < _Nm, "index is out of bounds");
272 #endif // _GLIBCXX_PROFILE_ARRAY