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
17 namespace std { namespace experimental {
19 template< typename T >
25 typedef const T& const_reference;
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;
36 // construct/copy/destroy:
37 explicit dynarray(size_type c);
38 dynarray(size_type c, const T& v);
39 dynarray(const dynarray& d);
40 dynarray(initializer_list<T>);
42 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);
50 dynarray& operator=(const dynarray&) = delete;
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;
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;
69 size_type size() const noexcept;
70 size_type max_size() const noexcept;
71 bool empty() const noexcept;
74 reference operator[](size_type n);
75 const_reference operator[](size_type n) const;
78 const_reference front() const;
80 const_reference back() const;
82 const_reference at(size_type n) const;
83 reference at(size_type n);
87 const T* data() const noexcept;
89 // mutating member functions:
90 void fill(const T& v);
93 }} // std::experimental
97 #if _LIBCPP_STD_VER > 11
99 #include <__functional_base>
102 #include <initializer_list>
106 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
107 #pragma GCC system_header
111 #include <__undef_macros>
113 namespace std { namespace experimental { inline namespace __array_extensions_v1 {
116 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_DYNARRAY dynarray
120 typedef dynarray __self;
121 typedef _Tp value_type;
122 typedef value_type& reference;
123 typedef const value_type& const_reference;
124 typedef value_type* iterator;
125 typedef const value_type* const_iterator;
126 typedef value_type* pointer;
127 typedef const value_type* const_pointer;
128 typedef size_t size_type;
129 typedef ptrdiff_t difference_type;
130 typedef std::reverse_iterator<iterator> reverse_iterator;
131 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
135 value_type * __base_;
136 _LIBCPP_INLINE_VISIBILITY dynarray () noexcept : __size_(0), __base_(nullptr) {}
138 static inline _LIBCPP_INLINE_VISIBILITY
139 value_type* __allocate(size_t __count) {
140 if (numeric_limits<size_t>::max() / sizeof (value_type) <= __count)
141 __throw_bad_array_length();
143 return static_cast<value_type *>(
144 _VSTD::__libcpp_allocate(sizeof(value_type) * __count, __alignof(value_type)));
147 static inline _LIBCPP_INLINE_VISIBILITY
148 void __deallocate_value(value_type* __ptr ) noexcept {
149 _VSTD::__libcpp_deallocate(static_cast<void *>(__ptr), __alignof(value_type));
154 _LIBCPP_INLINE_VISIBILITY
155 explicit dynarray(size_type __c);
156 _LIBCPP_INLINE_VISIBILITY
157 dynarray(size_type __c, const value_type& __v);
158 _LIBCPP_INLINE_VISIBILITY
159 dynarray(const dynarray& __d);
160 _LIBCPP_INLINE_VISIBILITY
161 dynarray(initializer_list<value_type>);
163 // We're not implementing these right now.
164 // Updated with the resolution of LWG issue #2255
165 // template <typename _Alloc>
166 // dynarray(allocator_arg_t, const _Alloc& __alloc, size_type __c);
167 // template <typename _Alloc>
168 // dynarray(allocator_arg_t, const _Alloc& __alloc, size_type __c, const value_type& __v);
169 // template <typename _Alloc>
170 // dynarray(allocator_arg_t, const _Alloc& __alloc, const dynarray& __d);
171 // template <typename _Alloc>
172 // dynarray(allocator_arg_t, const _Alloc& __alloc, initializer_list<value_type>);
174 dynarray& operator=(const dynarray&) = delete;
175 _LIBCPP_INLINE_VISIBILITY
179 inline _LIBCPP_INLINE_VISIBILITY iterator begin() noexcept { return iterator(data()); }
180 inline _LIBCPP_INLINE_VISIBILITY const_iterator begin() const noexcept { return const_iterator(data()); }
181 inline _LIBCPP_INLINE_VISIBILITY const_iterator cbegin() const noexcept { return const_iterator(data()); }
182 inline _LIBCPP_INLINE_VISIBILITY iterator end() noexcept { return iterator(data() + __size_); }
183 inline _LIBCPP_INLINE_VISIBILITY const_iterator end() const noexcept { return const_iterator(data() + __size_); }
184 inline _LIBCPP_INLINE_VISIBILITY const_iterator cend() const noexcept { return const_iterator(data() + __size_); }
186 inline _LIBCPP_INLINE_VISIBILITY reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
187 inline _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
188 inline _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); }
189 inline _LIBCPP_INLINE_VISIBILITY reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
190 inline _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
191 inline _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); }
194 inline _LIBCPP_INLINE_VISIBILITY size_type size() const noexcept { return __size_; }
195 inline _LIBCPP_INLINE_VISIBILITY size_type max_size() const noexcept { return __size_; }
196 inline _LIBCPP_INLINE_VISIBILITY bool empty() const noexcept { return __size_ == 0; }
199 inline _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) { return data()[__n]; }
200 inline _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const { return data()[__n]; }
202 inline _LIBCPP_INLINE_VISIBILITY reference front() { return data()[0]; }
203 inline _LIBCPP_INLINE_VISIBILITY const_reference front() const { return data()[0]; }
204 inline _LIBCPP_INLINE_VISIBILITY reference back() { return data()[__size_-1]; }
205 inline _LIBCPP_INLINE_VISIBILITY const_reference back() const { return data()[__size_-1]; }
207 inline _LIBCPP_INLINE_VISIBILITY const_reference at(size_type __n) const;
208 inline _LIBCPP_INLINE_VISIBILITY reference at(size_type __n);
211 inline _LIBCPP_INLINE_VISIBILITY _Tp* data() noexcept { return __base_; }
212 inline _LIBCPP_INLINE_VISIBILITY const _Tp* data() const noexcept { return __base_; }
214 // mutating member functions:
215 inline _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __v) { fill_n(begin(), __size_, __v); }
220 dynarray<_Tp>::dynarray(size_type __c) : dynarray ()
222 __base_ = __allocate (__c);
223 value_type *__data = data ();
224 for ( __size_ = 0; __size_ < __c; ++__size_, ++__data )
225 ::new (__data) value_type;
230 dynarray<_Tp>::dynarray(size_type __c, const value_type& __v) : dynarray ()
232 __base_ = __allocate (__c);
233 value_type *__data = data ();
234 for ( __size_ = 0; __size_ < __c; ++__size_, ++__data )
235 ::new (__data) value_type (__v);
240 dynarray<_Tp>::dynarray(initializer_list<value_type> __il) : dynarray ()
242 size_t sz = __il.size();
243 __base_ = __allocate (sz);
244 value_type *__data = data ();
245 auto src = __il.begin();
246 for ( __size_ = 0; __size_ < sz; ++__size_, ++__data, ++src )
247 ::new (__data) value_type (*src);
252 dynarray<_Tp>::dynarray(const dynarray& __d) : dynarray ()
254 size_t sz = __d.size();
255 __base_ = __allocate (sz);
256 value_type *__data = data ();
257 auto src = __d.begin();
258 for ( __size_ = 0; __size_ < sz; ++__size_, ++__data, ++src )
259 ::new (__data) value_type (*src);
264 dynarray<_Tp>::~dynarray()
266 value_type *__data = data () + __size_;
267 for ( size_t i = 0; i < __size_; ++i )
268 (--__data)->value_type::~value_type();
269 __deallocate_value( __base_ );
273 inline _LIBCPP_INLINE_VISIBILITY
274 typename dynarray<_Tp>::reference
275 dynarray<_Tp>::at(size_type __n)
278 __throw_out_of_range("dynarray::at");
284 inline _LIBCPP_INLINE_VISIBILITY
285 typename dynarray<_Tp>::const_reference
286 dynarray<_Tp>::at(size_type __n) const
289 __throw_out_of_range("dynarray::at");
297 _LIBCPP_BEGIN_NAMESPACE_STD
298 template <class _Tp, class _Alloc>
299 struct _LIBCPP_TEMPLATE_VIS uses_allocator<std::experimental::dynarray<_Tp>, _Alloc> : true_type {};
300 _LIBCPP_END_NAMESPACE_STD
304 #endif // if _LIBCPP_STD_VER > 11
305 #endif // _LIBCPP_DYNARRAY