blob: d5c6ca5254ef781cb40eab6221274059ab75cf85 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- list ------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_LIST
12#define _LIBCPP_LIST
13
14/*
15 list synopsis
16
17namespace std
18{
19
20template <class T, class Alloc = allocator<T> >
21class list
22{
23public:
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
143template <class T, class Alloc>
144 bool operator==(const list<T,Alloc>& x, const list<T,Alloc>& y);
145template <class T, class Alloc>
146 bool operator< (const list<T,Alloc>& x, const list<T,Alloc>& y);
147template <class T, class Alloc>
148 bool operator!=(const list<T,Alloc>& x, const list<T,Alloc>& y);
149template <class T, class Alloc>
150 bool operator> (const list<T,Alloc>& x, const list<T,Alloc>& y);
151template <class T, class Alloc>
152 bool operator>=(const list<T,Alloc>& x, const list<T,Alloc>& y);
153template <class T, class Alloc>
154 bool operator<=(const list<T,Alloc>& x, const list<T,Alloc>& y);
155
156template <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
175template <class, class> struct __list_node;
176
177template <class _Tp, class _VoidPtr>
178struct __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 Hinnant82894812010-09-22 16:48:34 +0000190 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000191 __list_node_base()
192 : __prev_(static_cast<pointer>(this)),
193 __next_(static_cast<pointer>(this))
194 {}
195};
196
197template <class _Tp, class _VoidPtr>
198struct __list_node
199 : public __list_node_base<_Tp, _VoidPtr>
200{
201 _Tp __value_;
202};
203
204template <class, class> class list;
205template <class, class> class __list_imp;
206template <class, class> class __list_const_iterator;
207
208template <class _Tp, class _VoidPtr>
Howard Hinnant82894812010-09-22 16:48:34 +0000209class _LIBCPP_VISIBLE __list_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000210{
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 Hinnant82894812010-09-22 16:48:34 +0000220 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000221 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;
226public:
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 Hinnant82894812010-09-22 16:48:34 +0000239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant211f0ee2011-01-28 23:46:28 +0000240 __list_iterator() {}
241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000242 reference operator*() const {return __ptr_->__value_;}
Howard Hinnant82894812010-09-22 16:48:34 +0000243 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000244 pointer operator->() const {return &(operator*());}
245
Howard Hinnant82894812010-09-22 16:48:34 +0000246 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000247 __list_iterator& operator++() {__ptr_ = __ptr_->__next_; return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +0000248 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000249 __list_iterator operator++(int) {__list_iterator __t(*this); ++(*this); return __t;}
250
Howard Hinnant82894812010-09-22 16:48:34 +0000251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000252 __list_iterator& operator--() {__ptr_ = __ptr_->__prev_; return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +0000253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000254 __list_iterator operator--(int) {__list_iterator __t(*this); --(*this); return __t;}
255
Howard Hinnant82894812010-09-22 16:48:34 +0000256 friend _LIBCPP_INLINE_VISIBILITY
257 bool operator==(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000258 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +0000259 friend _LIBCPP_INLINE_VISIBILITY
260 bool operator!=(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000261 {return !(__x == __y);}
262};
263
264template <class _Tp, class _VoidPtr>
Howard Hinnant82894812010-09-22 16:48:34 +0000265class _LIBCPP_VISIBLE __list_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000266{
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 Hinnant82894812010-09-22 16:48:34 +0000276 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000277 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;
281public:
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 Hinnant82894812010-09-22 16:48:34 +0000294 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant211f0ee2011-01-28 23:46:28 +0000295 __list_const_iterator() {}
296 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000297 __list_const_iterator(__list_iterator<_Tp, _VoidPtr> __p) : __ptr_(__p.__ptr_) {}
298
Howard Hinnant82894812010-09-22 16:48:34 +0000299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000300 reference operator*() const {return __ptr_->__value_;}
Howard Hinnant82894812010-09-22 16:48:34 +0000301 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000302 pointer operator->() const {return &(operator*());}
303
Howard Hinnant82894812010-09-22 16:48:34 +0000304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000305 __list_const_iterator& operator++() {__ptr_ = __ptr_->__next_; return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +0000306 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000307 __list_const_iterator operator++(int) {__list_const_iterator __t(*this); ++(*this); return __t;}
308
Howard Hinnant82894812010-09-22 16:48:34 +0000309 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000310 __list_const_iterator& operator--() {__ptr_ = __ptr_->__prev_; return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +0000311 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000312 __list_const_iterator operator--(int) {__list_const_iterator __t(*this); --(*this); return __t;}
313
Howard Hinnant82894812010-09-22 16:48:34 +0000314 friend _LIBCPP_INLINE_VISIBILITY
315 bool operator==(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000316 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +0000317 friend _LIBCPP_INLINE_VISIBILITY
318 bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000319 {return !(__x == __y);}
320};
321
322template <class _Tp, class _Alloc>
323class __list_imp
324{
325 __list_imp(const __list_imp&);
326 __list_imp& operator=(const __list_imp&);
327protected:
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 Hinnant82894812010-09-22 16:48:34 +0000354 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000355 size_type& __sz() {return __size_alloc_.first();}
Howard Hinnant82894812010-09-22 16:48:34 +0000356 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000357 const size_type& __sz() const {return __size_alloc_.first();}
Howard Hinnant82894812010-09-22 16:48:34 +0000358 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000359 __node_allocator& __node_alloc() {return __size_alloc_.second();}
Howard Hinnant82894812010-09-22 16:48:34 +0000360 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000361 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 Hinnant82894812010-09-22 16:48:34 +0000369 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000370 bool empty() const {return __sz() == 0;}
371
Howard Hinnant82894812010-09-22 16:48:34 +0000372 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000373 iterator begin() {return iterator(__end_.__next_);}
Howard Hinnant82894812010-09-22 16:48:34 +0000374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000375 const_iterator begin() const {return const_iterator(__end_.__next_);}
Howard Hinnant82894812010-09-22 16:48:34 +0000376 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000377 iterator end() {return iterator(static_cast<__node_pointer> (&__end_));}
Howard Hinnant82894812010-09-22 16:48:34 +0000378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000379 const_iterator end() const {return const_iterator(static_cast<__node_const_pointer>(&__end_));}
380
381 void swap(__list_imp& __c);
382
Howard Hinnant82894812010-09-22 16:48:34 +0000383 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000384 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 Hinnant82894812010-09-22 16:48:34 +0000388 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000389 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
393private:
Howard Hinnant82894812010-09-22 16:48:34 +0000394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000395 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 Hinnant82894812010-09-22 16:48:34 +0000398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000399 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, true_type)
400 {
401 using _STD::swap;
402 swap(__x, __y);
403 }
Howard Hinnant82894812010-09-22 16:48:34 +0000404 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000405 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, false_type)
406 {}
407
Howard Hinnant82894812010-09-22 16:48:34 +0000408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000409 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 Hinnant82894812010-09-22 16:48:34 +0000416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000417 void __copy_assign_alloc(const __list_imp& __c, false_type)
418 {}
419
Howard Hinnant82894812010-09-22 16:48:34 +0000420 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000421 void __move_assign_alloc(const __list_imp& __c, true_type)
422 {
423 __node_alloc() = _STD::move(__c.__node_alloc());
424 }
425
Howard Hinnant82894812010-09-22 16:48:34 +0000426 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000427 void __move_assign_alloc(const __list_imp& __c, false_type)
428 {}
429};
430
431// Unlink nodes [__f, __l]
432template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000433inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000434void
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
441template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000442inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000443__list_imp<_Tp, _Alloc>::__list_imp()
444 : __size_alloc_(0)
445{
446}
447
448template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000449inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000450__list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a)
451 : __size_alloc_(0, __node_allocator(__a))
452{
453}
454
455template <class _Tp, class _Alloc>
456__list_imp<_Tp, _Alloc>::~__list_imp()
457{
458 clear();
459}
460
461template <class _Tp, class _Alloc>
462void
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
482template <class _Tp, class _Alloc>
483void
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
503template <class _Tp, class _Alloc = allocator<_Tp> >
Howard Hinnant82894812010-09-22 16:48:34 +0000504class _LIBCPP_VISIBLE list
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000505 : 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
513public:
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 Hinnant82894812010-09-22 16:48:34 +0000529 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000530 list() {}
Howard Hinnant82894812010-09-22 16:48:34 +0000531 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000532 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 Hinnant73d21a42010-09-04 23:28:19 +0000548#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000549 list(list&& __c);
550 list(list&& __c, const allocator_type& __a);
551 list& operator=(list&& __c);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000552#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +0000553 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000554 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 Hinnant82894812010-09-22 16:48:34 +0000561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000562 void assign(initializer_list<value_type> __il)
563 {assign(__il.begin(), __il.end());}
564
565 allocator_type get_allocator() const;
566
Howard Hinnant82894812010-09-22 16:48:34 +0000567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000568 size_type size() const {return base::__sz();}
Howard Hinnant82894812010-09-22 16:48:34 +0000569 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000570 bool empty() const {return base::empty();}
Howard Hinnant82894812010-09-22 16:48:34 +0000571 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000572 size_type max_size() const {return numeric_limits<difference_type>::max();}
573
Howard Hinnant82894812010-09-22 16:48:34 +0000574 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000575 iterator begin() {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000577 const_iterator begin() const {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000578 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000579 iterator end() {return base::end();}
Howard Hinnant82894812010-09-22 16:48:34 +0000580 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000581 const_iterator end() const {return base::end();}
Howard Hinnant82894812010-09-22 16:48:34 +0000582 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000583 const_iterator cbegin() const {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000584 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000585 const_iterator cend() const {return base::end();}
586
Howard Hinnant82894812010-09-22 16:48:34 +0000587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000588 reverse_iterator rbegin() {return reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +0000589 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000590 const_reverse_iterator rbegin() const {return const_reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +0000591 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000592 reverse_iterator rend() {return reverse_iterator(begin());}
Howard Hinnant82894812010-09-22 16:48:34 +0000593 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000594 const_reverse_iterator rend() const {return const_reverse_iterator(begin());}
Howard Hinnant82894812010-09-22 16:48:34 +0000595 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000596 const_reverse_iterator crbegin() const {return const_reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +0000597 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000598 const_reverse_iterator crend() const {return const_reverse_iterator(begin());}
599
Howard Hinnant82894812010-09-22 16:48:34 +0000600 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000601 reference front() {return base::__end_.__next_->__value_;}
Howard Hinnant82894812010-09-22 16:48:34 +0000602 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000603 const_reference front() const {return base::__end_.__next_->__value_;}
Howard Hinnant82894812010-09-22 16:48:34 +0000604 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000605 reference back() {return base::__end_.__prev_->__value_;}
Howard Hinnant82894812010-09-22 16:48:34 +0000606 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000607 const_reference back() const {return base::__end_.__prev_->__value_;}
608
Howard Hinnant73d21a42010-09-04 23:28:19 +0000609#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000610 void push_front(value_type&& __x);
611 void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000612#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000613 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 Hinnant73d21a42010-09-04 23:28:19 +0000619#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000620 iterator insert(const_iterator __p, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000621#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000622
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 Hinnant82894812010-09-22 16:48:34 +0000631 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000632 iterator insert(const_iterator __p, initializer_list<value_type> __il)
633 {return insert(__p, __il.begin(), __il.end());}
634
Howard Hinnant82894812010-09-22 16:48:34 +0000635 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000636 void swap(list& __c) {base::swap(__c);}
Howard Hinnant82894812010-09-22 16:48:34 +0000637 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000638 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 Hinnant73d21a42010-09-04 23:28:19 +0000650#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +0000651 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000652 void splice(const_iterator __p, list&& __c) {splice(__p, __c);}
653#endif
654 void splice(const_iterator __p, list& __c, const_iterator __i);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000655#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +0000656 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000657 void splice(const_iterator __p, list&& __c, const_iterator __i)
658 {splice(__p, __c, __i);}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000659#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000660 void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000661#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +0000662 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000663 void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l)
664 {splice(__p, __c, __f, __l);}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000665#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000666
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 Hinnant73d21a42010-09-04 23:28:19 +0000673#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +0000674 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000675 void merge(list&& __c) {merge(__c);}
676#endif
677 template <class _Comp>
678 void merge(list& __c, _Comp __comp);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000679#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000680 template <class _Comp>
Howard Hinnant82894812010-09-22 16:48:34 +0000681 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000682 void merge(list&& __c, _Comp __comp) {merge(__c, __comp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000683#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000684 void sort();
685 template <class _Comp>
686 void sort(_Comp __comp);
687
688 void reverse();
689
690private:
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
701template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000702inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000703void
704list<_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
712template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000713inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000714typename list<_Tp, _Alloc>::iterator
715list<_Tp, _Alloc>::__iterator(size_type __n)
716{
717 return __n <= base::__sz() / 2 ? next(begin(), __n)
718 : prev(end(), base::__sz() - __n);
719}
720
721template <class _Tp, class _Alloc>
722list<_Tp, _Alloc>::list(size_type __n)
723{
724 for (; __n > 0; --__n)
Howard Hinnant73d21a42010-09-04 23:28:19 +0000725#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000726 emplace_back();
727#else
728 push_back(value_type());
729#endif
730}
731
732template <class _Tp, class _Alloc>
733list<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
734{
735 for (; __n > 0; --__n)
736 push_back(__x);
737}
738
739template <class _Tp, class _Alloc>
740list<_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
747template <class _Tp, class _Alloc>
748template <class _InpIter>
749list<_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
756template <class _Tp, class _Alloc>
757template <class _InpIter>
758list<_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
766template <class _Tp, class _Alloc>
767list<_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
776template <class _Tp, class _Alloc>
777list<_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
784template <class _Tp, class _Alloc>
785list<_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
793template <class _Tp, class _Alloc>
794list<_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
801template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000802inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000803list<_Tp, _Alloc>&
804list<_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 Hinnant73d21a42010-09-04 23:28:19 +0000814#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000815
816template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000817inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000818list<_Tp, _Alloc>::list(list&& __c)
819 : base(allocator_type(_STD::move(__c.__node_alloc())))
820{
821 splice(end(), __c);
822}
823
824template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000825inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000826list<_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
838template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000839inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000840list<_Tp, _Alloc>&
841list<_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
848template <class _Tp, class _Alloc>
849void
850list<_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
861template <class _Tp, class _Alloc>
862void
863list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
864{
865 clear();
866 base::__move_assign_alloc(__c);
867 splice(end(), __c);
868}
869
Howard Hinnant73d21a42010-09-04 23:28:19 +0000870#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000871
872template <class _Tp, class _Alloc>
873template <class _InpIter>
874void
875list<_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
888template <class _Tp, class _Alloc>
889void
890list<_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
902template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000903inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000904_Alloc
905list<_Tp, _Alloc>::get_allocator() const
906{
907 return allocator_type(base::__node_alloc());
908}
909
910template <class _Tp, class _Alloc>
911typename list<_Tp, _Alloc>::iterator
912list<_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
924template <class _Tp, class _Alloc>
925typename list<_Tp, _Alloc>::iterator
926list<_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 Hinnant324bb032010-08-22 00:02:43 +0000944#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000945 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 Hinnant324bb032010-08-22 00:02:43 +0000968#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000969 __link_nodes(const_cast<__node&>(*__p.__ptr_), *__r.__ptr_, *__e.__ptr_);
970 base::__sz() += __ds;
971 }
972 return __r;
973}
974
975template <class _Tp, class _Alloc>
976template <class _InpIter>
977typename list<_Tp, _Alloc>::iterator
978list<_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 Hinnant324bb032010-08-22 00:02:43 +0000997#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000998 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 Hinnant324bb032010-08-22 00:02:43 +00001021#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001022 __link_nodes(const_cast<__node&>(*__p.__ptr_), *__r.__ptr_, *__e.__ptr_);
1023 base::__sz() += __ds;
1024 }
1025 return __r;
1026}
1027
1028template <class _Tp, class _Alloc>
1029void
1030list<_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
1041template <class _Tp, class _Alloc>
1042void
1043list<_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 Hinnant73d21a42010-09-04 23:28:19 +00001054#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001055
1056template <class _Tp, class _Alloc>
1057void
1058list<_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
1069template <class _Tp, class _Alloc>
1070void
1071list<_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 Hinnant73d21a42010-09-04 23:28:19 +00001082#ifndef _LIBCPP_HAS_NO_VARIADICS
1083
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001084template <class _Tp, class _Alloc>
1085template <class... _Args>
1086void
1087list<_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
1098template <class _Tp, class _Alloc>
1099template <class... _Args>
1100void
1101list<_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
1112template <class _Tp, class _Alloc>
1113template <class... _Args>
1114typename list<_Tp, _Alloc>::iterator
1115list<_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 Hinnant73d21a42010-09-04 23:28:19 +00001127#endif // _LIBCPP_HAS_NO_VARIADICS
1128
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001129template <class _Tp, class _Alloc>
1130typename list<_Tp, _Alloc>::iterator
1131list<_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 Hinnant73d21a42010-09-04 23:28:19 +00001143#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001144
1145template <class _Tp, class _Alloc>
1146void
1147list<_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
1157template <class _Tp, class _Alloc>
1158void
1159list<_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
1169template <class _Tp, class _Alloc>
1170typename list<_Tp, _Alloc>::iterator
1171list<_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
1183template <class _Tp, class _Alloc>
1184typename list<_Tp, _Alloc>::iterator
1185list<_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
1203template <class _Tp, class _Alloc>
1204void
1205list<_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 Hinnant324bb032010-08-22 00:02:43 +00001224#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001225 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 Hinnant324bb032010-08-22 00:02:43 +00001248#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001249 __link_nodes(static_cast<__node&>(base::__end_), *__r.__ptr_, *__e.__ptr_);
1250 base::__sz() += __ds;
1251 }
1252}
1253
1254template <class _Tp, class _Alloc>
1255void
1256list<_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 Hinnant324bb032010-08-22 00:02:43 +00001275#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001276 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 Hinnant324bb032010-08-22 00:02:43 +00001299#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001300 __link_nodes(static_cast<__node&>(base::__end_), *__r.__ptr_, *__e.__ptr_);
1301 base::__sz() += __ds;
Howard Hinnant324bb032010-08-22 00:02:43 +00001302 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001303}
1304
1305template <class _Tp, class _Alloc>
1306void
1307list<_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
1320template <class _Tp, class _Alloc>
1321void
1322list<_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
1334template <class _Tp, class _Alloc>
1335void
1336list<_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
1354template <class _Tp, class _Alloc>
1355void
1356list<_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
1372template <class _Tp, class _Alloc>
1373template <class _Pred>
1374void
1375list<_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
1391template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001392inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001393void
1394list<_Tp, _Alloc>::unique()
1395{
1396 unique(__equal_to<value_type>());
1397}
1398
1399template <class _Tp, class _Alloc>
1400template <class _BinaryPred>
1401void
1402list<_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
1414template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001415inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001416void
1417list<_Tp, _Alloc>::merge(list& __c)
1418{
1419 merge(__c, __less<value_type>());
1420}
1421
1422template <class _Tp, class _Alloc>
1423template <class _Comp>
1424void
1425list<_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
1458template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001459inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001460void
1461list<_Tp, _Alloc>::sort()
1462{
1463 sort(__less<value_type>());
1464}
1465
1466template <class _Tp, class _Alloc>
1467template <class _Comp>
Howard Hinnant82894812010-09-22 16:48:34 +00001468inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001469void
1470list<_Tp, _Alloc>::sort(_Comp __comp)
1471{
1472 __sort(begin(), end(), base::__sz(), __comp);
1473}
1474
1475template <class _Tp, class _Alloc>
1476template <class _Comp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001477typename list<_Tp, _Alloc>::iterator
1478list<_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
1538template <class _Tp, class _Alloc>
1539void
1540list<_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
1551template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001552inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001553bool
1554operator==(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
1559template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001560inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001561bool
1562operator< (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
1567template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001568inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001569bool
1570operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1571{
1572 return !(__x == __y);
1573}
1574
1575template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001576inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001577bool
1578operator> (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1579{
1580 return __y < __x;
1581}
1582
1583template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001584inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001585bool
1586operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1587{
1588 return !(__x < __y);
1589}
1590
1591template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001592inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001593bool
1594operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1595{
1596 return !(__y < __x);
1597}
1598
1599template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001600inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001601void
1602swap(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