this repo has no description
1// -*- C++ -*-
2//===---------------------------- array -----------------------------------===//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_ARRAY
12#define _LIBCPP_ARRAY
13
14/*
15 array synopsis
16
17namespace std
18{
19template <class T, size_t N >
20struct array
21{
22 // types:
23 typedef T & reference;
24 typedef const T & const_reference;
25 typedef implementation defined iterator;
26 typedef implementation defined const_iterator;
27 typedef size_t size_type;
28 typedef ptrdiff_t difference_type;
29 typedef T value_type;
30 typedef T* pointer;
31 typedef const T* const_pointer;
32 typedef std::reverse_iterator<iterator> reverse_iterator;
33 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
34
35 // No explicit construct/copy/destroy for aggregate type
36 void fill(const T& u);
37 void swap(array& a) noexcept(is_nothrow_swappable_v<T>);
38
39 // iterators:
40 iterator begin() noexcept;
41 const_iterator begin() const noexcept;
42 iterator end() noexcept;
43 const_iterator end() const noexcept;
44
45 reverse_iterator rbegin() noexcept;
46 const_reverse_iterator rbegin() const noexcept;
47 reverse_iterator rend() noexcept;
48 const_reverse_iterator rend() const noexcept;
49
50 const_iterator cbegin() const noexcept;
51 const_iterator cend() const noexcept;
52 const_reverse_iterator crbegin() const noexcept;
53 const_reverse_iterator crend() const noexcept;
54
55 // capacity:
56 constexpr size_type size() const noexcept;
57 constexpr size_type max_size() const noexcept;
58 constexpr bool empty() const noexcept;
59
60 // element access:
61 reference operator[](size_type n);
62 const_reference operator[](size_type n) const; // constexpr in C++14
63 const_reference at(size_type n) const; // constexpr in C++14
64 reference at(size_type n);
65
66 reference front();
67 const_reference front() const; // constexpr in C++14
68 reference back();
69 const_reference back() const; // constexpr in C++14
70
71 T* data() noexcept;
72 const T* data() const noexcept;
73};
74
75template <class T, size_t N>
76 bool operator==(const array<T,N>& x, const array<T,N>& y);
77template <class T, size_t N>
78 bool operator!=(const array<T,N>& x, const array<T,N>& y);
79template <class T, size_t N>
80 bool operator<(const array<T,N>& x, const array<T,N>& y);
81template <class T, size_t N>
82 bool operator>(const array<T,N>& x, const array<T,N>& y);
83template <class T, size_t N>
84 bool operator<=(const array<T,N>& x, const array<T,N>& y);
85template <class T, size_t N>
86 bool operator>=(const array<T,N>& x, const array<T,N>& y);
87
88template <class T, size_t N >
89 void swap(array<T,N>& x, array<T,N>& y) noexcept(noexcept(x.swap(y)));
90
91template <class T> class tuple_size;
92template <size_t I, class T> class tuple_element;
93template <class T, size_t N> struct tuple_size<array<T, N>>;
94template <size_t I, class T, size_t N> struct tuple_element<I, array<T, N>>;
95template <size_t I, class T, size_t N> T& get(array<T, N>&) noexcept; // constexpr in C++14
96template <size_t I, class T, size_t N> const T& get(const array<T, N>&) noexcept; // constexpr in C++14
97template <size_t I, class T, size_t N> T&& get(array<T, N>&&) noexcept; // constexpr in C++14
98template <size_t I, class T, size_t N> const T&& get(const array<T, N>&&) noexcept; // constexpr in C++14
99
100} // std
101
102*/
103
104#include <__config>
105#include <__tuple>
106#include <type_traits>
107#include <utility>
108#include <iterator>
109#include <algorithm>
110#include <stdexcept>
111
112#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
113#pragma GCC system_header
114#endif
115
116
117
118_LIBCPP_BEGIN_NAMESPACE_STD
119
120template <class _Tp, size_t _Size>
121struct _LIBCPP_TEMPLATE_VIS array
122{
123 // types:
124 typedef array __self;
125 typedef _Tp value_type;
126 typedef value_type& reference;
127 typedef const value_type& const_reference;
128 typedef value_type* iterator;
129 typedef const value_type* const_iterator;
130 typedef value_type* pointer;
131 typedef const value_type* const_pointer;
132 typedef size_t size_type;
133 typedef ptrdiff_t difference_type;
134 typedef std::reverse_iterator<iterator> reverse_iterator;
135 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
136
137 value_type __elems_[_Size > 0 ? _Size : 1];
138
139 // No explicit construct/copy/destroy for aggregate type
140 _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __u)
141 {_VSTD::fill_n(__elems_, _Size, __u);}
142 _LIBCPP_INLINE_VISIBILITY
143 void swap(array& __a) _NOEXCEPT_(_Size == 0 || __is_nothrow_swappable<_Tp>::value)
144 { __swap_dispatch((std::integral_constant<bool, _Size == 0>()), __a); }
145
146 _LIBCPP_INLINE_VISIBILITY
147 void __swap_dispatch(std::true_type, array&) {}
148
149 _LIBCPP_INLINE_VISIBILITY
150 void __swap_dispatch(std::false_type, array& __a)
151 { _VSTD::swap_ranges(__elems_, __elems_ + _Size, __a.__elems_);}
152
153 // iterators:
154 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
155 iterator begin() _NOEXCEPT {return iterator(__elems_);}
156 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
157 const_iterator begin() const _NOEXCEPT {return const_iterator(__elems_);}
158 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
159 iterator end() _NOEXCEPT {return iterator(__elems_ + _Size);}
160 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
161 const_iterator end() const _NOEXCEPT {return const_iterator(__elems_ + _Size);}
162
163 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
164 reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
165 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
166 const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());}
167 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
168 reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
169 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
170 const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());}
171
172 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
173 const_iterator cbegin() const _NOEXCEPT {return begin();}
174 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
175 const_iterator cend() const _NOEXCEPT {return end();}
176 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
177 const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
178 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
179 const_reverse_iterator crend() const _NOEXCEPT {return rend();}
180
181 // capacity:
182 _LIBCPP_INLINE_VISIBILITY
183 _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return _Size;}
184 _LIBCPP_INLINE_VISIBILITY
185 _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return _Size;}
186 _LIBCPP_INLINE_VISIBILITY
187 _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return _Size == 0;}
188
189 // element access:
190 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
191 reference operator[](size_type __n) {return __elems_[__n];}
192 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
193 const_reference operator[](size_type __n) const {return __elems_[__n];}
194
195 _LIBCPP_CONSTEXPR_AFTER_CXX14 reference at(size_type __n);
196 _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference at(size_type __n) const;
197
198 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference front() {return __elems_[0];}
199 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference front() const {return __elems_[0];}
200 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference back() {return __elems_[_Size > 0 ? _Size-1 : 0];}
201 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference back() const {return __elems_[_Size > 0 ? _Size-1 : 0];}
202
203 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
204 value_type* data() _NOEXCEPT {return __elems_;}
205 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
206 const value_type* data() const _NOEXCEPT {return __elems_;}
207};
208
209template <class _Tp, size_t _Size>
210_LIBCPP_CONSTEXPR_AFTER_CXX14
211typename array<_Tp, _Size>::reference
212array<_Tp, _Size>::at(size_type __n)
213{
214 if (__n >= _Size)
215 __throw_out_of_range("array::at");
216
217 return __elems_[__n];
218}
219
220template <class _Tp, size_t _Size>
221_LIBCPP_CONSTEXPR_AFTER_CXX11
222typename array<_Tp, _Size>::const_reference
223array<_Tp, _Size>::at(size_type __n) const
224{
225 if (__n >= _Size)
226 __throw_out_of_range("array::at");
227 return __elems_[__n];
228}
229
230template <class _Tp, size_t _Size>
231inline _LIBCPP_INLINE_VISIBILITY
232bool
233operator==(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
234{
235 return _VSTD::equal(__x.__elems_, __x.__elems_ + _Size, __y.__elems_);
236}
237
238template <class _Tp, size_t _Size>
239inline _LIBCPP_INLINE_VISIBILITY
240bool
241operator!=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
242{
243 return !(__x == __y);
244}
245
246template <class _Tp, size_t _Size>
247inline _LIBCPP_INLINE_VISIBILITY
248bool
249operator<(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
250{
251 return _VSTD::lexicographical_compare(__x.__elems_, __x.__elems_ + _Size, __y.__elems_, __y.__elems_ + _Size);
252}
253
254template <class _Tp, size_t _Size>
255inline _LIBCPP_INLINE_VISIBILITY
256bool
257operator>(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
258{
259 return __y < __x;
260}
261
262template <class _Tp, size_t _Size>
263inline _LIBCPP_INLINE_VISIBILITY
264bool
265operator<=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
266{
267 return !(__y < __x);
268}
269
270template <class _Tp, size_t _Size>
271inline _LIBCPP_INLINE_VISIBILITY
272bool
273operator>=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
274{
275 return !(__x < __y);
276}
277
278template <class _Tp, size_t _Size>
279inline _LIBCPP_INLINE_VISIBILITY
280typename enable_if
281<
282 _Size == 0 ||
283 __is_swappable<_Tp>::value,
284 void
285>::type
286swap(array<_Tp, _Size>& __x, array<_Tp, _Size>& __y)
287 _NOEXCEPT_(noexcept(__x.swap(__y)))
288{
289 __x.swap(__y);
290}
291
292template <class _Tp, size_t _Size>
293class _LIBCPP_TEMPLATE_VIS tuple_size<array<_Tp, _Size> >
294 : public integral_constant<size_t, _Size> {};
295
296template <size_t _Ip, class _Tp, size_t _Size>
297class _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, array<_Tp, _Size> >
298{
299 static_assert(_Ip < _Size, "Index out of bounds in std::tuple_element<> (std::array)");
300public:
301 typedef _Tp type;
302};
303
304template <size_t _Ip, class _Tp, size_t _Size>
305inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
306_Tp&
307get(array<_Tp, _Size>& __a) _NOEXCEPT
308{
309 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array)");
310 return __a.__elems_[_Ip];
311}
312
313template <size_t _Ip, class _Tp, size_t _Size>
314inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
315const _Tp&
316get(const array<_Tp, _Size>& __a) _NOEXCEPT
317{
318 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array)");
319 return __a.__elems_[_Ip];
320}
321
322#ifndef _LIBCPP_CXX03_LANG
323
324template <size_t _Ip, class _Tp, size_t _Size>
325inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
326_Tp&&
327get(array<_Tp, _Size>&& __a) _NOEXCEPT
328{
329 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array &&)");
330 return _VSTD::move(__a.__elems_[_Ip]);
331}
332
333template <size_t _Ip, class _Tp, size_t _Size>
334inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
335const _Tp&&
336get(const array<_Tp, _Size>&& __a) _NOEXCEPT
337{
338 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array &&)");
339 return _VSTD::move(__a.__elems_[_Ip]);
340}
341
342#endif // !_LIBCPP_CXX03_LANG
343
344_LIBCPP_END_NAMESPACE_STD
345
346#endif // _LIBCPP_ARRAY