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