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