Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 1 | // -*- 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 Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 14 | /* |
| 15 | dynarray synopsis |
| 16 | |
Marshall Clow | 648ad10 | 2013-11-13 22:44:48 +0000 | [diff] [blame] | 17 | namespace std { namespace experimental { |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 18 | |
| 19 | template< typename T > |
| 20 | class 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 | |
| 35 | public: |
| 36 | // construct/copy/destroy: |
| 37 | explicit dynarray(size_type c); |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 38 | dynarray(size_type c, const T& v); |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 39 | dynarray(const dynarray& d); |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 40 | dynarray(initializer_list<T>); |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 41 | |
Marshall Clow | 0002f96 | 2014-07-23 16:58:25 +0000 | [diff] [blame] | 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); |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 50 | 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 Clow | 648ad10 | 2013-11-13 22:44:48 +0000 | [diff] [blame] | 93 | }} // std::experimental |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 94 | |
| 95 | */ |
Eric Fiselier | 8122fda | 2017-01-07 03:01:24 +0000 | [diff] [blame] | 96 | #include <__config> |
| 97 | #if _LIBCPP_STD_VER > 11 |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 98 | |
| 99 | #include <__functional_base> |
| 100 | #include <iterator> |
| 101 | #include <stdexcept> |
| 102 | #include <initializer_list> |
| 103 | #include <new> |
| 104 | #include <algorithm> |
| 105 | |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 106 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 107 | #pragma GCC system_header |
| 108 | #endif |
| 109 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 110 | _LIBCPP_PUSH_MACROS |
| 111 | #include <__undef_macros> |
| 112 | |
Marshall Clow | 648ad10 | 2013-11-13 22:44:48 +0000 | [diff] [blame] | 113 | namespace std { namespace experimental { inline namespace __array_extensions_v1 { |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 114 | |
| 115 | template <class _Tp> |
Mehdi Amini | 228053d | 2017-05-04 17:08:54 +0000 | [diff] [blame] | 116 | struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_DYNARRAY dynarray |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 117 | { |
| 118 | public: |
| 119 | // types: |
| 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; |
| 132 | |
| 133 | private: |
| 134 | size_t __size_; |
| 135 | value_type * __base_; |
Eric Fiselier | c16974d | 2015-10-01 07:29:38 +0000 | [diff] [blame] | 136 | _LIBCPP_ALWAYS_INLINE dynarray () noexcept : __size_(0), __base_(nullptr) {} |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 137 | |
| 138 | static inline _LIBCPP_INLINE_VISIBILITY value_type* __allocate ( size_t count ) |
| 139 | { |
| 140 | if ( numeric_limits<size_t>::max() / sizeof (value_type) <= count ) |
Marshall Clow | 8fea161 | 2016-08-25 15:09:01 +0000 | [diff] [blame] | 141 | __throw_bad_array_length(); |
| 142 | |
Richard Smith | 1f1c147 | 2014-06-04 19:54:15 +0000 | [diff] [blame] | 143 | return static_cast<value_type *> (_VSTD::__allocate (sizeof(value_type) * count)); |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Eric Fiselier | 8122fda | 2017-01-07 03:01:24 +0000 | [diff] [blame] | 146 | static inline _LIBCPP_INLINE_VISIBILITY void __deallocate_value( value_type* __ptr ) noexcept |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 147 | { |
Eric Fiselier | 8122fda | 2017-01-07 03:01:24 +0000 | [diff] [blame] | 148 | _VSTD::__libcpp_deallocate (static_cast<void *> (__ptr)); |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | public: |
| 152 | |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 153 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 154 | explicit dynarray(size_type __c); |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 155 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 156 | dynarray(size_type __c, const value_type& __v); |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 157 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 158 | dynarray(const dynarray& __d); |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 159 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 160 | dynarray(initializer_list<value_type>); |
| 161 | |
| 162 | // We're not implementing these right now. |
Marshall Clow | 0002f96 | 2014-07-23 16:58:25 +0000 | [diff] [blame] | 163 | // Updated with the resolution of LWG issue #2255 |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 164 | // template <typename _Alloc> |
Marshall Clow | 0002f96 | 2014-07-23 16:58:25 +0000 | [diff] [blame] | 165 | // dynarray(allocator_arg_t, const _Alloc& __alloc, size_type __c); |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 166 | // template <typename _Alloc> |
Marshall Clow | 0002f96 | 2014-07-23 16:58:25 +0000 | [diff] [blame] | 167 | // dynarray(allocator_arg_t, const _Alloc& __alloc, size_type __c, const value_type& __v); |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 168 | // template <typename _Alloc> |
Marshall Clow | 0002f96 | 2014-07-23 16:58:25 +0000 | [diff] [blame] | 169 | // dynarray(allocator_arg_t, const _Alloc& __alloc, const dynarray& __d); |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 170 | // template <typename _Alloc> |
Marshall Clow | 0002f96 | 2014-07-23 16:58:25 +0000 | [diff] [blame] | 171 | // dynarray(allocator_arg_t, const _Alloc& __alloc, initializer_list<value_type>); |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 172 | |
| 173 | dynarray& operator=(const dynarray&) = delete; |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 174 | _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 175 | ~dynarray(); |
| 176 | |
| 177 | // iterators: |
| 178 | inline _LIBCPP_INLINE_VISIBILITY iterator begin() noexcept { return iterator(data()); } |
| 179 | inline _LIBCPP_INLINE_VISIBILITY const_iterator begin() const noexcept { return const_iterator(data()); } |
| 180 | inline _LIBCPP_INLINE_VISIBILITY const_iterator cbegin() const noexcept { return const_iterator(data()); } |
| 181 | inline _LIBCPP_INLINE_VISIBILITY iterator end() noexcept { return iterator(data() + __size_); } |
| 182 | inline _LIBCPP_INLINE_VISIBILITY const_iterator end() const noexcept { return const_iterator(data() + __size_); } |
| 183 | inline _LIBCPP_INLINE_VISIBILITY const_iterator cend() const noexcept { return const_iterator(data() + __size_); } |
| 184 | |
| 185 | inline _LIBCPP_INLINE_VISIBILITY reverse_iterator rbegin() noexcept { return reverse_iterator(end()); } |
| 186 | inline _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); } |
| 187 | inline _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); } |
| 188 | inline _LIBCPP_INLINE_VISIBILITY reverse_iterator rend() noexcept { return reverse_iterator(begin()); } |
| 189 | inline _LIBCPP_INLINE_VISIBILITY const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); } |
| 190 | inline _LIBCPP_INLINE_VISIBILITY const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); } |
| 191 | |
| 192 | // capacity: |
| 193 | inline _LIBCPP_INLINE_VISIBILITY size_type size() const noexcept { return __size_; } |
| 194 | inline _LIBCPP_INLINE_VISIBILITY size_type max_size() const noexcept { return __size_; } |
| 195 | inline _LIBCPP_INLINE_VISIBILITY bool empty() const noexcept { return __size_ == 0; } |
| 196 | |
| 197 | // element access: |
| 198 | inline _LIBCPP_INLINE_VISIBILITY reference operator[](size_type __n) { return data()[__n]; } |
| 199 | inline _LIBCPP_INLINE_VISIBILITY const_reference operator[](size_type __n) const { return data()[__n]; } |
| 200 | |
| 201 | inline _LIBCPP_INLINE_VISIBILITY reference front() { return data()[0]; } |
| 202 | inline _LIBCPP_INLINE_VISIBILITY const_reference front() const { return data()[0]; } |
| 203 | inline _LIBCPP_INLINE_VISIBILITY reference back() { return data()[__size_-1]; } |
| 204 | inline _LIBCPP_INLINE_VISIBILITY const_reference back() const { return data()[__size_-1]; } |
| 205 | |
| 206 | inline _LIBCPP_INLINE_VISIBILITY const_reference at(size_type __n) const; |
| 207 | inline _LIBCPP_INLINE_VISIBILITY reference at(size_type __n); |
| 208 | |
| 209 | // data access: |
| 210 | inline _LIBCPP_INLINE_VISIBILITY _Tp* data() noexcept { return __base_; } |
| 211 | inline _LIBCPP_INLINE_VISIBILITY const _Tp* data() const noexcept { return __base_; } |
| 212 | |
| 213 | // mutating member functions: |
| 214 | inline _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __v) { fill_n(begin(), __size_, __v); } |
| 215 | }; |
| 216 | |
| 217 | template <class _Tp> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 218 | inline |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 219 | dynarray<_Tp>::dynarray(size_type __c) : dynarray () |
| 220 | { |
| 221 | __base_ = __allocate (__c); |
| 222 | value_type *__data = data (); |
| 223 | for ( __size_ = 0; __size_ < __c; ++__size_, ++__data ) |
| 224 | ::new (__data) value_type; |
| 225 | } |
| 226 | |
| 227 | template <class _Tp> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 228 | inline |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 229 | dynarray<_Tp>::dynarray(size_type __c, const value_type& __v) : dynarray () |
| 230 | { |
| 231 | __base_ = __allocate (__c); |
| 232 | value_type *__data = data (); |
| 233 | for ( __size_ = 0; __size_ < __c; ++__size_, ++__data ) |
| 234 | ::new (__data) value_type (__v); |
| 235 | } |
| 236 | |
| 237 | template <class _Tp> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 238 | inline |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 239 | dynarray<_Tp>::dynarray(initializer_list<value_type> __il) : dynarray () |
| 240 | { |
| 241 | size_t sz = __il.size(); |
| 242 | __base_ = __allocate (sz); |
| 243 | value_type *__data = data (); |
| 244 | auto src = __il.begin(); |
| 245 | for ( __size_ = 0; __size_ < sz; ++__size_, ++__data, ++src ) |
| 246 | ::new (__data) value_type (*src); |
| 247 | } |
| 248 | |
| 249 | template <class _Tp> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 250 | inline |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 251 | dynarray<_Tp>::dynarray(const dynarray& __d) : dynarray () |
| 252 | { |
| 253 | size_t sz = __d.size(); |
| 254 | __base_ = __allocate (sz); |
| 255 | value_type *__data = data (); |
| 256 | auto src = __d.begin(); |
| 257 | for ( __size_ = 0; __size_ < sz; ++__size_, ++__data, ++src ) |
| 258 | ::new (__data) value_type (*src); |
| 259 | } |
| 260 | |
| 261 | template <class _Tp> |
Evgeniy Stepanov | 3b68ae5 | 2016-04-22 01:04:55 +0000 | [diff] [blame] | 262 | inline |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 263 | dynarray<_Tp>::~dynarray() |
| 264 | { |
| 265 | value_type *__data = data () + __size_; |
| 266 | for ( size_t i = 0; i < __size_; ++i ) |
| 267 | (--__data)->value_type::~value_type(); |
Eric Fiselier | 8122fda | 2017-01-07 03:01:24 +0000 | [diff] [blame] | 268 | __deallocate_value( __base_ ); |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | template <class _Tp> |
| 272 | inline _LIBCPP_INLINE_VISIBILITY |
| 273 | typename dynarray<_Tp>::reference |
| 274 | dynarray<_Tp>::at(size_type __n) |
| 275 | { |
| 276 | if (__n >= __size_) |
Marshall Clow | 8fea161 | 2016-08-25 15:09:01 +0000 | [diff] [blame] | 277 | __throw_out_of_range("dynarray::at"); |
| 278 | |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 279 | return data()[__n]; |
| 280 | } |
| 281 | |
| 282 | template <class _Tp> |
| 283 | inline _LIBCPP_INLINE_VISIBILITY |
| 284 | typename dynarray<_Tp>::const_reference |
| 285 | dynarray<_Tp>::at(size_type __n) const |
| 286 | { |
| 287 | if (__n >= __size_) |
Marshall Clow | 8fea161 | 2016-08-25 15:09:01 +0000 | [diff] [blame] | 288 | __throw_out_of_range("dynarray::at"); |
| 289 | |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 290 | return data()[__n]; |
| 291 | } |
| 292 | |
Marshall Clow | 648ad10 | 2013-11-13 22:44:48 +0000 | [diff] [blame] | 293 | }}} |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 294 | |
Marshall Clow | 648ad10 | 2013-11-13 22:44:48 +0000 | [diff] [blame] | 295 | |
| 296 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 297 | template <class _Tp, class _Alloc> |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 298 | struct _LIBCPP_TEMPLATE_VIS uses_allocator<std::experimental::dynarray<_Tp>, _Alloc> : true_type {}; |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 299 | _LIBCPP_END_NAMESPACE_STD |
| 300 | |
Eric Fiselier | f4433a3 | 2017-05-31 22:07:49 +0000 | [diff] [blame] | 301 | _LIBCPP_POP_MACROS |
| 302 | |
Marshall Clow | b04f54b | 2013-09-13 15:22:55 +0000 | [diff] [blame] | 303 | #endif // if _LIBCPP_STD_VER > 11 |
| 304 | #endif // _LIBCPP_DYNARRAY |