blob: fa515e0bb1f72e0ae85f21a8a501bff047b41c17 [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
Howard Hinnantc5607272011-06-03 17:30:28 +000039 list()
40 noexcept(is_nothrow_default_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000041 explicit list(const allocator_type& a);
42 explicit list(size_type n);
43 list(size_type n, const value_type& value);
44 list(size_type n, const value_type& value, const allocator_type& a);
45 template <class Iter>
46 list(Iter first, Iter last);
47 template <class Iter>
48 list(Iter first, Iter last, const allocator_type& a);
49 list(const list& x);
50 list(const list&, const allocator_type& a);
Howard Hinnantc5607272011-06-03 17:30:28 +000051 list(list&& x)
52 noexcept(is_nothrow_move_constructible<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000053 list(list&&, const allocator_type& a);
54 list(initializer_list<value_type>);
55 list(initializer_list<value_type>, const allocator_type& a);
56
57 ~list();
58
59 list& operator=(const list& x);
Howard Hinnantc5607272011-06-03 17:30:28 +000060 list& operator=(list&& x)
61 noexcept(
62 allocator_type::propagate_on_container_move_assignment::value &&
63 is_nothrow_move_assignable<allocator_type>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000064 list& operator=(initializer_list<value_type>);
65 template <class Iter>
66 void assign(Iter first, Iter last);
67 void assign(size_type n, const value_type& t);
68 void assign(initializer_list<value_type>);
69
Howard Hinnantc5607272011-06-03 17:30:28 +000070 allocator_type get_allocator() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000071
Howard Hinnantc5607272011-06-03 17:30:28 +000072 iterator begin() noexcept;
73 const_iterator begin() const noexcept;
74 iterator end() noexcept;
75 const_iterator end() const noexcept;
76 reverse_iterator rbegin() noexcept;
77 const_reverse_iterator rbegin() const noexcept;
78 reverse_iterator rend() noexcept;
79 const_reverse_iterator rend() const noexcept;
80 const_iterator cbegin() const noexcept;
81 const_iterator cend() const noexcept;
82 const_reverse_iterator crbegin() const noexcept;
83 const_reverse_iterator crend() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000084
85 reference front();
86 const_reference front() const;
87 reference back();
88 const_reference back() const;
89
Howard Hinnantc5607272011-06-03 17:30:28 +000090 bool empty() const noexcept;
91 size_type size() const noexcept;
92 size_type max_size() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000093
94 template <class... Args>
95 void emplace_front(Args&&... args);
96 void pop_front();
97 template <class... Args>
98 void emplace_back(Args&&... args);
99 void pop_back();
100 void push_front(const value_type& x);
101 void push_front(value_type&& x);
102 void push_back(const value_type& x);
103 void push_back(value_type&& x);
104 template <class... Args>
105 iterator emplace(const_iterator position, Args&&... args);
106 iterator insert(const_iterator position, const value_type& x);
107 iterator insert(const_iterator position, value_type&& x);
108 iterator insert(const_iterator position, size_type n, const value_type& x);
109 template <class Iter>
110 iterator insert(const_iterator position, Iter first, Iter last);
111 iterator insert(const_iterator position, initializer_list<value_type> il);
112
113 iterator erase(const_iterator position);
114 iterator erase(const_iterator position, const_iterator last);
115
116 void resize(size_type sz);
117 void resize(size_type sz, const value_type& c);
118
Howard Hinnantc5607272011-06-03 17:30:28 +0000119 void swap(list&)
120 noexcept(!allocator_type::propagate_on_container_swap::value ||
121 __is_nothrow_swappable<allocator_type>::value);
122 void clear() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000123
124 void splice(const_iterator position, list& x);
125 void splice(const_iterator position, list&& x);
126 void splice(const_iterator position, list& x, const_iterator i);
127 void splice(const_iterator position, list&& x, const_iterator i);
128 void splice(const_iterator position, list& x, const_iterator first,
129 const_iterator last);
130 void splice(const_iterator position, list&& x, const_iterator first,
131 const_iterator last);
132
133 void remove(const value_type& value);
134 template <class Pred> void remove_if(Pred pred);
135 void unique();
136 template <class BinaryPredicate>
137 void unique(BinaryPredicate binary_pred);
138 void merge(list& x);
139 void merge(list&& x);
140 template <class Compare>
141 void merge(list& x, Compare comp);
142 template <class Compare>
143 void merge(list&& x, Compare comp);
144 void sort();
145 template <class Compare>
146 void sort(Compare comp);
Howard Hinnantc5607272011-06-03 17:30:28 +0000147 void reverse() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000148};
149
150template <class T, class Alloc>
151 bool operator==(const list<T,Alloc>& x, const list<T,Alloc>& y);
152template <class T, class Alloc>
153 bool operator< (const list<T,Alloc>& x, const list<T,Alloc>& y);
154template <class T, class Alloc>
155 bool operator!=(const list<T,Alloc>& x, const list<T,Alloc>& y);
156template <class T, class Alloc>
157 bool operator> (const list<T,Alloc>& x, const list<T,Alloc>& y);
158template <class T, class Alloc>
159 bool operator>=(const list<T,Alloc>& x, const list<T,Alloc>& y);
160template <class T, class Alloc>
161 bool operator<=(const list<T,Alloc>& x, const list<T,Alloc>& y);
162
163template <class T, class Alloc>
Howard Hinnantc5607272011-06-03 17:30:28 +0000164 void swap(list<T,Alloc>& x, list<T,Alloc>& y)
165 noexcept(noexcept(x.swap(y)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000166
167} // std
168
169*/
170
171#include <__config>
172
173#include <memory>
174#include <limits>
175#include <initializer_list>
176#include <iterator>
177#include <algorithm>
178
179#pragma GCC system_header
180
181_LIBCPP_BEGIN_NAMESPACE_STD
182
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000183template <class _Tp, class _VoidPtr> struct __list_node;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000184
185template <class _Tp, class _VoidPtr>
186struct __list_node_base
187{
188 typedef typename pointer_traits<_VoidPtr>::template
189#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
190 rebind<__list_node<_Tp, _VoidPtr> > pointer;
191#else
192 rebind<__list_node<_Tp, _VoidPtr> >::other pointer;
193#endif
194
195 pointer __prev_;
196 pointer __next_;
197
Howard Hinnant82894812010-09-22 16:48:34 +0000198 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000199 __list_node_base()
200 : __prev_(static_cast<pointer>(this)),
201 __next_(static_cast<pointer>(this))
202 {}
203};
204
205template <class _Tp, class _VoidPtr>
206struct __list_node
207 : public __list_node_base<_Tp, _VoidPtr>
208{
209 _Tp __value_;
210};
211
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000212template <class _Tp, class _Alloc> class list;
213template <class _Tp, class _Alloc> class __list_imp;
214template <class _Tp, class _VoidPtr> class __list_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000215
216template <class _Tp, class _VoidPtr>
Howard Hinnant82894812010-09-22 16:48:34 +0000217class _LIBCPP_VISIBLE __list_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000218{
219 typedef typename pointer_traits<_VoidPtr>::template
220#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
221 rebind<__list_node<_Tp, _VoidPtr> > __node_pointer;
222#else
223 rebind<__list_node<_Tp, _VoidPtr> >::other __node_pointer;
224#endif
225
226 __node_pointer __ptr_;
227
Howard Hinnant82894812010-09-22 16:48:34 +0000228 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000229 explicit __list_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000230
231 template<class, class> friend class list;
232 template<class, class> friend class __list_imp;
233 template<class, class> friend class __list_const_iterator;
234public:
235 typedef bidirectional_iterator_tag iterator_category;
236 typedef _Tp value_type;
237 typedef value_type& reference;
238 typedef typename pointer_traits<_VoidPtr>::template
239#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
240 rebind<value_type>
241#else
242 rebind<value_type>::other
243#endif
244 pointer;
245 typedef typename pointer_traits<pointer>::difference_type difference_type;
246
Howard Hinnant82894812010-09-22 16:48:34 +0000247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000248 __list_iterator() _NOEXCEPT {}
Howard Hinnant211f0ee2011-01-28 23:46:28 +0000249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000250 reference operator*() const {return __ptr_->__value_;}
Howard Hinnant82894812010-09-22 16:48:34 +0000251 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000252 pointer operator->() const {return &(operator*());}
253
Howard Hinnant82894812010-09-22 16:48:34 +0000254 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000255 __list_iterator& operator++() {__ptr_ = __ptr_->__next_; return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +0000256 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000257 __list_iterator operator++(int) {__list_iterator __t(*this); ++(*this); return __t;}
258
Howard Hinnant82894812010-09-22 16:48:34 +0000259 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000260 __list_iterator& operator--() {__ptr_ = __ptr_->__prev_; return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +0000261 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000262 __list_iterator operator--(int) {__list_iterator __t(*this); --(*this); return __t;}
263
Howard Hinnant82894812010-09-22 16:48:34 +0000264 friend _LIBCPP_INLINE_VISIBILITY
265 bool operator==(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000266 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +0000267 friend _LIBCPP_INLINE_VISIBILITY
268 bool operator!=(const __list_iterator& __x, const __list_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000269 {return !(__x == __y);}
270};
271
272template <class _Tp, class _VoidPtr>
Howard Hinnant82894812010-09-22 16:48:34 +0000273class _LIBCPP_VISIBLE __list_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000274{
275 typedef typename pointer_traits<_VoidPtr>::template
276#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
277 rebind<const __list_node<_Tp, _VoidPtr> > __node_pointer;
278#else
279 rebind<const __list_node<_Tp, _VoidPtr> >::other __node_pointer;
280#endif
281
282 __node_pointer __ptr_;
283
Howard Hinnant82894812010-09-22 16:48:34 +0000284 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000285 explicit __list_const_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000286
287 template<class, class> friend class list;
288 template<class, class> friend class __list_imp;
289public:
290 typedef bidirectional_iterator_tag iterator_category;
291 typedef _Tp value_type;
292 typedef const value_type& reference;
293 typedef typename pointer_traits<_VoidPtr>::template
294#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
295 rebind<const value_type>
296#else
297 rebind<const value_type>::other
298#endif
299 pointer;
300 typedef typename pointer_traits<pointer>::difference_type difference_type;
301
Howard Hinnant82894812010-09-22 16:48:34 +0000302 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000303 __list_const_iterator() _NOEXCEPT {}
Howard Hinnant211f0ee2011-01-28 23:46:28 +0000304 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000305 __list_const_iterator(__list_iterator<_Tp, _VoidPtr> __p) _NOEXCEPT
306 : __ptr_(__p.__ptr_) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000307
Howard Hinnant82894812010-09-22 16:48:34 +0000308 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000309 reference operator*() const {return __ptr_->__value_;}
Howard Hinnant82894812010-09-22 16:48:34 +0000310 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000311 pointer operator->() const {return &(operator*());}
312
Howard Hinnant82894812010-09-22 16:48:34 +0000313 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000314 __list_const_iterator& operator++() {__ptr_ = __ptr_->__next_; return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +0000315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000316 __list_const_iterator operator++(int) {__list_const_iterator __t(*this); ++(*this); return __t;}
317
Howard Hinnant82894812010-09-22 16:48:34 +0000318 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000319 __list_const_iterator& operator--() {__ptr_ = __ptr_->__prev_; return *this;}
Howard Hinnant82894812010-09-22 16:48:34 +0000320 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000321 __list_const_iterator operator--(int) {__list_const_iterator __t(*this); --(*this); return __t;}
322
Howard Hinnant82894812010-09-22 16:48:34 +0000323 friend _LIBCPP_INLINE_VISIBILITY
324 bool operator==(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000325 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant82894812010-09-22 16:48:34 +0000326 friend _LIBCPP_INLINE_VISIBILITY
327 bool operator!=(const __list_const_iterator& __x, const __list_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000328 {return !(__x == __y);}
329};
330
331template <class _Tp, class _Alloc>
332class __list_imp
333{
334 __list_imp(const __list_imp&);
335 __list_imp& operator=(const __list_imp&);
336protected:
337 typedef _Tp value_type;
338 typedef _Alloc allocator_type;
339 typedef allocator_traits<allocator_type> __alloc_traits;
340 typedef typename __alloc_traits::size_type size_type;
341 typedef typename __alloc_traits::void_pointer __void_pointer;
342 typedef __list_iterator<value_type, __void_pointer> iterator;
343 typedef __list_const_iterator<value_type, __void_pointer> const_iterator;
344 typedef __list_node_base<value_type, __void_pointer> __node_base;
345 typedef __list_node<value_type, __void_pointer> __node;
346 typedef typename __alloc_traits::template
347#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
348 rebind_alloc<__node>
349#else
350 rebind_alloc<__node>::other
351#endif
352 __node_allocator;
353 typedef allocator_traits<__node_allocator> __node_alloc_traits;
354 typedef typename __node_alloc_traits::pointer __node_pointer;
355 typedef typename __node_alloc_traits::const_pointer __node_const_pointer;
356 typedef typename __alloc_traits::pointer pointer;
357 typedef typename __alloc_traits::const_pointer const_pointer;
358 typedef typename __alloc_traits::difference_type difference_type;
359
360 __node_base __end_;
361 __compressed_pair<size_type, __node_allocator> __size_alloc_;
362
Howard Hinnant82894812010-09-22 16:48:34 +0000363 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000364 size_type& __sz() _NOEXCEPT {return __size_alloc_.first();}
Howard Hinnant82894812010-09-22 16:48:34 +0000365 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000366 const size_type& __sz() const _NOEXCEPT
367 {return __size_alloc_.first();}
Howard Hinnant82894812010-09-22 16:48:34 +0000368 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000369 __node_allocator& __node_alloc() _NOEXCEPT
370 {return __size_alloc_.second();}
Howard Hinnant82894812010-09-22 16:48:34 +0000371 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000372 const __node_allocator& __node_alloc() const _NOEXCEPT
373 {return __size_alloc_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000374
Howard Hinnantc5607272011-06-03 17:30:28 +0000375 static void __unlink_nodes(__node_base& __f, __node_base& __l) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000376
Howard Hinnantc5607272011-06-03 17:30:28 +0000377 __list_imp()
378 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000379 __list_imp(const allocator_type& __a);
380 ~__list_imp();
Howard Hinnantc5607272011-06-03 17:30:28 +0000381 void clear() _NOEXCEPT;
Howard Hinnant82894812010-09-22 16:48:34 +0000382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000383 bool empty() const _NOEXCEPT {return __sz() == 0;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000384
Howard Hinnant82894812010-09-22 16:48:34 +0000385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000386 iterator begin() _NOEXCEPT
387 {return iterator(__end_.__next_);}
Howard Hinnant82894812010-09-22 16:48:34 +0000388 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000389 const_iterator begin() const _NOEXCEPT
390 {return const_iterator(__end_.__next_);}
Howard Hinnant82894812010-09-22 16:48:34 +0000391 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000392 iterator end() _NOEXCEPT
393 {return iterator(static_cast<__node_pointer> (&__end_));}
Howard Hinnant82894812010-09-22 16:48:34 +0000394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000395 const_iterator end() const _NOEXCEPT
396 {return const_iterator(static_cast<__node_const_pointer>(&__end_));}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000397
Howard Hinnantc5607272011-06-03 17:30:28 +0000398 void swap(__list_imp& __c)
399 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
400 __is_nothrow_swappable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000401
Howard Hinnant82894812010-09-22 16:48:34 +0000402 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000403 void __copy_assign_alloc(const __list_imp& __c)
404 {__copy_assign_alloc(__c, integral_constant<bool,
405 __node_alloc_traits::propagate_on_container_copy_assignment::value>());}
406
Howard Hinnant82894812010-09-22 16:48:34 +0000407 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000408 void __move_assign_alloc(__list_imp& __c)
Howard Hinnantc5607272011-06-03 17:30:28 +0000409 _NOEXCEPT_(
410 !__node_alloc_traits::propagate_on_container_move_assignment::value ||
411 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000412 {__move_assign_alloc(__c, integral_constant<bool,
413 __node_alloc_traits::propagate_on_container_move_assignment::value>());}
414
415private:
Howard Hinnant82894812010-09-22 16:48:34 +0000416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000417 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y)
Howard Hinnantc5607272011-06-03 17:30:28 +0000418 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
419 __is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000420 {__swap_alloc(__x, __y, integral_constant<bool,
421 __node_alloc_traits::propagate_on_container_swap::value>());}
Howard Hinnant82894812010-09-22 16:48:34 +0000422 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000423 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, true_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000424 _NOEXCEPT_(__is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000425 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000426 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000427 swap(__x, __y);
428 }
Howard Hinnant82894812010-09-22 16:48:34 +0000429 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000430 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, false_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000431 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000432 {}
433
Howard Hinnant82894812010-09-22 16:48:34 +0000434 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000435 void __copy_assign_alloc(const __list_imp& __c, true_type)
436 {
437 if (__node_alloc() != __c.__node_alloc())
438 clear();
439 __node_alloc() = __c.__node_alloc();
440 }
441
Howard Hinnant82894812010-09-22 16:48:34 +0000442 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000443 void __copy_assign_alloc(const __list_imp& __c, false_type)
444 {}
445
Howard Hinnant82894812010-09-22 16:48:34 +0000446 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000447 void __move_assign_alloc(const __list_imp& __c, true_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000448 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000449 {
Howard Hinnant0949eed2011-06-30 21:18:19 +0000450 __node_alloc() = _VSTD::move(__c.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000451 }
452
Howard Hinnant82894812010-09-22 16:48:34 +0000453 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000454 void __move_assign_alloc(const __list_imp& __c, false_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000455 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000456 {}
457};
458
459// Unlink nodes [__f, __l]
460template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000461inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000462void
463__list_imp<_Tp, _Alloc>::__unlink_nodes(__node_base& __f, __node_base& __l)
Howard Hinnantc5607272011-06-03 17:30:28 +0000464 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465{
466 __f.__prev_->__next_ = __l.__next_;
467 __l.__next_->__prev_ = __f.__prev_;
468}
469
470template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000471inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000472__list_imp<_Tp, _Alloc>::__list_imp()
Howard Hinnantc5607272011-06-03 17:30:28 +0000473 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000474 : __size_alloc_(0)
475{
476}
477
478template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000479inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000480__list_imp<_Tp, _Alloc>::__list_imp(const allocator_type& __a)
481 : __size_alloc_(0, __node_allocator(__a))
482{
483}
484
485template <class _Tp, class _Alloc>
486__list_imp<_Tp, _Alloc>::~__list_imp()
487{
488 clear();
489}
490
491template <class _Tp, class _Alloc>
492void
Howard Hinnantc5607272011-06-03 17:30:28 +0000493__list_imp<_Tp, _Alloc>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000494{
495 if (!empty())
496 {
497 __node_allocator& __na = __node_alloc();
498 iterator __f = begin();
499 iterator __l = end();
500 __unlink_nodes(*__f.__ptr_, *__l.__ptr_->__prev_);
501 __sz() = 0;
502 while (__f != __l)
503 {
504 __node& __n = *__f.__ptr_;
505 ++__f;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000506 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n.__value_));
507 __node_alloc_traits::deallocate(__na, _VSTD::addressof(__n), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000508 }
509 }
510}
511
512template <class _Tp, class _Alloc>
513void
514__list_imp<_Tp, _Alloc>::swap(__list_imp& __c)
Howard Hinnantc5607272011-06-03 17:30:28 +0000515 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
516 __is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000517{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000518 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000519 __swap_alloc(__node_alloc(), __c.__node_alloc());
520 swap(__sz(), __c.__sz());
521 swap(__end_, __c.__end_);
522 if (__sz() == 0)
523 __end_.__next_ = __end_.__prev_ = &static_cast<__node&>(__end_);
524 else
525 __end_.__prev_->__next_ = __end_.__next_->__prev_
526 = &static_cast<__node&>(__end_);
527 if (__c.__sz() == 0)
528 __c.__end_.__next_ = __c.__end_.__prev_
529 = &static_cast<__node&>(__c.__end_);
530 else
531 __c.__end_.__prev_->__next_ = __c.__end_.__next_->__prev_
532 = &static_cast<__node&>(__c.__end_);
533}
534
535template <class _Tp, class _Alloc = allocator<_Tp> >
Howard Hinnant82894812010-09-22 16:48:34 +0000536class _LIBCPP_VISIBLE list
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000537 : private __list_imp<_Tp, _Alloc>
538{
539 typedef __list_imp<_Tp, _Alloc> base;
540 typedef typename base::__node __node;
541 typedef typename base::__node_allocator __node_allocator;
542 typedef typename base::__node_pointer __node_pointer;
543 typedef typename base::__node_alloc_traits __node_alloc_traits;
544
545public:
546 typedef _Tp value_type;
547 typedef _Alloc allocator_type;
548 static_assert((is_same<value_type, typename allocator_type::value_type>::value),
549 "Invalid allocator::value_type");
550 typedef value_type& reference;
551 typedef const value_type& const_reference;
552 typedef typename base::pointer pointer;
553 typedef typename base::const_pointer const_pointer;
554 typedef typename base::size_type size_type;
555 typedef typename base::difference_type difference_type;
556 typedef typename base::iterator iterator;
557 typedef typename base::const_iterator const_iterator;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000558 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
559 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000560
Howard Hinnant82894812010-09-22 16:48:34 +0000561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000562 list()
563 _NOEXCEPT_(is_nothrow_default_constructible<__node_allocator>::value)
564 {}
Howard Hinnant82894812010-09-22 16:48:34 +0000565 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000566 list(const allocator_type& __a) : base(__a) {}
567 list(size_type __n);
568 list(size_type __n, const value_type& __x);
569 list(size_type __n, const value_type& __x, const allocator_type& __a);
570 template <class _InpIter>
571 list(_InpIter __f, _InpIter __l,
572 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
573 template <class _InpIter>
574 list(_InpIter __f, _InpIter __l, const allocator_type& __a,
575 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
576
577 list(const list& __c);
578 list(const list& __c, const allocator_type& __a);
579 list& operator=(const list& __c);
Howard Hinnante3e32912011-08-12 21:56:02 +0000580#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000581 list(initializer_list<value_type> __il);
582 list(initializer_list<value_type> __il, const allocator_type& __a);
Howard Hinnante3e32912011-08-12 21:56:02 +0000583#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant73d21a42010-09-04 23:28:19 +0000584#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc5607272011-06-03 17:30:28 +0000585 list(list&& __c)
586 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000587 list(list&& __c, const allocator_type& __a);
Howard Hinnantc5607272011-06-03 17:30:28 +0000588 list& operator=(list&& __c)
589 _NOEXCEPT_(
590 __node_alloc_traits::propagate_on_container_move_assignment::value &&
591 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000592#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnante3e32912011-08-12 21:56:02 +0000593#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant82894812010-09-22 16:48:34 +0000594 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000595 list& operator=(initializer_list<value_type> __il)
596 {assign(__il.begin(), __il.end()); return *this;}
Howard Hinnante3e32912011-08-12 21:56:02 +0000597#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000598
599 template <class _InpIter>
600 void assign(_InpIter __f, _InpIter __l,
601 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
602 void assign(size_type __n, const value_type& __x);
Howard Hinnante3e32912011-08-12 21:56:02 +0000603#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant82894812010-09-22 16:48:34 +0000604 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000605 void assign(initializer_list<value_type> __il)
606 {assign(__il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000607#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000608
Howard Hinnantc5607272011-06-03 17:30:28 +0000609 allocator_type get_allocator() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000610
Howard Hinnant82894812010-09-22 16:48:34 +0000611 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000612 size_type size() const _NOEXCEPT {return base::__sz();}
Howard Hinnant82894812010-09-22 16:48:34 +0000613 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000614 bool empty() const _NOEXCEPT {return base::empty();}
Howard Hinnant82894812010-09-22 16:48:34 +0000615 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000616 size_type max_size() const _NOEXCEPT
617 {return numeric_limits<difference_type>::max();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000618
Howard Hinnant82894812010-09-22 16:48:34 +0000619 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000620 iterator begin() _NOEXCEPT {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000621 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000622 const_iterator begin() const _NOEXCEPT {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000623 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000624 iterator end() _NOEXCEPT {return base::end();}
Howard Hinnant82894812010-09-22 16:48:34 +0000625 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000626 const_iterator end() const _NOEXCEPT {return base::end();}
Howard Hinnant82894812010-09-22 16:48:34 +0000627 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000628 const_iterator cbegin() const _NOEXCEPT {return base::begin();}
Howard Hinnant82894812010-09-22 16:48:34 +0000629 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000630 const_iterator cend() const _NOEXCEPT {return base::end();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000631
Howard Hinnant82894812010-09-22 16:48:34 +0000632 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000633 reverse_iterator rbegin() _NOEXCEPT
634 {return reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +0000635 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000636 const_reverse_iterator rbegin() const _NOEXCEPT
637 {return const_reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +0000638 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000639 reverse_iterator rend() _NOEXCEPT
640 {return reverse_iterator(begin());}
Howard Hinnant82894812010-09-22 16:48:34 +0000641 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000642 const_reverse_iterator rend() const _NOEXCEPT
643 {return const_reverse_iterator(begin());}
Howard Hinnant82894812010-09-22 16:48:34 +0000644 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000645 const_reverse_iterator crbegin() const _NOEXCEPT
646 {return const_reverse_iterator(end());}
Howard Hinnant82894812010-09-22 16:48:34 +0000647 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000648 const_reverse_iterator crend() const _NOEXCEPT
649 {return const_reverse_iterator(begin());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000650
Howard Hinnant82894812010-09-22 16:48:34 +0000651 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000652 reference front() {return base::__end_.__next_->__value_;}
Howard Hinnant82894812010-09-22 16:48:34 +0000653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000654 const_reference front() const {return base::__end_.__next_->__value_;}
Howard Hinnant82894812010-09-22 16:48:34 +0000655 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000656 reference back() {return base::__end_.__prev_->__value_;}
Howard Hinnant82894812010-09-22 16:48:34 +0000657 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000658 const_reference back() const {return base::__end_.__prev_->__value_;}
659
Howard Hinnant73d21a42010-09-04 23:28:19 +0000660#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000661 void push_front(value_type&& __x);
662 void push_back(value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000663#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000664 template <class... _Args>
665 void emplace_front(_Args&&... __args);
666 template <class... _Args>
667 void emplace_back(_Args&&... __args);
668 template <class... _Args>
669 iterator emplace(const_iterator __p, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000670#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000671 iterator insert(const_iterator __p, value_type&& __x);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000672#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000673
674 void push_front(const value_type& __x);
675 void push_back(const value_type& __x);
676
677 iterator insert(const_iterator __p, const value_type& __x);
678 iterator insert(const_iterator __p, size_type __n, const value_type& __x);
679 template <class _InpIter>
680 iterator insert(const_iterator __p, _InpIter __f, _InpIter __l,
681 typename enable_if<__is_input_iterator<_InpIter>::value>::type* = 0);
Howard Hinnante3e32912011-08-12 21:56:02 +0000682#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnant82894812010-09-22 16:48:34 +0000683 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000684 iterator insert(const_iterator __p, initializer_list<value_type> __il)
685 {return insert(__p, __il.begin(), __il.end());}
Howard Hinnante3e32912011-08-12 21:56:02 +0000686#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000687
Howard Hinnant82894812010-09-22 16:48:34 +0000688 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000689 void swap(list& __c)
690 _NOEXCEPT_(!__node_alloc_traits::propagate_on_container_swap::value ||
691 __is_nothrow_swappable<__node_allocator>::value)
692 {base::swap(__c);}
Howard Hinnant82894812010-09-22 16:48:34 +0000693 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc5607272011-06-03 17:30:28 +0000694 void clear() _NOEXCEPT {base::clear();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000695
696 void pop_front();
697 void pop_back();
698
699 iterator erase(const_iterator __p);
700 iterator erase(const_iterator __f, const_iterator __l);
701
702 void resize(size_type __n);
703 void resize(size_type __n, const value_type& __x);
704
705 void splice(const_iterator __p, list& __c);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000706#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +0000707 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000708 void splice(const_iterator __p, list&& __c) {splice(__p, __c);}
709#endif
710 void splice(const_iterator __p, list& __c, const_iterator __i);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000711#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +0000712 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000713 void splice(const_iterator __p, list&& __c, const_iterator __i)
714 {splice(__p, __c, __i);}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000715#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000716 void splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000717#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +0000718 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000719 void splice(const_iterator __p, list&& __c, const_iterator __f, const_iterator __l)
720 {splice(__p, __c, __f, __l);}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000721#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000722
723 void remove(const value_type& __x);
724 template <class _Pred> void remove_if(_Pred __pred);
725 void unique();
726 template <class _BinaryPred>
727 void unique(_BinaryPred __binary_pred);
728 void merge(list& __c);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000729#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant82894812010-09-22 16:48:34 +0000730 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000731 void merge(list&& __c) {merge(__c);}
732#endif
733 template <class _Comp>
734 void merge(list& __c, _Comp __comp);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000735#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000736 template <class _Comp>
Howard Hinnant82894812010-09-22 16:48:34 +0000737 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000738 void merge(list&& __c, _Comp __comp) {merge(__c, __comp);}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000739#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000740 void sort();
741 template <class _Comp>
742 void sort(_Comp __comp);
743
Howard Hinnantc5607272011-06-03 17:30:28 +0000744 void reverse() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000745
746private:
747 static void __link_nodes(__node& __p, __node& __f, __node& __l);
748 iterator __iterator(size_type __n);
749 template <class _Comp>
750 static iterator __sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp);
751
Howard Hinnantc5607272011-06-03 17:30:28 +0000752 void __move_assign(list& __c, true_type)
753 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000754 void __move_assign(list& __c, false_type);
755};
756
757// Link in nodes [__f, __l] just prior to __p
758template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000759inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000760void
761list<_Tp, _Alloc>::__link_nodes(__node& __p, __node& __f, __node& __l)
762{
763 __p.__prev_->__next_ = &__f;
764 __f.__prev_ = __p.__prev_;
765 __p.__prev_ = &__l;
766 __l.__next_ = &__p;
767}
768
769template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000770inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000771typename list<_Tp, _Alloc>::iterator
772list<_Tp, _Alloc>::__iterator(size_type __n)
773{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000774 return __n <= base::__sz() / 2 ? _VSTD::next(begin(), __n)
775 : _VSTD::prev(end(), base::__sz() - __n);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000776}
777
778template <class _Tp, class _Alloc>
779list<_Tp, _Alloc>::list(size_type __n)
780{
781 for (; __n > 0; --__n)
Howard Hinnant73d21a42010-09-04 23:28:19 +0000782#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000783 emplace_back();
784#else
785 push_back(value_type());
786#endif
787}
788
789template <class _Tp, class _Alloc>
790list<_Tp, _Alloc>::list(size_type __n, const value_type& __x)
791{
792 for (; __n > 0; --__n)
793 push_back(__x);
794}
795
796template <class _Tp, class _Alloc>
797list<_Tp, _Alloc>::list(size_type __n, const value_type& __x, const allocator_type& __a)
798 : base(__a)
799{
800 for (; __n > 0; --__n)
801 push_back(__x);
802}
803
804template <class _Tp, class _Alloc>
805template <class _InpIter>
806list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l,
807 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
808{
809 for (; __f != __l; ++__f)
810 push_back(*__f);
811}
812
813template <class _Tp, class _Alloc>
814template <class _InpIter>
815list<_Tp, _Alloc>::list(_InpIter __f, _InpIter __l, const allocator_type& __a,
816 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
817 : base(__a)
818{
819 for (; __f != __l; ++__f)
820 push_back(*__f);
821}
822
823template <class _Tp, class _Alloc>
824list<_Tp, _Alloc>::list(const list& __c)
825 : base(allocator_type(
826 __node_alloc_traits::select_on_container_copy_construction(
827 __c.__node_alloc())))
828{
829 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
830 push_back(*__i);
831}
832
833template <class _Tp, class _Alloc>
834list<_Tp, _Alloc>::list(const list& __c, const allocator_type& __a)
835 : base(__a)
836{
837 for (const_iterator __i = __c.begin(), __e = __c.end(); __i != __e; ++__i)
838 push_back(*__i);
839}
840
Howard Hinnante3e32912011-08-12 21:56:02 +0000841#ifndef _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
842
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000843template <class _Tp, class _Alloc>
844list<_Tp, _Alloc>::list(initializer_list<value_type> __il, const allocator_type& __a)
845 : base(__a)
846{
847 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
848 __e = __il.end(); __i != __e; ++__i)
849 push_back(*__i);
850}
851
852template <class _Tp, class _Alloc>
853list<_Tp, _Alloc>::list(initializer_list<value_type> __il)
854{
855 for (typename initializer_list<value_type>::const_iterator __i = __il.begin(),
856 __e = __il.end(); __i != __e; ++__i)
857 push_back(*__i);
858}
859
Howard Hinnante3e32912011-08-12 21:56:02 +0000860#endif // _LIBCPP_HAS_NO_GENERALIZED_INITIALIZERS
861
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000862template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000863inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000864list<_Tp, _Alloc>&
865list<_Tp, _Alloc>::operator=(const list& __c)
866{
867 if (this != &__c)
868 {
869 base::__copy_assign_alloc(__c);
870 assign(__c.begin(), __c.end());
871 }
872 return *this;
873}
874
Howard Hinnant73d21a42010-09-04 23:28:19 +0000875#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000876
877template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000878inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000879list<_Tp, _Alloc>::list(list&& __c)
Howard Hinnantc5607272011-06-03 17:30:28 +0000880 _NOEXCEPT_(is_nothrow_move_constructible<__node_allocator>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000881 : base(allocator_type(_VSTD::move(__c.__node_alloc())))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000882{
883 splice(end(), __c);
884}
885
886template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000887inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000888list<_Tp, _Alloc>::list(list&& __c, const allocator_type& __a)
889 : base(__a)
890{
891 if (__a == __c.get_allocator())
892 splice(end(), __c);
893 else
894 {
895 typedef move_iterator<iterator> _I;
896 assign(_I(__c.begin()), _I(__c.end()));
897 }
898}
899
900template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000901inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000902list<_Tp, _Alloc>&
903list<_Tp, _Alloc>::operator=(list&& __c)
Howard Hinnantc5607272011-06-03 17:30:28 +0000904 _NOEXCEPT_(
905 __node_alloc_traits::propagate_on_container_move_assignment::value &&
906 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000907{
908 __move_assign(__c, integral_constant<bool,
909 __node_alloc_traits::propagate_on_container_move_assignment::value>());
910 return *this;
911}
912
913template <class _Tp, class _Alloc>
914void
915list<_Tp, _Alloc>::__move_assign(list& __c, false_type)
916{
917 if (base::__node_alloc() != __c.__node_alloc())
918 {
919 typedef move_iterator<iterator> _I;
920 assign(_I(__c.begin()), _I(__c.end()));
921 }
922 else
923 __move_assign(__c, true_type());
924}
925
926template <class _Tp, class _Alloc>
927void
928list<_Tp, _Alloc>::__move_assign(list& __c, true_type)
Howard Hinnantc5607272011-06-03 17:30:28 +0000929 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000930{
931 clear();
932 base::__move_assign_alloc(__c);
933 splice(end(), __c);
934}
935
Howard Hinnant73d21a42010-09-04 23:28:19 +0000936#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000937
938template <class _Tp, class _Alloc>
939template <class _InpIter>
940void
941list<_Tp, _Alloc>::assign(_InpIter __f, _InpIter __l,
942 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
943{
944 iterator __i = begin();
945 iterator __e = end();
946 for (; __f != __l && __i != __e; ++__f, ++__i)
947 *__i = *__f;
948 if (__i == __e)
949 insert(__e, __f, __l);
950 else
951 erase(__i, __e);
952}
953
954template <class _Tp, class _Alloc>
955void
956list<_Tp, _Alloc>::assign(size_type __n, const value_type& __x)
957{
958 iterator __i = begin();
959 iterator __e = end();
960 for (; __n > 0 && __i != __e; --__n, ++__i)
961 *__i = __x;
962 if (__i == __e)
963 insert(__e, __n, __x);
964 else
965 erase(__i, __e);
966}
967
968template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +0000969inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000970_Alloc
Howard Hinnantc5607272011-06-03 17:30:28 +0000971list<_Tp, _Alloc>::get_allocator() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000972{
973 return allocator_type(base::__node_alloc());
974}
975
976template <class _Tp, class _Alloc>
977typename list<_Tp, _Alloc>::iterator
978list<_Tp, _Alloc>::insert(const_iterator __p, const value_type& __x)
979{
980 __node_allocator& __na = base::__node_alloc();
981 typedef __allocator_destructor<__node_allocator> _D;
982 unique_ptr<__node, _D> __hold(__node_alloc_traits::allocate(__na, 1), _D(__na, 1));
983 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +0000984 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000985 __link_nodes(const_cast<__node&>(*__p.__ptr_), *__hold, *__hold);
986 ++base::__sz();
987 return iterator(__hold.release());
988}
989
990template <class _Tp, class _Alloc>
991typename list<_Tp, _Alloc>::iterator
992list<_Tp, _Alloc>::insert(const_iterator __p, size_type __n, const value_type& __x)
993{
994 iterator __r(const_cast<__node_pointer>(__p.__ptr_));
995 if (__n > 0)
996 {
997 size_type __ds = 0;
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 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001002 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001003 ++__ds;
1004 __r = iterator(__hold.get());
1005 __hold.release();
1006 iterator __e = __r;
1007#ifndef _LIBCPP_NO_EXCEPTIONS
1008 try
1009 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001010#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001011 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1012 {
1013 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001014 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001015 __e.__ptr_->__next_ = __hold.get();
1016 __hold->__prev_ = __e.__ptr_;
1017 __hold.release();
1018 }
1019#ifndef _LIBCPP_NO_EXCEPTIONS
1020 }
1021 catch (...)
1022 {
1023 while (true)
1024 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001025 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001026 __node_pointer __prev = __e.__ptr_->__prev_;
1027 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1028 if (__prev == 0)
1029 break;
1030 __e = iterator(__prev);
1031 }
1032 throw;
1033 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001034#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001035 __link_nodes(const_cast<__node&>(*__p.__ptr_), *__r.__ptr_, *__e.__ptr_);
1036 base::__sz() += __ds;
1037 }
1038 return __r;
1039}
1040
1041template <class _Tp, class _Alloc>
1042template <class _InpIter>
1043typename list<_Tp, _Alloc>::iterator
1044list<_Tp, _Alloc>::insert(const_iterator __p, _InpIter __f, _InpIter __l,
1045 typename enable_if<__is_input_iterator<_InpIter>::value>::type*)
1046{
1047 iterator __r(const_cast<__node_pointer>(__p.__ptr_));
1048 if (__f != __l)
1049 {
1050 size_type __ds = 0;
1051 __node_allocator& __na = base::__node_alloc();
1052 typedef __allocator_destructor<__node_allocator> _D;
1053 unique_ptr<__node, _D> __hold(__node_alloc_traits::allocate(__na, 1), _D(__na, 1));
1054 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001055 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001056 ++__ds;
1057 __r = iterator(__hold.get());
1058 __hold.release();
1059 iterator __e = __r;
1060#ifndef _LIBCPP_NO_EXCEPTIONS
1061 try
1062 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001063#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001064 for (++__f; __f != __l; ++__f, ++__e, ++__ds)
1065 {
1066 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001067 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), *__f);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001068 __e.__ptr_->__next_ = __hold.get();
1069 __hold->__prev_ = __e.__ptr_;
1070 __hold.release();
1071 }
1072#ifndef _LIBCPP_NO_EXCEPTIONS
1073 }
1074 catch (...)
1075 {
1076 while (true)
1077 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001078 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001079 __node_pointer __prev = __e.__ptr_->__prev_;
1080 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1081 if (__prev == 0)
1082 break;
1083 __e = iterator(__prev);
1084 }
1085 throw;
1086 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001087#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001088 __link_nodes(const_cast<__node&>(*__p.__ptr_), *__r.__ptr_, *__e.__ptr_);
1089 base::__sz() += __ds;
1090 }
1091 return __r;
1092}
1093
1094template <class _Tp, class _Alloc>
1095void
1096list<_Tp, _Alloc>::push_front(const value_type& __x)
1097{
1098 __node_allocator& __na = base::__node_alloc();
1099 typedef __allocator_destructor<__node_allocator> _D;
1100 unique_ptr<__node, _D> __hold(__node_alloc_traits::allocate(__na, 1), _D(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001101 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001102 __link_nodes(*base::__end_.__next_, *__hold, *__hold);
1103 ++base::__sz();
1104 __hold.release();
1105}
1106
1107template <class _Tp, class _Alloc>
1108void
1109list<_Tp, _Alloc>::push_back(const value_type& __x)
1110{
1111 __node_allocator& __na = base::__node_alloc();
1112 typedef __allocator_destructor<__node_allocator> _D;
1113 unique_ptr<__node, _D> __hold(__node_alloc_traits::allocate(__na, 1), _D(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001114 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001115 __link_nodes(static_cast<__node&>(base::__end_), *__hold, *__hold);
1116 ++base::__sz();
1117 __hold.release();
1118}
1119
Howard Hinnant73d21a42010-09-04 23:28:19 +00001120#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001121
1122template <class _Tp, class _Alloc>
1123void
1124list<_Tp, _Alloc>::push_front(value_type&& __x)
1125{
1126 __node_allocator& __na = base::__node_alloc();
1127 typedef __allocator_destructor<__node_allocator> _D;
1128 unique_ptr<__node, _D> __hold(__node_alloc_traits::allocate(__na, 1), _D(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001129 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001130 __link_nodes(*base::__end_.__next_, *__hold, *__hold);
1131 ++base::__sz();
1132 __hold.release();
1133}
1134
1135template <class _Tp, class _Alloc>
1136void
1137list<_Tp, _Alloc>::push_back(value_type&& __x)
1138{
1139 __node_allocator& __na = base::__node_alloc();
1140 typedef __allocator_destructor<__node_allocator> _D;
1141 unique_ptr<__node, _D> __hold(__node_alloc_traits::allocate(__na, 1), _D(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001142 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001143 __link_nodes(static_cast<__node&>(base::__end_), *__hold, *__hold);
1144 ++base::__sz();
1145 __hold.release();
1146}
1147
Howard Hinnant73d21a42010-09-04 23:28:19 +00001148#ifndef _LIBCPP_HAS_NO_VARIADICS
1149
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001150template <class _Tp, class _Alloc>
1151template <class... _Args>
1152void
1153list<_Tp, _Alloc>::emplace_front(_Args&&... __args)
1154{
1155 __node_allocator& __na = base::__node_alloc();
1156 typedef __allocator_destructor<__node_allocator> _D;
1157 unique_ptr<__node, _D> __hold(__node_alloc_traits::allocate(__na, 1), _D(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001158 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001159 __link_nodes(*base::__end_.__next_, *__hold, *__hold);
1160 ++base::__sz();
1161 __hold.release();
1162}
1163
1164template <class _Tp, class _Alloc>
1165template <class... _Args>
1166void
1167list<_Tp, _Alloc>::emplace_back(_Args&&... __args)
1168{
1169 __node_allocator& __na = base::__node_alloc();
1170 typedef __allocator_destructor<__node_allocator> _D;
1171 unique_ptr<__node, _D> __hold(__node_alloc_traits::allocate(__na, 1), _D(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001172 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001173 __link_nodes(static_cast<__node&>(base::__end_), *__hold, *__hold);
1174 ++base::__sz();
1175 __hold.release();
1176}
1177
1178template <class _Tp, class _Alloc>
1179template <class... _Args>
1180typename list<_Tp, _Alloc>::iterator
1181list<_Tp, _Alloc>::emplace(const_iterator __p, _Args&&... __args)
1182{
1183 __node_allocator& __na = base::__node_alloc();
1184 typedef __allocator_destructor<__node_allocator> _D;
1185 unique_ptr<__node, _D> __hold(__node_alloc_traits::allocate(__na, 1), _D(__na, 1));
1186 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001187 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001188 __link_nodes(const_cast<__node&>(*__p.__ptr_), *__hold, *__hold);
1189 ++base::__sz();
1190 return iterator(__hold.release());
1191}
1192
Howard Hinnant73d21a42010-09-04 23:28:19 +00001193#endif // _LIBCPP_HAS_NO_VARIADICS
1194
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001195template <class _Tp, class _Alloc>
1196typename list<_Tp, _Alloc>::iterator
1197list<_Tp, _Alloc>::insert(const_iterator __p, value_type&& __x)
1198{
1199 __node_allocator& __na = base::__node_alloc();
1200 typedef __allocator_destructor<__node_allocator> _D;
1201 unique_ptr<__node, _D> __hold(__node_alloc_traits::allocate(__na, 1), _D(__na, 1));
1202 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001203 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), _VSTD::move(__x));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001204 __link_nodes(const_cast<__node&>(*__p.__ptr_), *__hold, *__hold);
1205 ++base::__sz();
1206 return iterator(__hold.release());
1207}
1208
Howard Hinnant73d21a42010-09-04 23:28:19 +00001209#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001210
1211template <class _Tp, class _Alloc>
1212void
1213list<_Tp, _Alloc>::pop_front()
1214{
1215 __node_allocator& __na = base::__node_alloc();
1216 __node& __n = *base::__end_.__next_;
1217 base::__unlink_nodes(__n, __n);
1218 --base::__sz();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001219 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n.__value_));
1220 __node_alloc_traits::deallocate(__na, _VSTD::addressof(__n), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001221}
1222
1223template <class _Tp, class _Alloc>
1224void
1225list<_Tp, _Alloc>::pop_back()
1226{
1227 __node_allocator& __na = base::__node_alloc();
1228 __node& __n = *base::__end_.__prev_;
1229 base::__unlink_nodes(__n, __n);
1230 --base::__sz();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001231 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n.__value_));
1232 __node_alloc_traits::deallocate(__na, _VSTD::addressof(__n), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001233}
1234
1235template <class _Tp, class _Alloc>
1236typename list<_Tp, _Alloc>::iterator
1237list<_Tp, _Alloc>::erase(const_iterator __p)
1238{
1239 __node_allocator& __na = base::__node_alloc();
1240 __node& __n = const_cast<__node&>(*__p.__ptr_);
1241 __node_pointer __r = __n.__next_;
1242 base::__unlink_nodes(__n, __n);
1243 --base::__sz();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001244 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n.__value_));
1245 __node_alloc_traits::deallocate(__na, _VSTD::addressof(__n), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001246 return iterator(__r);
1247}
1248
1249template <class _Tp, class _Alloc>
1250typename list<_Tp, _Alloc>::iterator
1251list<_Tp, _Alloc>::erase(const_iterator __f, const_iterator __l)
1252{
1253 if (__f != __l)
1254 {
1255 __node_allocator& __na = base::__node_alloc();
1256 base::__unlink_nodes(const_cast<__node&>(*__f.__ptr_), *__l.__ptr_->__prev_);
1257 while (__f != __l)
1258 {
1259 __node& __n = const_cast<__node&>(*__f.__ptr_);
1260 ++__f;
1261 --base::__sz();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001262 __node_alloc_traits::destroy(__na, _VSTD::addressof(__n.__value_));
1263 __node_alloc_traits::deallocate(__na, _VSTD::addressof(__n), 1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001264 }
1265 }
1266 return iterator(const_cast<__node_pointer>(__l.__ptr_));
1267}
1268
1269template <class _Tp, class _Alloc>
1270void
1271list<_Tp, _Alloc>::resize(size_type __n)
1272{
1273 if (__n < base::__sz())
1274 erase(__iterator(__n), end());
1275 else if (__n > base::__sz())
1276 {
1277 __n -= base::__sz();
1278 size_type __ds = 0;
1279 __node_allocator& __na = base::__node_alloc();
1280 typedef __allocator_destructor<__node_allocator> _D;
1281 unique_ptr<__node, _D> __hold(__node_alloc_traits::allocate(__na, 1), _D(__na, 1));
1282 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001283 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001284 ++__ds;
1285 iterator __r = iterator(__hold.release());
1286 iterator __e = __r;
1287#ifndef _LIBCPP_NO_EXCEPTIONS
1288 try
1289 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001290#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001291 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1292 {
1293 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001294 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001295 __e.__ptr_->__next_ = __hold.get();
1296 __hold->__prev_ = __e.__ptr_;
1297 __hold.release();
1298 }
1299#ifndef _LIBCPP_NO_EXCEPTIONS
1300 }
1301 catch (...)
1302 {
1303 while (true)
1304 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001305 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001306 __node_pointer __prev = __e.__ptr_->__prev_;
1307 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1308 if (__prev == 0)
1309 break;
1310 __e = iterator(__prev);
1311 }
1312 throw;
1313 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001314#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001315 __link_nodes(static_cast<__node&>(base::__end_), *__r.__ptr_, *__e.__ptr_);
1316 base::__sz() += __ds;
1317 }
1318}
1319
1320template <class _Tp, class _Alloc>
1321void
1322list<_Tp, _Alloc>::resize(size_type __n, const value_type& __x)
1323{
1324 if (__n < base::__sz())
1325 erase(__iterator(__n), end());
1326 else if (__n > base::__sz())
1327 {
1328 __n -= base::__sz();
1329 size_type __ds = 0;
1330 __node_allocator& __na = base::__node_alloc();
1331 typedef __allocator_destructor<__node_allocator> _D;
1332 unique_ptr<__node, _D> __hold(__node_alloc_traits::allocate(__na, 1), _D(__na, 1));
1333 __hold->__prev_ = 0;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001334 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001335 ++__ds;
1336 iterator __r = iterator(__hold.release());
1337 iterator __e = __r;
1338#ifndef _LIBCPP_NO_EXCEPTIONS
1339 try
1340 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001341#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001342 for (--__n; __n != 0; --__n, ++__e, ++__ds)
1343 {
1344 __hold.reset(__node_alloc_traits::allocate(__na, 1));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001345 __node_alloc_traits::construct(__na, _VSTD::addressof(__hold->__value_), __x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001346 __e.__ptr_->__next_ = __hold.get();
1347 __hold->__prev_ = __e.__ptr_;
1348 __hold.release();
1349 }
1350#ifndef _LIBCPP_NO_EXCEPTIONS
1351 }
1352 catch (...)
1353 {
1354 while (true)
1355 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001356 __node_alloc_traits::destroy(__na, _VSTD::addressof(*__e));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001357 __node_pointer __prev = __e.__ptr_->__prev_;
1358 __node_alloc_traits::deallocate(__na, __e.__ptr_, 1);
1359 if (__prev == 0)
1360 break;
1361 __e = iterator(__prev);
1362 }
1363 throw;
1364 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001365#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001366 __link_nodes(static_cast<__node&>(base::__end_), *__r.__ptr_, *__e.__ptr_);
1367 base::__sz() += __ds;
Howard Hinnant324bb032010-08-22 00:02:43 +00001368 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001369}
1370
1371template <class _Tp, class _Alloc>
1372void
1373list<_Tp, _Alloc>::splice(const_iterator __p, list& __c)
1374{
1375 if (!__c.empty())
1376 {
1377 __node& __f = *__c.__end_.__next_;
1378 __node& __l = *__c.__end_.__prev_;
1379 base::__unlink_nodes(__f, __l);
1380 __link_nodes(const_cast<__node&>(*__p.__ptr_), __f, __l);
1381 base::__sz() += __c.__sz();
1382 __c.__sz() = 0;
1383 }
1384}
1385
1386template <class _Tp, class _Alloc>
1387void
1388list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __i)
1389{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001390 if (__p != __i && __p != _VSTD::next(__i))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001391 {
1392 __node& __f = const_cast<__node&>(*__i.__ptr_);
1393 base::__unlink_nodes(__f, __f);
1394 __link_nodes(const_cast<__node&>(*__p.__ptr_), __f, __f);
1395 --__c.__sz();
1396 ++base::__sz();
1397 }
1398}
1399
1400template <class _Tp, class _Alloc>
1401void
1402list<_Tp, _Alloc>::splice(const_iterator __p, list& __c, const_iterator __f, const_iterator __l)
1403{
1404 if (__f != __l)
1405 {
1406 if (this != &__c)
1407 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001408 size_type __s = _VSTD::distance(__f, __l);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001409 __c.__sz() -= __s;
1410 base::__sz() += __s;
1411 }
1412 __node& __first = const_cast<__node&>(*__f.__ptr_);
1413 --__l;
1414 __node& __last = const_cast<__node&>(*__l.__ptr_);
1415 base::__unlink_nodes(__first, __last);
1416 __link_nodes(const_cast<__node&>(*__p.__ptr_), __first, __last);
1417 }
1418}
1419
1420template <class _Tp, class _Alloc>
1421void
1422list<_Tp, _Alloc>::remove(const value_type& __x)
1423{
1424 for (iterator __i = begin(), __e = end(); __i != __e;)
1425 {
1426 if (*__i == __x)
1427 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001428 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001429 for (; __j != __e && *__j == __x; ++__j)
1430 ;
1431 __i = erase(__i, __j);
1432 }
1433 else
1434 ++__i;
1435 }
1436}
1437
1438template <class _Tp, class _Alloc>
1439template <class _Pred>
1440void
1441list<_Tp, _Alloc>::remove_if(_Pred __pred)
1442{
1443 for (iterator __i = begin(), __e = end(); __i != __e;)
1444 {
1445 if (__pred(*__i))
1446 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001447 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001448 for (; __j != __e && __pred(*__j); ++__j)
1449 ;
1450 __i = erase(__i, __j);
1451 }
1452 else
1453 ++__i;
1454 }
1455}
1456
1457template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001458inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001459void
1460list<_Tp, _Alloc>::unique()
1461{
1462 unique(__equal_to<value_type>());
1463}
1464
1465template <class _Tp, class _Alloc>
1466template <class _BinaryPred>
1467void
1468list<_Tp, _Alloc>::unique(_BinaryPred __binary_pred)
1469{
1470 for (iterator __i = begin(), __e = end(); __i != __e;)
1471 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001472 iterator __j = _VSTD::next(__i);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001473 for (; __j != __e && __binary_pred(*__i, *__j); ++__j)
1474 ;
1475 if (++__i != __j)
1476 __i = erase(__i, __j);
1477 }
1478}
1479
1480template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001481inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001482void
1483list<_Tp, _Alloc>::merge(list& __c)
1484{
1485 merge(__c, __less<value_type>());
1486}
1487
1488template <class _Tp, class _Alloc>
1489template <class _Comp>
1490void
1491list<_Tp, _Alloc>::merge(list& __c, _Comp __comp)
1492{
1493 if (this != &__c)
1494 {
1495 iterator __f1 = begin();
1496 iterator __e1 = end();
1497 iterator __f2 = __c.begin();
1498 iterator __e2 = __c.end();
1499 while (__f1 != __e1 && __f2 != __e2)
1500 {
1501 if (__comp(*__f2, *__f1))
1502 {
1503 size_type __ds = 1;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001504 iterator __m2 = _VSTD::next(__f2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001505 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2, ++__ds)
1506 ;
1507 base::__sz() += __ds;
1508 __c.__sz() -= __ds;
1509 __node& __f = *__f2.__ptr_;
1510 __node& __l = *__m2.__ptr_->__prev_;
1511 __f2 = __m2;
1512 base::__unlink_nodes(__f, __l);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001513 __m2 = _VSTD::next(__f1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001514 __link_nodes(*__f1.__ptr_, __f, __l);
1515 __f1 = __m2;
1516 }
1517 else
1518 ++__f1;
1519 }
1520 splice(__e1, __c);
1521 }
1522}
1523
1524template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001525inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001526void
1527list<_Tp, _Alloc>::sort()
1528{
1529 sort(__less<value_type>());
1530}
1531
1532template <class _Tp, class _Alloc>
1533template <class _Comp>
Howard Hinnant82894812010-09-22 16:48:34 +00001534inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001535void
1536list<_Tp, _Alloc>::sort(_Comp __comp)
1537{
1538 __sort(begin(), end(), base::__sz(), __comp);
1539}
1540
1541template <class _Tp, class _Alloc>
1542template <class _Comp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001543typename list<_Tp, _Alloc>::iterator
1544list<_Tp, _Alloc>::__sort(iterator __f1, iterator __e2, size_type __n, _Comp& __comp)
1545{
1546 switch (__n)
1547 {
1548 case 0:
1549 case 1:
1550 return __f1;
1551 case 2:
1552 if (__comp(*--__e2, *__f1))
1553 {
1554 __node& __f = *__e2.__ptr_;
1555 base::__unlink_nodes(__f, __f);
1556 __link_nodes(*__f1.__ptr_, __f, __f);
1557 return __e2;
1558 }
1559 return __f1;
1560 }
1561 size_type __n2 = __n / 2;
Howard Hinnant0949eed2011-06-30 21:18:19 +00001562 iterator __e1 = _VSTD::next(__f1, __n2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001563 iterator __r = __f1 = __sort(__f1, __e1, __n2, __comp);
1564 iterator __f2 = __e1 = __sort(__e1, __e2, __n - __n2, __comp);
1565 if (__comp(*__f2, *__f1))
1566 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001567 iterator __m2 = _VSTD::next(__f2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001568 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
1569 ;
1570 __node& __f = *__f2.__ptr_;
1571 __node& __l = *__m2.__ptr_->__prev_;
1572 __r = __f2;
1573 __e1 = __f2 = __m2;
1574 base::__unlink_nodes(__f, __l);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001575 __m2 = _VSTD::next(__f1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001576 __link_nodes(*__f1.__ptr_, __f, __l);
1577 __f1 = __m2;
1578 }
1579 else
1580 ++__f1;
1581 while (__f1 != __e1 && __f2 != __e2)
1582 {
1583 if (__comp(*__f2, *__f1))
1584 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001585 iterator __m2 = _VSTD::next(__f2);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001586 for (; __m2 != __e2 && __comp(*__m2, *__f1); ++__m2)
1587 ;
1588 __node& __f = *__f2.__ptr_;
1589 __node& __l = *__m2.__ptr_->__prev_;
1590 if (__e1 == __f2)
1591 __e1 = __m2;
1592 __f2 = __m2;
1593 base::__unlink_nodes(__f, __l);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001594 __m2 = _VSTD::next(__f1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001595 __link_nodes(*__f1.__ptr_, __f, __l);
1596 __f1 = __m2;
1597 }
1598 else
1599 ++__f1;
1600 }
1601 return __r;
1602}
1603
1604template <class _Tp, class _Alloc>
1605void
Howard Hinnantc5607272011-06-03 17:30:28 +00001606list<_Tp, _Alloc>::reverse() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001607{
1608 if (base::__sz() > 1)
1609 {
1610 iterator __e = end();
1611 for (iterator __i = begin(); __i != __e; --__i)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001612 _VSTD::swap(__i.__ptr_->__prev_, __i.__ptr_->__next_);
1613 _VSTD::swap(__e.__ptr_->__prev_, __e.__ptr_->__next_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001614 }
1615}
1616
1617template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001618inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001619bool
1620operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1621{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001622 return __x.size() == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001623}
1624
1625template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001626inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001627bool
1628operator< (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1629{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001630 return _VSTD::lexicographical_compare(__x.begin(), __x.end(), __y.begin(), __y.end());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001631}
1632
1633template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001634inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001635bool
1636operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1637{
1638 return !(__x == __y);
1639}
1640
1641template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001642inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001643bool
1644operator> (const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1645{
1646 return __y < __x;
1647}
1648
1649template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001650inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001651bool
1652operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1653{
1654 return !(__x < __y);
1655}
1656
1657template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001658inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001659bool
1660operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1661{
1662 return !(__y < __x);
1663}
1664
1665template <class _Tp, class _Alloc>
Howard Hinnant82894812010-09-22 16:48:34 +00001666inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001667void
1668swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
Howard Hinnantc5607272011-06-03 17:30:28 +00001669 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001670{
1671 __x.swap(__y);
1672}
1673
1674_LIBCPP_END_NAMESPACE_STD
1675
1676#endif // _LIBCPP_LIST