1 // Profile array implementation -*- C++ -*-
3 // Copyright (C) 2012-2013 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)
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(__N("array::at"));
142 return _AT_Type::_S_ref(_M_elems, __n);
145 constexpr const_reference
146 at(size_type __n) const
148 // Result of conditional expression must be an lvalue so use
149 // boolean ? lvalue : (throw-expr, lvalue)
150 return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n)
151 : (std::__throw_out_of_range(__N("array::at")),
152 _AT_Type::_S_ref(_M_elems, 0));
159 constexpr const_reference
161 { return _AT_Type::_S_ref(_M_elems, 0); }
165 { return _Nm ? *(end() - 1) : *end(); }
167 constexpr const_reference
170 return _Nm ? _AT_Type::_S_ref(_M_elems, _Nm - 1)
171 : _AT_Type::_S_ref(_M_elems, 0);
176 { return std::__addressof(_AT_Type::_S_ref(_M_elems, 0)); }
179 data() const noexcept
180 { return std::__addressof(_AT_Type::_S_ref(_M_elems, 0)); }
183 // Array comparisons.
184 template<typename _Tp, std::size_t _Nm>
186 operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
187 { return std::equal(__one.begin(), __one.end(), __two.begin()); }
189 template<typename _Tp, std::size_t _Nm>
191 operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
192 { return !(__one == __two); }
194 template<typename _Tp, std::size_t _Nm>
196 operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
198 return std::lexicographical_compare(__a.begin(), __a.end(),
199 __b.begin(), __b.end());
202 template<typename _Tp, std::size_t _Nm>
204 operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
205 { return __two < __one; }
207 template<typename _Tp, std::size_t _Nm>
209 operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
210 { return !(__one > __two); }
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 // Specialized algorithms.
218 template<typename _Tp, std::size_t _Nm>
220 swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
221 noexcept(noexcept(__one.swap(__two)))
222 { __one.swap(__two); }
224 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
226 get(array<_Tp, _Nm>& __arr) noexcept
228 static_assert(_Int < _Nm, "index is out of bounds");
229 return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
230 _S_ref(__arr._M_elems, _Int);
233 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
235 get(array<_Tp, _Nm>&& __arr) noexcept
237 static_assert(_Int < _Nm, "index is out of bounds");
238 return std::move(get<_Int>(__arr));
241 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
243 get(const array<_Tp, _Nm>& __arr) noexcept
245 static_assert(_Int < _Nm, "index is out of bounds");
246 return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
247 _S_ref(__arr._M_elems, _Int);
249 } // namespace __profile
251 // Tuple interface to class template array.
254 template<typename _Tp, std::size_t _Nm>
255 struct tuple_size<__profile::array<_Tp, _Nm>>
256 : public integral_constant<std::size_t, _Nm> { };
259 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
260 struct tuple_element<_Int, __profile::array<_Tp, _Nm>>
262 static_assert(_Int < _Nm, "index is out of bounds");
267 #endif // _GLIBCXX_PROFILE_ARRAY