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