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