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