2 //===-------------------------- dynarray ----------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_DYNARRAY
12 #define _LIBCPP_DYNARRAY
15 #if _LIBCPP_STD_VER > 11
20 namespace std { namespace experimental {
22 template< typename T >
28 typedef const T& const_reference;
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;
39 // construct/copy/destroy:
40 explicit dynarray(size_type c);
41 dynarray(size_type c, const T& v);
42 dynarray(const dynarray& d);
43 dynarray(initializer_list<T>);
45 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);
53 dynarray& operator=(const dynarray&) = delete;
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;
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;
72 size_type size() const noexcept;
73 size_type max_size() const noexcept;
74 bool empty() const noexcept;
77 reference operator[](size_type n);
78 const_reference operator[](size_type n) const;
81 const_reference front() const;
83 const_reference back() const;
85 const_reference at(size_type n) const;
86 reference at(size_type n);
90 const T* data() const noexcept;
92 // mutating member functions:
93 void fill(const T& v);
96 }} // std::experimental
100 #include <__functional_base>
103 #include <initializer_list>
107 #include <__undef___deallocate>
109 #if defined(_LIBCPP_NO_EXCEPTIONS)
113 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
114 #pragma GCC system_header
117 namespace std { namespace experimental { inline namespace __array_extensions_v1 {
120 struct _LIBCPP_TYPE_VIS_ONLY dynarray
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;
139 value_type * __base_;
140 _LIBCPP_ALWAYS_INLINE dynarray () noexcept : __base_(nullptr), __size_(0) {}
142 static inline _LIBCPP_INLINE_VISIBILITY value_type* __allocate ( size_t count )
144 if ( numeric_limits<size_t>::max() / sizeof (value_type) <= count )
146 #ifndef _LIBCPP_NO_EXCEPTIONS
147 throw bad_array_length();
149 assert(!"dynarray::allocation");
152 return static_cast<value_type *> (_VSTD::__allocate (sizeof(value_type) * count));
155 static inline _LIBCPP_INLINE_VISIBILITY void __deallocate ( value_type* __ptr ) noexcept
157 _VSTD::__deallocate (static_cast<void *> (__ptr));
162 explicit dynarray(size_type __c);
163 dynarray(size_type __c, const value_type& __v);
164 dynarray(const dynarray& __d);
165 dynarray(initializer_list<value_type>);
167 // We're not implementing these right now.
168 // Updated with the resolution of LWG issue #2255
169 // template <typename _Alloc>
170 // dynarray(allocator_arg_t, const _Alloc& __alloc, size_type __c);
171 // template <typename _Alloc>
172 // dynarray(allocator_arg_t, const _Alloc& __alloc, size_type __c, const value_type& __v);
173 // template <typename _Alloc>
174 // dynarray(allocator_arg_t, const _Alloc& __alloc, const dynarray& __d);
175 // template <typename _Alloc>
176 // dynarray(allocator_arg_t, const _Alloc& __alloc, initializer_list<value_type>);
178 dynarray& operator=(const dynarray&) = delete;
182 inline _LIBCPP_INLINE_VISIBILITY iterator begin() noexcept { return iterator(data()); }
183 inline _LIBCPP_INLINE_VISIBILITY const_iterator begin() const noexcept { return const_iterator(data()); }
184 inline _LIBCPP_INLINE_VISIBILITY const_iterator cbegin() const noexcept { return const_iterator(data()); }
185 inline _LIBCPP_INLINE_VISIBILITY iterator end() noexcept { return iterator(data() + __size_); }
186 inline _LIBCPP_INLINE_VISIBILITY const_iterator end() const noexcept { return const_iterator(data() + __size_); }
187 inline _LIBCPP_INLINE_VISIBILITY const_iterator cend() const noexcept { return const_iterator(data() + __size_); }
189 inline _LIBCPP_INLINE_VISIBILITY reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
190 inline _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
191 inline _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); }
192 inline _LIBCPP_INLINE_VISIBILITY reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
193 inline _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
194 inline _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); }
197 inline _LIBCPP_INLINE_VISIBILITY size_type size() const noexcept { return __size_; }
198 inline _LIBCPP_INLINE_VISIBILITY size_type max_size() const noexcept { return __size_; }
199 inline _LIBCPP_INLINE_VISIBILITY bool empty() const noexcept { return __size_ == 0; }
202 inline _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) { return data()[__n]; }
203 inline _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const { return data()[__n]; }
205 inline _LIBCPP_INLINE_VISIBILITY reference front() { return data()[0]; }
206 inline _LIBCPP_INLINE_VISIBILITY const_reference front() const { return data()[0]; }
207 inline _LIBCPP_INLINE_VISIBILITY reference back() { return data()[__size_-1]; }
208 inline _LIBCPP_INLINE_VISIBILITY const_reference back() const { return data()[__size_-1]; }
210 inline _LIBCPP_INLINE_VISIBILITY const_reference at(size_type __n) const;
211 inline _LIBCPP_INLINE_VISIBILITY reference at(size_type __n);
214 inline _LIBCPP_INLINE_VISIBILITY _Tp* data() noexcept { return __base_; }
215 inline _LIBCPP_INLINE_VISIBILITY const _Tp* data() const noexcept { return __base_; }
217 // mutating member functions:
218 inline _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __v) { fill_n(begin(), __size_, __v); }
222 inline _LIBCPP_INLINE_VISIBILITY
223 dynarray<_Tp>::dynarray(size_type __c) : dynarray ()
225 __base_ = __allocate (__c);
226 value_type *__data = data ();
227 for ( __size_ = 0; __size_ < __c; ++__size_, ++__data )
228 ::new (__data) value_type;
232 inline _LIBCPP_INLINE_VISIBILITY
233 dynarray<_Tp>::dynarray(size_type __c, const value_type& __v) : dynarray ()
235 __base_ = __allocate (__c);
236 value_type *__data = data ();
237 for ( __size_ = 0; __size_ < __c; ++__size_, ++__data )
238 ::new (__data) value_type (__v);
242 inline _LIBCPP_INLINE_VISIBILITY
243 dynarray<_Tp>::dynarray(initializer_list<value_type> __il) : dynarray ()
245 size_t sz = __il.size();
246 __base_ = __allocate (sz);
247 value_type *__data = data ();
248 auto src = __il.begin();
249 for ( __size_ = 0; __size_ < sz; ++__size_, ++__data, ++src )
250 ::new (__data) value_type (*src);
254 inline _LIBCPP_INLINE_VISIBILITY
255 dynarray<_Tp>::dynarray(const dynarray& __d) : dynarray ()
257 size_t sz = __d.size();
258 __base_ = __allocate (sz);
259 value_type *__data = data ();
260 auto src = __d.begin();
261 for ( __size_ = 0; __size_ < sz; ++__size_, ++__data, ++src )
262 ::new (__data) value_type (*src);
266 inline _LIBCPP_INLINE_VISIBILITY
267 dynarray<_Tp>::~dynarray()
269 value_type *__data = data () + __size_;
270 for ( size_t i = 0; i < __size_; ++i )
271 (--__data)->value_type::~value_type();
272 __deallocate ( __base_ );
276 inline _LIBCPP_INLINE_VISIBILITY
277 typename dynarray<_Tp>::reference
278 dynarray<_Tp>::at(size_type __n)
282 #ifndef _LIBCPP_NO_EXCEPTIONS
283 throw out_of_range("dynarray::at");
285 assert(!"dynarray::at out_of_range");
292 inline _LIBCPP_INLINE_VISIBILITY
293 typename dynarray<_Tp>::const_reference
294 dynarray<_Tp>::at(size_type __n) const
298 #ifndef _LIBCPP_NO_EXCEPTIONS
299 throw out_of_range("dynarray::at");
301 assert(!"dynarray::at out_of_range");
310 _LIBCPP_BEGIN_NAMESPACE_STD
311 template <class _Tp, class _Alloc>
312 struct _LIBCPP_TYPE_VIS_ONLY uses_allocator<std::experimental::dynarray<_Tp>, _Alloc> : true_type {};
313 _LIBCPP_END_NAMESPACE_STD
315 #endif // if _LIBCPP_STD_VER > 11
316 #endif // _LIBCPP_DYNARRAY