blob: 2e02a43ed524a15c801f15268e43c6eeeeca339c [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- C++ -*-
2//===---------------------------- array -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:165//
Howard Hinnantb64f8b02010-11-16 22:09:026// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:168//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_ARRAY
12#define _LIBCPP_ARRAY
13
14/*
15 array synopsis
16
17namespace std
18{
Howard Hinnant324bb032010-08-22 00:02:4319template <class T, size_t N >
Howard Hinnantbc8d3f92010-05-11 19:42:1620struct array
Howard Hinnant324bb032010-08-22 00:02:4321{
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;
Howard Hinnantbc8d3f92010-05-11 19:42:1630 typedef T* pointer;
31 typedef const T* const_pointer;
Howard Hinnant324bb032010-08-22 00:02:4332 typedef std::reverse_iterator<iterator> reverse_iterator;
33 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:1634
Howard Hinnant324bb032010-08-22 00:02:4335 // No explicit construct/copy/destroy for aggregate type
36 void fill(const T& u);
Howard Hinnantf0562af2011-05-31 21:06:3337 void swap(array& a) noexcept(noexcept(swap(declval<T&>(), declval<T&>())));
Howard Hinnantbc8d3f92010-05-11 19:42:1638
Howard Hinnant324bb032010-08-22 00:02:4339 // iterators:
Howard Hinnantf0562af2011-05-31 21:06:3340 iterator begin() noexcept;
41 const_iterator begin() const noexcept;
42 iterator end() noexcept;
43 const_iterator end() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1644
Howard Hinnantf0562af2011-05-31 21:06:3345 reverse_iterator rbegin() noexcept;
46 const_reverse_iterator rbegin() const noexcept;
47 reverse_iterator rend() noexcept;
48 const_reverse_iterator rend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1649
Howard Hinnantf0562af2011-05-31 21:06:3350 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;
Howard Hinnantbc8d3f92010-05-11 19:42:1654
Howard Hinnant324bb032010-08-22 00:02:4355 // capacity:
Howard Hinnantf0562af2011-05-31 21:06:3356 constexpr size_type size() const noexcept;
57 constexpr size_type max_size() const noexcept;
Howard Hinnant08bce172012-07-20 19:20:4958 constexpr bool empty() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1659
Howard Hinnant324bb032010-08-22 00:02:4360 // element access:
61 reference operator[](size_type n);
Marshall Clow8fc4f5a2013-07-17 18:25:3662 const_reference operator[](size_type n) const; // constexpr in C++14
63 const_reference at(size_type n) const; // constexpr in C++14
Howard Hinnant324bb032010-08-22 00:02:4364 reference at(size_type n);
Howard Hinnantbc8d3f92010-05-11 19:42:1665
Howard Hinnant324bb032010-08-22 00:02:4366 reference front();
Marshall Clow8fc4f5a2013-07-17 18:25:3667 const_reference front() const; // constexpr in C++14
Howard Hinnant324bb032010-08-22 00:02:4368 reference back();
Marshall Clow8fc4f5a2013-07-17 18:25:3669 const_reference back() const; // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:1670
Howard Hinnantf0562af2011-05-31 21:06:3371 T* data() noexcept;
72 const T* data() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:1673};
74
Howard Hinnant324bb032010-08-22 00:02:4375template <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);
Howard Hinnantbc8d3f92010-05-11 19:42:1687
Howard Hinnant324bb032010-08-22 00:02:4388template <class T, size_t N >
Howard Hinnantf0562af2011-05-31 21:06:3389 void swap(array<T,N>& x, array<T,N>& y) noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:1690
Howard Hinnant324bb032010-08-22 00:02:4391template <class T> class tuple_size;
92template <int I, class T> class tuple_element;
Howard Hinnantbc8d3f92010-05-11 19:42:1693template <class T, size_t N> struct tuple_size<array<T, N>>;
Howard Hinnant324bb032010-08-22 00:02:4394template <int I, class T, size_t N> struct tuple_element<I, array<T, N>>;
Marshall Clow8fc4f5a2013-07-17 18:25:3695template <int I, class T, size_t N> T& get(array<T, N>&) noexcept; // constexpr in C++14
96template <int I, class T, size_t N> const T& get(const array<T, N>&) noexcept; // constexpr in C++14
97template <int I, class T, size_t N> T&& get(array<T, N>&&) noexcept; // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:1698
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
Howard Hinnant08e17472011-10-17 20:05:10114#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16115#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10116#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16117
118_LIBCPP_BEGIN_NAMESPACE_STD
119
Howard Hinnant324bb032010-08-22 00:02:43120template <class _Tp, size_t _Size>
Howard Hinnant0f678bd2013-08-12 18:38:34121struct _LIBCPP_TYPE_VIS_ONLY array
Howard Hinnantbc8d3f92010-05-11 19:42:16122{
123 // types:
124 typedef array __self;
Howard Hinnant324bb032010-08-22 00:02:43125 typedef _Tp value_type;
126 typedef value_type& reference;
127 typedef const value_type& const_reference;
Howard Hinnantbc8d3f92010-05-11 19:42:16128 typedef value_type* iterator;
129 typedef const value_type* const_iterator;
130 typedef value_type* pointer;
131 typedef const value_type* const_pointer;
Howard Hinnant324bb032010-08-22 00:02:43132 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;
Howard Hinnantbc8d3f92010-05-11 19:42:16136
137 value_type __elems_[_Size > 0 ? _Size : 1];
138
Howard Hinnant324bb032010-08-22 00:02:43139 // No explicit construct/copy/destroy for aggregate type
Howard Hinnant333f50d2010-09-21 20:16:37140 _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __u)
Howard Hinnant0949eed2011-06-30 21:18:19141 {_VSTD::fill_n(__elems_, _Size, __u);}
Howard Hinnantf0562af2011-05-31 21:06:33142 _LIBCPP_INLINE_VISIBILITY
143 void swap(array& __a) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value)
Howard Hinnant0949eed2011-06-30 21:18:19144 {_VSTD::swap_ranges(__elems_, __elems_ + _Size, __a.__elems_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16145
Howard Hinnant324bb032010-08-22 00:02:43146 // iterators:
Howard Hinnantf0562af2011-05-31 21:06:33147 _LIBCPP_INLINE_VISIBILITY
148 iterator begin() _NOEXCEPT {return iterator(__elems_);}
149 _LIBCPP_INLINE_VISIBILITY
150 const_iterator begin() const _NOEXCEPT {return const_iterator(__elems_);}
151 _LIBCPP_INLINE_VISIBILITY
152 iterator end() _NOEXCEPT {return iterator(__elems_ + _Size);}
153 _LIBCPP_INLINE_VISIBILITY
154 const_iterator end() const _NOEXCEPT {return const_iterator(__elems_ + _Size);}
Howard Hinnantbc8d3f92010-05-11 19:42:16155
Howard Hinnantf0562af2011-05-31 21:06:33156 _LIBCPP_INLINE_VISIBILITY
157 reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
158 _LIBCPP_INLINE_VISIBILITY
159 const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());}
160 _LIBCPP_INLINE_VISIBILITY
161 reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
162 _LIBCPP_INLINE_VISIBILITY
163 const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16164
Howard Hinnantf0562af2011-05-31 21:06:33165 _LIBCPP_INLINE_VISIBILITY
166 const_iterator cbegin() const _NOEXCEPT {return begin();}
167 _LIBCPP_INLINE_VISIBILITY
168 const_iterator cend() const _NOEXCEPT {return end();}
169 _LIBCPP_INLINE_VISIBILITY
170 const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
171 _LIBCPP_INLINE_VISIBILITY
172 const_reverse_iterator crend() const _NOEXCEPT {return rend();}
Howard Hinnantbc8d3f92010-05-11 19:42:16173
Howard Hinnant324bb032010-08-22 00:02:43174 // capacity:
Howard Hinnantf0562af2011-05-31 21:06:33175 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant08bce172012-07-20 19:20:49176 _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return _Size;}
Howard Hinnantf0562af2011-05-31 21:06:33177 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant08bce172012-07-20 19:20:49178 _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return _Size;}
Howard Hinnantf0562af2011-05-31 21:06:33179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant08bce172012-07-20 19:20:49180 _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return _Size == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16181
Howard Hinnant324bb032010-08-22 00:02:43182 // element access:
Howard Hinnantbc8d3f92010-05-11 19:42:16183 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __elems_[__n];}
Marshall Clow8fc4f5a2013-07-17 18:25:36184 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference operator[](size_type __n) const {return __elems_[__n];}
Howard Hinnant324bb032010-08-22 00:02:43185 reference at(size_type __n);
Marshall Clow8fc4f5a2013-07-17 18:25:36186 _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference at(size_type __n) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16187
188 _LIBCPP_INLINE_VISIBILITY reference front() {return __elems_[0];}
Marshall Clow8fc4f5a2013-07-17 18:25:36189 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference front() const {return __elems_[0];}
Howard Hinnantbc8d3f92010-05-11 19:42:16190 _LIBCPP_INLINE_VISIBILITY reference back() {return __elems_[_Size > 0 ? _Size-1 : 0];}
Marshall Clow8fc4f5a2013-07-17 18:25:36191 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference back() const {return __elems_[_Size > 0 ? _Size-1 : 0];}
Howard Hinnantbc8d3f92010-05-11 19:42:16192
Howard Hinnantf0562af2011-05-31 21:06:33193 _LIBCPP_INLINE_VISIBILITY
194 value_type* data() _NOEXCEPT {return __elems_;}
195 _LIBCPP_INLINE_VISIBILITY
196 const value_type* data() const _NOEXCEPT {return __elems_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16197};
198
Howard Hinnant324bb032010-08-22 00:02:43199template <class _Tp, size_t _Size>
Howard Hinnantbc8d3f92010-05-11 19:42:16200typename array<_Tp, _Size>::reference
201array<_Tp, _Size>::at(size_type __n)
202{
203 if (__n >= _Size)
204#ifndef _LIBCPP_NO_EXCEPTIONS
205 throw out_of_range("array::at");
206#else
207 assert(!"array::at out_of_range");
208#endif
209 return __elems_[__n];
210}
211
Howard Hinnant324bb032010-08-22 00:02:43212template <class _Tp, size_t _Size>
Marshall Clow8fc4f5a2013-07-17 18:25:36213_LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16214typename array<_Tp, _Size>::const_reference
215array<_Tp, _Size>::at(size_type __n) const
216{
217 if (__n >= _Size)
218#ifndef _LIBCPP_NO_EXCEPTIONS
219 throw out_of_range("array::at");
220#else
221 assert(!"array::at out_of_range");
222#endif
223 return __elems_[__n];
224}
225
Howard Hinnant324bb032010-08-22 00:02:43226template <class _Tp, size_t _Size>
Howard Hinnant1e564242013-10-04 22:09:00227inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16228bool
229operator==(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
230{
Howard Hinnant0949eed2011-06-30 21:18:19231 return _VSTD::equal(__x.__elems_, __x.__elems_ + _Size, __y.__elems_);
Howard Hinnantbc8d3f92010-05-11 19:42:16232}
233
Howard Hinnant324bb032010-08-22 00:02:43234template <class _Tp, size_t _Size>
Howard Hinnant1e564242013-10-04 22:09:00235inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16236bool
237operator!=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
238{
239 return !(__x == __y);
240}
241
Howard Hinnant324bb032010-08-22 00:02:43242template <class _Tp, size_t _Size>
Howard Hinnant1e564242013-10-04 22:09:00243inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16244bool
245operator<(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
246{
Howard Hinnant0949eed2011-06-30 21:18:19247 return _VSTD::lexicographical_compare(__x.__elems_, __x.__elems_ + _Size, __y.__elems_, __y.__elems_ + _Size);
Howard Hinnantbc8d3f92010-05-11 19:42:16248}
249
Howard Hinnant324bb032010-08-22 00:02:43250template <class _Tp, size_t _Size>
Howard Hinnant1e564242013-10-04 22:09:00251inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16252bool
253operator>(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
254{
255 return __y < __x;
256}
257
Howard Hinnant324bb032010-08-22 00:02:43258template <class _Tp, size_t _Size>
Howard Hinnant1e564242013-10-04 22:09:00259inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16260bool
261operator<=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
262{
263 return !(__y < __x);
264}
265
Howard Hinnant324bb032010-08-22 00:02:43266template <class _Tp, size_t _Size>
Howard Hinnant1e564242013-10-04 22:09:00267inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16268bool
269operator>=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
270{
271 return !(__x < __y);
272}
273
Howard Hinnant324bb032010-08-22 00:02:43274template <class _Tp, size_t _Size>
Howard Hinnant1e564242013-10-04 22:09:00275inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantaabf2872011-06-01 19:59:32276typename enable_if
277<
278 __is_swappable<_Tp>::value,
279 void
280>::type
Howard Hinnantbc8d3f92010-05-11 19:42:16281swap(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
Howard Hinnantf0562af2011-05-31 21:06:33282 _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16283{
284 __x.swap(__y);
285}
286
287template <class _Tp, size_t _Size>
Howard Hinnant0f678bd2013-08-12 18:38:34288class _LIBCPP_TYPE_VIS_ONLY tuple_size<array<_Tp, _Size> >
Howard Hinnant333f50d2010-09-21 20:16:37289 : public integral_constant<size_t, _Size> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16290
Howard Hinnantbc8d3f92010-05-11 19:42:16291template <size_t _Ip, class _Tp, size_t _Size>
Howard Hinnant0f678bd2013-08-12 18:38:34292class _LIBCPP_TYPE_VIS_ONLY tuple_element<_Ip, array<_Tp, _Size> >
Howard Hinnantbc8d3f92010-05-11 19:42:16293{
294public:
295 typedef _Tp type;
296};
297
298template <size_t _Ip, class _Tp, size_t _Size>
Howard Hinnant1e564242013-10-04 22:09:00299inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16300_Tp&
Howard Hinnantf0562af2011-05-31 21:06:33301get(array<_Tp, _Size>& __a) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16302{
Marshall Clowa46482e2012-12-18 16:46:30303 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array)");
Marshall Clow8fc4f5a2013-07-17 18:25:36304 return __a.__elems_[_Ip];
Howard Hinnantbc8d3f92010-05-11 19:42:16305}
306
307template <size_t _Ip, class _Tp, size_t _Size>
Howard Hinnant1e564242013-10-04 22:09:00308inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16309const _Tp&
Howard Hinnantf0562af2011-05-31 21:06:33310get(const array<_Tp, _Size>& __a) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16311{
Marshall Clowa46482e2012-12-18 16:46:30312 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array)");
Marshall Clow8fc4f5a2013-07-17 18:25:36313 return __a.__elems_[_Ip];
Howard Hinnantbc8d3f92010-05-11 19:42:16314}
315
Howard Hinnantcd2254b2010-11-17 19:52:17316#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
317
318template <size_t _Ip, class _Tp, size_t _Size>
Howard Hinnant1e564242013-10-04 22:09:00319inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantcd2254b2010-11-17 19:52:17320_Tp&&
Howard Hinnantf0562af2011-05-31 21:06:33321get(array<_Tp, _Size>&& __a) _NOEXCEPT
Howard Hinnantcd2254b2010-11-17 19:52:17322{
Marshall Clowa46482e2012-12-18 16:46:30323 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array &&)");
Marshall Clow8fc4f5a2013-07-17 18:25:36324 return _VSTD::move(__a.__elems_[_Ip]);
Howard Hinnantcd2254b2010-11-17 19:52:17325}
326
327#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
328
Howard Hinnantbc8d3f92010-05-11 19:42:16329_LIBCPP_END_NAMESPACE_STD
330
331#endif // _LIBCPP_ARRAY