blob: 4a06908e11b16004578f1015948b26fcbc0964a0 [file] [log] [blame]
Marshall Clow27a1c252013-09-13 15:22:55 +00001// -*- C++ -*-
2//===-------------------------- dynarray ----------------------------------===//
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_DYNARRAY
12#define _LIBCPP_DYNARRAY
13
14#include <__config>
15#if _LIBCPP_STD_VER > 11
16
17/*
18 dynarray synopsis
19
Marshall Clow6b7c2ae2013-11-13 22:44:48 +000020namespace std { namespace experimental {
Marshall Clow27a1c252013-09-13 15:22:55 +000021
22template< typename T >
23class dynarray
24{
25 // types:
26 typedef T value_type;
27 typedef T& reference;
28 typedef const T& const_reference;
29 typedef T* pointer;
30 typedef const T* const_pointer;
31 typedef implementation-defined iterator;
32 typedef implementation-defined const_iterator;
33 typedef reverse_iterator<iterator> reverse_iterator;
34 typedef reverse_iterator<const_iterator> const_reverse_iterator;
35 typedef size_t size_type;
36 typedef ptrdiff_t difference_type;
37
38public:
39 // construct/copy/destroy:
40 explicit dynarray(size_type c);
Marshall Clow27a1c252013-09-13 15:22:55 +000041 dynarray(size_type c, const T& v);
Marshall Clow27a1c252013-09-13 15:22:55 +000042 dynarray(const dynarray& d);
Marshall Clow27a1c252013-09-13 15:22:55 +000043 dynarray(initializer_list<T>);
Marshall Clow27a1c252013-09-13 15:22:55 +000044
Marshall Clowde2404b2014-07-23 16:58:25 +000045 template <class Alloc>
46 dynarray(allocator_arg_t, const Alloc& a, size_type c, const Alloc& alloc);
47 template <class Alloc>
48 dynarray(allocator_arg_t, const Alloc& a, size_type c, const T& v, const Alloc& alloc);
49 template <class Alloc>
50 dynarray(allocator_arg_t, const Alloc& a, const dynarray& d, const Alloc& alloc);
51 template <class Alloc>
52 dynarray(allocator_arg_t, const Alloc& a, initializer_list<T>, const Alloc& alloc);
Marshall Clow27a1c252013-09-13 15:22:55 +000053 dynarray& operator=(const dynarray&) = delete;
54 ~dynarray();
55
56 // iterators:
57 iterator begin() noexcept;
58 const_iterator begin() const noexcept;
59 const_iterator cbegin() const noexcept;
60 iterator end() noexcept;
61 const_iterator end() const noexcept;
62 const_iterator cend() const noexcept;
63
64 reverse_iterator rbegin() noexcept;
65 const_reverse_iterator rbegin() const noexcept;
66 const_reverse_iterator crbegin() const noexcept;
67 reverse_iterator rend() noexcept;
68 const_reverse_iterator rend() const noexcept;
69 const_reverse_iterator crend() const noexcept;
70
71 // capacity:
72 size_type size() const noexcept;
73 size_type max_size() const noexcept;
74 bool empty() const noexcept;
75
76 // element access:
77 reference operator[](size_type n);
78 const_reference operator[](size_type n) const;
79
80 reference front();
81 const_reference front() const;
82 reference back();
83 const_reference back() const;
84
85 const_reference at(size_type n) const;
86 reference at(size_type n);
87
88 // data access:
89 T* data() noexcept;
90 const T* data() const noexcept;
91
92 // mutating member functions:
93 void fill(const T& v);
94};
95
Marshall Clow6b7c2ae2013-11-13 22:44:48 +000096}} // std::experimental
Marshall Clow27a1c252013-09-13 15:22:55 +000097
98*/
99
100#include <__functional_base>
101#include <iterator>
102#include <stdexcept>
103#include <initializer_list>
104#include <new>
105#include <algorithm>
106
Saleem Abdulrasoolf1b30c42015-02-13 22:15:32 +0000107#include <__undef___deallocate>
108
Marshall Clow27a1c252013-09-13 15:22:55 +0000109#if defined(_LIBCPP_NO_EXCEPTIONS)
110 #include <cassert>
111#endif
112
113#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
114#pragma GCC system_header
115#endif
116
Marshall Clow6b7c2ae2013-11-13 22:44:48 +0000117namespace std { namespace experimental { inline namespace __array_extensions_v1 {
Marshall Clow27a1c252013-09-13 15:22:55 +0000118
119template <class _Tp>
120struct _LIBCPP_TYPE_VIS_ONLY dynarray
121{
122public:
123 // types:
124 typedef dynarray __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
137private:
138 size_t __size_;
139 value_type * __base_;
Eric Fiselier5289b0d2015-10-01 07:29:38 +0000140 _LIBCPP_ALWAYS_INLINE dynarray () noexcept : __size_(0), __base_(nullptr) {}
Marshall Clow27a1c252013-09-13 15:22:55 +0000141
142 static inline _LIBCPP_INLINE_VISIBILITY value_type* __allocate ( size_t count )
143 {
144 if ( numeric_limits<size_t>::max() / sizeof (value_type) <= count )
145 {
146#ifndef _LIBCPP_NO_EXCEPTIONS
147 throw bad_array_length();
148#else
149 assert(!"dynarray::allocation");
150#endif
151 }
Richard Smith73c1fce2014-06-04 19:54:15 +0000152 return static_cast<value_type *> (_VSTD::__allocate (sizeof(value_type) * count));
Marshall Clow27a1c252013-09-13 15:22:55 +0000153 }
154
155 static inline _LIBCPP_INLINE_VISIBILITY void __deallocate ( value_type* __ptr ) noexcept
156 {
Richard Smith73c1fce2014-06-04 19:54:15 +0000157 _VSTD::__deallocate (static_cast<void *> (__ptr));
Marshall Clow27a1c252013-09-13 15:22:55 +0000158 }
159
160public:
161
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000162 _LIBCPP_INLINE_VISIBILITY
Marshall Clow27a1c252013-09-13 15:22:55 +0000163 explicit dynarray(size_type __c);
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000164 _LIBCPP_INLINE_VISIBILITY
Marshall Clow27a1c252013-09-13 15:22:55 +0000165 dynarray(size_type __c, const value_type& __v);
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000166 _LIBCPP_INLINE_VISIBILITY
Marshall Clow27a1c252013-09-13 15:22:55 +0000167 dynarray(const dynarray& __d);
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000168 _LIBCPP_INLINE_VISIBILITY
Marshall Clow27a1c252013-09-13 15:22:55 +0000169 dynarray(initializer_list<value_type>);
170
171// We're not implementing these right now.
Marshall Clowde2404b2014-07-23 16:58:25 +0000172// Updated with the resolution of LWG issue #2255
Marshall Clow27a1c252013-09-13 15:22:55 +0000173// template <typename _Alloc>
Marshall Clowde2404b2014-07-23 16:58:25 +0000174// dynarray(allocator_arg_t, const _Alloc& __alloc, size_type __c);
Marshall Clow27a1c252013-09-13 15:22:55 +0000175// template <typename _Alloc>
Marshall Clowde2404b2014-07-23 16:58:25 +0000176// dynarray(allocator_arg_t, const _Alloc& __alloc, size_type __c, const value_type& __v);
Marshall Clow27a1c252013-09-13 15:22:55 +0000177// template <typename _Alloc>
Marshall Clowde2404b2014-07-23 16:58:25 +0000178// dynarray(allocator_arg_t, const _Alloc& __alloc, const dynarray& __d);
Marshall Clow27a1c252013-09-13 15:22:55 +0000179// template <typename _Alloc>
Marshall Clowde2404b2014-07-23 16:58:25 +0000180// dynarray(allocator_arg_t, const _Alloc& __alloc, initializer_list<value_type>);
Marshall Clow27a1c252013-09-13 15:22:55 +0000181
182 dynarray& operator=(const dynarray&) = delete;
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000183 _LIBCPP_INLINE_VISIBILITY
Marshall Clow27a1c252013-09-13 15:22:55 +0000184 ~dynarray();
185
186 // iterators:
187 inline _LIBCPP_INLINE_VISIBILITY iterator begin() noexcept { return iterator(data()); }
188 inline _LIBCPP_INLINE_VISIBILITY const_iterator begin() const noexcept { return const_iterator(data()); }
189 inline _LIBCPP_INLINE_VISIBILITY const_iterator cbegin() const noexcept { return const_iterator(data()); }
190 inline _LIBCPP_INLINE_VISIBILITY iterator end() noexcept { return iterator(data() + __size_); }
191 inline _LIBCPP_INLINE_VISIBILITY const_iterator end() const noexcept { return const_iterator(data() + __size_); }
192 inline _LIBCPP_INLINE_VISIBILITY const_iterator cend() const noexcept { return const_iterator(data() + __size_); }
193
194 inline _LIBCPP_INLINE_VISIBILITY reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
195 inline _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
196 inline _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); }
197 inline _LIBCPP_INLINE_VISIBILITY reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
198 inline _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
199 inline _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); }
200
201 // capacity:
202 inline _LIBCPP_INLINE_VISIBILITY size_type size() const noexcept { return __size_; }
203 inline _LIBCPP_INLINE_VISIBILITY size_type max_size() const noexcept { return __size_; }
204 inline _LIBCPP_INLINE_VISIBILITY bool empty() const noexcept { return __size_ == 0; }
205
206 // element access:
207 inline _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) { return data()[__n]; }
208 inline _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const { return data()[__n]; }
209
210 inline _LIBCPP_INLINE_VISIBILITY reference front() { return data()[0]; }
211 inline _LIBCPP_INLINE_VISIBILITY const_reference front() const { return data()[0]; }
212 inline _LIBCPP_INLINE_VISIBILITY reference back() { return data()[__size_-1]; }
213 inline _LIBCPP_INLINE_VISIBILITY const_reference back() const { return data()[__size_-1]; }
214
215 inline _LIBCPP_INLINE_VISIBILITY const_reference at(size_type __n) const;
216 inline _LIBCPP_INLINE_VISIBILITY reference at(size_type __n);
217
218 // data access:
219 inline _LIBCPP_INLINE_VISIBILITY _Tp* data() noexcept { return __base_; }
220 inline _LIBCPP_INLINE_VISIBILITY const _Tp* data() const noexcept { return __base_; }
221
222 // mutating member functions:
223 inline _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __v) { fill_n(begin(), __size_, __v); }
224};
225
226template <class _Tp>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000227inline
Marshall Clow27a1c252013-09-13 15:22:55 +0000228dynarray<_Tp>::dynarray(size_type __c) : dynarray ()
229{
230 __base_ = __allocate (__c);
231 value_type *__data = data ();
232 for ( __size_ = 0; __size_ < __c; ++__size_, ++__data )
233 ::new (__data) value_type;
234}
235
236template <class _Tp>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000237inline
Marshall Clow27a1c252013-09-13 15:22:55 +0000238dynarray<_Tp>::dynarray(size_type __c, const value_type& __v) : dynarray ()
239{
240 __base_ = __allocate (__c);
241 value_type *__data = data ();
242 for ( __size_ = 0; __size_ < __c; ++__size_, ++__data )
243 ::new (__data) value_type (__v);
244}
245
246template <class _Tp>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000247inline
Marshall Clow27a1c252013-09-13 15:22:55 +0000248dynarray<_Tp>::dynarray(initializer_list<value_type> __il) : dynarray ()
249{
250 size_t sz = __il.size();
251 __base_ = __allocate (sz);
252 value_type *__data = data ();
253 auto src = __il.begin();
254 for ( __size_ = 0; __size_ < sz; ++__size_, ++__data, ++src )
255 ::new (__data) value_type (*src);
256}
257
258template <class _Tp>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000259inline
Marshall Clow27a1c252013-09-13 15:22:55 +0000260dynarray<_Tp>::dynarray(const dynarray& __d) : dynarray ()
261{
262 size_t sz = __d.size();
263 __base_ = __allocate (sz);
264 value_type *__data = data ();
265 auto src = __d.begin();
266 for ( __size_ = 0; __size_ < sz; ++__size_, ++__data, ++src )
267 ::new (__data) value_type (*src);
268}
269
270template <class _Tp>
Evgeniy Stepanov9341a8a2016-04-22 01:04:55 +0000271inline
Marshall Clow27a1c252013-09-13 15:22:55 +0000272dynarray<_Tp>::~dynarray()
273{
274 value_type *__data = data () + __size_;
275 for ( size_t i = 0; i < __size_; ++i )
276 (--__data)->value_type::~value_type();
277 __deallocate ( __base_ );
278}
279
280template <class _Tp>
281inline _LIBCPP_INLINE_VISIBILITY
282typename dynarray<_Tp>::reference
283dynarray<_Tp>::at(size_type __n)
284{
285 if (__n >= __size_)
286 {
287#ifndef _LIBCPP_NO_EXCEPTIONS
288 throw out_of_range("dynarray::at");
289#else
290 assert(!"dynarray::at out_of_range");
291#endif
292 }
293 return data()[__n];
294}
295
296template <class _Tp>
297inline _LIBCPP_INLINE_VISIBILITY
298typename dynarray<_Tp>::const_reference
299dynarray<_Tp>::at(size_type __n) const
300{
301 if (__n >= __size_)
302 {
303#ifndef _LIBCPP_NO_EXCEPTIONS
304 throw out_of_range("dynarray::at");
305#else
306 assert(!"dynarray::at out_of_range");
307#endif
308 }
309 return data()[__n];
310}
311
Marshall Clow6b7c2ae2013-11-13 22:44:48 +0000312}}}
Marshall Clow27a1c252013-09-13 15:22:55 +0000313
Marshall Clow6b7c2ae2013-11-13 22:44:48 +0000314
315_LIBCPP_BEGIN_NAMESPACE_STD
316template <class _Tp, class _Alloc>
317struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<std::experimental::dynarray<_Tp>, _Alloc> : true_type {};
Marshall Clow27a1c252013-09-13 15:22:55 +0000318_LIBCPP_END_NAMESPACE_STD
319
320#endif // if _LIBCPP_STD_VER > 11
321#endif // _LIBCPP_DYNARRAY