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