blob: 0ee59ef79177c14c1f0ef38a3b7e1d88867b8f14 [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//
6// This file is distributed under the University of Illinois Open Source
7// License. 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{
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 Hinnantbc8d3f92010-05-11 19:42:16 +000097
98} // std
99
100*/
101
102#include <__config>
103#include <__tuple>
104#include <type_traits>
105#include <utility>
106#include <iterator>
107#include <algorithm>
108#include <stdexcept>
109#if defined(_LIBCPP_NO_EXCEPTIONS)
110 #include <cassert>
111#endif
112
113#pragma GCC system_header
114
115_LIBCPP_BEGIN_NAMESPACE_STD
116
Howard Hinnant324bb032010-08-22 00:02:43 +0000117template <class _Tp, size_t _Size>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000118struct _LIBCPP_VISIBLE array
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000119{
120 // types:
121 typedef array __self;
Howard Hinnant324bb032010-08-22 00:02:43 +0000122 typedef _Tp value_type;
123 typedef value_type& reference;
124 typedef const value_type& const_reference;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000125 typedef value_type* iterator;
126 typedef const value_type* const_iterator;
127 typedef value_type* pointer;
128 typedef const value_type* const_pointer;
Howard Hinnant324bb032010-08-22 00:02:43 +0000129 typedef size_t size_type;
130 typedef ptrdiff_t difference_type;
131 typedef std::reverse_iterator<iterator> reverse_iterator;
132 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000133
134 value_type __elems_[_Size > 0 ? _Size : 1];
135
Howard Hinnant324bb032010-08-22 00:02:43 +0000136 // No explicit construct/copy/destroy for aggregate type
Howard Hinnant333f50d2010-09-21 20:16:37 +0000137 _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __u)
138 {_STD::fill_n(__elems_, _Size, __u);}
139 _LIBCPP_INLINE_VISIBILITY void swap(array& __a)
140 {_STD::swap_ranges(__elems_, __elems_ + _Size, __a.__elems_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000141
Howard Hinnant324bb032010-08-22 00:02:43 +0000142 // iterators:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000143 _LIBCPP_INLINE_VISIBILITY iterator begin() {return iterator(__elems_);}
144 _LIBCPP_INLINE_VISIBILITY const_iterator begin() const {return const_iterator(__elems_);}
145 _LIBCPP_INLINE_VISIBILITY iterator end() {return iterator(__elems_ + _Size);}
146 _LIBCPP_INLINE_VISIBILITY const_iterator end() const {return const_iterator(__elems_ + _Size);}
147
148 _LIBCPP_INLINE_VISIBILITY reverse_iterator rbegin() {return reverse_iterator(end());}
149 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rbegin() const {return const_reverse_iterator(end());}
150 _LIBCPP_INLINE_VISIBILITY reverse_iterator rend() {return reverse_iterator(begin());}
151 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rend() const {return const_reverse_iterator(begin());}
152
153 _LIBCPP_INLINE_VISIBILITY const_iterator cbegin() const {return begin();}
154 _LIBCPP_INLINE_VISIBILITY const_iterator cend() const {return end();}
155 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crbegin() const {return rbegin();}
156 _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crend() const {return rend();}
157
Howard Hinnant324bb032010-08-22 00:02:43 +0000158 // capacity:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000159 _LIBCPP_INLINE_VISIBILITY /*constexpr*/ size_type size() const {return _Size;}
160 _LIBCPP_INLINE_VISIBILITY /*constexpr*/ size_type max_size() const {return _Size;}
161 _LIBCPP_INLINE_VISIBILITY bool empty() const {return _Size == 0;}
162
Howard Hinnant324bb032010-08-22 00:02:43 +0000163 // element access:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000164 _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) {return __elems_[__n];}
165 _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const {return __elems_[__n];}
Howard Hinnant324bb032010-08-22 00:02:43 +0000166 reference at(size_type __n);
167 const_reference at(size_type __n) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000168
169 _LIBCPP_INLINE_VISIBILITY reference front() {return __elems_[0];}
170 _LIBCPP_INLINE_VISIBILITY const_reference front() const {return __elems_[0];}
171 _LIBCPP_INLINE_VISIBILITY reference back() {return __elems_[_Size > 0 ? _Size-1 : 0];}
172 _LIBCPP_INLINE_VISIBILITY const_reference back() const {return __elems_[_Size > 0 ? _Size-1 : 0];}
173
174 _LIBCPP_INLINE_VISIBILITY value_type* data() {return __elems_;}
Howard Hinnant324bb032010-08-22 00:02:43 +0000175 _LIBCPP_INLINE_VISIBILITY const value_type* data() const {return __elems_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000176};
177
Howard Hinnant324bb032010-08-22 00:02:43 +0000178template <class _Tp, size_t _Size>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000179typename array<_Tp, _Size>::reference
180array<_Tp, _Size>::at(size_type __n)
181{
182 if (__n >= _Size)
183#ifndef _LIBCPP_NO_EXCEPTIONS
184 throw out_of_range("array::at");
185#else
186 assert(!"array::at out_of_range");
187#endif
188 return __elems_[__n];
189}
190
Howard Hinnant324bb032010-08-22 00:02:43 +0000191template <class _Tp, size_t _Size>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000192typename array<_Tp, _Size>::const_reference
193array<_Tp, _Size>::at(size_type __n) const
194{
195 if (__n >= _Size)
196#ifndef _LIBCPP_NO_EXCEPTIONS
197 throw out_of_range("array::at");
198#else
199 assert(!"array::at out_of_range");
200#endif
201 return __elems_[__n];
202}
203
Howard Hinnant324bb032010-08-22 00:02:43 +0000204template <class _Tp, size_t _Size>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000205_LIBCPP_INLINE_VISIBILITY inline
206bool
207operator==(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
208{
209 return _STD::equal(__x.__elems_, __x.__elems_ + _Size, __y.__elems_);
210}
211
Howard Hinnant324bb032010-08-22 00:02:43 +0000212template <class _Tp, size_t _Size>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000213_LIBCPP_INLINE_VISIBILITY inline
214bool
215operator!=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
216{
217 return !(__x == __y);
218}
219
Howard Hinnant324bb032010-08-22 00:02:43 +0000220template <class _Tp, size_t _Size>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000221_LIBCPP_INLINE_VISIBILITY inline
222bool
223operator<(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
224{
225 return _STD::lexicographical_compare(__x.__elems_, __x.__elems_ + _Size, __y.__elems_, __y.__elems_ + _Size);
226}
227
Howard Hinnant324bb032010-08-22 00:02:43 +0000228template <class _Tp, size_t _Size>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000229_LIBCPP_INLINE_VISIBILITY inline
230bool
231operator>(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
232{
233 return __y < __x;
234}
235
Howard Hinnant324bb032010-08-22 00:02:43 +0000236template <class _Tp, size_t _Size>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000237_LIBCPP_INLINE_VISIBILITY inline
238bool
239operator<=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
240{
241 return !(__y < __x);
242}
243
Howard Hinnant324bb032010-08-22 00:02:43 +0000244template <class _Tp, size_t _Size>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000245_LIBCPP_INLINE_VISIBILITY inline
246bool
247operator>=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
248{
249 return !(__x < __y);
250}
251
Howard Hinnant324bb032010-08-22 00:02:43 +0000252template <class _Tp, size_t _Size>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000253_LIBCPP_INLINE_VISIBILITY inline
254void
255swap(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
256{
257 __x.swap(__y);
258}
259
260template <class _Tp, size_t _Size>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000261class _LIBCPP_VISIBLE tuple_size<array<_Tp, _Size> >
262 : public integral_constant<size_t, _Size> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000263
264template <class _Tp, size_t _Size>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000265class _LIBCPP_VISIBLE tuple_size<const array<_Tp, _Size> >
266 : public integral_constant<size_t, _Size> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000267
268template <size_t _Ip, class _Tp, size_t _Size>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000269class _LIBCPP_VISIBLE tuple_element<_Ip, array<_Tp, _Size> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000270{
271public:
272 typedef _Tp type;
273};
274
275template <size_t _Ip, class _Tp, size_t _Size>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000276class _LIBCPP_VISIBLE tuple_element<_Ip, const array<_Tp, _Size> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000277{
278public:
279 typedef const _Tp type;
280};
281
282template <size_t _Ip, class _Tp, size_t _Size>
283_LIBCPP_INLINE_VISIBILITY inline
284_Tp&
285get(array<_Tp, _Size>& __a)
286{
287 return __a[_Ip];
288}
289
290template <size_t _Ip, class _Tp, size_t _Size>
291_LIBCPP_INLINE_VISIBILITY inline
292const _Tp&
293get(const array<_Tp, _Size>& __a)
294{
295 return __a[_Ip];
296}
297
298_LIBCPP_END_NAMESPACE_STD
299
300#endif // _LIBCPP_ARRAY