1 // Profile array implementation -*- C++ -*-
3 // Copyright (C) 2012-2017 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
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 // No explicit construct/copy/destroy for aggregate type.
63 fill(const value_type& __u)
64 { std::fill_n(begin(), size(), __u); }
68 noexcept(_AT_Type::_Is_nothrow_swappable::value)
69 { std::swap_ranges(begin(), end(), __other.begin()); }
72 _GLIBCXX17_CONSTEXPR iterator
74 { return iterator(data()); }
76 _GLIBCXX17_CONSTEXPR const_iterator
77 begin() const noexcept
78 { return const_iterator(data()); }
80 _GLIBCXX17_CONSTEXPR iterator
82 { return iterator(data() + _Nm); }
84 _GLIBCXX17_CONSTEXPR const_iterator
86 { return const_iterator(data() + _Nm); }
88 _GLIBCXX17_CONSTEXPR reverse_iterator
90 { return reverse_iterator(end()); }
92 _GLIBCXX17_CONSTEXPR const_reverse_iterator
93 rbegin() const noexcept
94 { return const_reverse_iterator(end()); }
96 _GLIBCXX17_CONSTEXPR reverse_iterator
98 { return reverse_iterator(begin()); }
100 _GLIBCXX17_CONSTEXPR const_reverse_iterator
101 rend() const noexcept
102 { return const_reverse_iterator(begin()); }
104 _GLIBCXX17_CONSTEXPR const_iterator
105 cbegin() const noexcept
106 { return const_iterator(data()); }
108 _GLIBCXX17_CONSTEXPR const_iterator
109 cend() const noexcept
110 { return const_iterator(data() + _Nm); }
112 _GLIBCXX17_CONSTEXPR const_reverse_iterator
113 crbegin() const noexcept
114 { return const_reverse_iterator(end()); }
116 _GLIBCXX17_CONSTEXPR const_reverse_iterator
117 crend() const noexcept
118 { return const_reverse_iterator(begin()); }
122 size() const noexcept { return _Nm; }
125 max_size() const noexcept { return _Nm; }
128 empty() const noexcept { return size() == 0; }
132 operator[](size_type __n) noexcept
133 { return _AT_Type::_S_ref(_M_elems, __n); }
135 constexpr const_reference
136 operator[](size_type __n) const noexcept
137 { return _AT_Type::_S_ref(_M_elems, __n); }
139 _GLIBCXX17_CONSTEXPR reference
143 std::__throw_out_of_range_fmt(__N("array::at: __n "
144 "(which is %zu) >= _Nm "
147 return _AT_Type::_S_ref(_M_elems, __n);
150 constexpr const_reference
151 at(size_type __n) const
153 // Result of conditional expression must be an lvalue so use
154 // boolean ? lvalue : (throw-expr, lvalue)
155 return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n)
156 : (std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
157 ">= _Nm (which is %zu)"),
159 _AT_Type::_S_ref(_M_elems, 0));
162 _GLIBCXX17_CONSTEXPR reference
166 constexpr const_reference
167 front() const noexcept
168 { return _AT_Type::_S_ref(_M_elems, 0); }
170 _GLIBCXX17_CONSTEXPR reference
172 { return _Nm ? *(end() - 1) : *end(); }
174 constexpr const_reference
175 back() const noexcept
177 return _Nm ? _AT_Type::_S_ref(_M_elems, _Nm - 1)
178 : _AT_Type::_S_ref(_M_elems, 0);
181 _GLIBCXX17_CONSTEXPR pointer
183 { return _AT_Type::_S_ptr(_M_elems); }
185 _GLIBCXX17_CONSTEXPR const_pointer
186 data() const noexcept
187 { return _AT_Type::_S_ptr(_M_elems); }
190 // Array comparisons.
191 template<typename _Tp, std::size_t _Nm>
193 operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
194 { return std::equal(__one.begin(), __one.end(), __two.begin()); }
196 template<typename _Tp, std::size_t _Nm>
198 operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
199 { return !(__one == __two); }
201 template<typename _Tp, std::size_t _Nm>
203 operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
205 return std::lexicographical_compare(__a.begin(), __a.end(),
206 __b.begin(), __b.end());
209 template<typename _Tp, std::size_t _Nm>
211 operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
212 { return __two < __one; }
214 template<typename _Tp, std::size_t _Nm>
216 operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
217 { return !(__one > __two); }
219 template<typename _Tp, std::size_t _Nm>
221 operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
222 { return !(__one < __two); }
224 // Specialized algorithms.
225 template<typename _Tp, std::size_t _Nm>
227 swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
228 noexcept(noexcept(__one.swap(__two)))
229 { __one.swap(__two); }
231 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
233 get(array<_Tp, _Nm>& __arr) noexcept
235 static_assert(_Int < _Nm, "index is out of bounds");
236 return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
237 _S_ref(__arr._M_elems, _Int);
240 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
242 get(array<_Tp, _Nm>&& __arr) noexcept
244 static_assert(_Int < _Nm, "index is out of bounds");
245 return std::move(__profile::get<_Int>(__arr));
248 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
250 get(const array<_Tp, _Nm>& __arr) noexcept
252 static_assert(_Int < _Nm, "index is out of bounds");
253 return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
254 _S_ref(__arr._M_elems, _Int);
256 } // namespace __profile
258 _GLIBCXX_BEGIN_NAMESPACE_VERSION
259 // Tuple interface to class template array.
262 template<typename _Tp, std::size_t _Nm>
263 struct tuple_size<std::__profile::array<_Tp, _Nm>>
264 : public integral_constant<std::size_t, _Nm> { };
267 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
268 struct tuple_element<_Int, std::__profile::array<_Tp, _Nm>>
270 static_assert(_Int < _Nm, "index is out of bounds");
274 template<typename _Tp, std::size_t _Nm>
275 struct __is_tuple_like_impl<std::__profile::array<_Tp, _Nm>> : true_type
278 _GLIBCXX_END_NAMESPACE_VERSION
281 #endif // _GLIBCXX_PROFILE_ARRAY