re PR libstdc++/43813 ([DR1234] vector<T*>(3, NULL) fails to compile)
[gcc.git] / libstdc++-v3 / include / bits / stl_list.h
1 // List implementation -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 // 2011, 2012 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
25
26 /*
27 *
28 * Copyright (c) 1994
29 * Hewlett-Packard Company
30 *
31 * Permission to use, copy, modify, distribute and sell this software
32 * and its documentation for any purpose is hereby granted without fee,
33 * provided that the above copyright notice appear in all copies and
34 * that both that copyright notice and this permission notice appear
35 * in supporting documentation. Hewlett-Packard Company makes no
36 * representations about the suitability of this software for any
37 * purpose. It is provided "as is" without express or implied warranty.
38 *
39 *
40 * Copyright (c) 1996,1997
41 * Silicon Graphics Computer Systems, Inc.
42 *
43 * Permission to use, copy, modify, distribute and sell this software
44 * and its documentation for any purpose is hereby granted without fee,
45 * provided that the above copyright notice appear in all copies and
46 * that both that copyright notice and this permission notice appear
47 * in supporting documentation. Silicon Graphics makes no
48 * representations about the suitability of this software for any
49 * purpose. It is provided "as is" without express or implied warranty.
50 */
51
52 /** @file bits/stl_list.h
53 * This is an internal header file, included by other library headers.
54 * Do not attempt to use it directly. @headername{list}
55 */
56
57 #ifndef _STL_LIST_H
58 #define _STL_LIST_H 1
59
60 #include <bits/concept_check.h>
61 #ifdef __GXX_EXPERIMENTAL_CXX0X__
62 #include <initializer_list>
63 #endif
64
65 namespace std _GLIBCXX_VISIBILITY(default)
66 {
67 namespace __detail
68 {
69 _GLIBCXX_BEGIN_NAMESPACE_VERSION
70
71 // Supporting structures are split into common and templated
72 // types; the latter publicly inherits from the former in an
73 // effort to reduce code duplication. This results in some
74 // "needless" static_cast'ing later on, but it's all safe
75 // downcasting.
76
77 /// Common part of a node in the %list.
78 struct _List_node_base
79 {
80 _List_node_base* _M_next;
81 _List_node_base* _M_prev;
82
83 static void
84 swap(_List_node_base& __x, _List_node_base& __y) _GLIBCXX_USE_NOEXCEPT;
85
86 void
87 _M_transfer(_List_node_base* const __first,
88 _List_node_base* const __last) _GLIBCXX_USE_NOEXCEPT;
89
90 void
91 _M_reverse() _GLIBCXX_USE_NOEXCEPT;
92
93 void
94 _M_hook(_List_node_base* const __position) _GLIBCXX_USE_NOEXCEPT;
95
96 void
97 _M_unhook() _GLIBCXX_USE_NOEXCEPT;
98 };
99
100 _GLIBCXX_END_NAMESPACE_VERSION
101 } // namespace detail
102
103 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
104
105 /// An actual node in the %list.
106 template<typename _Tp>
107 struct _List_node : public __detail::_List_node_base
108 {
109 ///< User's data.
110 _Tp _M_data;
111
112 #ifdef __GXX_EXPERIMENTAL_CXX0X__
113 template<typename... _Args>
114 _List_node(_Args&&... __args)
115 : __detail::_List_node_base(), _M_data(std::forward<_Args>(__args)...)
116 { }
117 #endif
118 };
119
120 /**
121 * @brief A list::iterator.
122 *
123 * All the functions are op overloads.
124 */
125 template<typename _Tp>
126 struct _List_iterator
127 {
128 typedef _List_iterator<_Tp> _Self;
129 typedef _List_node<_Tp> _Node;
130
131 typedef ptrdiff_t difference_type;
132 typedef std::bidirectional_iterator_tag iterator_category;
133 typedef _Tp value_type;
134 typedef _Tp* pointer;
135 typedef _Tp& reference;
136
137 _List_iterator()
138 : _M_node() { }
139
140 explicit
141 _List_iterator(__detail::_List_node_base* __x)
142 : _M_node(__x) { }
143
144 // Must downcast from _List_node_base to _List_node to get to _M_data.
145 reference
146 operator*() const
147 { return static_cast<_Node*>(_M_node)->_M_data; }
148
149 pointer
150 operator->() const
151 { return std::__addressof(static_cast<_Node*>(_M_node)->_M_data); }
152
153 _Self&
154 operator++()
155 {
156 _M_node = _M_node->_M_next;
157 return *this;
158 }
159
160 _Self
161 operator++(int)
162 {
163 _Self __tmp = *this;
164 _M_node = _M_node->_M_next;
165 return __tmp;
166 }
167
168 _Self&
169 operator--()
170 {
171 _M_node = _M_node->_M_prev;
172 return *this;
173 }
174
175 _Self
176 operator--(int)
177 {
178 _Self __tmp = *this;
179 _M_node = _M_node->_M_prev;
180 return __tmp;
181 }
182
183 bool
184 operator==(const _Self& __x) const
185 { return _M_node == __x._M_node; }
186
187 bool
188 operator!=(const _Self& __x) const
189 { return _M_node != __x._M_node; }
190
191 // The only member points to the %list element.
192 __detail::_List_node_base* _M_node;
193 };
194
195 /**
196 * @brief A list::const_iterator.
197 *
198 * All the functions are op overloads.
199 */
200 template<typename _Tp>
201 struct _List_const_iterator
202 {
203 typedef _List_const_iterator<_Tp> _Self;
204 typedef const _List_node<_Tp> _Node;
205 typedef _List_iterator<_Tp> iterator;
206
207 typedef ptrdiff_t difference_type;
208 typedef std::bidirectional_iterator_tag iterator_category;
209 typedef _Tp value_type;
210 typedef const _Tp* pointer;
211 typedef const _Tp& reference;
212
213 _List_const_iterator()
214 : _M_node() { }
215
216 explicit
217 _List_const_iterator(const __detail::_List_node_base* __x)
218 : _M_node(__x) { }
219
220 _List_const_iterator(const iterator& __x)
221 : _M_node(__x._M_node) { }
222
223 // Must downcast from List_node_base to _List_node to get to
224 // _M_data.
225 reference
226 operator*() const
227 { return static_cast<_Node*>(_M_node)->_M_data; }
228
229 pointer
230 operator->() const
231 { return std::__addressof(static_cast<_Node*>(_M_node)->_M_data); }
232
233 _Self&
234 operator++()
235 {
236 _M_node = _M_node->_M_next;
237 return *this;
238 }
239
240 _Self
241 operator++(int)
242 {
243 _Self __tmp = *this;
244 _M_node = _M_node->_M_next;
245 return __tmp;
246 }
247
248 _Self&
249 operator--()
250 {
251 _M_node = _M_node->_M_prev;
252 return *this;
253 }
254
255 _Self
256 operator--(int)
257 {
258 _Self __tmp = *this;
259 _M_node = _M_node->_M_prev;
260 return __tmp;
261 }
262
263 bool
264 operator==(const _Self& __x) const
265 { return _M_node == __x._M_node; }
266
267 bool
268 operator!=(const _Self& __x) const
269 { return _M_node != __x._M_node; }
270
271 // The only member points to the %list element.
272 const __detail::_List_node_base* _M_node;
273 };
274
275 template<typename _Val>
276 inline bool
277 operator==(const _List_iterator<_Val>& __x,
278 const _List_const_iterator<_Val>& __y)
279 { return __x._M_node == __y._M_node; }
280
281 template<typename _Val>
282 inline bool
283 operator!=(const _List_iterator<_Val>& __x,
284 const _List_const_iterator<_Val>& __y)
285 { return __x._M_node != __y._M_node; }
286
287
288 /// See bits/stl_deque.h's _Deque_base for an explanation.
289 template<typename _Tp, typename _Alloc>
290 class _List_base
291 {
292 protected:
293 // NOTA BENE
294 // The stored instance is not actually of "allocator_type"'s
295 // type. Instead we rebind the type to
296 // Allocator<List_node<Tp>>, which according to [20.1.5]/4
297 // should probably be the same. List_node<Tp> is not the same
298 // size as Tp (it's two pointers larger), and specializations on
299 // Tp may go unused because List_node<Tp> is being bound
300 // instead.
301 //
302 // We put this to the test in the constructors and in
303 // get_allocator, where we use conversions between
304 // allocator_type and _Node_alloc_type. The conversion is
305 // required by table 32 in [20.1.5].
306 typedef typename _Alloc::template rebind<_List_node<_Tp> >::other
307 _Node_alloc_type;
308
309 typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
310
311 struct _List_impl
312 : public _Node_alloc_type
313 {
314 __detail::_List_node_base _M_node;
315
316 #ifdef __GXX_EXPERIMENTAL_CXX0X__
317 size_t _M_size;
318 #endif
319
320 _List_impl()
321 : _Node_alloc_type(), _M_node()
322 #ifdef __GXX_EXPERIMENTAL_CXX0X__
323 , _M_size(0)
324 #endif
325 { }
326
327 _List_impl(const _Node_alloc_type& __a)
328 : _Node_alloc_type(__a), _M_node()
329 #ifdef __GXX_EXPERIMENTAL_CXX0X__
330 , _M_size(0)
331 #endif
332 { }
333
334 #ifdef __GXX_EXPERIMENTAL_CXX0X__
335 _List_impl(_Node_alloc_type&& __a)
336 : _Node_alloc_type(std::move(__a)), _M_node(), _M_size(0)
337 { }
338 #endif
339 };
340
341 _List_impl _M_impl;
342
343 _List_node<_Tp>*
344 _M_get_node()
345 {
346 _List_node<_Tp>* __tmp = _M_impl._Node_alloc_type::allocate(1);
347 #ifdef __GXX_EXPERIMENTAL_CXX0X__
348 ++_M_impl._M_size;
349 #endif
350 return __tmp;
351 }
352
353 void
354 _M_put_node(_List_node<_Tp>* __p)
355 {
356 _M_impl._Node_alloc_type::deallocate(__p, 1);
357 #ifdef __GXX_EXPERIMENTAL_CXX0X__
358 --_M_impl._M_size;
359 #endif
360 }
361
362 public:
363 typedef _Alloc allocator_type;
364
365 _Node_alloc_type&
366 _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
367 { return *static_cast<_Node_alloc_type*>(&_M_impl); }
368
369 const _Node_alloc_type&
370 _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
371 { return *static_cast<const _Node_alloc_type*>(&_M_impl); }
372
373 _Tp_alloc_type
374 _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
375 { return _Tp_alloc_type(_M_get_Node_allocator()); }
376
377 allocator_type
378 get_allocator() const _GLIBCXX_NOEXCEPT
379 { return allocator_type(_M_get_Node_allocator()); }
380
381 _List_base()
382 : _M_impl()
383 { _M_init(); }
384
385 _List_base(const _Node_alloc_type& __a)
386 : _M_impl(__a)
387 { _M_init(); }
388
389 #ifdef __GXX_EXPERIMENTAL_CXX0X__
390 _List_base(_List_base&& __x)
391 : _M_impl(std::move(__x._M_get_Node_allocator()))
392 {
393 _M_init();
394 __detail::_List_node_base::swap(_M_impl._M_node, __x._M_impl._M_node);
395 std::swap(_M_impl._M_size, __x._M_impl._M_size);
396 }
397 #endif
398
399 // This is what actually destroys the list.
400 ~_List_base() _GLIBCXX_NOEXCEPT
401 { _M_clear(); }
402
403 void
404 _M_clear();
405
406 void
407 _M_init()
408 {
409 this->_M_impl._M_node._M_next = &this->_M_impl._M_node;
410 this->_M_impl._M_node._M_prev = &this->_M_impl._M_node;
411 }
412 };
413
414 /**
415 * @brief A standard container with linear time access to elements,
416 * and fixed time insertion/deletion at any point in the sequence.
417 *
418 * @ingroup sequences
419 *
420 * Meets the requirements of a <a href="tables.html#65">container</a>, a
421 * <a href="tables.html#66">reversible container</a>, and a
422 * <a href="tables.html#67">sequence</a>, including the
423 * <a href="tables.html#68">optional sequence requirements</a> with the
424 * %exception of @c at and @c operator[].
425 *
426 * This is a @e doubly @e linked %list. Traversal up and down the
427 * %list requires linear time, but adding and removing elements (or
428 * @e nodes) is done in constant time, regardless of where the
429 * change takes place. Unlike std::vector and std::deque,
430 * random-access iterators are not provided, so subscripting ( @c
431 * [] ) access is not allowed. For algorithms which only need
432 * sequential access, this lack makes no difference.
433 *
434 * Also unlike the other standard containers, std::list provides
435 * specialized algorithms %unique to linked lists, such as
436 * splicing, sorting, and in-place reversal.
437 *
438 * A couple points on memory allocation for list<Tp>:
439 *
440 * First, we never actually allocate a Tp, we allocate
441 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
442 * that after elements from %list<X,Alloc1> are spliced into
443 * %list<X,Alloc2>, destroying the memory of the second %list is a
444 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
445 *
446 * Second, a %list conceptually represented as
447 * @code
448 * A <---> B <---> C <---> D
449 * @endcode
450 * is actually circular; a link exists between A and D. The %list
451 * class holds (as its only data member) a private list::iterator
452 * pointing to @e D, not to @e A! To get to the head of the %list,
453 * we start at the tail and move forward by one. When this member
454 * iterator's next/previous pointers refer to itself, the %list is
455 * %empty.
456 */
457 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
458 class list : protected _List_base<_Tp, _Alloc>
459 {
460 // concept requirements
461 typedef typename _Alloc::value_type _Alloc_value_type;
462 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
463 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
464
465 typedef _List_base<_Tp, _Alloc> _Base;
466 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
467 typedef typename _Base::_Node_alloc_type _Node_alloc_type;
468
469 public:
470 typedef _Tp value_type;
471 typedef typename _Tp_alloc_type::pointer pointer;
472 typedef typename _Tp_alloc_type::const_pointer const_pointer;
473 typedef typename _Tp_alloc_type::reference reference;
474 typedef typename _Tp_alloc_type::const_reference const_reference;
475 typedef _List_iterator<_Tp> iterator;
476 typedef _List_const_iterator<_Tp> const_iterator;
477 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
478 typedef std::reverse_iterator<iterator> reverse_iterator;
479 typedef size_t size_type;
480 typedef ptrdiff_t difference_type;
481 typedef _Alloc allocator_type;
482
483 protected:
484 // Note that pointers-to-_Node's can be ctor-converted to
485 // iterator types.
486 typedef _List_node<_Tp> _Node;
487
488 using _Base::_M_impl;
489 using _Base::_M_put_node;
490 using _Base::_M_get_node;
491 using _Base::_M_get_Tp_allocator;
492 using _Base::_M_get_Node_allocator;
493
494 /**
495 * @param __args An instance of user data.
496 *
497 * Allocates space for a new node and constructs a copy of
498 * @a __args in it.
499 */
500 #ifndef __GXX_EXPERIMENTAL_CXX0X__
501 _Node*
502 _M_create_node(const value_type& __x)
503 {
504 _Node* __p = this->_M_get_node();
505 __try
506 {
507 _M_get_Tp_allocator().construct
508 (std::__addressof(__p->_M_data), __x);
509 }
510 __catch(...)
511 {
512 _M_put_node(__p);
513 __throw_exception_again;
514 }
515 return __p;
516 }
517 #else
518 template<typename... _Args>
519 _Node*
520 _M_create_node(_Args&&... __args)
521 {
522 _Node* __p = this->_M_get_node();
523 __try
524 {
525 _M_get_Node_allocator().construct(__p,
526 std::forward<_Args>(__args)...);
527 }
528 __catch(...)
529 {
530 _M_put_node(__p);
531 __throw_exception_again;
532 }
533 return __p;
534 }
535 #endif
536
537 public:
538 // [23.2.2.1] construct/copy/destroy
539 // (assign() and get_allocator() are also listed in this section)
540 /**
541 * @brief Default constructor creates no elements.
542 */
543 list()
544 : _Base() { }
545
546 /**
547 * @brief Creates a %list with no elements.
548 * @param __a An allocator object.
549 */
550 explicit
551 list(const allocator_type& __a)
552 : _Base(_Node_alloc_type(__a)) { }
553
554 #ifdef __GXX_EXPERIMENTAL_CXX0X__
555 /**
556 * @brief Creates a %list with default constructed elements.
557 * @param __n The number of elements to initially create.
558 *
559 * This constructor fills the %list with @a __n default
560 * constructed elements.
561 */
562 explicit
563 list(size_type __n)
564 : _Base()
565 { _M_default_initialize(__n); }
566
567 /**
568 * @brief Creates a %list with copies of an exemplar element.
569 * @param __n The number of elements to initially create.
570 * @param __value An element to copy.
571 * @param __a An allocator object.
572 *
573 * This constructor fills the %list with @a __n copies of @a __value.
574 */
575 list(size_type __n, const value_type& __value,
576 const allocator_type& __a = allocator_type())
577 : _Base(_Node_alloc_type(__a))
578 { _M_fill_initialize(__n, __value); }
579 #else
580 /**
581 * @brief Creates a %list with copies of an exemplar element.
582 * @param __n The number of elements to initially create.
583 * @param __value An element to copy.
584 * @param __a An allocator object.
585 *
586 * This constructor fills the %list with @a __n copies of @a __value.
587 */
588 explicit
589 list(size_type __n, const value_type& __value = value_type(),
590 const allocator_type& __a = allocator_type())
591 : _Base(_Node_alloc_type(__a))
592 { _M_fill_initialize(__n, __value); }
593 #endif
594
595 /**
596 * @brief %List copy constructor.
597 * @param __x A %list of identical element and allocator types.
598 *
599 * The newly-created %list uses a copy of the allocation object used
600 * by @a __x.
601 */
602 list(const list& __x)
603 : _Base(__x._M_get_Node_allocator())
604 { _M_initialize_dispatch(__x.begin(), __x.end(), __false_type()); }
605
606 #ifdef __GXX_EXPERIMENTAL_CXX0X__
607 /**
608 * @brief %List move constructor.
609 * @param __x A %list of identical element and allocator types.
610 *
611 * The newly-created %list contains the exact contents of @a __x.
612 * The contents of @a __x are a valid, but unspecified %list.
613 */
614 list(list&& __x) noexcept
615 : _Base(std::move(__x)) { }
616
617 /**
618 * @brief Builds a %list from an initializer_list
619 * @param __l An initializer_list of value_type.
620 * @param __a An allocator object.
621 *
622 * Create a %list consisting of copies of the elements in the
623 * initializer_list @a __l. This is linear in __l.size().
624 */
625 list(initializer_list<value_type> __l,
626 const allocator_type& __a = allocator_type())
627 : _Base(_Node_alloc_type(__a))
628 { _M_initialize_dispatch(__l.begin(), __l.end(), __false_type()); }
629 #endif
630
631 /**
632 * @brief Builds a %list from a range.
633 * @param __first An input iterator.
634 * @param __last An input iterator.
635 * @param __a An allocator object.
636 *
637 * Create a %list consisting of copies of the elements from
638 * [@a __first,@a __last). This is linear in N (where N is
639 * distance(@a __first,@a __last)).
640 */
641 #ifdef __GXX_EXPERIMENTAL_CXX0X__
642 template<typename _InputIterator,
643 typename = std::_RequireInputIter<_InputIterator>>
644 list(_InputIterator __first, _InputIterator __last,
645 const allocator_type& __a = allocator_type())
646 : _Base(_Node_alloc_type(__a))
647 { _M_initialize_dispatch(__first, __last, __false_type()); }
648 #else
649 template<typename _InputIterator>
650 list(_InputIterator __first, _InputIterator __last,
651 const allocator_type& __a = allocator_type())
652 : _Base(_Node_alloc_type(__a))
653 {
654 // Check whether it's an integral type. If so, it's not an iterator.
655 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
656 _M_initialize_dispatch(__first, __last, _Integral());
657 }
658 #endif
659
660 /**
661 * No explicit dtor needed as the _Base dtor takes care of
662 * things. The _Base dtor only erases the elements, and note
663 * that if the elements themselves are pointers, the pointed-to
664 * memory is not touched in any way. Managing the pointer is
665 * the user's responsibility.
666 */
667
668 /**
669 * @brief %List assignment operator.
670 * @param __x A %list of identical element and allocator types.
671 *
672 * All the elements of @a __x are copied, but unlike the copy
673 * constructor, the allocator object is not copied.
674 */
675 list&
676 operator=(const list& __x);
677
678 #ifdef __GXX_EXPERIMENTAL_CXX0X__
679 /**
680 * @brief %List move assignment operator.
681 * @param __x A %list of identical element and allocator types.
682 *
683 * The contents of @a __x are moved into this %list (without copying).
684 * @a __x is a valid, but unspecified %list
685 */
686 list&
687 operator=(list&& __x)
688 {
689 // NB: DR 1204.
690 // NB: DR 675.
691 this->clear();
692 this->swap(__x);
693 return *this;
694 }
695
696 /**
697 * @brief %List initializer list assignment operator.
698 * @param __l An initializer_list of value_type.
699 *
700 * Replace the contents of the %list with copies of the elements
701 * in the initializer_list @a __l. This is linear in l.size().
702 */
703 list&
704 operator=(initializer_list<value_type> __l)
705 {
706 this->assign(__l.begin(), __l.end());
707 return *this;
708 }
709 #endif
710
711 /**
712 * @brief Assigns a given value to a %list.
713 * @param __n Number of elements to be assigned.
714 * @param __val Value to be assigned.
715 *
716 * This function fills a %list with @a __n copies of the given
717 * value. Note that the assignment completely changes the %list
718 * and that the resulting %list's size is the same as the number
719 * of elements assigned. Old data may be lost.
720 */
721 void
722 assign(size_type __n, const value_type& __val)
723 { _M_fill_assign(__n, __val); }
724
725 /**
726 * @brief Assigns a range to a %list.
727 * @param __first An input iterator.
728 * @param __last An input iterator.
729 *
730 * This function fills a %list with copies of the elements in the
731 * range [@a __first,@a __last).
732 *
733 * Note that the assignment completely changes the %list and
734 * that the resulting %list's size is the same as the number of
735 * elements assigned. Old data may be lost.
736 */
737 #ifdef __GXX_EXPERIMENTAL_CXX0X__
738 template<typename _InputIterator,
739 typename = std::_RequireInputIter<_InputIterator>>
740 void
741 assign(_InputIterator __first, _InputIterator __last)
742 { _M_assign_dispatch(__first, __last, __false_type()); }
743 #else
744 template<typename _InputIterator>
745 void
746 assign(_InputIterator __first, _InputIterator __last)
747 {
748 // Check whether it's an integral type. If so, it's not an iterator.
749 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
750 _M_assign_dispatch(__first, __last, _Integral());
751 }
752 #endif
753
754 #ifdef __GXX_EXPERIMENTAL_CXX0X__
755 /**
756 * @brief Assigns an initializer_list to a %list.
757 * @param __l An initializer_list of value_type.
758 *
759 * Replace the contents of the %list with copies of the elements
760 * in the initializer_list @a __l. This is linear in __l.size().
761 */
762 void
763 assign(initializer_list<value_type> __l)
764 { this->assign(__l.begin(), __l.end()); }
765 #endif
766
767 /// Get a copy of the memory allocation object.
768 allocator_type
769 get_allocator() const _GLIBCXX_NOEXCEPT
770 { return _Base::get_allocator(); }
771
772 // iterators
773 /**
774 * Returns a read/write iterator that points to the first element in the
775 * %list. Iteration is done in ordinary element order.
776 */
777 iterator
778 begin() _GLIBCXX_NOEXCEPT
779 { return iterator(this->_M_impl._M_node._M_next); }
780
781 /**
782 * Returns a read-only (constant) iterator that points to the
783 * first element in the %list. Iteration is done in ordinary
784 * element order.
785 */
786 const_iterator
787 begin() const _GLIBCXX_NOEXCEPT
788 { return const_iterator(this->_M_impl._M_node._M_next); }
789
790 /**
791 * Returns a read/write iterator that points one past the last
792 * element in the %list. Iteration is done in ordinary element
793 * order.
794 */
795 iterator
796 end() _GLIBCXX_NOEXCEPT
797 { return iterator(&this->_M_impl._M_node); }
798
799 /**
800 * Returns a read-only (constant) iterator that points one past
801 * the last element in the %list. Iteration is done in ordinary
802 * element order.
803 */
804 const_iterator
805 end() const _GLIBCXX_NOEXCEPT
806 { return const_iterator(&this->_M_impl._M_node); }
807
808 /**
809 * Returns a read/write reverse iterator that points to the last
810 * element in the %list. Iteration is done in reverse element
811 * order.
812 */
813 reverse_iterator
814 rbegin() _GLIBCXX_NOEXCEPT
815 { return reverse_iterator(end()); }
816
817 /**
818 * Returns a read-only (constant) reverse iterator that points to
819 * the last element in the %list. Iteration is done in reverse
820 * element order.
821 */
822 const_reverse_iterator
823 rbegin() const _GLIBCXX_NOEXCEPT
824 { return const_reverse_iterator(end()); }
825
826 /**
827 * Returns a read/write reverse iterator that points to one
828 * before the first element in the %list. Iteration is done in
829 * reverse element order.
830 */
831 reverse_iterator
832 rend() _GLIBCXX_NOEXCEPT
833 { return reverse_iterator(begin()); }
834
835 /**
836 * Returns a read-only (constant) reverse iterator that points to one
837 * before the first element in the %list. Iteration is done in reverse
838 * element order.
839 */
840 const_reverse_iterator
841 rend() const _GLIBCXX_NOEXCEPT
842 { return const_reverse_iterator(begin()); }
843
844 #ifdef __GXX_EXPERIMENTAL_CXX0X__
845 /**
846 * Returns a read-only (constant) iterator that points to the
847 * first element in the %list. Iteration is done in ordinary
848 * element order.
849 */
850 const_iterator
851 cbegin() const noexcept
852 { return const_iterator(this->_M_impl._M_node._M_next); }
853
854 /**
855 * Returns a read-only (constant) iterator that points one past
856 * the last element in the %list. Iteration is done in ordinary
857 * element order.
858 */
859 const_iterator
860 cend() const noexcept
861 { return const_iterator(&this->_M_impl._M_node); }
862
863 /**
864 * Returns a read-only (constant) reverse iterator that points to
865 * the last element in the %list. Iteration is done in reverse
866 * element order.
867 */
868 const_reverse_iterator
869 crbegin() const noexcept
870 { return const_reverse_iterator(end()); }
871
872 /**
873 * Returns a read-only (constant) reverse iterator that points to one
874 * before the first element in the %list. Iteration is done in reverse
875 * element order.
876 */
877 const_reverse_iterator
878 crend() const noexcept
879 { return const_reverse_iterator(begin()); }
880 #endif
881
882 // [23.2.2.2] capacity
883 /**
884 * Returns true if the %list is empty. (Thus begin() would equal
885 * end().)
886 */
887 bool
888 empty() const _GLIBCXX_NOEXCEPT
889 { return this->_M_impl._M_node._M_next == &this->_M_impl._M_node; }
890
891 /** Returns the number of elements in the %list. */
892 size_type
893 size() const _GLIBCXX_NOEXCEPT
894 {
895 #ifdef __GXX_EXPERIMENTAL_CXX0X__
896 return this->_M_impl._M_size;
897 #else
898 return std::distance(begin(), end());
899 #endif
900 }
901
902 /** Returns the size() of the largest possible %list. */
903 size_type
904 max_size() const _GLIBCXX_NOEXCEPT
905 { return _M_get_Node_allocator().max_size(); }
906
907 #ifdef __GXX_EXPERIMENTAL_CXX0X__
908 /**
909 * @brief Resizes the %list to the specified number of elements.
910 * @param __new_size Number of elements the %list should contain.
911 *
912 * This function will %resize the %list to the specified number
913 * of elements. If the number is smaller than the %list's
914 * current size the %list is truncated, otherwise default
915 * constructed elements are appended.
916 */
917 void
918 resize(size_type __new_size);
919
920 /**
921 * @brief Resizes the %list to the specified number of elements.
922 * @param __new_size Number of elements the %list should contain.
923 * @param __x Data with which new elements should be populated.
924 *
925 * This function will %resize the %list to the specified number
926 * of elements. If the number is smaller than the %list's
927 * current size the %list is truncated, otherwise the %list is
928 * extended and new elements are populated with given data.
929 */
930 void
931 resize(size_type __new_size, const value_type& __x);
932 #else
933 /**
934 * @brief Resizes the %list to the specified number of elements.
935 * @param __new_size Number of elements the %list should contain.
936 * @param __x Data with which new elements should be populated.
937 *
938 * This function will %resize the %list to the specified number
939 * of elements. If the number is smaller than the %list's
940 * current size the %list is truncated, otherwise the %list is
941 * extended and new elements are populated with given data.
942 */
943 void
944 resize(size_type __new_size, value_type __x = value_type());
945 #endif
946
947 // element access
948 /**
949 * Returns a read/write reference to the data at the first
950 * element of the %list.
951 */
952 reference
953 front()
954 { return *begin(); }
955
956 /**
957 * Returns a read-only (constant) reference to the data at the first
958 * element of the %list.
959 */
960 const_reference
961 front() const
962 { return *begin(); }
963
964 /**
965 * Returns a read/write reference to the data at the last element
966 * of the %list.
967 */
968 reference
969 back()
970 {
971 iterator __tmp = end();
972 --__tmp;
973 return *__tmp;
974 }
975
976 /**
977 * Returns a read-only (constant) reference to the data at the last
978 * element of the %list.
979 */
980 const_reference
981 back() const
982 {
983 const_iterator __tmp = end();
984 --__tmp;
985 return *__tmp;
986 }
987
988 // [23.2.2.3] modifiers
989 /**
990 * @brief Add data to the front of the %list.
991 * @param __x Data to be added.
992 *
993 * This is a typical stack operation. The function creates an
994 * element at the front of the %list and assigns the given data
995 * to it. Due to the nature of a %list this operation can be
996 * done in constant time, and does not invalidate iterators and
997 * references.
998 */
999 void
1000 push_front(const value_type& __x)
1001 { this->_M_insert(begin(), __x); }
1002
1003 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1004 void
1005 push_front(value_type&& __x)
1006 { this->_M_insert(begin(), std::move(__x)); }
1007
1008 template<typename... _Args>
1009 void
1010 emplace_front(_Args&&... __args)
1011 { this->_M_insert(begin(), std::forward<_Args>(__args)...); }
1012 #endif
1013
1014 /**
1015 * @brief Removes first element.
1016 *
1017 * This is a typical stack operation. It shrinks the %list by
1018 * one. Due to the nature of a %list this operation can be done
1019 * in constant time, and only invalidates iterators/references to
1020 * the element being removed.
1021 *
1022 * Note that no data is returned, and if the first element's data
1023 * is needed, it should be retrieved before pop_front() is
1024 * called.
1025 */
1026 void
1027 pop_front()
1028 { this->_M_erase(begin()); }
1029
1030 /**
1031 * @brief Add data to the end of the %list.
1032 * @param __x Data to be added.
1033 *
1034 * This is a typical stack operation. The function creates an
1035 * element at the end of the %list and assigns the given data to
1036 * it. Due to the nature of a %list this operation can be done
1037 * in constant time, and does not invalidate iterators and
1038 * references.
1039 */
1040 void
1041 push_back(const value_type& __x)
1042 { this->_M_insert(end(), __x); }
1043
1044 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1045 void
1046 push_back(value_type&& __x)
1047 { this->_M_insert(end(), std::move(__x)); }
1048
1049 template<typename... _Args>
1050 void
1051 emplace_back(_Args&&... __args)
1052 { this->_M_insert(end(), std::forward<_Args>(__args)...); }
1053 #endif
1054
1055 /**
1056 * @brief Removes last element.
1057 *
1058 * This is a typical stack operation. It shrinks the %list by
1059 * one. Due to the nature of a %list this operation can be done
1060 * in constant time, and only invalidates iterators/references to
1061 * the element being removed.
1062 *
1063 * Note that no data is returned, and if the last element's data
1064 * is needed, it should be retrieved before pop_back() is called.
1065 */
1066 void
1067 pop_back()
1068 { this->_M_erase(iterator(this->_M_impl._M_node._M_prev)); }
1069
1070 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1071 /**
1072 * @brief Constructs object in %list before specified iterator.
1073 * @param __position A const_iterator into the %list.
1074 * @param __args Arguments.
1075 * @return An iterator that points to the inserted data.
1076 *
1077 * This function will insert an object of type T constructed
1078 * with T(std::forward<Args>(args)...) before the specified
1079 * location. Due to the nature of a %list this operation can
1080 * be done in constant time, and does not invalidate iterators
1081 * and references.
1082 */
1083 template<typename... _Args>
1084 iterator
1085 emplace(iterator __position, _Args&&... __args);
1086 #endif
1087
1088 /**
1089 * @brief Inserts given value into %list before specified iterator.
1090 * @param __position An iterator into the %list.
1091 * @param __x Data to be inserted.
1092 * @return An iterator that points to the inserted data.
1093 *
1094 * This function will insert a copy of the given value before
1095 * the specified location. Due to the nature of a %list this
1096 * operation can be done in constant time, and does not
1097 * invalidate iterators and references.
1098 */
1099 iterator
1100 insert(iterator __position, const value_type& __x);
1101
1102 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1103 /**
1104 * @brief Inserts given rvalue into %list before specified iterator.
1105 * @param __position An iterator into the %list.
1106 * @param __x Data to be inserted.
1107 * @return An iterator that points to the inserted data.
1108 *
1109 * This function will insert a copy of the given rvalue before
1110 * the specified location. Due to the nature of a %list this
1111 * operation can be done in constant time, and does not
1112 * invalidate iterators and references.
1113 */
1114 iterator
1115 insert(iterator __position, value_type&& __x)
1116 { return emplace(__position, std::move(__x)); }
1117
1118 /**
1119 * @brief Inserts the contents of an initializer_list into %list
1120 * before specified iterator.
1121 * @param __p An iterator into the %list.
1122 * @param __l An initializer_list of value_type.
1123 *
1124 * This function will insert copies of the data in the
1125 * initializer_list @a l into the %list before the location
1126 * specified by @a p.
1127 *
1128 * This operation is linear in the number of elements inserted and
1129 * does not invalidate iterators and references.
1130 */
1131 void
1132 insert(iterator __p, initializer_list<value_type> __l)
1133 { this->insert(__p, __l.begin(), __l.end()); }
1134 #endif
1135
1136 /**
1137 * @brief Inserts a number of copies of given data into the %list.
1138 * @param __position An iterator into the %list.
1139 * @param __n Number of elements to be inserted.
1140 * @param __x Data to be inserted.
1141 *
1142 * This function will insert a specified number of copies of the
1143 * given data before the location specified by @a position.
1144 *
1145 * This operation is linear in the number of elements inserted and
1146 * does not invalidate iterators and references.
1147 */
1148 void
1149 insert(iterator __position, size_type __n, const value_type& __x)
1150 {
1151 list __tmp(__n, __x, get_allocator());
1152 splice(__position, __tmp);
1153 }
1154
1155 /**
1156 * @brief Inserts a range into the %list.
1157 * @param __position An iterator into the %list.
1158 * @param __first An input iterator.
1159 * @param __last An input iterator.
1160 *
1161 * This function will insert copies of the data in the range [@a
1162 * first,@a last) into the %list before the location specified by
1163 * @a position.
1164 *
1165 * This operation is linear in the number of elements inserted and
1166 * does not invalidate iterators and references.
1167 */
1168 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1169 template<typename _InputIterator,
1170 typename = std::_RequireInputIter<_InputIterator>>
1171 #else
1172 template<typename _InputIterator>
1173 #endif
1174 void
1175 insert(iterator __position, _InputIterator __first,
1176 _InputIterator __last)
1177 {
1178 list __tmp(__first, __last, get_allocator());
1179 splice(__position, __tmp);
1180 }
1181
1182 /**
1183 * @brief Remove element at given position.
1184 * @param __position Iterator pointing to element to be erased.
1185 * @return An iterator pointing to the next element (or end()).
1186 *
1187 * This function will erase the element at the given position and thus
1188 * shorten the %list by one.
1189 *
1190 * Due to the nature of a %list this operation can be done in
1191 * constant time, and only invalidates iterators/references to
1192 * the element being removed. The user is also cautioned that
1193 * this function only erases the element, and that if the element
1194 * is itself a pointer, the pointed-to memory is not touched in
1195 * any way. Managing the pointer is the user's responsibility.
1196 */
1197 iterator
1198 erase(iterator __position);
1199
1200 /**
1201 * @brief Remove a range of elements.
1202 * @param __first Iterator pointing to the first element to be erased.
1203 * @param __last Iterator pointing to one past the last element to be
1204 * erased.
1205 * @return An iterator pointing to the element pointed to by @a last
1206 * prior to erasing (or end()).
1207 *
1208 * This function will erase the elements in the range @a
1209 * [first,last) and shorten the %list accordingly.
1210 *
1211 * This operation is linear time in the size of the range and only
1212 * invalidates iterators/references to the element being removed.
1213 * The user is also cautioned that this function only erases the
1214 * elements, and that if the elements themselves are pointers, the
1215 * pointed-to memory is not touched in any way. Managing the pointer
1216 * is the user's responsibility.
1217 */
1218 iterator
1219 erase(iterator __first, iterator __last)
1220 {
1221 while (__first != __last)
1222 __first = erase(__first);
1223 return __last;
1224 }
1225
1226 /**
1227 * @brief Swaps data with another %list.
1228 * @param __x A %list of the same element and allocator types.
1229 *
1230 * This exchanges the elements between two lists in constant
1231 * time. Note that the global std::swap() function is
1232 * specialized such that std::swap(l1,l2) will feed to this
1233 * function.
1234 */
1235 void
1236 swap(list& __x)
1237 {
1238 __detail::_List_node_base::swap(this->_M_impl._M_node,
1239 __x._M_impl._M_node);
1240 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1241 std::swap(this->_M_impl._M_size, __x._M_impl._M_size);
1242 #endif
1243
1244 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1245 // 431. Swapping containers with unequal allocators.
1246 std::__alloc_swap<typename _Base::_Node_alloc_type>::
1247 _S_do_it(_M_get_Node_allocator(), __x._M_get_Node_allocator());
1248 }
1249
1250 /**
1251 * Erases all the elements. Note that this function only erases
1252 * the elements, and that if the elements themselves are
1253 * pointers, the pointed-to memory is not touched in any way.
1254 * Managing the pointer is the user's responsibility.
1255 */
1256 void
1257 clear() _GLIBCXX_NOEXCEPT
1258 {
1259 _Base::_M_clear();
1260 _Base::_M_init();
1261 }
1262
1263 // [23.2.2.4] list operations
1264 /**
1265 * @brief Insert contents of another %list.
1266 * @param __position Iterator referencing the element to insert before.
1267 * @param __x Source list.
1268 *
1269 * The elements of @a __x are inserted in constant time in front of
1270 * the element referenced by @a __position. @a __x becomes an empty
1271 * list.
1272 *
1273 * Requires this != @a __x.
1274 */
1275 void
1276 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1277 splice(iterator __position, list&& __x)
1278 #else
1279 splice(iterator __position, list& __x)
1280 #endif
1281 {
1282 if (!__x.empty())
1283 {
1284 _M_check_equal_allocators(__x);
1285
1286 this->_M_transfer(__position, __x.begin(), __x.end());
1287
1288 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1289 this->_M_impl._M_size += __x.size();
1290 __x._M_impl._M_size = 0;
1291 #endif
1292 }
1293 }
1294
1295 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1296 void
1297 splice(iterator __position, list& __x)
1298 { splice(__position, std::move(__x)); }
1299 #endif
1300
1301 /**
1302 * @brief Insert element from another %list.
1303 * @param __position Iterator referencing the element to insert before.
1304 * @param __x Source list.
1305 * @param __i Iterator referencing the element to move.
1306 *
1307 * Removes the element in list @a __x referenced by @a __i and
1308 * inserts it into the current list before @a __position.
1309 */
1310 void
1311 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1312 splice(iterator __position, list&& __x, iterator __i)
1313 #else
1314 splice(iterator __position, list& __x, iterator __i)
1315 #endif
1316 {
1317 iterator __j = __i;
1318 ++__j;
1319 if (__position == __i || __position == __j)
1320 return;
1321
1322 if (this != &__x)
1323 {
1324 _M_check_equal_allocators(__x);
1325
1326 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1327 ++this->_M_impl._M_size;
1328 --__x._M_impl._M_size;
1329 #endif
1330 }
1331
1332 this->_M_transfer(__position, __i, __j);
1333 }
1334
1335 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1336 void
1337 splice(iterator __position, list& __x, iterator __i)
1338 { splice(__position, std::move(__x), __i); }
1339 #endif
1340
1341 /**
1342 * @brief Insert range from another %list.
1343 * @param __position Iterator referencing the element to insert before.
1344 * @param __x Source list.
1345 * @param __first Iterator referencing the start of range in x.
1346 * @param __last Iterator referencing the end of range in x.
1347 *
1348 * Removes elements in the range [__first,__last) and inserts them
1349 * before @a __position in constant time.
1350 *
1351 * Undefined if @a __position is in [__first,__last).
1352 */
1353 void
1354 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1355 splice(iterator __position, list&& __x, iterator __first,
1356 iterator __last)
1357 #else
1358 splice(iterator __position, list& __x, iterator __first,
1359 iterator __last)
1360 #endif
1361 {
1362 if (__first != __last)
1363 {
1364 if (this != &__x)
1365 {
1366 _M_check_equal_allocators(__x);
1367
1368 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1369 const size_type __size = std::distance(__first, __last);
1370 this->_M_impl._M_size += __size;
1371 __x._M_impl._M_size -= __size;
1372 #endif
1373 }
1374
1375 this->_M_transfer(__position, __first, __last);
1376 }
1377 }
1378
1379 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1380 void
1381 splice(iterator __position, list& __x, iterator __first, iterator __last)
1382 { splice(__position, std::move(__x), __first, __last); }
1383 #endif
1384
1385 /**
1386 * @brief Remove all elements equal to value.
1387 * @param __value The value to remove.
1388 *
1389 * Removes every element in the list equal to @a value.
1390 * Remaining elements stay in list order. Note that this
1391 * function only erases the elements, and that if the elements
1392 * themselves are pointers, the pointed-to memory is not
1393 * touched in any way. Managing the pointer is the user's
1394 * responsibility.
1395 */
1396 void
1397 remove(const _Tp& __value);
1398
1399 /**
1400 * @brief Remove all elements satisfying a predicate.
1401 * @tparam _Predicate Unary predicate function or object.
1402 *
1403 * Removes every element in the list for which the predicate
1404 * returns true. Remaining elements stay in list order. Note
1405 * that this function only erases the elements, and that if the
1406 * elements themselves are pointers, the pointed-to memory is
1407 * not touched in any way. Managing the pointer is the user's
1408 * responsibility.
1409 */
1410 template<typename _Predicate>
1411 void
1412 remove_if(_Predicate);
1413
1414 /**
1415 * @brief Remove consecutive duplicate elements.
1416 *
1417 * For each consecutive set of elements with the same value,
1418 * remove all but the first one. Remaining elements stay in
1419 * list order. Note that this function only erases the
1420 * elements, and that if the elements themselves are pointers,
1421 * the pointed-to memory is not touched in any way. Managing
1422 * the pointer is the user's responsibility.
1423 */
1424 void
1425 unique();
1426
1427 /**
1428 * @brief Remove consecutive elements satisfying a predicate.
1429 * @tparam _BinaryPredicate Binary predicate function or object.
1430 *
1431 * For each consecutive set of elements [first,last) that
1432 * satisfy predicate(first,i) where i is an iterator in
1433 * [first,last), remove all but the first one. Remaining
1434 * elements stay in list order. Note that this function only
1435 * erases the elements, and that if the elements themselves are
1436 * pointers, the pointed-to memory is not touched in any way.
1437 * Managing the pointer is the user's responsibility.
1438 */
1439 template<typename _BinaryPredicate>
1440 void
1441 unique(_BinaryPredicate);
1442
1443 /**
1444 * @brief Merge sorted lists.
1445 * @param __x Sorted list to merge.
1446 *
1447 * Assumes that both @a __x and this list are sorted according to
1448 * operator<(). Merges elements of @a __x into this list in
1449 * sorted order, leaving @a __x empty when complete. Elements in
1450 * this list precede elements in @a __x that are equal.
1451 */
1452 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1453 void
1454 merge(list&& __x);
1455
1456 void
1457 merge(list& __x)
1458 { merge(std::move(__x)); }
1459 #else
1460 void
1461 merge(list& __x);
1462 #endif
1463
1464 /**
1465 * @brief Merge sorted lists according to comparison function.
1466 * @tparam _StrictWeakOrdering Comparison function defining
1467 * sort order.
1468 * @param __x Sorted list to merge.
1469 * @param __comp Comparison functor.
1470 *
1471 * Assumes that both @a __x and this list are sorted according to
1472 * StrictWeakOrdering. Merges elements of @a __x into this list
1473 * in sorted order, leaving @a __x empty when complete. Elements
1474 * in this list precede elements in @a __x that are equivalent
1475 * according to StrictWeakOrdering().
1476 */
1477 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1478 template<typename _StrictWeakOrdering>
1479 void
1480 merge(list&& __x, _StrictWeakOrdering __comp);
1481
1482 template<typename _StrictWeakOrdering>
1483 void
1484 merge(list& __x, _StrictWeakOrdering __comp)
1485 { merge(std::move(__x), __comp); }
1486 #else
1487 template<typename _StrictWeakOrdering>
1488 void
1489 merge(list& __x, _StrictWeakOrdering __comp);
1490 #endif
1491
1492 /**
1493 * @brief Reverse the elements in list.
1494 *
1495 * Reverse the order of elements in the list in linear time.
1496 */
1497 void
1498 reverse() _GLIBCXX_NOEXCEPT
1499 { this->_M_impl._M_node._M_reverse(); }
1500
1501 /**
1502 * @brief Sort the elements.
1503 *
1504 * Sorts the elements of this list in NlogN time. Equivalent
1505 * elements remain in list order.
1506 */
1507 void
1508 sort();
1509
1510 /**
1511 * @brief Sort the elements according to comparison function.
1512 *
1513 * Sorts the elements of this list in NlogN time. Equivalent
1514 * elements remain in list order.
1515 */
1516 template<typename _StrictWeakOrdering>
1517 void
1518 sort(_StrictWeakOrdering);
1519
1520 protected:
1521 // Internal constructor functions follow.
1522
1523 // Called by the range constructor to implement [23.1.1]/9
1524
1525 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1526 // 438. Ambiguity in the "do the right thing" clause
1527 template<typename _Integer>
1528 void
1529 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1530 { _M_fill_initialize(static_cast<size_type>(__n), __x); }
1531
1532 // Called by the range constructor to implement [23.1.1]/9
1533 template<typename _InputIterator>
1534 void
1535 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1536 __false_type)
1537 {
1538 for (; __first != __last; ++__first)
1539 push_back(*__first);
1540 }
1541
1542 // Called by list(n,v,a), and the range constructor when it turns out
1543 // to be the same thing.
1544 void
1545 _M_fill_initialize(size_type __n, const value_type& __x)
1546 {
1547 for (; __n; --__n)
1548 push_back(__x);
1549 }
1550
1551 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1552 // Called by list(n).
1553 void
1554 _M_default_initialize(size_type __n)
1555 {
1556 for (; __n; --__n)
1557 emplace_back();
1558 }
1559
1560 // Called by resize(sz).
1561 void
1562 _M_default_append(size_type __n);
1563 #endif
1564
1565 // Internal assign functions follow.
1566
1567 // Called by the range assign to implement [23.1.1]/9
1568
1569 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1570 // 438. Ambiguity in the "do the right thing" clause
1571 template<typename _Integer>
1572 void
1573 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1574 { _M_fill_assign(__n, __val); }
1575
1576 // Called by the range assign to implement [23.1.1]/9
1577 template<typename _InputIterator>
1578 void
1579 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1580 __false_type);
1581
1582 // Called by assign(n,t), and the range assign when it turns out
1583 // to be the same thing.
1584 void
1585 _M_fill_assign(size_type __n, const value_type& __val);
1586
1587
1588 // Moves the elements from [first,last) before position.
1589 void
1590 _M_transfer(iterator __position, iterator __first, iterator __last)
1591 { __position._M_node->_M_transfer(__first._M_node, __last._M_node); }
1592
1593 // Inserts new element at position given and with value given.
1594 #ifndef __GXX_EXPERIMENTAL_CXX0X__
1595 void
1596 _M_insert(iterator __position, const value_type& __x)
1597 {
1598 _Node* __tmp = _M_create_node(__x);
1599 __tmp->_M_hook(__position._M_node);
1600 }
1601 #else
1602 template<typename... _Args>
1603 void
1604 _M_insert(iterator __position, _Args&&... __args)
1605 {
1606 _Node* __tmp = _M_create_node(std::forward<_Args>(__args)...);
1607 __tmp->_M_hook(__position._M_node);
1608 }
1609 #endif
1610
1611 // Erases element at position given.
1612 void
1613 _M_erase(iterator __position)
1614 {
1615 __position._M_node->_M_unhook();
1616 _Node* __n = static_cast<_Node*>(__position._M_node);
1617 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1618 _M_get_Node_allocator().destroy(__n);
1619 #else
1620 _M_get_Tp_allocator().destroy(std::__addressof(__n->_M_data));
1621 #endif
1622 _M_put_node(__n);
1623 }
1624
1625 // To implement the splice (and merge) bits of N1599.
1626 void
1627 _M_check_equal_allocators(list& __x)
1628 {
1629 if (std::__alloc_neq<typename _Base::_Node_alloc_type>::
1630 _S_do_it(_M_get_Node_allocator(), __x._M_get_Node_allocator()))
1631 __throw_runtime_error(__N("list::_M_check_equal_allocators"));
1632 }
1633 };
1634
1635 /**
1636 * @brief List equality comparison.
1637 * @param __x A %list.
1638 * @param __y A %list of the same type as @a __x.
1639 * @return True iff the size and elements of the lists are equal.
1640 *
1641 * This is an equivalence relation. It is linear in the size of
1642 * the lists. Lists are considered equivalent if their sizes are
1643 * equal, and if corresponding elements compare equal.
1644 */
1645 template<typename _Tp, typename _Alloc>
1646 inline bool
1647 operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1648 {
1649 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1650 return (__x.size() == __y.size()
1651 && std::equal(__x.begin(), __x.end(), __y.begin()));
1652 #else
1653 typedef typename list<_Tp, _Alloc>::const_iterator const_iterator;
1654 const_iterator __end1 = __x.end();
1655 const_iterator __end2 = __y.end();
1656
1657 const_iterator __i1 = __x.begin();
1658 const_iterator __i2 = __y.begin();
1659 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2)
1660 {
1661 ++__i1;
1662 ++__i2;
1663 }
1664 return __i1 == __end1 && __i2 == __end2;
1665 #endif
1666 }
1667
1668 /**
1669 * @brief List ordering relation.
1670 * @param __x A %list.
1671 * @param __y A %list of the same type as @a __x.
1672 * @return True iff @a __x is lexicographically less than @a __y.
1673 *
1674 * This is a total ordering relation. It is linear in the size of the
1675 * lists. The elements must be comparable with @c <.
1676 *
1677 * See std::lexicographical_compare() for how the determination is made.
1678 */
1679 template<typename _Tp, typename _Alloc>
1680 inline bool
1681 operator<(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1682 { return std::lexicographical_compare(__x.begin(), __x.end(),
1683 __y.begin(), __y.end()); }
1684
1685 /// Based on operator==
1686 template<typename _Tp, typename _Alloc>
1687 inline bool
1688 operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1689 { return !(__x == __y); }
1690
1691 /// Based on operator<
1692 template<typename _Tp, typename _Alloc>
1693 inline bool
1694 operator>(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1695 { return __y < __x; }
1696
1697 /// Based on operator<
1698 template<typename _Tp, typename _Alloc>
1699 inline bool
1700 operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1701 { return !(__y < __x); }
1702
1703 /// Based on operator<
1704 template<typename _Tp, typename _Alloc>
1705 inline bool
1706 operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1707 { return !(__x < __y); }
1708
1709 /// See std::list::swap().
1710 template<typename _Tp, typename _Alloc>
1711 inline void
1712 swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
1713 { __x.swap(__y); }
1714
1715 _GLIBCXX_END_NAMESPACE_CONTAINER
1716 } // namespace std
1717
1718 #endif /* _STL_LIST_H */