[multiple changes]
[gcc.git] / libstdc++-v3 / include / bits / stl_list.h
1 // List implementation -*- C++ -*-
2
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 // 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 2, 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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 /*
32 *
33 * Copyright (c) 1994
34 * Hewlett-Packard Company
35 *
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Hewlett-Packard Company makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
43 *
44 *
45 * Copyright (c) 1996,1997
46 * Silicon Graphics Computer Systems, Inc.
47 *
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Silicon Graphics makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
55 */
56
57 /** @file stl_list.h
58 * This is an internal header file, included by other library headers.
59 * You should not attempt to use it directly.
60 */
61
62 #ifndef _STL_LIST_H
63 #define _STL_LIST_H 1
64
65 #include <bits/concept_check.h>
66
67 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
68
69 // Supporting structures are split into common and templated types; the
70 // latter publicly inherits from the former in an effort to reduce code
71 // duplication. This results in some "needless" static_cast'ing later on,
72 // but it's all safe downcasting.
73
74 /// @if maint Common part of a node in the %list. @endif
75 struct _List_node_base
76 {
77 _List_node_base* _M_next; ///< Self-explanatory
78 _List_node_base* _M_prev; ///< Self-explanatory
79
80 static void
81 swap(_List_node_base& __x, _List_node_base& __y);
82
83 void
84 transfer(_List_node_base * const __first,
85 _List_node_base * const __last);
86
87 void
88 reverse();
89
90 void
91 hook(_List_node_base * const __position);
92
93 void
94 unhook();
95 };
96
97 /// @if maint An actual node in the %list. @endif
98 template<typename _Tp>
99 struct _List_node : public _List_node_base
100 {
101 _Tp _M_data; ///< User's data.
102 };
103
104 /**
105 * @brief A list::iterator.
106 *
107 * @if maint
108 * All the functions are op overloads.
109 * @endif
110 */
111 template<typename _Tp>
112 struct _List_iterator
113 {
114 typedef _List_iterator<_Tp> _Self;
115 typedef _List_node<_Tp> _Node;
116
117 typedef ptrdiff_t difference_type;
118 typedef std::bidirectional_iterator_tag iterator_category;
119 typedef _Tp value_type;
120 typedef _Tp* pointer;
121 typedef _Tp& reference;
122
123 _List_iterator()
124 : _M_node() { }
125
126 explicit
127 _List_iterator(_List_node_base* __x)
128 : _M_node(__x) { }
129
130 // Must downcast from List_node_base to _List_node to get to _M_data.
131 reference
132 operator*() const
133 { return static_cast<_Node*>(_M_node)->_M_data; }
134
135 pointer
136 operator->() const
137 { return &static_cast<_Node*>(_M_node)->_M_data; }
138
139 _Self&
140 operator++()
141 {
142 _M_node = _M_node->_M_next;
143 return *this;
144 }
145
146 _Self
147 operator++(int)
148 {
149 _Self __tmp = *this;
150 _M_node = _M_node->_M_next;
151 return __tmp;
152 }
153
154 _Self&
155 operator--()
156 {
157 _M_node = _M_node->_M_prev;
158 return *this;
159 }
160
161 _Self
162 operator--(int)
163 {
164 _Self __tmp = *this;
165 _M_node = _M_node->_M_prev;
166 return __tmp;
167 }
168
169 bool
170 operator==(const _Self& __x) const
171 { return _M_node == __x._M_node; }
172
173 bool
174 operator!=(const _Self& __x) const
175 { return _M_node != __x._M_node; }
176
177 // The only member points to the %list element.
178 _List_node_base* _M_node;
179 };
180
181 /**
182 * @brief A list::const_iterator.
183 *
184 * @if maint
185 * All the functions are op overloads.
186 * @endif
187 */
188 template<typename _Tp>
189 struct _List_const_iterator
190 {
191 typedef _List_const_iterator<_Tp> _Self;
192 typedef const _List_node<_Tp> _Node;
193 typedef _List_iterator<_Tp> iterator;
194
195 typedef ptrdiff_t difference_type;
196 typedef std::bidirectional_iterator_tag iterator_category;
197 typedef _Tp value_type;
198 typedef const _Tp* pointer;
199 typedef const _Tp& reference;
200
201 _List_const_iterator()
202 : _M_node() { }
203
204 explicit
205 _List_const_iterator(const _List_node_base* __x)
206 : _M_node(__x) { }
207
208 _List_const_iterator(const iterator& __x)
209 : _M_node(__x._M_node) { }
210
211 // Must downcast from List_node_base to _List_node to get to
212 // _M_data.
213 reference
214 operator*() const
215 { return static_cast<_Node*>(_M_node)->_M_data; }
216
217 pointer
218 operator->() const
219 { return &static_cast<_Node*>(_M_node)->_M_data; }
220
221 _Self&
222 operator++()
223 {
224 _M_node = _M_node->_M_next;
225 return *this;
226 }
227
228 _Self
229 operator++(int)
230 {
231 _Self __tmp = *this;
232 _M_node = _M_node->_M_next;
233 return __tmp;
234 }
235
236 _Self&
237 operator--()
238 {
239 _M_node = _M_node->_M_prev;
240 return *this;
241 }
242
243 _Self
244 operator--(int)
245 {
246 _Self __tmp = *this;
247 _M_node = _M_node->_M_prev;
248 return __tmp;
249 }
250
251 bool
252 operator==(const _Self& __x) const
253 { return _M_node == __x._M_node; }
254
255 bool
256 operator!=(const _Self& __x) const
257 { return _M_node != __x._M_node; }
258
259 // The only member points to the %list element.
260 const _List_node_base* _M_node;
261 };
262
263 template<typename _Val>
264 inline bool
265 operator==(const _List_iterator<_Val>& __x,
266 const _List_const_iterator<_Val>& __y)
267 { return __x._M_node == __y._M_node; }
268
269 template<typename _Val>
270 inline bool
271 operator!=(const _List_iterator<_Val>& __x,
272 const _List_const_iterator<_Val>& __y)
273 { return __x._M_node != __y._M_node; }
274
275
276 /**
277 * @if maint
278 * See bits/stl_deque.h's _Deque_base for an explanation.
279 * @endif
280 */
281 template<typename _Tp, typename _Alloc>
282 class _List_base
283 {
284 protected:
285 // NOTA BENE
286 // The stored instance is not actually of "allocator_type"'s
287 // type. Instead we rebind the type to
288 // Allocator<List_node<Tp>>, which according to [20.1.5]/4
289 // should probably be the same. List_node<Tp> is not the same
290 // size as Tp (it's two pointers larger), and specializations on
291 // Tp may go unused because List_node<Tp> is being bound
292 // instead.
293 //
294 // We put this to the test in the constructors and in
295 // get_allocator, where we use conversions between
296 // allocator_type and _Node_alloc_type. The conversion is
297 // required by table 32 in [20.1.5].
298 typedef typename _Alloc::template rebind<_List_node<_Tp> >::other
299 _Node_alloc_type;
300
301 typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
302
303 struct _List_impl
304 : public _Node_alloc_type
305 {
306 _List_node_base _M_node;
307
308 _List_impl()
309 : _Node_alloc_type(), _M_node()
310 { }
311
312 _List_impl(const _Node_alloc_type& __a)
313 : _Node_alloc_type(__a), _M_node()
314 { }
315 };
316
317 _List_impl _M_impl;
318
319 _List_node<_Tp>*
320 _M_get_node()
321 { return _M_impl._Node_alloc_type::allocate(1); }
322
323 void
324 _M_put_node(_List_node<_Tp>* __p)
325 { _M_impl._Node_alloc_type::deallocate(__p, 1); }
326
327 public:
328 typedef _Alloc allocator_type;
329
330 _Node_alloc_type&
331 _M_get_Node_allocator()
332 { return *static_cast<_Node_alloc_type*>(&this->_M_impl); }
333
334 const _Node_alloc_type&
335 _M_get_Node_allocator() const
336 { return *static_cast<const _Node_alloc_type*>(&this->_M_impl); }
337
338 _Tp_alloc_type
339 _M_get_Tp_allocator() const
340 { return _Tp_alloc_type(_M_get_Node_allocator()); }
341
342 allocator_type
343 get_allocator() const
344 { return allocator_type(_M_get_Node_allocator()); }
345
346 _List_base()
347 : _M_impl()
348 { _M_init(); }
349
350 _List_base(const allocator_type& __a)
351 : _M_impl(__a)
352 { _M_init(); }
353
354 // This is what actually destroys the list.
355 ~_List_base()
356 { _M_clear(); }
357
358 void
359 _M_clear();
360
361 void
362 _M_init()
363 {
364 this->_M_impl._M_node._M_next = &this->_M_impl._M_node;
365 this->_M_impl._M_node._M_prev = &this->_M_impl._M_node;
366 }
367 };
368
369 /**
370 * @brief A standard container with linear time access to elements,
371 * and fixed time insertion/deletion at any point in the sequence.
372 *
373 * @ingroup Containers
374 * @ingroup Sequences
375 *
376 * Meets the requirements of a <a href="tables.html#65">container</a>, a
377 * <a href="tables.html#66">reversible container</a>, and a
378 * <a href="tables.html#67">sequence</a>, including the
379 * <a href="tables.html#68">optional sequence requirements</a> with the
380 * %exception of @c at and @c operator[].
381 *
382 * This is a @e doubly @e linked %list. Traversal up and down the
383 * %list requires linear time, but adding and removing elements (or
384 * @e nodes) is done in constant time, regardless of where the
385 * change takes place. Unlike std::vector and std::deque,
386 * random-access iterators are not provided, so subscripting ( @c
387 * [] ) access is not allowed. For algorithms which only need
388 * sequential access, this lack makes no difference.
389 *
390 * Also unlike the other standard containers, std::list provides
391 * specialized algorithms %unique to linked lists, such as
392 * splicing, sorting, and in-place reversal.
393 *
394 * @if maint
395 * A couple points on memory allocation for list<Tp>:
396 *
397 * First, we never actually allocate a Tp, we allocate
398 * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
399 * that after elements from %list<X,Alloc1> are spliced into
400 * %list<X,Alloc2>, destroying the memory of the second %list is a
401 * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
402 *
403 * Second, a %list conceptually represented as
404 * @code
405 * A <---> B <---> C <---> D
406 * @endcode
407 * is actually circular; a link exists between A and D. The %list
408 * class holds (as its only data member) a private list::iterator
409 * pointing to @e D, not to @e A! To get to the head of the %list,
410 * we start at the tail and move forward by one. When this member
411 * iterator's next/previous pointers refer to itself, the %list is
412 * %empty. @endif
413 */
414 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
415 class list : protected _List_base<_Tp, _Alloc>
416 {
417 // concept requirements
418 typedef typename _Alloc::value_type _Alloc_value_type;
419 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
420 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
421
422 typedef _List_base<_Tp, _Alloc> _Base;
423 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
424
425 public:
426 typedef _Tp value_type;
427 typedef typename _Tp_alloc_type::pointer pointer;
428 typedef typename _Tp_alloc_type::const_pointer const_pointer;
429 typedef typename _Tp_alloc_type::reference reference;
430 typedef typename _Tp_alloc_type::const_reference const_reference;
431 typedef _List_iterator<_Tp> iterator;
432 typedef _List_const_iterator<_Tp> const_iterator;
433 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
434 typedef std::reverse_iterator<iterator> reverse_iterator;
435 typedef size_t size_type;
436 typedef ptrdiff_t difference_type;
437 typedef _Alloc allocator_type;
438
439 protected:
440 // Note that pointers-to-_Node's can be ctor-converted to
441 // iterator types.
442 typedef _List_node<_Tp> _Node;
443
444 using _Base::_M_impl;
445 using _Base::_M_put_node;
446 using _Base::_M_get_node;
447 using _Base::_M_get_Tp_allocator;
448 using _Base::_M_get_Node_allocator;
449
450 /**
451 * @if maint
452 * @param x An instance of user data.
453 *
454 * Allocates space for a new node and constructs a copy of @a x in it.
455 * @endif
456 */
457 _Node*
458 _M_create_node(const value_type& __x)
459 {
460 _Node* __p = this->_M_get_node();
461 try
462 {
463 _M_get_Tp_allocator().construct(&__p->_M_data, __x);
464 }
465 catch(...)
466 {
467 _M_put_node(__p);
468 __throw_exception_again;
469 }
470 return __p;
471 }
472
473 public:
474 // [23.2.2.1] construct/copy/destroy
475 // (assign() and get_allocator() are also listed in this section)
476 /**
477 * @brief Default constructor creates no elements.
478 */
479 list()
480 : _Base() { }
481
482 /**
483 * @brief Creates a %list with no elements.
484 * @param a An allocator object.
485 */
486 explicit
487 list(const allocator_type& __a)
488 : _Base(__a) { }
489
490 /**
491 * @brief Creates a %list with copies of an exemplar element.
492 * @param n The number of elements to initially create.
493 * @param value An element to copy.
494 * @param a An allocator object.
495 *
496 * This constructor fills the %list with @a n copies of @a value.
497 */
498 explicit
499 list(size_type __n, const value_type& __value = value_type(),
500 const allocator_type& __a = allocator_type())
501 : _Base(__a)
502 { _M_fill_initialize(__n, __value); }
503
504 /**
505 * @brief %List copy constructor.
506 * @param x A %list of identical element and allocator types.
507 *
508 * The newly-created %list uses a copy of the allocation object used
509 * by @a x.
510 */
511 list(const list& __x)
512 : _Base(__x._M_get_Node_allocator())
513 { _M_initialize_dispatch(__x.begin(), __x.end(), __false_type()); }
514
515 #ifdef __GXX_EXPERIMENTAL_CXX0X__
516 /**
517 * @brief %List move constructor.
518 * @param x A %list of identical element and allocator types.
519 *
520 * The newly-created %list contains the exact contents of @a x.
521 * The contents of @a x are a valid, but unspecified %list.
522 */
523 list(list&& __x)
524 : _Base(__x._M_get_Node_allocator())
525 { this->swap(__x); }
526 #endif
527
528 /**
529 * @brief Builds a %list from a range.
530 * @param first An input iterator.
531 * @param last An input iterator.
532 * @param a An allocator object.
533 *
534 * Create a %list consisting of copies of the elements from
535 * [@a first,@a last). This is linear in N (where N is
536 * distance(@a first,@a last)).
537 */
538 template<typename _InputIterator>
539 list(_InputIterator __first, _InputIterator __last,
540 const allocator_type& __a = allocator_type())
541 : _Base(__a)
542 {
543 // Check whether it's an integral type. If so, it's not an iterator.
544 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
545 _M_initialize_dispatch(__first, __last, _Integral());
546 }
547
548 /**
549 * No explicit dtor needed as the _Base dtor takes care of
550 * things. The _Base dtor only erases the elements, and note
551 * that if the elements themselves are pointers, the pointed-to
552 * memory is not touched in any way. Managing the pointer is
553 * the user's responsibilty.
554 */
555
556 /**
557 * @brief %List assignment operator.
558 * @param x A %list of identical element and allocator types.
559 *
560 * All the elements of @a x are copied, but unlike the copy
561 * constructor, the allocator object is not copied.
562 */
563 list&
564 operator=(const list& __x);
565
566 #ifdef __GXX_EXPERIMENTAL_CXX0X__
567 /**
568 * @brief %List move assignment operator.
569 * @param x A %list of identical element and allocator types.
570 *
571 * The contents of @a x are moved into this %list (without copying).
572 * @a x is a valid, but unspecified %list
573 */
574 list&
575 operator=(list&& __x)
576 {
577 this->swap(__x);
578 return *this;
579 }
580 #endif
581
582 /**
583 * @brief Assigns a given value to a %list.
584 * @param n Number of elements to be assigned.
585 * @param val Value to be assigned.
586 *
587 * This function fills a %list with @a n copies of the given
588 * value. Note that the assignment completely changes the %list
589 * and that the resulting %list's size is the same as the number
590 * of elements assigned. Old data may be lost.
591 */
592 void
593 assign(size_type __n, const value_type& __val)
594 { _M_fill_assign(__n, __val); }
595
596 /**
597 * @brief Assigns a range to a %list.
598 * @param first An input iterator.
599 * @param last An input iterator.
600 *
601 * This function fills a %list with copies of the elements in the
602 * range [@a first,@a last).
603 *
604 * Note that the assignment completely changes the %list and
605 * that the resulting %list's size is the same as the number of
606 * elements assigned. Old data may be lost.
607 */
608 template<typename _InputIterator>
609 void
610 assign(_InputIterator __first, _InputIterator __last)
611 {
612 // Check whether it's an integral type. If so, it's not an iterator.
613 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
614 _M_assign_dispatch(__first, __last, _Integral());
615 }
616
617 /// Get a copy of the memory allocation object.
618 allocator_type
619 get_allocator() const
620 { return _Base::get_allocator(); }
621
622 // iterators
623 /**
624 * Returns a read/write iterator that points to the first element in the
625 * %list. Iteration is done in ordinary element order.
626 */
627 iterator
628 begin()
629 { return iterator(this->_M_impl._M_node._M_next); }
630
631 /**
632 * Returns a read-only (constant) iterator that points to the
633 * first element in the %list. Iteration is done in ordinary
634 * element order.
635 */
636 const_iterator
637 begin() const
638 { return const_iterator(this->_M_impl._M_node._M_next); }
639
640 /**
641 * Returns a read/write iterator that points one past the last
642 * element in the %list. Iteration is done in ordinary element
643 * order.
644 */
645 iterator
646 end()
647 { return iterator(&this->_M_impl._M_node); }
648
649 /**
650 * Returns a read-only (constant) iterator that points one past
651 * the last element in the %list. Iteration is done in ordinary
652 * element order.
653 */
654 const_iterator
655 end() const
656 { return const_iterator(&this->_M_impl._M_node); }
657
658 /**
659 * Returns a read/write reverse iterator that points to the last
660 * element in the %list. Iteration is done in reverse element
661 * order.
662 */
663 reverse_iterator
664 rbegin()
665 { return reverse_iterator(end()); }
666
667 /**
668 * Returns a read-only (constant) reverse iterator that points to
669 * the last element in the %list. Iteration is done in reverse
670 * element order.
671 */
672 const_reverse_iterator
673 rbegin() const
674 { return const_reverse_iterator(end()); }
675
676 /**
677 * Returns a read/write reverse iterator that points to one
678 * before the first element in the %list. Iteration is done in
679 * reverse element order.
680 */
681 reverse_iterator
682 rend()
683 { return reverse_iterator(begin()); }
684
685 /**
686 * Returns a read-only (constant) reverse iterator that points to one
687 * before the first element in the %list. Iteration is done in reverse
688 * element order.
689 */
690 const_reverse_iterator
691 rend() const
692 { return const_reverse_iterator(begin()); }
693
694 // [23.2.2.2] capacity
695 /**
696 * Returns true if the %list is empty. (Thus begin() would equal
697 * end().)
698 */
699 bool
700 empty() const
701 { return this->_M_impl._M_node._M_next == &this->_M_impl._M_node; }
702
703 /** Returns the number of elements in the %list. */
704 size_type
705 size() const
706 { return std::distance(begin(), end()); }
707
708 /** Returns the size() of the largest possible %list. */
709 size_type
710 max_size() const
711 { return _M_get_Tp_allocator().max_size(); }
712
713 /**
714 * @brief Resizes the %list to the specified number of elements.
715 * @param new_size Number of elements the %list should contain.
716 * @param x Data with which new elements should be populated.
717 *
718 * This function will %resize the %list to the specified number
719 * of elements. If the number is smaller than the %list's
720 * current size the %list is truncated, otherwise the %list is
721 * extended and new elements are populated with given data.
722 */
723 void
724 resize(size_type __new_size, value_type __x = value_type());
725
726 // element access
727 /**
728 * Returns a read/write reference to the data at the first
729 * element of the %list.
730 */
731 reference
732 front()
733 { return *begin(); }
734
735 /**
736 * Returns a read-only (constant) reference to the data at the first
737 * element of the %list.
738 */
739 const_reference
740 front() const
741 { return *begin(); }
742
743 /**
744 * Returns a read/write reference to the data at the last element
745 * of the %list.
746 */
747 reference
748 back()
749 {
750 iterator __tmp = end();
751 --__tmp;
752 return *__tmp;
753 }
754
755 /**
756 * Returns a read-only (constant) reference to the data at the last
757 * element of the %list.
758 */
759 const_reference
760 back() const
761 {
762 const_iterator __tmp = end();
763 --__tmp;
764 return *__tmp;
765 }
766
767 // [23.2.2.3] modifiers
768 /**
769 * @brief Add data to the front of the %list.
770 * @param x Data to be added.
771 *
772 * This is a typical stack operation. The function creates an
773 * element at the front of the %list and assigns the given data
774 * to it. Due to the nature of a %list this operation can be
775 * done in constant time, and does not invalidate iterators and
776 * references.
777 */
778 void
779 push_front(const value_type& __x)
780 { this->_M_insert(begin(), __x); }
781
782 /**
783 * @brief Removes first element.
784 *
785 * This is a typical stack operation. It shrinks the %list by
786 * one. Due to the nature of a %list this operation can be done
787 * in constant time, and only invalidates iterators/references to
788 * the element being removed.
789 *
790 * Note that no data is returned, and if the first element's data
791 * is needed, it should be retrieved before pop_front() is
792 * called.
793 */
794 void
795 pop_front()
796 { this->_M_erase(begin()); }
797
798 /**
799 * @brief Add data to the end of the %list.
800 * @param x Data to be added.
801 *
802 * This is a typical stack operation. The function creates an
803 * element at the end of the %list and assigns the given data to
804 * it. Due to the nature of a %list this operation can be done
805 * in constant time, and does not invalidate iterators and
806 * references.
807 */
808 void
809 push_back(const value_type& __x)
810 { this->_M_insert(end(), __x); }
811
812 /**
813 * @brief Removes last element.
814 *
815 * This is a typical stack operation. It shrinks the %list by
816 * one. Due to the nature of a %list this operation can be done
817 * in constant time, and only invalidates iterators/references to
818 * the element being removed.
819 *
820 * Note that no data is returned, and if the last element's data
821 * is needed, it should be retrieved before pop_back() is called.
822 */
823 void
824 pop_back()
825 { this->_M_erase(iterator(this->_M_impl._M_node._M_prev)); }
826
827 /**
828 * @brief Inserts given value into %list before specified iterator.
829 * @param position An iterator into the %list.
830 * @param x Data to be inserted.
831 * @return An iterator that points to the inserted data.
832 *
833 * This function will insert a copy of the given value before
834 * the specified location. Due to the nature of a %list this
835 * operation can be done in constant time, and does not
836 * invalidate iterators and references.
837 */
838 iterator
839 insert(iterator __position, const value_type& __x);
840
841 /**
842 * @brief Inserts a number of copies of given data into the %list.
843 * @param position An iterator into the %list.
844 * @param n Number of elements to be inserted.
845 * @param x Data to be inserted.
846 *
847 * This function will insert a specified number of copies of the
848 * given data before the location specified by @a position.
849 *
850 * This operation is linear in the number of elements inserted and
851 * does not invalidate iterators and references.
852 */
853 void
854 insert(iterator __position, size_type __n, const value_type& __x)
855 {
856 list __tmp(__n, __x, _M_get_Node_allocator());
857 splice(__position, __tmp);
858 }
859
860 /**
861 * @brief Inserts a range into the %list.
862 * @param position An iterator into the %list.
863 * @param first An input iterator.
864 * @param last An input iterator.
865 *
866 * This function will insert copies of the data in the range [@a
867 * first,@a last) into the %list before the location specified by
868 * @a position.
869 *
870 * This operation is linear in the number of elements inserted and
871 * does not invalidate iterators and references.
872 */
873 template<typename _InputIterator>
874 void
875 insert(iterator __position, _InputIterator __first,
876 _InputIterator __last)
877 {
878 list __tmp(__first, __last, _M_get_Node_allocator());
879 splice(__position, __tmp);
880 }
881
882 /**
883 * @brief Remove element at given position.
884 * @param position Iterator pointing to element to be erased.
885 * @return An iterator pointing to the next element (or end()).
886 *
887 * This function will erase the element at the given position and thus
888 * shorten the %list by one.
889 *
890 * Due to the nature of a %list this operation can be done in
891 * constant time, and only invalidates iterators/references to
892 * the element being removed. The user is also cautioned that
893 * this function only erases the element, and that if the element
894 * is itself a pointer, the pointed-to memory is not touched in
895 * any way. Managing the pointer is the user's responsibilty.
896 */
897 iterator
898 erase(iterator __position);
899
900 /**
901 * @brief Remove a range of elements.
902 * @param first Iterator pointing to the first element to be erased.
903 * @param last Iterator pointing to one past the last element to be
904 * erased.
905 * @return An iterator pointing to the element pointed to by @a last
906 * prior to erasing (or end()).
907 *
908 * This function will erase the elements in the range @a
909 * [first,last) and shorten the %list accordingly.
910 *
911 * This operation is linear time in the size of the range and only
912 * invalidates iterators/references to the element being removed.
913 * The user is also cautioned that this function only erases the
914 * elements, and that if the elements themselves are pointers, the
915 * pointed-to memory is not touched in any way. Managing the pointer
916 * is the user's responsibilty.
917 */
918 iterator
919 erase(iterator __first, iterator __last)
920 {
921 while (__first != __last)
922 __first = erase(__first);
923 return __last;
924 }
925
926 /**
927 * @brief Swaps data with another %list.
928 * @param x A %list of the same element and allocator types.
929 *
930 * This exchanges the elements between two lists in constant
931 * time. Note that the global std::swap() function is
932 * specialized such that std::swap(l1,l2) will feed to this
933 * function.
934 */
935 void
936 #ifdef __GXX_EXPERIMENTAL_CXX0X__
937 swap(list&& __x)
938 #else
939 swap(list& __x)
940 #endif
941 {
942 _List_node_base::swap(this->_M_impl._M_node, __x._M_impl._M_node);
943
944 // _GLIBCXX_RESOLVE_LIB_DEFECTS
945 // 431. Swapping containers with unequal allocators.
946 std::__alloc_swap<typename _Base::_Node_alloc_type>::
947 _S_do_it(_M_get_Node_allocator(), __x._M_get_Node_allocator());
948 }
949
950 /**
951 * Erases all the elements. Note that this function only erases
952 * the elements, and that if the elements themselves are
953 * pointers, the pointed-to memory is not touched in any way.
954 * Managing the pointer is the user's responsibilty.
955 */
956 void
957 clear()
958 {
959 _Base::_M_clear();
960 _Base::_M_init();
961 }
962
963 // [23.2.2.4] list operations
964 /**
965 * @brief Insert contents of another %list.
966 * @param position Iterator referencing the element to insert before.
967 * @param x Source list.
968 *
969 * The elements of @a x are inserted in constant time in front of
970 * the element referenced by @a position. @a x becomes an empty
971 * list.
972 *
973 * Requires this != @a x.
974 */
975 void
976 splice(iterator __position, list& __x)
977 {
978 if (!__x.empty())
979 {
980 _M_check_equal_allocators(__x);
981
982 this->_M_transfer(__position, __x.begin(), __x.end());
983 }
984 }
985
986 /**
987 * @brief Insert element from another %list.
988 * @param position Iterator referencing the element to insert before.
989 * @param x Source list.
990 * @param i Iterator referencing the element to move.
991 *
992 * Removes the element in list @a x referenced by @a i and
993 * inserts it into the current list before @a position.
994 */
995 void
996 splice(iterator __position, list& __x, iterator __i)
997 {
998 iterator __j = __i;
999 ++__j;
1000 if (__position == __i || __position == __j)
1001 return;
1002
1003 if (this != &__x)
1004 _M_check_equal_allocators(__x);
1005
1006 this->_M_transfer(__position, __i, __j);
1007 }
1008
1009 /**
1010 * @brief Insert range from another %list.
1011 * @param position Iterator referencing the element to insert before.
1012 * @param x Source list.
1013 * @param first Iterator referencing the start of range in x.
1014 * @param last Iterator referencing the end of range in x.
1015 *
1016 * Removes elements in the range [first,last) and inserts them
1017 * before @a position in constant time.
1018 *
1019 * Undefined if @a position is in [first,last).
1020 */
1021 void
1022 splice(iterator __position, list& __x, iterator __first, iterator __last)
1023 {
1024 if (__first != __last)
1025 {
1026 if (this != &__x)
1027 _M_check_equal_allocators(__x);
1028
1029 this->_M_transfer(__position, __first, __last);
1030 }
1031 }
1032
1033 /**
1034 * @brief Remove all elements equal to value.
1035 * @param value The value to remove.
1036 *
1037 * Removes every element in the list equal to @a value.
1038 * Remaining elements stay in list order. Note that this
1039 * function only erases the elements, and that if the elements
1040 * themselves are pointers, the pointed-to memory is not
1041 * touched in any way. Managing the pointer is the user's
1042 * responsibilty.
1043 */
1044 void
1045 remove(const _Tp& __value);
1046
1047 /**
1048 * @brief Remove all elements satisfying a predicate.
1049 * @param Predicate Unary predicate function or object.
1050 *
1051 * Removes every element in the list for which the predicate
1052 * returns true. Remaining elements stay in list order. Note
1053 * that this function only erases the elements, and that if the
1054 * elements themselves are pointers, the pointed-to memory is
1055 * not touched in any way. Managing the pointer is the user's
1056 * responsibilty.
1057 */
1058 template<typename _Predicate>
1059 void
1060 remove_if(_Predicate);
1061
1062 /**
1063 * @brief Remove consecutive duplicate elements.
1064 *
1065 * For each consecutive set of elements with the same value,
1066 * remove all but the first one. Remaining elements stay in
1067 * list order. Note that this function only erases the
1068 * elements, and that if the elements themselves are pointers,
1069 * the pointed-to memory is not touched in any way. Managing
1070 * the pointer is the user's responsibilty.
1071 */
1072 void
1073 unique();
1074
1075 /**
1076 * @brief Remove consecutive elements satisfying a predicate.
1077 * @param BinaryPredicate Binary predicate function or object.
1078 *
1079 * For each consecutive set of elements [first,last) that
1080 * satisfy predicate(first,i) where i is an iterator in
1081 * [first,last), remove all but the first one. Remaining
1082 * elements stay in list order. Note that this function only
1083 * erases the elements, and that if the elements themselves are
1084 * pointers, the pointed-to memory is not touched in any way.
1085 * Managing the pointer is the user's responsibilty.
1086 */
1087 template<typename _BinaryPredicate>
1088 void
1089 unique(_BinaryPredicate);
1090
1091 /**
1092 * @brief Merge sorted lists.
1093 * @param x Sorted list to merge.
1094 *
1095 * Assumes that both @a x and this list are sorted according to
1096 * operator<(). Merges elements of @a x into this list in
1097 * sorted order, leaving @a x empty when complete. Elements in
1098 * this list precede elements in @a x that are equal.
1099 */
1100 void
1101 merge(list& __x);
1102
1103 /**
1104 * @brief Merge sorted lists according to comparison function.
1105 * @param x Sorted list to merge.
1106 * @param StrictWeakOrdering Comparison function definining
1107 * sort order.
1108 *
1109 * Assumes that both @a x and this list are sorted according to
1110 * StrictWeakOrdering. Merges elements of @a x into this list
1111 * in sorted order, leaving @a x empty when complete. Elements
1112 * in this list precede elements in @a x that are equivalent
1113 * according to StrictWeakOrdering().
1114 */
1115 template<typename _StrictWeakOrdering>
1116 void
1117 merge(list&, _StrictWeakOrdering);
1118
1119 /**
1120 * @brief Reverse the elements in list.
1121 *
1122 * Reverse the order of elements in the list in linear time.
1123 */
1124 void
1125 reverse()
1126 { this->_M_impl._M_node.reverse(); }
1127
1128 /**
1129 * @brief Sort the elements.
1130 *
1131 * Sorts the elements of this list in NlogN time. Equivalent
1132 * elements remain in list order.
1133 */
1134 void
1135 sort();
1136
1137 /**
1138 * @brief Sort the elements according to comparison function.
1139 *
1140 * Sorts the elements of this list in NlogN time. Equivalent
1141 * elements remain in list order.
1142 */
1143 template<typename _StrictWeakOrdering>
1144 void
1145 sort(_StrictWeakOrdering);
1146
1147 protected:
1148 // Internal constructor functions follow.
1149
1150 // Called by the range constructor to implement [23.1.1]/9
1151
1152 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1153 // 438. Ambiguity in the "do the right thing" clause
1154 template<typename _Integer>
1155 void
1156 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1157 { _M_fill_initialize(static_cast<size_type>(__n), __x); }
1158
1159 // Called by the range constructor to implement [23.1.1]/9
1160 template<typename _InputIterator>
1161 void
1162 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1163 __false_type)
1164 {
1165 for (; __first != __last; ++__first)
1166 push_back(*__first);
1167 }
1168
1169 // Called by list(n,v,a), and the range constructor when it turns out
1170 // to be the same thing.
1171 void
1172 _M_fill_initialize(size_type __n, const value_type& __x)
1173 {
1174 for (; __n > 0; --__n)
1175 push_back(__x);
1176 }
1177
1178
1179 // Internal assign functions follow.
1180
1181 // Called by the range assign to implement [23.1.1]/9
1182
1183 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1184 // 438. Ambiguity in the "do the right thing" clause
1185 template<typename _Integer>
1186 void
1187 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1188 { _M_fill_assign(__n, __val); }
1189
1190 // Called by the range assign to implement [23.1.1]/9
1191 template<typename _InputIterator>
1192 void
1193 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1194 __false_type);
1195
1196 // Called by assign(n,t), and the range assign when it turns out
1197 // to be the same thing.
1198 void
1199 _M_fill_assign(size_type __n, const value_type& __val);
1200
1201
1202 // Moves the elements from [first,last) before position.
1203 void
1204 _M_transfer(iterator __position, iterator __first, iterator __last)
1205 { __position._M_node->transfer(__first._M_node, __last._M_node); }
1206
1207 // Inserts new element at position given and with value given.
1208 void
1209 _M_insert(iterator __position, const value_type& __x)
1210 {
1211 _Node* __tmp = _M_create_node(__x);
1212 __tmp->hook(__position._M_node);
1213 }
1214
1215 // Erases element at position given.
1216 void
1217 _M_erase(iterator __position)
1218 {
1219 __position._M_node->unhook();
1220 _Node* __n = static_cast<_Node*>(__position._M_node);
1221 _M_get_Tp_allocator().destroy(&__n->_M_data);
1222 _M_put_node(__n);
1223 }
1224
1225 // To implement the splice (and merge) bits of N1599.
1226 void
1227 _M_check_equal_allocators(list& __x)
1228 {
1229 if (_M_get_Node_allocator() != __x._M_get_Node_allocator())
1230 __throw_runtime_error(__N("list::_M_check_equal_allocators"));
1231 }
1232 };
1233
1234 /**
1235 * @brief List equality comparison.
1236 * @param x A %list.
1237 * @param y A %list of the same type as @a x.
1238 * @return True iff the size and elements of the lists are equal.
1239 *
1240 * This is an equivalence relation. It is linear in the size of
1241 * the lists. Lists are considered equivalent if their sizes are
1242 * equal, and if corresponding elements compare equal.
1243 */
1244 template<typename _Tp, typename _Alloc>
1245 inline bool
1246 operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1247 {
1248 typedef typename list<_Tp, _Alloc>::const_iterator const_iterator;
1249 const_iterator __end1 = __x.end();
1250 const_iterator __end2 = __y.end();
1251
1252 const_iterator __i1 = __x.begin();
1253 const_iterator __i2 = __y.begin();
1254 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2)
1255 {
1256 ++__i1;
1257 ++__i2;
1258 }
1259 return __i1 == __end1 && __i2 == __end2;
1260 }
1261
1262 /**
1263 * @brief List ordering relation.
1264 * @param x A %list.
1265 * @param y A %list of the same type as @a x.
1266 * @return True iff @a x is lexicographically less than @a y.
1267 *
1268 * This is a total ordering relation. It is linear in the size of the
1269 * lists. The elements must be comparable with @c <.
1270 *
1271 * See std::lexicographical_compare() for how the determination is made.
1272 */
1273 template<typename _Tp, typename _Alloc>
1274 inline bool
1275 operator<(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1276 { return std::lexicographical_compare(__x.begin(), __x.end(),
1277 __y.begin(), __y.end()); }
1278
1279 /// Based on operator==
1280 template<typename _Tp, typename _Alloc>
1281 inline bool
1282 operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1283 { return !(__x == __y); }
1284
1285 /// Based on operator<
1286 template<typename _Tp, typename _Alloc>
1287 inline bool
1288 operator>(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1289 { return __y < __x; }
1290
1291 /// Based on operator<
1292 template<typename _Tp, typename _Alloc>
1293 inline bool
1294 operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1295 { return !(__y < __x); }
1296
1297 /// Based on operator<
1298 template<typename _Tp, typename _Alloc>
1299 inline bool
1300 operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1301 { return !(__x < __y); }
1302
1303 /// See std::list::swap().
1304 template<typename _Tp, typename _Alloc>
1305 inline void
1306 swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
1307 { __x.swap(__y); }
1308
1309 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1310 template<typename _Tp, typename _Alloc>
1311 inline void
1312 swap(list<_Tp, _Alloc>&& __x, list<_Tp, _Alloc>& __y)
1313 { __x.swap(__y); }
1314
1315 template<typename _Tp, typename _Alloc>
1316 inline void
1317 swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>&& __y)
1318 { __x.swap(__y); }
1319 #endif
1320
1321 _GLIBCXX_END_NESTED_NAMESPACE
1322
1323 #endif /* _STL_LIST_H */