Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===----------------------- forward_list ---------------------------------===// |
| 3 | // |
Howard Hinnant | 5b08a8a | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 4 | // The LLVM Compiler Infrastructure |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 5 | // |
Howard Hinnant | 412dbeb | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 6 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 7 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP_FORWARD_LIST |
| 12 | #define _LIBCPP_FORWARD_LIST |
| 13 | |
| 14 | /* |
| 15 | forward_list synopsis |
| 16 | |
| 17 | namespace std |
| 18 | { |
| 19 | |
| 20 | template <class T, class Allocator = allocator<T>> |
| 21 | class forward_list |
| 22 | { |
| 23 | public: |
| 24 | typedef T value_type; |
| 25 | typedef Allocator allocator_type; |
| 26 | |
| 27 | typedef value_type& reference; |
| 28 | typedef const value_type& const_reference; |
| 29 | typedef typename allocator_traits<allocator_type>::pointer pointer; |
| 30 | typedef typename allocator_traits<allocator_type>::const_pointer const_pointer; |
| 31 | typedef typename allocator_traits<allocator_type>::size_type size_type; |
| 32 | typedef typename allocator_traits<allocator_type>::difference_type difference_type; |
| 33 | |
| 34 | typedef <details> iterator; |
| 35 | typedef <details> const_iterator; |
| 36 | |
| 37 | forward_list(); |
| 38 | explicit forward_list(const allocator_type& a); |
| 39 | explicit forward_list(size_type n); |
| 40 | forward_list(size_type n, const value_type& v); |
| 41 | forward_list(size_type n, const value_type& v, const allocator_type& a); |
| 42 | template <class InputIterator> |
| 43 | forward_list(InputIterator first, InputIterator last); |
| 44 | template <class InputIterator> |
| 45 | forward_list(InputIterator first, InputIterator last, const allocator_type& a); |
| 46 | forward_list(const forward_list& x); |
| 47 | forward_list(const forward_list& x, const allocator_type& a); |
| 48 | forward_list(forward_list&& x); |
| 49 | forward_list(forward_list&& x, const allocator_type& a); |
| 50 | forward_list(initializer_list<value_type> il); |
| 51 | forward_list(initializer_list<value_type> il, const allocator_type& a); |
| 52 | |
| 53 | ~forward_list(); |
| 54 | |
| 55 | forward_list& operator=(const forward_list& x); |
| 56 | forward_list& operator=(forward_list&& x); |
| 57 | forward_list& operator=(initializer_list<value_type> il); |
| 58 | |
| 59 | template <class InputIterator> |
| 60 | void assign(InputIterator first, InputIterator last); |
| 61 | void assign(size_type n, const value_type& v); |
| 62 | void assign(initializer_list<value_type> il); |
| 63 | |
| 64 | allocator_type get_allocator() const; |
| 65 | |
| 66 | iterator begin(); |
| 67 | const_iterator begin() const; |
| 68 | iterator end(); |
| 69 | const_iterator end() const; |
| 70 | |
| 71 | const_iterator cbegin() const; |
| 72 | const_iterator cend() const; |
| 73 | |
| 74 | iterator before_begin(); |
| 75 | const_iterator before_begin() const; |
| 76 | const_iterator cbefore_begin() const; |
| 77 | |
| 78 | bool empty() const; |
| 79 | size_type max_size() const; |
| 80 | |
| 81 | reference front(); |
| 82 | const_reference front() const; |
| 83 | |
| 84 | template <class... Args> void emplace_front(Args&&... args); |
| 85 | void push_front(const value_type& v); |
| 86 | void push_front(value_type&& v); |
| 87 | |
| 88 | void pop_front(); |
| 89 | |
| 90 | template <class... Args> |
| 91 | iterator emplace_after(const_iterator p, Args&&... args); |
| 92 | iterator insert_after(const_iterator p, const value_type& v); |
| 93 | iterator insert_after(const_iterator p, value_type&& v); |
| 94 | iterator insert_after(const_iterator p, size_type n, const value_type& v); |
| 95 | template <class InputIterator> |
| 96 | iterator insert_after(const_iterator p, |
| 97 | InputIterator first, InputIterator last); |
| 98 | iterator insert_after(const_iterator p, initializer_list<value_type> il); |
| 99 | |
Howard Hinnant | 3db8803 | 2010-08-21 20:58:44 +0000 | [diff] [blame] | 100 | iterator erase_after(const_iterator p); |
| 101 | iterator erase_after(const_iterator first, const_iterator last); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 102 | |
| 103 | void swap(forward_list& x); |
| 104 | |
| 105 | void resize(size_type n); |
| 106 | void resize(size_type n, const value_type& v); |
| 107 | void clear(); |
| 108 | |
Howard Hinnant | eb92df7 | 2011-01-27 21:00:35 +0000 | [diff] [blame] | 109 | void splice_after(const_iterator p, forward_list& x); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 110 | void splice_after(const_iterator p, forward_list&& x); |
Howard Hinnant | eb92df7 | 2011-01-27 21:00:35 +0000 | [diff] [blame] | 111 | void splice_after(const_iterator p, forward_list& x, const_iterator i); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 112 | void splice_after(const_iterator p, forward_list&& x, const_iterator i); |
Howard Hinnant | eb92df7 | 2011-01-27 21:00:35 +0000 | [diff] [blame] | 113 | void splice_after(const_iterator p, forward_list& x, |
| 114 | const_iterator first, const_iterator last); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 115 | void splice_after(const_iterator p, forward_list&& x, |
| 116 | const_iterator first, const_iterator last); |
| 117 | void remove(const value_type& v); |
| 118 | template <class Predicate> void remove_if(Predicate pred); |
| 119 | void unique(); |
| 120 | template <class BinaryPredicate> void unique(BinaryPredicate binary_pred); |
Howard Hinnant | eb92df7 | 2011-01-27 21:00:35 +0000 | [diff] [blame] | 121 | void merge(forward_list& x); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 122 | void merge(forward_list&& x); |
Howard Hinnant | eb92df7 | 2011-01-27 21:00:35 +0000 | [diff] [blame] | 123 | template <class Compare> void merge(forward_list& x, Compare comp); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 124 | template <class Compare> void merge(forward_list&& x, Compare comp); |
| 125 | void sort(); |
| 126 | template <class Compare> void sort(Compare comp); |
| 127 | void reverse(); |
| 128 | }; |
| 129 | |
| 130 | template <class T, class Allocator> |
| 131 | bool operator==(const forward_list<T, Allocator>& x, |
| 132 | const forward_list<T, Allocator>& y); |
| 133 | |
| 134 | template <class T, class Allocator> |
| 135 | bool operator< (const forward_list<T, Allocator>& x, |
| 136 | const forward_list<T, Allocator>& y); |
| 137 | |
| 138 | template <class T, class Allocator> |
| 139 | bool operator!=(const forward_list<T, Allocator>& x, |
| 140 | const forward_list<T, Allocator>& y); |
| 141 | |
| 142 | template <class T, class Allocator> |
| 143 | bool operator> (const forward_list<T, Allocator>& x, |
| 144 | const forward_list<T, Allocator>& y); |
| 145 | |
| 146 | template <class T, class Allocator> |
| 147 | bool operator>=(const forward_list<T, Allocator>& x, |
| 148 | const forward_list<T, Allocator>& y); |
| 149 | |
| 150 | template <class T, class Allocator> |
| 151 | bool operator<=(const forward_list<T, Allocator>& x, |
| 152 | const forward_list<T, Allocator>& y); |
| 153 | |
| 154 | template <class T, class Allocator> |
| 155 | void swap(forward_list<T, Allocator>& x, forward_list<T, Allocator>& y); |
| 156 | |
| 157 | } // std |
| 158 | |
| 159 | */ |
| 160 | |
| 161 | #include <__config> |
| 162 | |
| 163 | #include <initializer_list> |
| 164 | #include <memory> |
| 165 | #include <limits> |
| 166 | #include <iterator> |
| 167 | #include <algorithm> |
| 168 | |
| 169 | #pragma GCC system_header |
| 170 | |
| 171 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 172 | |
| 173 | template <class, class> struct __forward_list_node; |
| 174 | |
| 175 | template <class _NodePtr> |
| 176 | struct __forward_begin_node |
| 177 | { |
| 178 | typedef __forward_begin_node __self; |
| 179 | typedef _NodePtr pointer; |
| 180 | |
| 181 | pointer __next_; |
| 182 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 183 | _LIBCPP_INLINE_VISIBILITY __forward_begin_node() : __next_(nullptr) {} |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | template <class _Tp, class _VoidPtr> |
| 187 | struct __forward_list_node |
| 188 | : public __forward_begin_node |
| 189 | < |
| 190 | typename pointer_traits<_VoidPtr>::template |
| 191 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 192 | rebind<__forward_list_node<_Tp, _VoidPtr> > |
| 193 | #else |
| 194 | rebind<__forward_list_node<_Tp, _VoidPtr> >::other |
| 195 | #endif |
| 196 | > |
| 197 | { |
| 198 | typedef _Tp value_type; |
| 199 | |
| 200 | value_type __value_; |
| 201 | }; |
| 202 | |
| 203 | template<class, class> class forward_list; |
| 204 | template<class> class __forward_list_const_iterator; |
| 205 | |
| 206 | template <class _NodePtr> |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 207 | class _LIBCPP_VISIBLE __forward_list_iterator |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 208 | { |
| 209 | typedef _NodePtr __node_pointer; |
| 210 | |
| 211 | __node_pointer __ptr_; |
| 212 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 213 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 214 | explicit __forward_list_iterator(__node_pointer __p) : __ptr_(__p) {} |
| 215 | |
| 216 | template<class, class> friend class forward_list; |
| 217 | template<class> friend class __forward_list_const_iterator; |
| 218 | |
| 219 | public: |
| 220 | typedef forward_iterator_tag iterator_category; |
| 221 | typedef typename pointer_traits<__node_pointer>::element_type::value_type |
| 222 | value_type; |
| 223 | typedef value_type& reference; |
Howard Hinnant | b3371f6 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 224 | typedef typename pointer_traits<__node_pointer>::difference_type |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 225 | difference_type; |
| 226 | typedef typename pointer_traits<__node_pointer>::template |
| 227 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 228 | rebind<value_type> |
| 229 | #else |
| 230 | rebind<value_type>::other |
| 231 | #endif |
| 232 | pointer; |
| 233 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 234 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 235 | __forward_list_iterator() : __ptr_(nullptr) {} |
| 236 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 237 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 238 | reference operator*() const {return __ptr_->__value_;} |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 239 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 240 | pointer operator->() const {return &__ptr_->__value_;} |
| 241 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 242 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 243 | __forward_list_iterator& operator++() |
| 244 | { |
| 245 | __ptr_ = __ptr_->__next_; |
| 246 | return *this; |
| 247 | } |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 248 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 249 | __forward_list_iterator operator++(int) |
| 250 | { |
| 251 | __forward_list_iterator __t(*this); |
| 252 | ++(*this); |
| 253 | return __t; |
| 254 | } |
| 255 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 256 | friend _LIBCPP_INLINE_VISIBILITY |
| 257 | bool operator==(const __forward_list_iterator& __x, |
| 258 | const __forward_list_iterator& __y) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 259 | {return __x.__ptr_ == __y.__ptr_;} |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 260 | friend _LIBCPP_INLINE_VISIBILITY |
| 261 | bool operator!=(const __forward_list_iterator& __x, |
| 262 | const __forward_list_iterator& __y) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 263 | {return !(__x == __y);} |
| 264 | }; |
| 265 | |
| 266 | template <class _NodeConstPtr> |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 267 | class _LIBCPP_VISIBLE __forward_list_const_iterator |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 268 | { |
| 269 | typedef _NodeConstPtr __node_const_pointer; |
| 270 | |
| 271 | __node_const_pointer __ptr_; |
| 272 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 273 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 274 | explicit __forward_list_const_iterator(__node_const_pointer __p) |
| 275 | : __ptr_(__p) {} |
| 276 | |
| 277 | typedef typename remove_const |
| 278 | < |
| 279 | typename pointer_traits<__node_const_pointer>::element_type |
| 280 | >::type __node; |
| 281 | typedef typename pointer_traits<__node_const_pointer>::template |
| 282 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 283 | rebind<__node> |
| 284 | #else |
| 285 | rebind<__node>::other |
| 286 | #endif |
| 287 | __node_pointer; |
| 288 | |
| 289 | template<class, class> friend class forward_list; |
| 290 | |
| 291 | public: |
| 292 | typedef forward_iterator_tag iterator_category; |
| 293 | typedef typename __node::value_type value_type; |
| 294 | typedef const value_type& reference; |
Howard Hinnant | b3371f6 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 295 | typedef typename pointer_traits<__node_const_pointer>::difference_type |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 296 | difference_type; |
| 297 | typedef typename pointer_traits<__node_const_pointer>::template |
| 298 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 299 | rebind<const value_type> |
| 300 | #else |
| 301 | rebind<const value_type>::other |
| 302 | #endif |
| 303 | pointer; |
| 304 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 305 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 306 | __forward_list_const_iterator() : __ptr_(nullptr) {} |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 307 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 308 | __forward_list_const_iterator(__forward_list_iterator<__node_pointer> __p) |
| 309 | : __ptr_(__p.__ptr_) {} |
| 310 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 311 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 312 | reference operator*() const {return __ptr_->__value_;} |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 313 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 314 | pointer operator->() const {return &__ptr_->__value_;} |
| 315 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 316 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 317 | __forward_list_const_iterator& operator++() |
| 318 | { |
| 319 | __ptr_ = __ptr_->__next_; |
| 320 | return *this; |
| 321 | } |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 322 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 323 | __forward_list_const_iterator operator++(int) |
| 324 | { |
| 325 | __forward_list_const_iterator __t(*this); |
| 326 | ++(*this); |
| 327 | return __t; |
| 328 | } |
| 329 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 330 | friend _LIBCPP_INLINE_VISIBILITY |
| 331 | bool operator==(const __forward_list_const_iterator& __x, |
| 332 | const __forward_list_const_iterator& __y) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 333 | {return __x.__ptr_ == __y.__ptr_;} |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 334 | friend _LIBCPP_INLINE_VISIBILITY |
| 335 | bool operator!=(const __forward_list_const_iterator& __x, |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 336 | const __forward_list_const_iterator& __y) |
| 337 | {return !(__x == __y);} |
| 338 | }; |
| 339 | |
| 340 | template <class _Tp, class _Alloc> |
| 341 | class __forward_list_base |
| 342 | { |
| 343 | protected: |
| 344 | typedef _Tp value_type; |
| 345 | typedef _Alloc allocator_type; |
| 346 | |
| 347 | typedef typename allocator_traits<allocator_type>::void_pointer void_pointer; |
| 348 | typedef __forward_list_node<value_type, void_pointer> __node; |
| 349 | typedef typename __node::__self __begin_node; |
| 350 | typedef typename allocator_traits<allocator_type>::template |
| 351 | #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES |
| 352 | rebind_alloc<__node> |
| 353 | #else |
| 354 | rebind_alloc<__node>::other |
| 355 | #endif |
| 356 | __node_allocator; |
| 357 | typedef allocator_traits<__node_allocator> __node_traits; |
| 358 | typedef typename __node_traits::pointer __node_pointer; |
| 359 | typedef typename __node_traits::const_pointer __node_const_pointer; |
| 360 | |
| 361 | __compressed_pair<__begin_node, __node_allocator> __before_begin_; |
| 362 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 363 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 364 | __node_pointer __before_begin() |
| 365 | {return pointer_traits<__node_pointer>::pointer_to( |
| 366 | static_cast<__node&>(__before_begin_.first()));} |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 367 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 368 | __node_const_pointer __before_begin() const |
| 369 | {return pointer_traits<__node_const_pointer>::pointer_to( |
| 370 | static_cast<const __node&>(__before_begin_.first()));} |
| 371 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 372 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 373 | __node_allocator& __alloc() {return __before_begin_.second();} |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 374 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 375 | const __node_allocator& __alloc() const {return __before_begin_.second();} |
| 376 | |
| 377 | typedef __forward_list_iterator<__node_pointer> iterator; |
| 378 | typedef __forward_list_const_iterator<__node_const_pointer> const_iterator; |
| 379 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 380 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 381 | __forward_list_base() |
| 382 | : __before_begin_(__begin_node()) {} |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 383 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 384 | __forward_list_base(const allocator_type& __a) |
| 385 | : __before_begin_(__begin_node(), __node_allocator(__a)) {} |
| 386 | |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 387 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 388 | __forward_list_base(__forward_list_base&& __x); |
| 389 | __forward_list_base(__forward_list_base&& __x, const allocator_type& __a); |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 390 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 391 | |
| 392 | private: |
| 393 | __forward_list_base(const __forward_list_base&); |
| 394 | __forward_list_base& operator=(const __forward_list_base&); |
| 395 | protected: |
| 396 | |
| 397 | ~__forward_list_base(); |
| 398 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 399 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 400 | void __copy_assign_alloc(const __forward_list_base& __x) |
| 401 | {__copy_assign_alloc(__x, integral_constant<bool, |
| 402 | __node_traits::propagate_on_container_copy_assignment::value>());} |
| 403 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 404 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 405 | void __move_assign_alloc(__forward_list_base& __x) |
| 406 | {__move_assign_alloc(__x, integral_constant<bool, |
| 407 | __node_traits::propagate_on_container_move_assignment::value>());} |
| 408 | |
| 409 | void swap(__forward_list_base& __x); |
| 410 | void clear(); |
| 411 | |
| 412 | private: |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 413 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 414 | void __copy_assign_alloc(const __forward_list_base&, false_type) {} |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 415 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 416 | void __copy_assign_alloc(const __forward_list_base& __x, true_type) |
| 417 | { |
| 418 | if (__alloc() != __x.__alloc()) |
| 419 | clear(); |
| 420 | __alloc() = __x.__alloc(); |
| 421 | } |
| 422 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 423 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 424 | void __move_assign_alloc(__forward_list_base& __x, false_type) {} |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 425 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 426 | void __move_assign_alloc(__forward_list_base& __x, true_type) |
| 427 | {__alloc() = _STD::move(__x.__alloc());} |
| 428 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 429 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 430 | static void __swap_alloc(__node_allocator& __x, __node_allocator& __y) |
| 431 | {__swap_alloc(__x, __y, integral_constant<bool, |
| 432 | __node_traits::propagate_on_container_swap::value>());} |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 433 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 434 | static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, |
| 435 | false_type) |
| 436 | {} |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 437 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 438 | static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, |
| 439 | true_type) |
| 440 | { |
| 441 | using _STD::swap; |
| 442 | swap(__x, __y); |
| 443 | } |
| 444 | }; |
| 445 | |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 446 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 447 | |
| 448 | template <class _Tp, class _Alloc> |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 449 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 450 | __forward_list_base<_Tp, _Alloc>::__forward_list_base(__forward_list_base&& __x) |
| 451 | : __before_begin_(_STD::move(__x.__before_begin_)) |
| 452 | { |
| 453 | __x.__before_begin()->__next_ = nullptr; |
| 454 | } |
| 455 | |
| 456 | template <class _Tp, class _Alloc> |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 457 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 458 | __forward_list_base<_Tp, _Alloc>::__forward_list_base(__forward_list_base&& __x, |
| 459 | const allocator_type& __a) |
| 460 | : __before_begin_(__begin_node(), __node_allocator(__a)) |
| 461 | { |
| 462 | if (__alloc() == __x.__alloc()) |
| 463 | { |
| 464 | __before_begin()->__next_ = __x.__before_begin()->__next_; |
| 465 | __x.__before_begin()->__next_ = nullptr; |
| 466 | } |
| 467 | } |
| 468 | |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 469 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 470 | |
| 471 | template <class _Tp, class _Alloc> |
| 472 | __forward_list_base<_Tp, _Alloc>::~__forward_list_base() |
| 473 | { |
| 474 | clear(); |
| 475 | } |
| 476 | |
| 477 | template <class _Tp, class _Alloc> |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 478 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 479 | void |
| 480 | __forward_list_base<_Tp, _Alloc>::swap(__forward_list_base& __x) |
| 481 | { |
| 482 | __swap_alloc(__alloc(), __x.__alloc()); |
| 483 | using _STD::swap; |
| 484 | swap(__before_begin()->__next_, __x.__before_begin()->__next_); |
| 485 | } |
| 486 | |
| 487 | template <class _Tp, class _Alloc> |
| 488 | void |
| 489 | __forward_list_base<_Tp, _Alloc>::clear() |
| 490 | { |
| 491 | __node_allocator& __a = __alloc(); |
| 492 | for (__node_pointer __p = __before_begin()->__next_; __p != nullptr;) |
| 493 | { |
| 494 | __node_pointer __next = __p->__next_; |
Howard Hinnant | 72c5e14 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 495 | __node_traits::destroy(__a, _STD::addressof(__p->__value_)); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 496 | __node_traits::deallocate(__a, __p, 1); |
| 497 | __p = __next; |
| 498 | } |
| 499 | __before_begin()->__next_ = nullptr; |
| 500 | } |
| 501 | |
| 502 | template <class _Tp, class _Alloc = allocator<_Tp> > |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 503 | class _LIBCPP_VISIBLE forward_list |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 504 | : private __forward_list_base<_Tp, _Alloc> |
| 505 | { |
| 506 | typedef __forward_list_base<_Tp, _Alloc> base; |
| 507 | public: |
| 508 | typedef _Tp value_type; |
| 509 | typedef _Alloc allocator_type; |
| 510 | |
| 511 | typedef value_type& reference; |
| 512 | typedef const value_type& const_reference; |
| 513 | typedef typename allocator_traits<allocator_type>::pointer pointer; |
| 514 | typedef typename allocator_traits<allocator_type>::const_pointer const_pointer; |
| 515 | typedef typename allocator_traits<allocator_type>::size_type size_type; |
| 516 | typedef typename allocator_traits<allocator_type>::difference_type difference_type; |
| 517 | |
| 518 | typedef typename base::iterator iterator; |
| 519 | typedef typename base::const_iterator const_iterator; |
| 520 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 521 | _LIBCPP_INLINE_VISIBILITY forward_list() {} // = default; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 522 | explicit forward_list(const allocator_type& __a); |
| 523 | explicit forward_list(size_type __n); |
| 524 | forward_list(size_type __n, const value_type& __v); |
| 525 | forward_list(size_type __n, const value_type& __v, const allocator_type& __a); |
| 526 | template <class _InputIterator> |
| 527 | forward_list(_InputIterator __f, _InputIterator __l, |
| 528 | typename enable_if< |
| 529 | __is_input_iterator<_InputIterator>::value |
| 530 | >::type* = nullptr); |
| 531 | template <class _InputIterator> |
| 532 | forward_list(_InputIterator __f, _InputIterator __l, |
| 533 | const allocator_type& __a, |
| 534 | typename enable_if< |
| 535 | __is_input_iterator<_InputIterator>::value |
| 536 | >::type* = nullptr); |
| 537 | forward_list(const forward_list& __x); |
| 538 | forward_list(const forward_list& __x, const allocator_type& __a); |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 539 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 540 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 541 | forward_list(forward_list&& __x) : base(_STD::move(__x)) {} |
| 542 | forward_list(forward_list&& __x, const allocator_type& __a); |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 543 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 544 | forward_list(initializer_list<value_type> __il); |
| 545 | forward_list(initializer_list<value_type> __il, const allocator_type& __a); |
| 546 | |
| 547 | // ~forward_list() = default; |
| 548 | |
| 549 | forward_list& operator=(const forward_list& __x); |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 550 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 551 | forward_list& operator=(forward_list&& __x); |
| 552 | #endif |
| 553 | forward_list& operator=(initializer_list<value_type> __il); |
| 554 | |
| 555 | template <class _InputIterator> |
| 556 | typename enable_if |
| 557 | < |
| 558 | __is_input_iterator<_InputIterator>::value, |
| 559 | void |
| 560 | >::type |
| 561 | assign(_InputIterator __f, _InputIterator __l); |
| 562 | void assign(size_type __n, const value_type& __v); |
| 563 | void assign(initializer_list<value_type> __il); |
| 564 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 565 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 566 | allocator_type get_allocator() const {return allocator_type(base::__alloc());} |
| 567 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 568 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 569 | iterator begin() {return iterator(base::__before_begin()->__next_);} |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 570 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 571 | const_iterator begin() const {return const_iterator(base::__before_begin()->__next_);} |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 572 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 573 | iterator end() {return iterator(nullptr);} |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 574 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 575 | const_iterator end() const {return const_iterator(nullptr);} |
| 576 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 577 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 578 | const_iterator cbegin() const {return const_iterator(base::__before_begin()->__next_);} |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 579 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 580 | const_iterator cend() const {return const_iterator(nullptr);} |
| 581 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 582 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 583 | iterator before_begin() {return iterator(base::__before_begin());} |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 584 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 585 | const_iterator before_begin() const {return const_iterator(base::__before_begin());} |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 586 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 587 | const_iterator cbefore_begin() const {return const_iterator(base::__before_begin());} |
| 588 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 589 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 590 | bool empty() const {return base::__before_begin()->__next_ == nullptr;} |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 591 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 592 | size_type max_size() const {return numeric_limits<size_type>::max();} |
| 593 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 594 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 595 | reference front() {return base::__before_begin()->__next_->__value_;} |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 596 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 597 | const_reference front() const {return base::__before_begin()->__next_->__value_;} |
| 598 | |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 599 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 600 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 601 | template <class... _Args> void emplace_front(_Args&&... __args); |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 602 | #endif |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 603 | void push_front(value_type&& __v); |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 604 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 605 | void push_front(const value_type& __v); |
| 606 | |
| 607 | void pop_front(); |
| 608 | |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 609 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 610 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 611 | template <class... _Args> |
| 612 | iterator emplace_after(const_iterator __p, _Args&&... __args); |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 613 | #endif // _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 614 | iterator insert_after(const_iterator __p, value_type&& __v); |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 615 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 616 | iterator insert_after(const_iterator __p, const value_type& __v); |
| 617 | iterator insert_after(const_iterator __p, size_type __n, const value_type& __v); |
| 618 | template <class _InputIterator> |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 619 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 620 | typename enable_if |
| 621 | < |
| 622 | __is_input_iterator<_InputIterator>::value, |
| 623 | iterator |
| 624 | >::type |
| 625 | insert_after(const_iterator __p, _InputIterator __f, _InputIterator __l); |
| 626 | iterator insert_after(const_iterator __p, initializer_list<value_type> __il) |
| 627 | {return insert_after(__p, __il.begin(), __il.end());} |
| 628 | |
Howard Hinnant | 3db8803 | 2010-08-21 20:58:44 +0000 | [diff] [blame] | 629 | iterator erase_after(const_iterator __p); |
| 630 | iterator erase_after(const_iterator __f, const_iterator __l); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 631 | |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 632 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 633 | void swap(forward_list& __x) {base::swap(__x);} |
| 634 | |
| 635 | void resize(size_type __n); |
| 636 | void resize(size_type __n, const value_type& __v); |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 637 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 638 | void clear() {base::clear();} |
| 639 | |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 640 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | eb92df7 | 2011-01-27 21:00:35 +0000 | [diff] [blame] | 641 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 642 | void splice_after(const_iterator __p, forward_list&& __x); |
Howard Hinnant | eb92df7 | 2011-01-27 21:00:35 +0000 | [diff] [blame] | 643 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 644 | void splice_after(const_iterator __p, forward_list&& __x, const_iterator __i); |
Howard Hinnant | eb92df7 | 2011-01-27 21:00:35 +0000 | [diff] [blame] | 645 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 646 | void splice_after(const_iterator __p, forward_list&& __x, |
| 647 | const_iterator __f, const_iterator __l); |
Howard Hinnant | eb92df7 | 2011-01-27 21:00:35 +0000 | [diff] [blame] | 648 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 649 | void splice_after(const_iterator __p, forward_list& __x); |
| 650 | void splice_after(const_iterator __p, forward_list& __x, const_iterator __i); |
| 651 | void splice_after(const_iterator __p, forward_list& __x, |
| 652 | const_iterator __f, const_iterator __l); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 653 | void remove(const value_type& __v); |
| 654 | template <class _Predicate> void remove_if(_Predicate __pred); |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 655 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 656 | void unique() {unique(__equal_to<value_type>());} |
| 657 | template <class _BinaryPredicate> void unique(_BinaryPredicate __binary_pred); |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 658 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 659 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | eb92df7 | 2011-01-27 21:00:35 +0000 | [diff] [blame] | 660 | void merge(forward_list&& __x) {merge(__x, __less<value_type>());} |
| 661 | template <class _Compare> |
| 662 | _LIBCPP_INLINE_VISIBILITY |
| 663 | void merge(forward_list&& __x, _Compare __comp) |
| 664 | {merge(__x, _STD::move(__comp));} |
| 665 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 666 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 667 | void merge(forward_list& __x) {merge(__x, __less<value_type>());} |
| 668 | template <class _Compare> void merge(forward_list& __x, _Compare __comp); |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 669 | _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 670 | void sort() {sort(__less<value_type>());} |
| 671 | template <class _Compare> void sort(_Compare __comp); |
| 672 | void reverse(); |
| 673 | |
| 674 | private: |
| 675 | typedef typename base::__node_allocator __node_allocator; |
| 676 | typedef typename base::__node __node; |
| 677 | typedef typename base::__node_traits __node_traits; |
| 678 | typedef typename base::__node_pointer __node_pointer; |
| 679 | |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 680 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 681 | void __move_assign(forward_list& __x, true_type); |
| 682 | void __move_assign(forward_list& __x, false_type); |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 683 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 684 | |
| 685 | template <class _Compare> |
| 686 | static |
| 687 | __node_pointer |
| 688 | __merge(__node_pointer __f1, __node_pointer __f2, _Compare& __comp); |
| 689 | |
| 690 | template <class _Compare> |
| 691 | static |
| 692 | __node_pointer |
| 693 | __sort(__node_pointer __f, difference_type __sz, _Compare& __comp); |
| 694 | }; |
| 695 | |
| 696 | template <class _Tp, class _Alloc> |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 697 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 698 | forward_list<_Tp, _Alloc>::forward_list(const allocator_type& __a) |
| 699 | : base(__a) |
| 700 | { |
| 701 | } |
| 702 | |
| 703 | template <class _Tp, class _Alloc> |
| 704 | forward_list<_Tp, _Alloc>::forward_list(size_type __n) |
| 705 | { |
| 706 | if (__n > 0) |
| 707 | { |
| 708 | __node_allocator& __a = base::__alloc(); |
| 709 | typedef __allocator_destructor<__node_allocator> _D; |
| 710 | unique_ptr<__node, _D> __h(nullptr, _D(__a, 1)); |
| 711 | for (__node_pointer __p = base::__before_begin(); __n > 0; --__n, |
| 712 | __p = __p->__next_) |
| 713 | { |
| 714 | __h.reset(__node_traits::allocate(__a, 1)); |
Howard Hinnant | 72c5e14 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 715 | __node_traits::construct(__a, _STD::addressof(__h->__value_)); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 716 | __h->__next_ = nullptr; |
| 717 | __p->__next_ = __h.release(); |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | template <class _Tp, class _Alloc> |
| 723 | forward_list<_Tp, _Alloc>::forward_list(size_type __n, const value_type& __v) |
| 724 | { |
| 725 | insert_after(cbefore_begin(), __n, __v); |
| 726 | } |
| 727 | |
| 728 | template <class _Tp, class _Alloc> |
| 729 | forward_list<_Tp, _Alloc>::forward_list(size_type __n, const value_type& __v, |
| 730 | const allocator_type& __a) |
| 731 | : base(__a) |
| 732 | { |
| 733 | insert_after(cbefore_begin(), __n, __v); |
| 734 | } |
| 735 | |
| 736 | template <class _Tp, class _Alloc> |
| 737 | template <class _InputIterator> |
| 738 | forward_list<_Tp, _Alloc>::forward_list(_InputIterator __f, _InputIterator __l, |
| 739 | typename enable_if< |
| 740 | __is_input_iterator<_InputIterator>::value |
| 741 | >::type*) |
| 742 | { |
| 743 | insert_after(cbefore_begin(), __f, __l); |
| 744 | } |
| 745 | |
| 746 | template <class _Tp, class _Alloc> |
| 747 | template <class _InputIterator> |
| 748 | forward_list<_Tp, _Alloc>::forward_list(_InputIterator __f, _InputIterator __l, |
| 749 | const allocator_type& __a, |
| 750 | typename enable_if< |
| 751 | __is_input_iterator<_InputIterator>::value |
| 752 | >::type*) |
| 753 | : base(__a) |
| 754 | { |
| 755 | insert_after(cbefore_begin(), __f, __l); |
| 756 | } |
| 757 | |
| 758 | template <class _Tp, class _Alloc> |
| 759 | forward_list<_Tp, _Alloc>::forward_list(const forward_list& __x) |
| 760 | : base(allocator_type( |
| 761 | __node_traits::select_on_container_copy_construction(__x.__alloc()) |
| 762 | ) |
| 763 | ) |
| 764 | { |
| 765 | insert_after(cbefore_begin(), __x.begin(), __x.end()); |
| 766 | } |
| 767 | |
| 768 | template <class _Tp, class _Alloc> |
| 769 | forward_list<_Tp, _Alloc>::forward_list(const forward_list& __x, |
| 770 | const allocator_type& __a) |
| 771 | : base(__a) |
| 772 | { |
| 773 | insert_after(cbefore_begin(), __x.begin(), __x.end()); |
| 774 | } |
| 775 | |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 776 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 777 | |
| 778 | template <class _Tp, class _Alloc> |
| 779 | forward_list<_Tp, _Alloc>::forward_list(forward_list&& __x, |
| 780 | const allocator_type& __a) |
| 781 | : base(_STD::move(__x), __a) |
| 782 | { |
| 783 | if (base::__alloc() != __x.__alloc()) |
| 784 | { |
| 785 | typedef move_iterator<iterator> _I; |
| 786 | insert_after(cbefore_begin(), _I(__x.begin()), _I(__x.end())); |
| 787 | } |
| 788 | } |
| 789 | |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 790 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 791 | |
| 792 | template <class _Tp, class _Alloc> |
| 793 | forward_list<_Tp, _Alloc>::forward_list(initializer_list<value_type> __il) |
| 794 | { |
| 795 | insert_after(cbefore_begin(), __il.begin(), __il.end()); |
| 796 | } |
| 797 | |
| 798 | template <class _Tp, class _Alloc> |
| 799 | forward_list<_Tp, _Alloc>::forward_list(initializer_list<value_type> __il, |
| 800 | const allocator_type& __a) |
| 801 | : base(__a) |
| 802 | { |
| 803 | insert_after(cbefore_begin(), __il.begin(), __il.end()); |
| 804 | } |
| 805 | |
| 806 | template <class _Tp, class _Alloc> |
| 807 | forward_list<_Tp, _Alloc>& |
| 808 | forward_list<_Tp, _Alloc>::operator=(const forward_list& __x) |
| 809 | { |
| 810 | if (this != &__x) |
| 811 | { |
| 812 | base::__copy_assign_alloc(__x); |
| 813 | assign(__x.begin(), __x.end()); |
| 814 | } |
| 815 | return *this; |
| 816 | } |
| 817 | |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 818 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 819 | |
| 820 | template <class _Tp, class _Alloc> |
| 821 | void |
| 822 | forward_list<_Tp, _Alloc>::__move_assign(forward_list& __x, true_type) |
| 823 | { |
| 824 | clear(); |
| 825 | base::__move_assign_alloc(__x); |
| 826 | base::__before_begin()->__next_ = __x.__before_begin()->__next_; |
| 827 | __x.__before_begin()->__next_ = nullptr; |
| 828 | } |
| 829 | |
| 830 | template <class _Tp, class _Alloc> |
| 831 | void |
| 832 | forward_list<_Tp, _Alloc>::__move_assign(forward_list& __x, false_type) |
| 833 | { |
| 834 | if (base::__alloc() == __x.__alloc()) |
| 835 | __move_assign(__x, true_type()); |
| 836 | else |
| 837 | { |
| 838 | typedef move_iterator<iterator> _I; |
| 839 | assign(_I(__x.begin()), _I(__x.end())); |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | template <class _Tp, class _Alloc> |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 844 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 845 | forward_list<_Tp, _Alloc>& |
| 846 | forward_list<_Tp, _Alloc>::operator=(forward_list&& __x) |
| 847 | { |
| 848 | __move_assign(__x, integral_constant<bool, |
| 849 | __node_traits::propagate_on_container_move_assignment::value>()); |
| 850 | return *this; |
| 851 | } |
| 852 | |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 853 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 854 | |
| 855 | template <class _Tp, class _Alloc> |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 856 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 857 | forward_list<_Tp, _Alloc>& |
| 858 | forward_list<_Tp, _Alloc>::operator=(initializer_list<value_type> __il) |
| 859 | { |
| 860 | assign(__il.begin(), __il.end()); |
| 861 | return *this; |
| 862 | } |
| 863 | |
| 864 | template <class _Tp, class _Alloc> |
| 865 | template <class _InputIterator> |
| 866 | typename enable_if |
| 867 | < |
| 868 | __is_input_iterator<_InputIterator>::value, |
| 869 | void |
| 870 | >::type |
| 871 | forward_list<_Tp, _Alloc>::assign(_InputIterator __f, _InputIterator __l) |
| 872 | { |
| 873 | iterator __i = before_begin(); |
Howard Hinnant | a0fe8c4 | 2011-02-14 19:12:38 +0000 | [diff] [blame^] | 874 | iterator __j = _STD::next(__i); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 875 | iterator __e = end(); |
| 876 | for (; __j != __e && __f != __l; ++__i, ++__j, ++__f) |
| 877 | *__j = *__f; |
| 878 | if (__j == __e) |
| 879 | insert_after(__i, __f, __l); |
| 880 | else |
| 881 | erase_after(__i, __e); |
| 882 | } |
| 883 | |
| 884 | template <class _Tp, class _Alloc> |
| 885 | void |
| 886 | forward_list<_Tp, _Alloc>::assign(size_type __n, const value_type& __v) |
| 887 | { |
| 888 | iterator __i = before_begin(); |
Howard Hinnant | a0fe8c4 | 2011-02-14 19:12:38 +0000 | [diff] [blame^] | 889 | iterator __j = _STD::next(__i); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 890 | iterator __e = end(); |
| 891 | for (; __j != __e && __n > 0; --__n, ++__i, ++__j) |
| 892 | *__j = __v; |
| 893 | if (__j == __e) |
| 894 | insert_after(__i, __n, __v); |
| 895 | else |
| 896 | erase_after(__i, __e); |
| 897 | } |
| 898 | |
| 899 | template <class _Tp, class _Alloc> |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 900 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 901 | void |
| 902 | forward_list<_Tp, _Alloc>::assign(initializer_list<value_type> __il) |
| 903 | { |
| 904 | assign(__il.begin(), __il.end()); |
| 905 | } |
| 906 | |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 907 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 908 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 909 | |
| 910 | template <class _Tp, class _Alloc> |
| 911 | template <class... _Args> |
| 912 | void |
| 913 | forward_list<_Tp, _Alloc>::emplace_front(_Args&&... __args) |
| 914 | { |
| 915 | __node_allocator& __a = base::__alloc(); |
| 916 | typedef __allocator_destructor<__node_allocator> _D; |
| 917 | unique_ptr<__node, _D> __h(__node_traits::allocate(__a, 1), _D(__a, 1)); |
Howard Hinnant | 72c5e14 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 918 | __node_traits::construct(__a, _STD::addressof(__h->__value_), |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 919 | _STD::forward<_Args>(__args)...); |
| 920 | __h->__next_ = base::__before_begin()->__next_; |
| 921 | base::__before_begin()->__next_ = __h.release(); |
| 922 | } |
| 923 | |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 924 | #endif // _LIBCPP_HAS_NO_VARIADICS |
| 925 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 926 | template <class _Tp, class _Alloc> |
| 927 | void |
| 928 | forward_list<_Tp, _Alloc>::push_front(value_type&& __v) |
| 929 | { |
| 930 | __node_allocator& __a = base::__alloc(); |
| 931 | typedef __allocator_destructor<__node_allocator> _D; |
| 932 | unique_ptr<__node, _D> __h(__node_traits::allocate(__a, 1), _D(__a, 1)); |
Howard Hinnant | 72c5e14 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 933 | __node_traits::construct(__a, _STD::addressof(__h->__value_), _STD::move(__v)); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 934 | __h->__next_ = base::__before_begin()->__next_; |
| 935 | base::__before_begin()->__next_ = __h.release(); |
| 936 | } |
| 937 | |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 938 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 939 | |
| 940 | template <class _Tp, class _Alloc> |
| 941 | void |
| 942 | forward_list<_Tp, _Alloc>::push_front(const value_type& __v) |
| 943 | { |
| 944 | __node_allocator& __a = base::__alloc(); |
| 945 | typedef __allocator_destructor<__node_allocator> _D; |
| 946 | unique_ptr<__node, _D> __h(__node_traits::allocate(__a, 1), _D(__a, 1)); |
Howard Hinnant | 72c5e14 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 947 | __node_traits::construct(__a, _STD::addressof(__h->__value_), __v); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 948 | __h->__next_ = base::__before_begin()->__next_; |
| 949 | base::__before_begin()->__next_ = __h.release(); |
| 950 | } |
| 951 | |
| 952 | template <class _Tp, class _Alloc> |
| 953 | void |
| 954 | forward_list<_Tp, _Alloc>::pop_front() |
| 955 | { |
| 956 | __node_allocator& __a = base::__alloc(); |
| 957 | __node_pointer __p = base::__before_begin()->__next_; |
| 958 | base::__before_begin()->__next_ = __p->__next_; |
Howard Hinnant | 72c5e14 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 959 | __node_traits::destroy(__a, _STD::addressof(__p->__value_)); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 960 | __node_traits::deallocate(__a, __p, 1); |
| 961 | } |
| 962 | |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 963 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 964 | #ifndef _LIBCPP_HAS_NO_VARIADICS |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 965 | |
| 966 | template <class _Tp, class _Alloc> |
| 967 | template <class... _Args> |
| 968 | typename forward_list<_Tp, _Alloc>::iterator |
| 969 | forward_list<_Tp, _Alloc>::emplace_after(const_iterator __p, _Args&&... __args) |
| 970 | { |
| 971 | __node_pointer const __r = const_cast<__node_pointer>(__p.__ptr_); |
| 972 | __node_allocator& __a = base::__alloc(); |
| 973 | typedef __allocator_destructor<__node_allocator> _D; |
| 974 | unique_ptr<__node, _D> __h(__node_traits::allocate(__a, 1), _D(__a, 1)); |
Howard Hinnant | 72c5e14 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 975 | __node_traits::construct(__a, _STD::addressof(__h->__value_), |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 976 | _STD::forward<_Args>(__args)...); |
| 977 | __h->__next_ = __r->__next_; |
| 978 | __r->__next_ = __h.release(); |
| 979 | return iterator(__r->__next_); |
| 980 | } |
| 981 | |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 982 | #endif // _LIBCPP_HAS_NO_VARIADICS |
| 983 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 984 | template <class _Tp, class _Alloc> |
| 985 | typename forward_list<_Tp, _Alloc>::iterator |
| 986 | forward_list<_Tp, _Alloc>::insert_after(const_iterator __p, value_type&& __v) |
| 987 | { |
| 988 | __node_pointer const __r = const_cast<__node_pointer>(__p.__ptr_); |
| 989 | __node_allocator& __a = base::__alloc(); |
| 990 | typedef __allocator_destructor<__node_allocator> _D; |
| 991 | unique_ptr<__node, _D> __h(__node_traits::allocate(__a, 1), _D(__a, 1)); |
Howard Hinnant | 72c5e14 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 992 | __node_traits::construct(__a, _STD::addressof(__h->__value_), _STD::move(__v)); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 993 | __h->__next_ = __r->__next_; |
| 994 | __r->__next_ = __h.release(); |
| 995 | return iterator(__r->__next_); |
| 996 | } |
| 997 | |
Howard Hinnant | 7609c9b | 2010-09-04 23:28:19 +0000 | [diff] [blame] | 998 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 999 | |
| 1000 | template <class _Tp, class _Alloc> |
| 1001 | typename forward_list<_Tp, _Alloc>::iterator |
| 1002 | forward_list<_Tp, _Alloc>::insert_after(const_iterator __p, const value_type& __v) |
| 1003 | { |
| 1004 | __node_pointer const __r = const_cast<__node_pointer>(__p.__ptr_); |
| 1005 | __node_allocator& __a = base::__alloc(); |
| 1006 | typedef __allocator_destructor<__node_allocator> _D; |
| 1007 | unique_ptr<__node, _D> __h(__node_traits::allocate(__a, 1), _D(__a, 1)); |
Howard Hinnant | 72c5e14 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1008 | __node_traits::construct(__a, _STD::addressof(__h->__value_), __v); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1009 | __h->__next_ = __r->__next_; |
| 1010 | __r->__next_ = __h.release(); |
| 1011 | return iterator(__r->__next_); |
| 1012 | } |
| 1013 | |
| 1014 | template <class _Tp, class _Alloc> |
| 1015 | typename forward_list<_Tp, _Alloc>::iterator |
| 1016 | forward_list<_Tp, _Alloc>::insert_after(const_iterator __p, size_type __n, |
| 1017 | const value_type& __v) |
| 1018 | { |
Howard Hinnant | e57dc14 | 2010-08-19 17:40:04 +0000 | [diff] [blame] | 1019 | __node_pointer __r = const_cast<__node_pointer>(__p.__ptr_); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1020 | if (__n > 0) |
| 1021 | { |
| 1022 | __node_allocator& __a = base::__alloc(); |
| 1023 | typedef __allocator_destructor<__node_allocator> _D; |
| 1024 | unique_ptr<__node, _D> __h(__node_traits::allocate(__a, 1), _D(__a, 1)); |
Howard Hinnant | 72c5e14 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1025 | __node_traits::construct(__a, _STD::addressof(__h->__value_), __v); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1026 | __node_pointer __first = __h.release(); |
| 1027 | __node_pointer __last = __first; |
| 1028 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1029 | try |
| 1030 | { |
Howard Hinnant | b3371f6 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1031 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1032 | for (--__n; __n != 0; --__n, __last = __last->__next_) |
| 1033 | { |
| 1034 | __h.reset(__node_traits::allocate(__a, 1)); |
Howard Hinnant | 72c5e14 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1035 | __node_traits::construct(__a, _STD::addressof(__h->__value_), __v); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1036 | __last->__next_ = __h.release(); |
| 1037 | } |
| 1038 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1039 | } |
| 1040 | catch (...) |
| 1041 | { |
| 1042 | while (__first != nullptr) |
| 1043 | { |
| 1044 | __node_pointer __next = __first->__next_; |
Howard Hinnant | 72c5e14 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1045 | __node_traits::destroy(__a, _STD::addressof(__first->__value_)); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1046 | __node_traits::deallocate(__a, __first, 1); |
| 1047 | __first = __next; |
| 1048 | } |
| 1049 | throw; |
| 1050 | } |
Howard Hinnant | b3371f6 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1051 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1052 | __last->__next_ = __r->__next_; |
| 1053 | __r->__next_ = __first; |
Howard Hinnant | e57dc14 | 2010-08-19 17:40:04 +0000 | [diff] [blame] | 1054 | __r = __last; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1055 | } |
| 1056 | return iterator(__r); |
| 1057 | } |
| 1058 | |
| 1059 | template <class _Tp, class _Alloc> |
| 1060 | template <class _InputIterator> |
| 1061 | typename enable_if |
| 1062 | < |
| 1063 | __is_input_iterator<_InputIterator>::value, |
| 1064 | typename forward_list<_Tp, _Alloc>::iterator |
| 1065 | >::type |
| 1066 | forward_list<_Tp, _Alloc>::insert_after(const_iterator __p, |
| 1067 | _InputIterator __f, _InputIterator __l) |
| 1068 | { |
Howard Hinnant | e57dc14 | 2010-08-19 17:40:04 +0000 | [diff] [blame] | 1069 | __node_pointer __r = const_cast<__node_pointer>(__p.__ptr_); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1070 | if (__f != __l) |
| 1071 | { |
| 1072 | __node_allocator& __a = base::__alloc(); |
| 1073 | typedef __allocator_destructor<__node_allocator> _D; |
| 1074 | unique_ptr<__node, _D> __h(__node_traits::allocate(__a, 1), _D(__a, 1)); |
Howard Hinnant | 72c5e14 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1075 | __node_traits::construct(__a, _STD::addressof(__h->__value_), *__f); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1076 | __node_pointer __first = __h.release(); |
| 1077 | __node_pointer __last = __first; |
| 1078 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1079 | try |
| 1080 | { |
Howard Hinnant | b3371f6 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1081 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1082 | for (++__f; __f != __l; ++__f, __last = __last->__next_) |
| 1083 | { |
| 1084 | __h.reset(__node_traits::allocate(__a, 1)); |
Howard Hinnant | 72c5e14 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1085 | __node_traits::construct(__a, _STD::addressof(__h->__value_), *__f); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1086 | __last->__next_ = __h.release(); |
| 1087 | } |
| 1088 | #ifndef _LIBCPP_NO_EXCEPTIONS |
| 1089 | } |
| 1090 | catch (...) |
| 1091 | { |
| 1092 | while (__first != nullptr) |
| 1093 | { |
| 1094 | __node_pointer __next = __first->__next_; |
Howard Hinnant | 72c5e14 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1095 | __node_traits::destroy(__a, _STD::addressof(__first->__value_)); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1096 | __node_traits::deallocate(__a, __first, 1); |
| 1097 | __first = __next; |
| 1098 | } |
| 1099 | throw; |
| 1100 | } |
Howard Hinnant | b3371f6 | 2010-08-22 00:02:43 +0000 | [diff] [blame] | 1101 | #endif // _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1102 | __last->__next_ = __r->__next_; |
| 1103 | __r->__next_ = __first; |
Howard Hinnant | e57dc14 | 2010-08-19 17:40:04 +0000 | [diff] [blame] | 1104 | __r = __last; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1105 | } |
| 1106 | return iterator(__r); |
| 1107 | } |
| 1108 | |
| 1109 | template <class _Tp, class _Alloc> |
Howard Hinnant | 3db8803 | 2010-08-21 20:58:44 +0000 | [diff] [blame] | 1110 | typename forward_list<_Tp, _Alloc>::iterator |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1111 | forward_list<_Tp, _Alloc>::erase_after(const_iterator __f) |
| 1112 | { |
| 1113 | __node_pointer __p = const_cast<__node_pointer>(__f.__ptr_); |
| 1114 | __node_pointer __n = __p->__next_; |
| 1115 | __p->__next_ = __n->__next_; |
| 1116 | __node_allocator& __a = base::__alloc(); |
Howard Hinnant | 72c5e14 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1117 | __node_traits::destroy(__a, _STD::addressof(__n->__value_)); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1118 | __node_traits::deallocate(__a, __n, 1); |
Howard Hinnant | 3db8803 | 2010-08-21 20:58:44 +0000 | [diff] [blame] | 1119 | return iterator(__p->__next_); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | template <class _Tp, class _Alloc> |
Howard Hinnant | 3db8803 | 2010-08-21 20:58:44 +0000 | [diff] [blame] | 1123 | typename forward_list<_Tp, _Alloc>::iterator |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1124 | forward_list<_Tp, _Alloc>::erase_after(const_iterator __f, const_iterator __l) |
| 1125 | { |
Howard Hinnant | 3db8803 | 2010-08-21 20:58:44 +0000 | [diff] [blame] | 1126 | __node_pointer __e = const_cast<__node_pointer>(__l.__ptr_); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1127 | if (__f != __l) |
| 1128 | { |
| 1129 | __node_pointer __p = const_cast<__node_pointer>(__f.__ptr_); |
| 1130 | __node_pointer __n = __p->__next_; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1131 | if (__n != __e) |
| 1132 | { |
| 1133 | __p->__next_ = __e; |
| 1134 | __node_allocator& __a = base::__alloc(); |
| 1135 | do |
| 1136 | { |
| 1137 | __p = __n->__next_; |
Howard Hinnant | 72c5e14 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1138 | __node_traits::destroy(__a, _STD::addressof(__n->__value_)); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1139 | __node_traits::deallocate(__a, __n, 1); |
| 1140 | __n = __p; |
| 1141 | } while (__n != __e); |
| 1142 | } |
| 1143 | } |
Howard Hinnant | 3db8803 | 2010-08-21 20:58:44 +0000 | [diff] [blame] | 1144 | return iterator(__e); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | template <class _Tp, class _Alloc> |
| 1148 | void |
| 1149 | forward_list<_Tp, _Alloc>::resize(size_type __n) |
| 1150 | { |
| 1151 | size_type __sz = 0; |
| 1152 | iterator __p = before_begin(); |
| 1153 | iterator __i = begin(); |
| 1154 | iterator __e = end(); |
| 1155 | for (; __i != __e && __sz < __n; ++__p, ++__i, ++__sz) |
| 1156 | ; |
| 1157 | if (__i != __e) |
| 1158 | erase_after(__p, __e); |
| 1159 | else |
| 1160 | { |
| 1161 | __n -= __sz; |
| 1162 | if (__n > 0) |
| 1163 | { |
| 1164 | __node_allocator& __a = base::__alloc(); |
| 1165 | typedef __allocator_destructor<__node_allocator> _D; |
| 1166 | unique_ptr<__node, _D> __h(nullptr, _D(__a, 1)); |
| 1167 | for (__node_pointer __ptr = __p.__ptr_; __n > 0; --__n, |
| 1168 | __ptr = __ptr->__next_) |
| 1169 | { |
| 1170 | __h.reset(__node_traits::allocate(__a, 1)); |
Howard Hinnant | 72c5e14 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1171 | __node_traits::construct(__a, _STD::addressof(__h->__value_)); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1172 | __h->__next_ = nullptr; |
| 1173 | __ptr->__next_ = __h.release(); |
| 1174 | } |
| 1175 | } |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | template <class _Tp, class _Alloc> |
| 1180 | void |
| 1181 | forward_list<_Tp, _Alloc>::resize(size_type __n, const value_type& __v) |
| 1182 | { |
| 1183 | size_type __sz = 0; |
| 1184 | iterator __p = before_begin(); |
| 1185 | iterator __i = begin(); |
| 1186 | iterator __e = end(); |
| 1187 | for (; __i != __e && __sz < __n; ++__p, ++__i, ++__sz) |
| 1188 | ; |
| 1189 | if (__i != __e) |
| 1190 | erase_after(__p, __e); |
| 1191 | else |
| 1192 | { |
| 1193 | __n -= __sz; |
| 1194 | if (__n > 0) |
| 1195 | { |
| 1196 | __node_allocator& __a = base::__alloc(); |
| 1197 | typedef __allocator_destructor<__node_allocator> _D; |
| 1198 | unique_ptr<__node, _D> __h(nullptr, _D(__a, 1)); |
| 1199 | for (__node_pointer __ptr = __p.__ptr_; __n > 0; --__n, |
| 1200 | __ptr = __ptr->__next_) |
| 1201 | { |
| 1202 | __h.reset(__node_traits::allocate(__a, 1)); |
Howard Hinnant | 72c5e14 | 2011-02-02 17:36:20 +0000 | [diff] [blame] | 1203 | __node_traits::construct(__a, _STD::addressof(__h->__value_), __v); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1204 | __h->__next_ = nullptr; |
| 1205 | __ptr->__next_ = __h.release(); |
| 1206 | } |
| 1207 | } |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | template <class _Tp, class _Alloc> |
| 1212 | void |
| 1213 | forward_list<_Tp, _Alloc>::splice_after(const_iterator __p, |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1214 | forward_list& __x) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1215 | { |
| 1216 | if (!__x.empty()) |
| 1217 | { |
| 1218 | if (__p.__ptr_->__next_ != nullptr) |
| 1219 | { |
| 1220 | const_iterator __lm1 = __x.before_begin(); |
| 1221 | while (__lm1.__ptr_->__next_ != nullptr) |
| 1222 | ++__lm1; |
| 1223 | const_cast<__node_pointer>(__lm1.__ptr_)->__next_ = |
| 1224 | const_cast<__node_pointer>(__p.__ptr_)->__next_; |
| 1225 | } |
| 1226 | const_cast<__node_pointer>(__p.__ptr_)->__next_ = |
| 1227 | const_cast<__node_pointer>(__x.__before_begin())->__next_; |
| 1228 | const_cast<__node_pointer>(__x.__before_begin())->__next_ = nullptr; |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | template <class _Tp, class _Alloc> |
| 1233 | void |
| 1234 | forward_list<_Tp, _Alloc>::splice_after(const_iterator __p, |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1235 | forward_list& __x, |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1236 | const_iterator __i) |
| 1237 | { |
Howard Hinnant | a0fe8c4 | 2011-02-14 19:12:38 +0000 | [diff] [blame^] | 1238 | const_iterator __lm1 = _STD::next(__i); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1239 | if (__p != __i && __p != __lm1) |
| 1240 | { |
| 1241 | const_cast<__node_pointer>(__i.__ptr_)->__next_ = |
| 1242 | const_cast<__node_pointer>(__lm1.__ptr_)->__next_; |
| 1243 | const_cast<__node_pointer>(__lm1.__ptr_)->__next_ = |
| 1244 | const_cast<__node_pointer>(__p.__ptr_)->__next_; |
| 1245 | const_cast<__node_pointer>(__p.__ptr_)->__next_ = |
| 1246 | const_cast<__node_pointer>(__lm1.__ptr_); |
| 1247 | } |
| 1248 | } |
| 1249 | |
| 1250 | template <class _Tp, class _Alloc> |
| 1251 | void |
| 1252 | forward_list<_Tp, _Alloc>::splice_after(const_iterator __p, |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1253 | forward_list& __x, |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1254 | const_iterator __f, const_iterator __l) |
| 1255 | { |
| 1256 | if (__f != __l && __p != __f) |
| 1257 | { |
| 1258 | const_iterator __lm1 = __f; |
| 1259 | while (__lm1.__ptr_->__next_ != __l.__ptr_) |
| 1260 | ++__lm1; |
| 1261 | if (__f != __lm1) |
| 1262 | { |
| 1263 | const_cast<__node_pointer>(__lm1.__ptr_)->__next_ = |
| 1264 | const_cast<__node_pointer>(__p.__ptr_)->__next_; |
| 1265 | const_cast<__node_pointer>(__p.__ptr_)->__next_ = |
| 1266 | const_cast<__node_pointer>(__f.__ptr_)->__next_; |
| 1267 | const_cast<__node_pointer>(__f.__ptr_)->__next_ = |
| 1268 | const_cast<__node_pointer>(__l.__ptr_); |
| 1269 | } |
| 1270 | } |
| 1271 | } |
| 1272 | |
Howard Hinnant | eb92df7 | 2011-01-27 21:00:35 +0000 | [diff] [blame] | 1273 | #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 1274 | |
| 1275 | template <class _Tp, class _Alloc> |
| 1276 | inline _LIBCPP_INLINE_VISIBILITY |
| 1277 | void |
| 1278 | forward_list<_Tp, _Alloc>::splice_after(const_iterator __p, |
| 1279 | forward_list&& __x) |
| 1280 | { |
| 1281 | splice_after(__p, __x); |
| 1282 | } |
| 1283 | |
| 1284 | template <class _Tp, class _Alloc> |
| 1285 | inline _LIBCPP_INLINE_VISIBILITY |
| 1286 | void |
| 1287 | forward_list<_Tp, _Alloc>::splice_after(const_iterator __p, |
| 1288 | forward_list&& __x, |
| 1289 | const_iterator __i) |
| 1290 | { |
| 1291 | splice_after(__p, __x, __i); |
| 1292 | } |
| 1293 | |
| 1294 | template <class _Tp, class _Alloc> |
| 1295 | inline _LIBCPP_INLINE_VISIBILITY |
| 1296 | void |
| 1297 | forward_list<_Tp, _Alloc>::splice_after(const_iterator __p, |
| 1298 | forward_list&& __x, |
| 1299 | const_iterator __f, const_iterator __l) |
| 1300 | { |
| 1301 | splice_after(__p, __x, __f, __l); |
| 1302 | } |
| 1303 | |
| 1304 | #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES |
| 1305 | |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1306 | template <class _Tp, class _Alloc> |
| 1307 | void |
| 1308 | forward_list<_Tp, _Alloc>::remove(const value_type& __v) |
| 1309 | { |
| 1310 | iterator __e = end(); |
| 1311 | for (iterator __i = before_begin(); __i.__ptr_->__next_ != nullptr;) |
| 1312 | { |
| 1313 | if (__i.__ptr_->__next_->__value_ == __v) |
| 1314 | { |
Howard Hinnant | a0fe8c4 | 2011-02-14 19:12:38 +0000 | [diff] [blame^] | 1315 | iterator __j = _STD::next(__i, 2); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1316 | for (; __j != __e && *__j == __v; ++__j) |
| 1317 | ; |
| 1318 | erase_after(__i, __j); |
| 1319 | if (__j == __e) |
| 1320 | break; |
| 1321 | __i = __j; |
| 1322 | } |
| 1323 | else |
| 1324 | ++__i; |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | template <class _Tp, class _Alloc> |
| 1329 | template <class _Predicate> |
| 1330 | void |
| 1331 | forward_list<_Tp, _Alloc>::remove_if(_Predicate __pred) |
| 1332 | { |
| 1333 | iterator __e = end(); |
| 1334 | for (iterator __i = before_begin(); __i.__ptr_->__next_ != nullptr;) |
| 1335 | { |
| 1336 | if (__pred(__i.__ptr_->__next_->__value_)) |
| 1337 | { |
Howard Hinnant | a0fe8c4 | 2011-02-14 19:12:38 +0000 | [diff] [blame^] | 1338 | iterator __j = _STD::next(__i, 2); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1339 | for (; __j != __e && __pred(*__j); ++__j) |
| 1340 | ; |
| 1341 | erase_after(__i, __j); |
| 1342 | if (__j == __e) |
| 1343 | break; |
| 1344 | __i = __j; |
| 1345 | } |
| 1346 | else |
| 1347 | ++__i; |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | template <class _Tp, class _Alloc> |
| 1352 | template <class _BinaryPredicate> |
| 1353 | void |
| 1354 | forward_list<_Tp, _Alloc>::unique(_BinaryPredicate __binary_pred) |
| 1355 | { |
| 1356 | for (iterator __i = begin(), __e = end(); __i != __e;) |
| 1357 | { |
Howard Hinnant | a0fe8c4 | 2011-02-14 19:12:38 +0000 | [diff] [blame^] | 1358 | iterator __j = _STD::next(__i); |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1359 | for (; __j != __e && __binary_pred(*__i, *__j); ++__j) |
| 1360 | ; |
| 1361 | if (__i.__ptr_->__next_ != __j.__ptr_) |
| 1362 | erase_after(__i, __j); |
| 1363 | __i = __j; |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | template <class _Tp, class _Alloc> |
| 1368 | template <class _Compare> |
| 1369 | void |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1370 | forward_list<_Tp, _Alloc>::merge(forward_list& __x, _Compare __comp) |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1371 | { |
| 1372 | if (this != &__x) |
| 1373 | { |
| 1374 | base::__before_begin()->__next_ = __merge(base::__before_begin()->__next_, |
| 1375 | __x.__before_begin()->__next_, |
| 1376 | __comp); |
| 1377 | __x.__before_begin()->__next_ = nullptr; |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | template <class _Tp, class _Alloc> |
| 1382 | template <class _Compare> |
| 1383 | typename forward_list<_Tp, _Alloc>::__node_pointer |
| 1384 | forward_list<_Tp, _Alloc>::__merge(__node_pointer __f1, __node_pointer __f2, |
| 1385 | _Compare& __comp) |
| 1386 | { |
| 1387 | if (__f1 == nullptr) |
| 1388 | return __f2; |
| 1389 | if (__f2 == nullptr) |
| 1390 | return __f1; |
| 1391 | __node_pointer __r; |
| 1392 | if (__comp(__f2->__value_, __f1->__value_)) |
| 1393 | { |
| 1394 | __node_pointer __t = __f2; |
| 1395 | while (__t->__next_ != nullptr && |
| 1396 | __comp(__t->__next_->__value_, __f1->__value_)) |
| 1397 | __t = __t->__next_; |
| 1398 | __r = __f2; |
| 1399 | __f2 = __t->__next_; |
| 1400 | __t->__next_ = __f1; |
| 1401 | } |
| 1402 | else |
| 1403 | __r = __f1; |
| 1404 | __node_pointer __p = __f1; |
| 1405 | __f1 = __f1->__next_; |
| 1406 | while (__f1 != nullptr && __f2 != nullptr) |
| 1407 | { |
| 1408 | if (__comp(__f2->__value_, __f1->__value_)) |
| 1409 | { |
| 1410 | __node_pointer __t = __f2; |
| 1411 | while (__t->__next_ != nullptr && |
| 1412 | __comp(__t->__next_->__value_, __f1->__value_)) |
| 1413 | __t = __t->__next_; |
| 1414 | __p->__next_ = __f2; |
| 1415 | __f2 = __t->__next_; |
| 1416 | __t->__next_ = __f1; |
| 1417 | } |
| 1418 | __p = __f1; |
| 1419 | __f1 = __f1->__next_; |
| 1420 | } |
| 1421 | if (__f2 != nullptr) |
| 1422 | __p->__next_ = __f2; |
| 1423 | return __r; |
| 1424 | } |
| 1425 | |
| 1426 | template <class _Tp, class _Alloc> |
| 1427 | template <class _Compare> |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 1428 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1429 | void |
| 1430 | forward_list<_Tp, _Alloc>::sort(_Compare __comp) |
| 1431 | { |
| 1432 | base::__before_begin()->__next_ = __sort(base::__before_begin()->__next_, |
| 1433 | _STD::distance(begin(), end()), __comp); |
| 1434 | } |
| 1435 | |
| 1436 | template <class _Tp, class _Alloc> |
| 1437 | template <class _Compare> |
| 1438 | typename forward_list<_Tp, _Alloc>::__node_pointer |
| 1439 | forward_list<_Tp, _Alloc>::__sort(__node_pointer __f1, difference_type __sz, |
| 1440 | _Compare& __comp) |
| 1441 | { |
| 1442 | switch (__sz) |
| 1443 | { |
| 1444 | case 0: |
| 1445 | case 1: |
| 1446 | return __f1; |
| 1447 | case 2: |
| 1448 | if (__comp(__f1->__next_->__value_, __f1->__value_)) |
| 1449 | { |
| 1450 | __node_pointer __t = __f1->__next_; |
| 1451 | __t->__next_ = __f1; |
| 1452 | __f1->__next_ = nullptr; |
| 1453 | __f1 = __t; |
| 1454 | } |
| 1455 | return __f1; |
| 1456 | } |
| 1457 | difference_type __sz1 = __sz / 2; |
| 1458 | difference_type __sz2 = __sz - __sz1; |
Howard Hinnant | a0fe8c4 | 2011-02-14 19:12:38 +0000 | [diff] [blame^] | 1459 | __node_pointer __t = _STD::next(iterator(__f1), __sz1 - 1).__ptr_; |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1460 | __node_pointer __f2 = __t->__next_; |
| 1461 | __t->__next_ = nullptr; |
| 1462 | return __merge(__sort(__f1, __sz1, __comp), |
| 1463 | __sort(__f2, __sz2, __comp), __comp); |
| 1464 | } |
| 1465 | |
| 1466 | template <class _Tp, class _Alloc> |
| 1467 | void |
| 1468 | forward_list<_Tp, _Alloc>::reverse() |
| 1469 | { |
| 1470 | __node_pointer __p = base::__before_begin()->__next_; |
| 1471 | if (__p != nullptr) |
| 1472 | { |
| 1473 | __node_pointer __f = __p->__next_; |
| 1474 | __p->__next_ = nullptr; |
| 1475 | while (__f != nullptr) |
| 1476 | { |
| 1477 | __node_pointer __t = __f->__next_; |
| 1478 | __f->__next_ = __p; |
| 1479 | __p = __f; |
| 1480 | __f = __t; |
| 1481 | } |
| 1482 | base::__before_begin()->__next_ = __p; |
| 1483 | } |
| 1484 | } |
| 1485 | |
| 1486 | template <class _Tp, class _Alloc> |
| 1487 | bool operator==(const forward_list<_Tp, _Alloc>& __x, |
| 1488 | const forward_list<_Tp, _Alloc>& __y) |
| 1489 | { |
| 1490 | typedef forward_list<_Tp, _Alloc> _C; |
| 1491 | typedef typename _C::const_iterator _I; |
| 1492 | _I __ix = __x.begin(); |
| 1493 | _I __ex = __x.end(); |
| 1494 | _I __iy = __y.begin(); |
| 1495 | _I __ey = __y.end(); |
| 1496 | for (; __ix != __ex && __iy != __ey; ++__ix, ++__iy) |
| 1497 | if (!(*__ix == *__iy)) |
| 1498 | return false; |
| 1499 | return (__ix == __ex) == (__iy == __ey); |
| 1500 | } |
| 1501 | |
| 1502 | template <class _Tp, class _Alloc> |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 1503 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1504 | bool operator!=(const forward_list<_Tp, _Alloc>& __x, |
| 1505 | const forward_list<_Tp, _Alloc>& __y) |
| 1506 | { |
| 1507 | return !(__x == __y); |
| 1508 | } |
| 1509 | |
| 1510 | template <class _Tp, class _Alloc> |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 1511 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1512 | bool operator< (const forward_list<_Tp, _Alloc>& __x, |
| 1513 | const forward_list<_Tp, _Alloc>& __y) |
| 1514 | { |
| 1515 | return _STD::lexicographical_compare(__x.begin(), __x.end(), |
| 1516 | __y.begin(), __y.end()); |
| 1517 | } |
| 1518 | |
| 1519 | template <class _Tp, class _Alloc> |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 1520 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1521 | bool operator> (const forward_list<_Tp, _Alloc>& __x, |
| 1522 | const forward_list<_Tp, _Alloc>& __y) |
| 1523 | { |
| 1524 | return __y < __x; |
| 1525 | } |
| 1526 | |
| 1527 | template <class _Tp, class _Alloc> |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 1528 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1529 | bool operator>=(const forward_list<_Tp, _Alloc>& __x, |
| 1530 | const forward_list<_Tp, _Alloc>& __y) |
| 1531 | { |
| 1532 | return !(__x < __y); |
| 1533 | } |
| 1534 | |
| 1535 | template <class _Tp, class _Alloc> |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 1536 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1537 | bool operator<=(const forward_list<_Tp, _Alloc>& __x, |
| 1538 | const forward_list<_Tp, _Alloc>& __y) |
| 1539 | { |
| 1540 | return !(__y < __x); |
| 1541 | } |
| 1542 | |
| 1543 | template <class _Tp, class _Alloc> |
Howard Hinnant | 0af133f | 2010-09-21 22:55:27 +0000 | [diff] [blame] | 1544 | inline _LIBCPP_INLINE_VISIBILITY |
Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1545 | void |
| 1546 | swap(forward_list<_Tp, _Alloc>& __x, forward_list<_Tp, _Alloc>& __y) |
| 1547 | { |
| 1548 | __x.swap(__y); |
| 1549 | } |
| 1550 | |
| 1551 | _LIBCPP_END_NAMESPACE_STD |
| 1552 | |
| 1553 | #endif // _LIBCPP_FORWARD_LIST |