xref: /llvm-project/libcxx/include/array (revision ce48a1137d56d368828d360e5f2a8162bac6517c)
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(noexcept(swap(declval<T&>(), declval<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    bool empty() const noexcept;
59
60    // element access:
61    reference operator[](size_type n);
62    const_reference operator[](size_type n) const;
63    const_reference at(size_type n) const;
64    reference at(size_type n);
65
66    reference front();
67    const_reference front() const;
68    reference back();
69    const_reference back() const;
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 <int I, class T> class tuple_element;
93template <class T, size_t N> struct tuple_size<array<T, N>>;
94template <int I, class T, size_t N> struct tuple_element<I, array<T, N>>;
95template <int I, class T, size_t N> T& get(array<T, N>&) noexcept;
96template <int I, class T, size_t N> const T& get(const array<T, N>&) noexcept;
97template <int I, class T, size_t N> T&& get(array<T, N>&&) noexcept;
98
99}  // std
100
101*/
102
103#include <__config>
104#include <__tuple>
105#include <type_traits>
106#include <utility>
107#include <iterator>
108#include <algorithm>
109#include <stdexcept>
110#if defined(_LIBCPP_NO_EXCEPTIONS)
111    #include <cassert>
112#endif
113
114#pragma GCC system_header
115
116_LIBCPP_BEGIN_NAMESPACE_STD
117
118template <class _Tp, size_t _Size>
119struct _LIBCPP_VISIBLE array
120{
121    // types:
122    typedef array __self;
123    typedef _Tp                                   value_type;
124    typedef value_type&                           reference;
125    typedef const value_type&                     const_reference;
126    typedef value_type*                           iterator;
127    typedef const value_type*                     const_iterator;
128    typedef value_type*                           pointer;
129    typedef const value_type*                     const_pointer;
130    typedef size_t                                size_type;
131    typedef ptrdiff_t                             difference_type;
132    typedef std::reverse_iterator<iterator>       reverse_iterator;
133    typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
134
135    value_type __elems_[_Size > 0 ? _Size : 1];
136
137    // No explicit construct/copy/destroy for aggregate type
138    _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __u)
139        {_VSTD::fill_n(__elems_, _Size, __u);}
140    _LIBCPP_INLINE_VISIBILITY
141    void swap(array& __a) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value)
142        {_VSTD::swap_ranges(__elems_, __elems_ + _Size, __a.__elems_);}
143
144    // iterators:
145    _LIBCPP_INLINE_VISIBILITY
146    iterator begin() _NOEXCEPT {return iterator(__elems_);}
147    _LIBCPP_INLINE_VISIBILITY
148    const_iterator begin() const _NOEXCEPT {return const_iterator(__elems_);}
149    _LIBCPP_INLINE_VISIBILITY
150    iterator end() _NOEXCEPT {return iterator(__elems_ + _Size);}
151    _LIBCPP_INLINE_VISIBILITY
152    const_iterator end() const _NOEXCEPT {return const_iterator(__elems_ + _Size);}
153
154    _LIBCPP_INLINE_VISIBILITY
155    reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
156    _LIBCPP_INLINE_VISIBILITY
157    const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());}
158    _LIBCPP_INLINE_VISIBILITY
159    reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
160    _LIBCPP_INLINE_VISIBILITY
161    const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());}
162
163    _LIBCPP_INLINE_VISIBILITY
164    const_iterator cbegin() const _NOEXCEPT {return begin();}
165    _LIBCPP_INLINE_VISIBILITY
166    const_iterator cend() const _NOEXCEPT {return end();}
167    _LIBCPP_INLINE_VISIBILITY
168    const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
169    _LIBCPP_INLINE_VISIBILITY
170    const_reverse_iterator crend() const _NOEXCEPT {return rend();}
171
172    // capacity:
173    _LIBCPP_INLINE_VISIBILITY
174    /*constexpr*/ size_type size() const _NOEXCEPT {return _Size;}
175    _LIBCPP_INLINE_VISIBILITY
176    /*constexpr*/ size_type max_size() const _NOEXCEPT {return _Size;}
177    _LIBCPP_INLINE_VISIBILITY
178    bool empty() const _NOEXCEPT {return _Size == 0;}
179
180    // element access:
181    _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n)             {return __elems_[__n];}
182    _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __elems_[__n];}
183    reference at(size_type __n);
184    const_reference at(size_type __n) const;
185
186    _LIBCPP_INLINE_VISIBILITY reference front()             {return __elems_[0];}
187    _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __elems_[0];}
188    _LIBCPP_INLINE_VISIBILITY reference back()              {return __elems_[_Size > 0 ? _Size-1 : 0];}
189    _LIBCPP_INLINE_VISIBILITY const_reference back() const  {return __elems_[_Size > 0 ? _Size-1 : 0];}
190
191    _LIBCPP_INLINE_VISIBILITY
192    value_type* data() _NOEXCEPT {return __elems_;}
193    _LIBCPP_INLINE_VISIBILITY
194    const value_type* data() const _NOEXCEPT {return __elems_;}
195};
196
197template <class _Tp, size_t _Size>
198typename array<_Tp, _Size>::reference
199array<_Tp, _Size>::at(size_type __n)
200{
201    if (__n >= _Size)
202#ifndef _LIBCPP_NO_EXCEPTIONS
203        throw out_of_range("array::at");
204#else
205        assert(!"array::at out_of_range");
206#endif
207    return __elems_[__n];
208}
209
210template <class _Tp, size_t _Size>
211typename array<_Tp, _Size>::const_reference
212array<_Tp, _Size>::at(size_type __n) const
213{
214    if (__n >= _Size)
215#ifndef _LIBCPP_NO_EXCEPTIONS
216        throw out_of_range("array::at");
217#else
218        assert(!"array::at out_of_range");
219#endif
220    return __elems_[__n];
221}
222
223template <class _Tp, size_t _Size>
224_LIBCPP_INLINE_VISIBILITY inline
225bool
226operator==(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
227{
228    return _VSTD::equal(__x.__elems_, __x.__elems_ + _Size, __y.__elems_);
229}
230
231template <class _Tp, size_t _Size>
232_LIBCPP_INLINE_VISIBILITY inline
233bool
234operator!=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
235{
236    return !(__x == __y);
237}
238
239template <class _Tp, size_t _Size>
240_LIBCPP_INLINE_VISIBILITY inline
241bool
242operator<(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
243{
244    return _VSTD::lexicographical_compare(__x.__elems_, __x.__elems_ + _Size, __y.__elems_, __y.__elems_ + _Size);
245}
246
247template <class _Tp, size_t _Size>
248_LIBCPP_INLINE_VISIBILITY inline
249bool
250operator>(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
251{
252    return __y < __x;
253}
254
255template <class _Tp, size_t _Size>
256_LIBCPP_INLINE_VISIBILITY inline
257bool
258operator<=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
259{
260    return !(__y < __x);
261}
262
263template <class _Tp, size_t _Size>
264_LIBCPP_INLINE_VISIBILITY inline
265bool
266operator>=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
267{
268    return !(__x < __y);
269}
270
271template <class _Tp, size_t _Size>
272_LIBCPP_INLINE_VISIBILITY inline
273typename enable_if
274<
275    __is_swappable<_Tp>::value,
276    void
277>::type
278swap(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
279                                  _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value)
280{
281    __x.swap(__y);
282}
283
284template <class _Tp, size_t _Size>
285class _LIBCPP_VISIBLE tuple_size<array<_Tp, _Size> >
286    : public integral_constant<size_t, _Size> {};
287
288template <class _Tp, size_t _Size>
289class _LIBCPP_VISIBLE tuple_size<const array<_Tp, _Size> >
290    : public integral_constant<size_t, _Size> {};
291
292template <size_t _Ip, class _Tp, size_t _Size>
293class _LIBCPP_VISIBLE tuple_element<_Ip, array<_Tp, _Size> >
294{
295public:
296    typedef _Tp type;
297};
298
299template <size_t _Ip, class _Tp, size_t _Size>
300class _LIBCPP_VISIBLE tuple_element<_Ip, const array<_Tp, _Size> >
301{
302public:
303    typedef const _Tp type;
304};
305
306template <size_t _Ip, class _Tp, size_t _Size>
307_LIBCPP_INLINE_VISIBILITY inline
308_Tp&
309get(array<_Tp, _Size>& __a) _NOEXCEPT
310{
311    return __a[_Ip];
312}
313
314template <size_t _Ip, class _Tp, size_t _Size>
315_LIBCPP_INLINE_VISIBILITY inline
316const _Tp&
317get(const array<_Tp, _Size>& __a) _NOEXCEPT
318{
319    return __a[_Ip];
320}
321
322#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
323
324template <size_t _Ip, class _Tp, size_t _Size>
325_LIBCPP_INLINE_VISIBILITY inline
326_Tp&&
327get(array<_Tp, _Size>&& __a) _NOEXCEPT
328{
329    return _VSTD::move(__a[_Ip]);
330}
331
332#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
333
334_LIBCPP_END_NAMESPACE_STD
335
336#endif  // _LIBCPP_ARRAY
337