blob: 3801262297f802b987f958915951d4b8d8e8a601 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- array -----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// 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:16 +00008//
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:43 +000019template <class T, size_t N >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000020struct array
Howard Hinnant324bb032010-08-22 00:02:43 +000021{
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:16 +000030 typedef T* pointer;
31 typedef const T* const_pointer;
Howard Hinnant324bb032010-08-22 00:02:43 +000032 typedef std::reverse_iterator<iterator> reverse_iterator;
33 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000034
Howard Hinnant324bb032010-08-22 00:02:43 +000035 // No explicit construct/copy/destroy for aggregate type
36 void fill(const T& u);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000037 void swap(array& a);
38
Howard Hinnant324bb032010-08-22 00:02:43 +000039 // iterators:
40 iterator begin();
41 const_iterator begin() const;
42 iterator end();
43 const_iterator end() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000044
Howard Hinnant324bb032010-08-22 00:02:43 +000045 reverse_iterator rbegin();
46 const_reverse_iterator rbegin() const;
47 reverse_iterator rend();
48 const_reverse_iterator rend() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000049
Howard Hinnant324bb032010-08-22 00:02:43 +000050 const_iterator cbegin() const;
51 const_iterator cend() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000052 const_reverse_iterator crbegin() const;
Howard Hinnant324bb032010-08-22 00:02:43 +000053 const_reverse_iterator crend() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000054
Howard Hinnant324bb032010-08-22 00:02:43 +000055 // capacity:
56 constexpr size_type size() const;
57 constexpr size_type max_size() const;
58 bool empty() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000059
Howard Hinnant324bb032010-08-22 00:02:43 +000060 // 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);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000065
Howard Hinnant324bb032010-08-22 00:02:43 +000066 reference front();
67 const_reference front() const;
68 reference back();
69 const_reference back() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000070
Howard Hinnant324bb032010-08-22 00:02:43 +000071 T* data();
72 const T* data() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000073};
74
Howard Hinnant324bb032010-08-22 00:02:43 +000075template <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:16 +000087
Howard Hinnant324bb032010-08-22 00:02:43 +000088template <class T, size_t N >
89 void swap(array<T,N>& x, array<T,N>& y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000090
Howard Hinnant324bb032010-08-22 00:02:43 +000091template <class T> class tuple_size;
92template <int I, class T> class tuple_element;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000093template <class T, size_t N> struct tuple_size<array<T, N>>;
Howard Hinnant324bb032010-08-22 00:02:43 +000094template <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>&);
96template <int I, class T, size_t N> const T& get(const array<T, N>&);
Howard Hinnantcd2254b2010-11-17 19:52:17 +000097template <int I, class T, size_t N> T&& get(array<T, N>&&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000098
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
Howard Hinnant324bb032010-08-22 00:02:43 +0000118template <class _Tp, size_t _Size>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000119struct _LIBCPP_VISIBLE array
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000120{
121 // types:
122 typedef array __self;
Howard Hinnant324bb032010-08-22 00:02:43 +0000123 typedef _Tp value_type;
124 typedef value_type& reference;
125 typedef const value_type& const_reference;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000126 typedef value_type* iterator;
127 typedef const value_type* const_iterator;
128 typedef value_type* pointer;
129 typedef const value_type* const_pointer;
Howard Hinnant324bb032010-08-22 00:02:43 +0000130 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;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000134
135 value_type __elems_[_Size > 0 ? _Size : 1];
136
Howard Hinnant324bb032010-08-22 00:02:43 +0000137 // No explicit construct/copy/destroy for aggregate type
Howard Hinnant333f50d2010-09-21 20:16:37 +0000138 _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __u)
139 {_STD::fill_n(__elems_, _Size, __u);}
140 _LIBCPP_INLINE_VISIBILITY void swap(array& __a)
141 {_STD::swap_ranges(__elems_, __elems_ + _Size, __a.__elems_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000142
Howard Hinnant324bb032010-08-22 00:02:43 +0000143 // iterators:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000144 _LIBCPP_INLINE_VISIBILITY iterator begin() {return iterator(__elems_);}
145 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const {return const_iterator(__elems_);}
146 _LIBCPP_INLINE_VISIBILITY iterator end() {return iterator(__elems_ + _Size);}
147 _LIBCPP_INLINE_VISIBILITY const_iterator end() const {return const_iterator(__elems_ + _Size);}
148
149 _LIBCPP_INLINE_VISIBILITY reverse_iterator rbegin() {return reverse_iterator(end());}
150 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rbegin() const {return const_reverse_iterator(end());}
151 _LIBCPP_INLINE_VISIBILITY reverse_iterator rend() {return reverse_iterator(begin());}
152 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rend() const {return const_reverse_iterator(begin());}
153
154 _LIBCPP_INLINE_VISIBILITY const_iterator cbegin() const {return begin();}
155 _LIBCPP_INLINE_VISIBILITY const_iterator cend() const {return end();}
156 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crbegin() const {return rbegin();}
157 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crend() const {return rend();}
158
Howard Hinnant324bb032010-08-22 00:02:43 +0000159 // capacity:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000160 _LIBCPP_INLINE_VISIBILITY /*constexpr*/ size_type size() const {return _Size;}
161 _LIBCPP_INLINE_VISIBILITY /*constexpr*/ size_type max_size() const {return _Size;}
162 _LIBCPP_INLINE_VISIBILITY bool empty() const {return _Size == 0;}
163
Howard Hinnant324bb032010-08-22 00:02:43 +0000164 // element access:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000165 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __elems_[__n];}
166 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __elems_[__n];}
Howard Hinnant324bb032010-08-22 00:02:43 +0000167 reference at(size_type __n);
168 const_reference at(size_type __n) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000169
170 _LIBCPP_INLINE_VISIBILITY reference front() {return __elems_[0];}
171 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __elems_[0];}
172 _LIBCPP_INLINE_VISIBILITY reference back() {return __elems_[_Size > 0 ? _Size-1 : 0];}
173 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __elems_[_Size > 0 ? _Size-1 : 0];}
174
175 _LIBCPP_INLINE_VISIBILITY value_type* data() {return __elems_;}
Howard Hinnant324bb032010-08-22 00:02:43 +0000176 _LIBCPP_INLINE_VISIBILITY const value_type* data() const {return __elems_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000177};
178
Howard Hinnant324bb032010-08-22 00:02:43 +0000179template <class _Tp, size_t _Size>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000180typename array<_Tp, _Size>::reference
181array<_Tp, _Size>::at(size_type __n)
182{
183 if (__n >= _Size)
184#ifndef _LIBCPP_NO_EXCEPTIONS
185 throw out_of_range("array::at");
186#else
187 assert(!"array::at out_of_range");
188#endif
189 return __elems_[__n];
190}
191
Howard Hinnant324bb032010-08-22 00:02:43 +0000192template <class _Tp, size_t _Size>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000193typename array<_Tp, _Size>::const_reference
194array<_Tp, _Size>::at(size_type __n) const
195{
196 if (__n >= _Size)
197#ifndef _LIBCPP_NO_EXCEPTIONS
198 throw out_of_range("array::at");
199#else
200 assert(!"array::at out_of_range");
201#endif
202 return __elems_[__n];
203}
204
Howard Hinnant324bb032010-08-22 00:02:43 +0000205template <class _Tp, size_t _Size>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000206_LIBCPP_INLINE_VISIBILITY inline
207bool
208operator==(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
209{
210 return _STD::equal(__x.__elems_, __x.__elems_ + _Size, __y.__elems_);
211}
212
Howard Hinnant324bb032010-08-22 00:02:43 +0000213template <class _Tp, size_t _Size>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000214_LIBCPP_INLINE_VISIBILITY inline
215bool
216operator!=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
217{
218 return !(__x == __y);
219}
220
Howard Hinnant324bb032010-08-22 00:02:43 +0000221template <class _Tp, size_t _Size>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000222_LIBCPP_INLINE_VISIBILITY inline
223bool
224operator<(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
225{
226 return _STD::lexicographical_compare(__x.__elems_, __x.__elems_ + _Size, __y.__elems_, __y.__elems_ + _Size);
227}
228
Howard Hinnant324bb032010-08-22 00:02:43 +0000229template <class _Tp, size_t _Size>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000230_LIBCPP_INLINE_VISIBILITY inline
231bool
232operator>(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
233{
234 return __y < __x;
235}
236
Howard Hinnant324bb032010-08-22 00:02:43 +0000237template <class _Tp, size_t _Size>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000238_LIBCPP_INLINE_VISIBILITY inline
239bool
240operator<=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
241{
242 return !(__y < __x);
243}
244
Howard Hinnant324bb032010-08-22 00:02:43 +0000245template <class _Tp, size_t _Size>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000246_LIBCPP_INLINE_VISIBILITY inline
247bool
248operator>=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
249{
250 return !(__x < __y);
251}
252
Howard Hinnant324bb032010-08-22 00:02:43 +0000253template <class _Tp, size_t _Size>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000254_LIBCPP_INLINE_VISIBILITY inline
255void
256swap(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
257{
258 __x.swap(__y);
259}
260
261template <class _Tp, size_t _Size>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000262class _LIBCPP_VISIBLE tuple_size<array<_Tp, _Size> >
263 : public integral_constant<size_t, _Size> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000264
265template <class _Tp, size_t _Size>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000266class _LIBCPP_VISIBLE tuple_size<const array<_Tp, _Size> >
267 : public integral_constant<size_t, _Size> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000268
269template <size_t _Ip, class _Tp, size_t _Size>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000270class _LIBCPP_VISIBLE tuple_element<_Ip, array<_Tp, _Size> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000271{
272public:
273 typedef _Tp type;
274};
275
276template <size_t _Ip, class _Tp, size_t _Size>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000277class _LIBCPP_VISIBLE tuple_element<_Ip, const array<_Tp, _Size> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000278{
279public:
280 typedef const _Tp type;
281};
282
283template <size_t _Ip, class _Tp, size_t _Size>
284_LIBCPP_INLINE_VISIBILITY inline
285_Tp&
286get(array<_Tp, _Size>& __a)
287{
288 return __a[_Ip];
289}
290
291template <size_t _Ip, class _Tp, size_t _Size>
292_LIBCPP_INLINE_VISIBILITY inline
293const _Tp&
294get(const array<_Tp, _Size>& __a)
295{
296 return __a[_Ip];
297}
298
Howard Hinnantcd2254b2010-11-17 19:52:17 +0000299#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
300
301template <size_t _Ip, class _Tp, size_t _Size>
302_LIBCPP_INLINE_VISIBILITY inline
303_Tp&&
304get(array<_Tp, _Size>&& __a)
305{
306 return _STD::move(__a[_Ip]);
307}
308
309#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
310
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000311_LIBCPP_END_NAMESPACE_STD
312
313#endif // _LIBCPP_ARRAY