blob: f96a0e5fed389eae3323024c52f303378e2a8998 [file] [log] [blame]
Marshall Clowb04f54b2013-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
Marshall Clowb04f54b2013-09-13 15:22:55 +000014/*
15 dynarray synopsis
16
Marshall Clow648ad102013-11-13 22:44:48 +000017namespace std { namespace experimental {
Marshall Clowb04f54b2013-09-13 15:22:55 +000018
19template< typename T >
20class dynarray
21{
22 // types:
23 typedef T value_type;
24 typedef T& reference;
25 typedef const T& const_reference;
26 typedef T* pointer;
27 typedef const T* const_pointer;
28 typedef implementation-defined iterator;
29 typedef implementation-defined const_iterator;
30 typedef reverse_iterator<iterator> reverse_iterator;
31 typedef reverse_iterator<const_iterator> const_reverse_iterator;
32 typedef size_t size_type;
33 typedef ptrdiff_t difference_type;
34
35public:
36 // construct/copy/destroy:
37 explicit dynarray(size_type c);
Marshall Clowb04f54b2013-09-13 15:22:55 +000038 dynarray(size_type c, const T& v);
Marshall Clowb04f54b2013-09-13 15:22:55 +000039 dynarray(const dynarray& d);
Marshall Clowb04f54b2013-09-13 15:22:55 +000040 dynarray(initializer_list<T>);
Marshall Clowb04f54b2013-09-13 15:22:55 +000041
Marshall Clow0002f962014-07-23 16:58:25 +000042 template <class Alloc>
43 dynarray(allocator_arg_t, const Alloc& a, size_type c, const Alloc& alloc);
44 template <class Alloc>
45 dynarray(allocator_arg_t, const Alloc& a, size_type c, const T& v, const Alloc& alloc);
46 template <class Alloc>
47 dynarray(allocator_arg_t, const Alloc& a, const dynarray& d, const Alloc& alloc);
48 template <class Alloc>
49 dynarray(allocator_arg_t, const Alloc& a, initializer_list<T>, const Alloc& alloc);
Marshall Clowb04f54b2013-09-13 15:22:55 +000050 dynarray& operator=(const dynarray&) = delete;
51 ~dynarray();
52
53 // iterators:
54 iterator begin() noexcept;
55 const_iterator begin() const noexcept;
56 const_iterator cbegin() const noexcept;
57 iterator end() noexcept;
58 const_iterator end() const noexcept;
59 const_iterator cend() const noexcept;
60
61 reverse_iterator rbegin() noexcept;
62 const_reverse_iterator rbegin() const noexcept;
63 const_reverse_iterator crbegin() const noexcept;
64 reverse_iterator rend() noexcept;
65 const_reverse_iterator rend() const noexcept;
66 const_reverse_iterator crend() const noexcept;
67
68 // capacity:
69 size_type size() const noexcept;
70 size_type max_size() const noexcept;
71 bool empty() const noexcept;
72
73 // element access:
74 reference operator[](size_type n);
75 const_reference operator[](size_type n) const;
76
77 reference front();
78 const_reference front() const;
79 reference back();
80 const_reference back() const;
81
82 const_reference at(size_type n) const;
83 reference at(size_type n);
84
85 // data access:
86 T* data() noexcept;
87 const T* data() const noexcept;
88
89 // mutating member functions:
90 void fill(const T& v);
91};
92
Marshall Clow648ad102013-11-13 22:44:48 +000093}} // std::experimental
Marshall Clowb04f54b2013-09-13 15:22:55 +000094
95*/
Eric Fiselier8122fda2017-01-07 03:01:24 +000096#include <__config>
97#if _LIBCPP_STD_VER > 11
Marshall Clowb04f54b2013-09-13 15:22:55 +000098
99#include <__functional_base>
100#include <iterator>
101#include <stdexcept>
102#include <initializer_list>
103#include <new>
104#include <algorithm>
105
Marshall Clowb04f54b2013-09-13 15:22:55 +0000106#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
107#pragma GCC system_header
108#endif
109
Marshall Clow648ad102013-11-13 22:44:48 +0000110namespace std { namespace experimental { inline namespace __array_extensions_v1 {
Marshall Clowb04f54b2013-09-13 15:22:55 +0000111
112template <class _Tp>
Mehdi Amini228053d2017-05-04 17:08:54 +0000113struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_DYNARRAY dynarray
Marshall Clowb04f54b2013-09-13 15:22:55 +0000114{
115public:
116 // types:
117 typedef dynarray __self;
118 typedef _Tp value_type;
119 typedef value_type& reference;
120 typedef const value_type& const_reference;
121 typedef value_type* iterator;
122 typedef const value_type* const_iterator;
123 typedef value_type* pointer;
124 typedef const value_type* const_pointer;
125 typedef size_t size_type;
126 typedef ptrdiff_t difference_type;
127 typedef std::reverse_iterator<iterator> reverse_iterator;
128 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
129
130private:
131 size_t __size_;
132 value_type * __base_;
Eric Fiselierc16974d2015-10-01 07:29:38 +0000133 _LIBCPP_ALWAYS_INLINE dynarray () noexcept : __size_(0), __base_(nullptr) {}
Marshall Clowb04f54b2013-09-13 15:22:55 +0000134
135 static inline _LIBCPP_INLINE_VISIBILITY value_type* __allocate ( size_t count )
136 {
137 if ( numeric_limits<size_t>::max() / sizeof (value_type) <= count )
Marshall Clow8fea1612016-08-25 15:09:01 +0000138 __throw_bad_array_length();
139
Richard Smith1f1c1472014-06-04 19:54:15 +0000140 return static_cast<value_type *> (_VSTD::__allocate (sizeof(value_type) * count));
Marshall Clowb04f54b2013-09-13 15:22:55 +0000141 }
142
Eric Fiselier8122fda2017-01-07 03:01:24 +0000143 static inline _LIBCPP_INLINE_VISIBILITY void __deallocate_value( value_type* __ptr ) noexcept
Marshall Clowb04f54b2013-09-13 15:22:55 +0000144 {
Eric Fiselier8122fda2017-01-07 03:01:24 +0000145 _VSTD::__libcpp_deallocate (static_cast<void *> (__ptr));
Marshall Clowb04f54b2013-09-13 15:22:55 +0000146 }
147
148public:
149
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000150 _LIBCPP_INLINE_VISIBILITY
Marshall Clowb04f54b2013-09-13 15:22:55 +0000151 explicit dynarray(size_type __c);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000152 _LIBCPP_INLINE_VISIBILITY
Marshall Clowb04f54b2013-09-13 15:22:55 +0000153 dynarray(size_type __c, const value_type& __v);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000154 _LIBCPP_INLINE_VISIBILITY
Marshall Clowb04f54b2013-09-13 15:22:55 +0000155 dynarray(const dynarray& __d);
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000156 _LIBCPP_INLINE_VISIBILITY
Marshall Clowb04f54b2013-09-13 15:22:55 +0000157 dynarray(initializer_list<value_type>);
158
159// We're not implementing these right now.
Marshall Clow0002f962014-07-23 16:58:25 +0000160// Updated with the resolution of LWG issue #2255
Marshall Clowb04f54b2013-09-13 15:22:55 +0000161// template <typename _Alloc>
Marshall Clow0002f962014-07-23 16:58:25 +0000162// dynarray(allocator_arg_t, const _Alloc& __alloc, size_type __c);
Marshall Clowb04f54b2013-09-13 15:22:55 +0000163// template <typename _Alloc>
Marshall Clow0002f962014-07-23 16:58:25 +0000164// dynarray(allocator_arg_t, const _Alloc& __alloc, size_type __c, const value_type& __v);
Marshall Clowb04f54b2013-09-13 15:22:55 +0000165// template <typename _Alloc>
Marshall Clow0002f962014-07-23 16:58:25 +0000166// dynarray(allocator_arg_t, const _Alloc& __alloc, const dynarray& __d);
Marshall Clowb04f54b2013-09-13 15:22:55 +0000167// template <typename _Alloc>
Marshall Clow0002f962014-07-23 16:58:25 +0000168// dynarray(allocator_arg_t, const _Alloc& __alloc, initializer_list<value_type>);
Marshall Clowb04f54b2013-09-13 15:22:55 +0000169
170 dynarray& operator=(const dynarray&) = delete;
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000171 _LIBCPP_INLINE_VISIBILITY
Marshall Clowb04f54b2013-09-13 15:22:55 +0000172 ~dynarray();
173
174 // iterators:
175 inline _LIBCPP_INLINE_VISIBILITY iterator begin() noexcept { return iterator(data()); }
176 inline _LIBCPP_INLINE_VISIBILITY const_iterator begin() const noexcept { return const_iterator(data()); }
177 inline _LIBCPP_INLINE_VISIBILITY const_iterator cbegin() const noexcept { return const_iterator(data()); }
178 inline _LIBCPP_INLINE_VISIBILITY iterator end() noexcept { return iterator(data() + __size_); }
179 inline _LIBCPP_INLINE_VISIBILITY const_iterator end() const noexcept { return const_iterator(data() + __size_); }
180 inline _LIBCPP_INLINE_VISIBILITY const_iterator cend() const noexcept { return const_iterator(data() + __size_); }
181
182 inline _LIBCPP_INLINE_VISIBILITY reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
183 inline _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
184 inline _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); }
185 inline _LIBCPP_INLINE_VISIBILITY reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
186 inline _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
187 inline _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); }
188
189 // capacity:
190 inline _LIBCPP_INLINE_VISIBILITY size_type size() const noexcept { return __size_; }
191 inline _LIBCPP_INLINE_VISIBILITY size_type max_size() const noexcept { return __size_; }
192 inline _LIBCPP_INLINE_VISIBILITY bool empty() const noexcept { return __size_ == 0; }
193
194 // element access:
195 inline _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) { return data()[__n]; }
196 inline _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const { return data()[__n]; }
197
198 inline _LIBCPP_INLINE_VISIBILITY reference front() { return data()[0]; }
199 inline _LIBCPP_INLINE_VISIBILITY const_reference front() const { return data()[0]; }
200 inline _LIBCPP_INLINE_VISIBILITY reference back() { return data()[__size_-1]; }
201 inline _LIBCPP_INLINE_VISIBILITY const_reference back() const { return data()[__size_-1]; }
202
203 inline _LIBCPP_INLINE_VISIBILITY const_reference at(size_type __n) const;
204 inline _LIBCPP_INLINE_VISIBILITY reference at(size_type __n);
205
206 // data access:
207 inline _LIBCPP_INLINE_VISIBILITY _Tp* data() noexcept { return __base_; }
208 inline _LIBCPP_INLINE_VISIBILITY const _Tp* data() const noexcept { return __base_; }
209
210 // mutating member functions:
211 inline _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __v) { fill_n(begin(), __size_, __v); }
212};
213
214template <class _Tp>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000215inline
Marshall Clowb04f54b2013-09-13 15:22:55 +0000216dynarray<_Tp>::dynarray(size_type __c) : dynarray ()
217{
218 __base_ = __allocate (__c);
219 value_type *__data = data ();
220 for ( __size_ = 0; __size_ < __c; ++__size_, ++__data )
221 ::new (__data) value_type;
222}
223
224template <class _Tp>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000225inline
Marshall Clowb04f54b2013-09-13 15:22:55 +0000226dynarray<_Tp>::dynarray(size_type __c, const value_type& __v) : dynarray ()
227{
228 __base_ = __allocate (__c);
229 value_type *__data = data ();
230 for ( __size_ = 0; __size_ < __c; ++__size_, ++__data )
231 ::new (__data) value_type (__v);
232}
233
234template <class _Tp>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000235inline
Marshall Clowb04f54b2013-09-13 15:22:55 +0000236dynarray<_Tp>::dynarray(initializer_list<value_type> __il) : dynarray ()
237{
238 size_t sz = __il.size();
239 __base_ = __allocate (sz);
240 value_type *__data = data ();
241 auto src = __il.begin();
242 for ( __size_ = 0; __size_ < sz; ++__size_, ++__data, ++src )
243 ::new (__data) value_type (*src);
244}
245
246template <class _Tp>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000247inline
Marshall Clowb04f54b2013-09-13 15:22:55 +0000248dynarray<_Tp>::dynarray(const dynarray& __d) : dynarray ()
249{
250 size_t sz = __d.size();
251 __base_ = __allocate (sz);
252 value_type *__data = data ();
253 auto src = __d.begin();
254 for ( __size_ = 0; __size_ < sz; ++__size_, ++__data, ++src )
255 ::new (__data) value_type (*src);
256}
257
258template <class _Tp>
Evgeniy Stepanov3b68ae52016-04-22 01:04:55 +0000259inline
Marshall Clowb04f54b2013-09-13 15:22:55 +0000260dynarray<_Tp>::~dynarray()
261{
262 value_type *__data = data () + __size_;
263 for ( size_t i = 0; i < __size_; ++i )
264 (--__data)->value_type::~value_type();
Eric Fiselier8122fda2017-01-07 03:01:24 +0000265 __deallocate_value( __base_ );
Marshall Clowb04f54b2013-09-13 15:22:55 +0000266}
267
268template <class _Tp>
269inline _LIBCPP_INLINE_VISIBILITY
270typename dynarray<_Tp>::reference
271dynarray<_Tp>::at(size_type __n)
272{
273 if (__n >= __size_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000274 __throw_out_of_range("dynarray::at");
275
Marshall Clowb04f54b2013-09-13 15:22:55 +0000276 return data()[__n];
277}
278
279template <class _Tp>
280inline _LIBCPP_INLINE_VISIBILITY
281typename dynarray<_Tp>::const_reference
282dynarray<_Tp>::at(size_type __n) const
283{
284 if (__n >= __size_)
Marshall Clow8fea1612016-08-25 15:09:01 +0000285 __throw_out_of_range("dynarray::at");
286
Marshall Clowb04f54b2013-09-13 15:22:55 +0000287 return data()[__n];
288}
289
Marshall Clow648ad102013-11-13 22:44:48 +0000290}}}
Marshall Clowb04f54b2013-09-13 15:22:55 +0000291
Marshall Clow648ad102013-11-13 22:44:48 +0000292
293_LIBCPP_BEGIN_NAMESPACE_STD
294template <class _Tp, class _Alloc>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000295struct _LIBCPP_TEMPLATE_VIS uses_allocator<std::experimental::dynarray<_Tp>, _Alloc> : true_type {};
Marshall Clowb04f54b2013-09-13 15:22:55 +0000296_LIBCPP_END_NAMESPACE_STD
297
298#endif // if _LIBCPP_STD_VER > 11
299#endif // _LIBCPP_DYNARRAY