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