FSF GCC merge 02/23/03
[official-gcc.git] / libstdc++-v3 / include / bits / stl_list.h
blobb8a59e106c72230eeba393fcd1c0e17118cedff1
1 // List implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003 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.
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.
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.
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.
32 * Copyright (c) 1994
33 * Hewlett-Packard Company
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.
44 * Copyright (c) 1996,1997
45 * Silicon Graphics Computer Systems, Inc.
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.
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.
61 #ifndef __GLIBCPP_INTERNAL_LIST_H
62 #define __GLIBCPP_INTERNAL_LIST_H
64 #include <bits/concept_check.h>
66 namespace std
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.
73 /// @if maint Common part of a node in the %list. @endif
74 struct _List_node_base
76 _List_node_base* _M_next; ///< Self-explanatory
77 _List_node_base* _M_prev; ///< Self-explanatory
80 /// @if maint An actual node in the %list. @endif
81 template<typename _Tp>
82 struct _List_node : public _List_node_base
84 _Tp _M_data; ///< User's data.
88 /**
89 * @if maint
90 * @brief Common part of a list::iterator.
92 * A simple type to walk a doubly-linked list. All operations here should
93 * be self-explanatory after taking any decent introductory data structures
94 * course.
95 * @endif
97 struct _List_iterator_base
99 typedef size_t size_type;
100 typedef ptrdiff_t difference_type;
101 typedef bidirectional_iterator_tag iterator_category;
103 /// The only member points to the %list element.
104 _List_node_base* _M_node;
106 _List_iterator_base(_List_node_base* __x)
107 : _M_node(__x)
110 _List_iterator_base()
113 /// Walk the %list forward.
114 void
115 _M_incr()
116 { _M_node = _M_node->_M_next; }
118 /// Walk the %list backward.
119 void
120 _M_decr()
121 { _M_node = _M_node->_M_prev; }
123 bool
124 operator==(const _List_iterator_base& __x) const
125 { return _M_node == __x._M_node; }
127 bool
128 operator!=(const _List_iterator_base& __x) const
129 { return _M_node != __x._M_node; }
133 * @brief A list::iterator.
135 * In addition to being used externally, a list holds one of these
136 * internally, pointing to the sequence of data.
138 * @if maint
139 * All the functions are op overloads.
140 * @endif
142 template<typename _Tp, typename _Ref, typename _Ptr>
143 struct _List_iterator : public _List_iterator_base
145 typedef _List_iterator<_Tp,_Tp&,_Tp*> iterator;
146 typedef _List_iterator<_Tp,const _Tp&,const _Tp*> const_iterator;
147 typedef _List_iterator<_Tp,_Ref,_Ptr> _Self;
149 typedef _Tp value_type;
150 typedef _Ptr pointer;
151 typedef _Ref reference;
152 typedef _List_node<_Tp> _Node;
154 _List_iterator(_Node* __x)
155 : _List_iterator_base(__x)
158 _List_iterator()
161 _List_iterator(const iterator& __x)
162 : _List_iterator_base(__x._M_node)
165 reference
166 operator*() const
167 { return static_cast<_Node*>(_M_node)->_M_data; }
168 // Must downcast from List_node_base to _List_node to get to _M_data.
170 pointer
171 operator->() const
172 { return &(operator*()); }
174 _Self&
175 operator++()
177 this->_M_incr();
178 return *this;
181 _Self
182 operator++(int)
184 _Self __tmp = *this;
185 this->_M_incr();
186 return __tmp;
189 _Self&
190 operator--()
192 this->_M_decr();
193 return *this;
196 _Self
197 operator--(int)
199 _Self __tmp = *this;
200 this->_M_decr();
201 return __tmp;
206 /// @if maint Primary default version. @endif
208 * @if maint
209 * See bits/stl_deque.h's _Deque_alloc_base for an explanation.
210 * @endif
212 template<typename _Tp, typename _Allocator, bool _IsStatic>
213 class _List_alloc_base
215 public:
216 typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type
217 allocator_type;
219 allocator_type
220 get_allocator() const { return _M_node_allocator; }
222 _List_alloc_base(const allocator_type& __a)
223 : _M_node_allocator(__a)
226 protected:
227 _List_node<_Tp>*
228 _M_get_node()
229 { return _M_node_allocator.allocate(1); }
231 void
232 _M_put_node(_List_node<_Tp>* __p)
233 { _M_node_allocator.deallocate(__p, 1); }
235 // NOTA BENE
236 // The stored instance is not actually of "allocator_type"'s type. Instead
237 // we rebind the type to Allocator<List_node<Tp>>, which according to
238 // [20.1.5]/4 should probably be the same. List_node<Tp> is not the same
239 // size as Tp (it's two pointers larger), and specializations on Tp may go
240 // unused because List_node<Tp> is being bound instead.
242 // We put this to the test in get_allocator above; if the two types are
243 // actually different, there had better be a conversion between them.
245 // None of the predefined allocators shipped with the library (as of 3.1)
246 // use this instantiation anyhow; they're all instanceless.
247 typename _Alloc_traits<_List_node<_Tp>, _Allocator>::allocator_type
248 _M_node_allocator;
250 _List_node<_Tp>* _M_node;
253 /// @if maint Specialization for instanceless allocators. @endif
254 template<typename _Tp, typename _Allocator>
255 class _List_alloc_base<_Tp, _Allocator, true>
257 public:
258 typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type
259 allocator_type;
261 allocator_type
262 get_allocator() const { return allocator_type(); }
264 _List_alloc_base(const allocator_type&)
267 protected:
268 // See comment in primary template class about why this is safe for the
269 // standard predefined classes.
270 typedef typename _Alloc_traits<_List_node<_Tp>, _Allocator>::_Alloc_type
271 _Alloc_type;
273 _List_node<_Tp>*
274 _M_get_node()
275 { return _Alloc_type::allocate(1); }
277 void
278 _M_put_node(_List_node<_Tp>* __p)
279 { _Alloc_type::deallocate(__p, 1); }
281 _List_node<_Tp>* _M_node;
286 * @if maint
287 * See bits/stl_deque.h's _Deque_base for an explanation.
288 * @endif
290 template <typename _Tp, typename _Alloc>
291 class _List_base
292 : public _List_alloc_base<_Tp, _Alloc,
293 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
295 public:
296 typedef _List_alloc_base<_Tp, _Alloc,
297 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
298 _Base;
299 typedef typename _Base::allocator_type allocator_type;
301 _List_base(const allocator_type& __a)
302 : _Base(__a)
304 this->_M_node = _M_get_node();
305 this->_M_node->_M_next = this->_M_node;
306 this->_M_node->_M_prev = this->_M_node;
309 // This is what actually destroys the list.
310 ~_List_base()
312 __clear();
313 _M_put_node(this->_M_node);
316 void
317 __clear();
322 * @brief A standard container with linear time access to elements, and
323 * fixed time insertion/deletion at any point in the sequence.
325 * @ingroup Containers
326 * @ingroup Sequences
328 * Meets the requirements of a <a href="tables.html#65">container</a>, a
329 * <a href="tables.html#66">reversible container</a>, and a
330 * <a href="tables.html#67">sequence</a>, including the
331 * <a href="tables.html#68">optional sequence requirements</a> with the
332 * %exception of @c at and @c operator[].
334 * This is a @e doubly @e linked %list. Traversal up and down the %list
335 * requires linear time, but adding and removing elements (or @e nodes) is
336 * done in constant time, regardless of where the change takes place.
337 * Unlike std::vector and std::deque, random-access iterators are not
338 * provided, so subscripting ( @c [] ) access is not allowed. For algorithms
339 * which only need sequential access, this lack makes no difference.
341 * Also unlike the other standard containers, std::list provides specialized
342 * algorithms %unique to linked lists, such as splicing, sorting, and
343 * in-place reversal.
345 * @if maint
346 * A couple points on memory allocation for list<Tp>:
348 * First, we never actually allocate a Tp, we allocate List_node<Tp>'s
349 * and trust [20.1.5]/4 to DTRT. This is to ensure that after elements from
350 * %list<X,Alloc1> are spliced into %list<X,Alloc2>, destroying the memory of
351 * the second %list is a valid operation, i.e., Alloc1 giveth and Alloc2
352 * taketh away.
354 * Second, a %list conceptually represented as
355 * @code
356 * A <---> B <---> C <---> D
357 * @endcode
358 * is actually circular; a link exists between A and D. The %list class
359 * holds (as its only data member) a private list::iterator pointing to
360 * @e D, not to @e A! To get to the head of the %list, we start at the tail
361 * and move forward by one. When this member iterator's next/previous
362 * pointers refer to itself, the %list is %empty.
363 * @endif
365 template<typename _Tp, typename _Alloc = allocator<_Tp> >
366 class list : protected _List_base<_Tp, _Alloc>
368 // concept requirements
369 __glibcpp_class_requires(_Tp, _SGIAssignableConcept)
371 typedef _List_base<_Tp, _Alloc> _Base;
373 public:
374 typedef _Tp value_type;
375 typedef value_type* pointer;
376 typedef const value_type* const_pointer;
377 typedef _List_iterator<_Tp,_Tp&,_Tp*> iterator;
378 typedef _List_iterator<_Tp,const _Tp&,const _Tp*> const_iterator;
379 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
380 typedef std::reverse_iterator<iterator> reverse_iterator;
381 typedef value_type& reference;
382 typedef const value_type& const_reference;
383 typedef size_t size_type;
384 typedef ptrdiff_t difference_type;
385 typedef typename _Base::allocator_type allocator_type;
387 protected:
388 // Note that pointers-to-_Node's can be ctor-converted to iterator types.
389 typedef _List_node<_Tp> _Node;
391 /** @if maint
392 * One data member plus two memory-handling functions. If the _Alloc
393 * type requires separate instances, then one of those will also be
394 * included, accumulated from the topmost parent.
395 * @endif
397 using _Base::_M_node;
398 using _Base::_M_put_node;
399 using _Base::_M_get_node;
402 * @if maint
403 * @param x An instance of user data.
405 * Allocates space for a new node and constructs a copy of @a x in it.
406 * @endif
408 _Node*
409 _M_create_node(const value_type& __x)
411 _Node* __p = _M_get_node();
412 try {
413 _Construct(&__p->_M_data, __x);
415 catch(...)
417 _M_put_node(__p);
418 __throw_exception_again;
420 return __p;
424 * @if maint
425 * Allocates space for a new node and default-constructs a new instance
426 * of @c value_type in it.
427 * @endif
429 _Node*
430 _M_create_node()
432 _Node* __p = _M_get_node();
433 try {
434 _Construct(&__p->_M_data);
436 catch(...)
438 _M_put_node(__p);
439 __throw_exception_again;
441 return __p;
444 public:
445 // [23.2.2.1] construct/copy/destroy
446 // (assign() and get_allocator() are also listed in this section)
448 * @brief Default constructor creates no elements.
450 explicit
451 list(const allocator_type& __a = allocator_type())
452 : _Base(__a) { }
455 * @brief Create a %list with copies of an exemplar element.
456 * @param n The number of elements to initially create.
457 * @param value An element to copy.
459 * This constructor fills the %list with @a n copies of @a value.
461 list(size_type __n, const value_type& __value,
462 const allocator_type& __a = allocator_type())
463 : _Base(__a)
464 { this->insert(begin(), __n, __value); }
467 * @brief Create a %list with default elements.
468 * @param n The number of elements to initially create.
470 * This constructor fills the %list with @a n copies of a
471 * default-constructed element.
473 explicit
474 list(size_type __n)
475 : _Base(allocator_type())
476 { this->insert(begin(), __n, value_type()); }
479 * @brief %List copy constructor.
480 * @param x A %list of identical element and allocator types.
482 * The newly-created %list uses a copy of the allocation object used
483 * by @a x.
485 list(const list& __x)
486 : _Base(__x.get_allocator())
487 { this->insert(begin(), __x.begin(), __x.end()); }
490 * @brief Builds a %list from a range.
491 * @param first An input iterator.
492 * @param last An input iterator.
494 * Create a %list consisting of copies of the elements from [first,last).
495 * This is linear in N (where N is distance(first,last)).
497 * @if maint
498 * We don't need any dispatching tricks here, because insert does all of
499 * that anyway.
500 * @endif
502 template<typename _InputIterator>
503 list(_InputIterator __first, _InputIterator __last,
504 const allocator_type& __a = allocator_type())
505 : _Base(__a)
506 { this->insert(begin(), __first, __last); }
509 * The dtor only erases the elements, and note that if the elements
510 * themselves are pointers, the pointed-to memory is not touched in any
511 * way. Managing the pointer is the user's responsibilty.
513 ~list() { }
516 * @brief %List assignment operator.
517 * @param x A %list of identical element and allocator types.
519 * All the elements of @a x are copied, but unlike the copy constructor,
520 * the allocator object is not copied.
522 list&
523 operator=(const list& __x);
526 * @brief Assigns a given value to a %list.
527 * @param n Number of elements to be assigned.
528 * @param val Value to be assigned.
530 * This function fills a %list with @a n copies of the given value.
531 * Note that the assignment completely changes the %list and that the
532 * resulting %list's size is the same as the number of elements assigned.
533 * Old data may be lost.
535 void
536 assign(size_type __n, const value_type& __val) { _M_fill_assign(__n, __val); }
539 * @brief Assigns a range to a %list.
540 * @param first An input iterator.
541 * @param last An input iterator.
543 * This function fills a %list with copies of the elements in the
544 * range [first,last).
546 * Note that the assignment completely changes the %list and that the
547 * resulting %list's size is the same as the number of elements assigned.
548 * Old data may be lost.
550 template<typename _InputIterator>
551 void
552 assign(_InputIterator __first, _InputIterator __last)
554 // Check whether it's an integral type. If so, it's not an iterator.
555 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
556 _M_assign_dispatch(__first, __last, _Integral());
559 /// Get a copy of the memory allocation object.
560 allocator_type
561 get_allocator() const { return _Base::get_allocator(); }
563 // iterators
565 * Returns a read/write iterator that points to the first element in the
566 * %list. Iteration is done in ordinary element order.
568 iterator
569 begin() { return static_cast<_Node*>(this->_M_node->_M_next); }
572 * Returns a read-only (constant) iterator that points to the first element
573 * in the %list. Iteration is done in ordinary element order.
575 const_iterator
576 begin() const { return static_cast<_Node*>(this->_M_node->_M_next); }
579 * Returns a read/write iterator that points one past the last element in
580 * the %list. Iteration is done in ordinary element order.
582 iterator
583 end() { return this->_M_node; }
586 * Returns a read-only (constant) iterator that points one past the last
587 * element in the %list. Iteration is done in ordinary element order.
589 const_iterator
590 end() const { return this->_M_node; }
593 * Returns a read/write reverse iterator that points to the last element in
594 * the %list. Iteration is done in reverse element order.
596 reverse_iterator
597 rbegin() { return reverse_iterator(end()); }
600 * Returns a read-only (constant) reverse iterator that points to the last
601 * element in the %list. Iteration is done in reverse element order.
603 const_reverse_iterator
604 rbegin() const { return const_reverse_iterator(end()); }
607 * Returns a read/write reverse iterator that points to one before the
608 * first element in the %list. Iteration is done in reverse element
609 * order.
611 reverse_iterator
612 rend() { return reverse_iterator(begin()); }
615 * Returns a read-only (constant) reverse iterator that points to one
616 * before the first element in the %list. Iteration is done in reverse
617 * element order.
619 const_reverse_iterator
620 rend() const
621 { return const_reverse_iterator(begin()); }
623 // [23.2.2.2] capacity
625 * Returns true if the %list is empty. (Thus begin() would equal end().)
627 bool
628 empty() const { return this->_M_node->_M_next == this->_M_node; }
630 /** Returns the number of elements in the %list. */
631 size_type
632 size() const { return std::distance(begin(), end()); }
634 /** Returns the size() of the largest possible %list. */
635 size_type
636 max_size() const { return size_type(-1); }
639 * @brief Resizes the %list to the specified number of elements.
640 * @param new_size Number of elements the %list should contain.
641 * @param x Data with which new elements should be populated.
643 * This function will %resize the %list to the specified number of
644 * elements. If the number is smaller than the %list's current size the
645 * %list is truncated, otherwise the %list is extended and new elements
646 * are populated with given data.
648 void
649 resize(size_type __new_size, const value_type& __x);
652 * @brief Resizes the %list to the specified number of elements.
653 * @param new_size Number of elements the %list should contain.
655 * This function will resize the %list to the specified number of
656 * elements. If the number is smaller than the %list's current size the
657 * %list is truncated, otherwise the %list is extended and new elements
658 * are default-constructed.
660 void
661 resize(size_type __new_size) { this->resize(__new_size, value_type()); }
663 // element access
665 * Returns a read/write reference to the data at the first element of the
666 * %list.
668 reference
669 front() { return *begin(); }
672 * Returns a read-only (constant) reference to the data at the first
673 * element of the %list.
675 const_reference
676 front() const { return *begin(); }
679 * Returns a read/write reference to the data at the last element of the
680 * %list.
682 reference
683 back() { return *(--end()); }
686 * Returns a read-only (constant) reference to the data at the last
687 * element of the %list.
689 const_reference
690 back() const { return *(--end()); }
692 // [23.2.2.3] modifiers
694 * @brief Add data to the front of the %list.
695 * @param x Data to be added.
697 * This is a typical stack operation. The function creates an element at
698 * the front of the %list and assigns the given data to it. Due to the
699 * nature of a %list this operation can be done in constant time, and
700 * does not invalidate iterators and references.
702 void
703 push_front(const value_type& __x) { this->insert(begin(), __x); }
706 * @brief Removes first element.
708 * This is a typical stack operation. It shrinks the %list by one.
709 * Due to the nature of a %list this operation can be done in constant
710 * time, and only invalidates iterators/references to the element being
711 * removed.
713 * Note that no data is returned, and if the first element's data is
714 * needed, it should be retrieved before pop_front() is called.
716 void
717 pop_front() { this->erase(begin()); }
720 * @brief Add data to the end of the %list.
721 * @param x Data to be added.
723 * This is a typical stack operation. The function creates an element at
724 * the end of the %list and assigns the given data to it. Due to the
725 * nature of a %list this operation can be done in constant time, and
726 * does not invalidate iterators and references.
728 void
729 push_back(const value_type& __x) { this->insert(end(), __x); }
732 * @brief Removes last element.
734 * This is a typical stack operation. It shrinks the %list by one.
735 * Due to the nature of a %list this operation can be done in constant
736 * time, and only invalidates iterators/references to the element being
737 * removed.
739 * Note that no data is returned, and if the last element's data is
740 * needed, it should be retrieved before pop_back() is called.
742 void
743 pop_back()
745 iterator __tmp = end();
746 this->erase(--__tmp);
750 * @brief Inserts given value into %list before specified iterator.
751 * @param position An iterator into the %list.
752 * @param x Data to be inserted.
753 * @return An iterator that points to the inserted data.
755 * This function will insert a copy of the given value before the specified
756 * location.
757 * Due to the nature of a %list this operation can be done in constant
758 * time, and does not invalidate iterators and references.
760 iterator
761 insert(iterator __position, const value_type& __x);
764 * @brief Inserts a number of copies of given data into the %list.
765 * @param position An iterator into the %list.
766 * @param n Number of elements to be inserted.
767 * @param x Data to be inserted.
769 * This function will insert a specified number of copies of the given data
770 * before the location specified by @a position.
772 * Due to the nature of a %list this operation can be done in constant
773 * time, and does not invalidate iterators and references.
775 void
776 insert(iterator __pos, size_type __n, const value_type& __x)
777 { _M_fill_insert(__pos, __n, __x); }
780 * @brief Inserts a range into the %list.
781 * @param pos An iterator into the %list.
782 * @param first An input iterator.
783 * @param last An input iterator.
785 * This function will insert copies of the data in the range [first,last)
786 * into the %list before the location specified by @a pos.
788 * Due to the nature of a %list this operation can be done in constant
789 * time, and does not invalidate iterators and references.
791 template<typename _InputIterator>
792 void
793 insert(iterator __pos, _InputIterator __first, _InputIterator __last)
795 // Check whether it's an integral type. If so, it's not an iterator.
796 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
797 _M_insert_dispatch(__pos, __first, __last, _Integral());
801 * @brief Remove element at given position.
802 * @param position Iterator pointing to element to be erased.
803 * @return An iterator pointing to the next element (or end()).
805 * This function will erase the element at the given position and thus
806 * shorten the %list by one.
808 * Due to the nature of a %list this operation can be done in constant
809 * time, and only invalidates iterators/references to the element being
810 * removed.
811 * The user is also cautioned that
812 * this function only erases the element, and that if the element is itself
813 * a pointer, the pointed-to memory is not touched in any way. Managing
814 * the pointer is the user's responsibilty.
816 iterator
817 erase(iterator __position);
820 * @brief Remove a range of elements.
821 * @param first Iterator pointing to the first element to be erased.
822 * @param last Iterator pointing to one past the last element to be
823 * erased.
824 * @return An iterator pointing to the element pointed to by @a last
825 * prior to erasing (or end()).
827 * This function will erase the elements in the range [first,last) and
828 * shorten the %list accordingly.
830 * Due to the nature of a %list this operation can be done in constant
831 * time, and only invalidates iterators/references to the element being
832 * removed.
833 * The user is also cautioned that
834 * this function only erases the elements, and that if the elements
835 * themselves are pointers, the pointed-to memory is not touched in any
836 * way. Managing the pointer is the user's responsibilty.
838 iterator
839 erase(iterator __first, iterator __last)
841 while (__first != __last)
842 erase(__first++);
843 return __last;
847 * @brief Swaps data with another %list.
848 * @param x A %list of the same element and allocator types.
850 * This exchanges the elements between two lists in constant time.
851 * (It is only swapping a single pointer, so it should be quite fast.)
852 * Note that the global std::swap() function is specialized such that
853 * std::swap(l1,l2) will feed to this function.
855 void
856 swap(list& __x) { std::swap(this->_M_node, __x._M_node); }
859 * Erases all the elements. Note that this function only erases the
860 * elements, and that if the elements themselves are pointers, the
861 * pointed-to memory is not touched in any way. Managing the pointer is
862 * the user's responsibilty.
864 void
865 clear() { _Base::__clear(); }
867 // [23.2.2.4] list operations
869 * @doctodo
871 void
872 splice(iterator __position, list& __x)
874 if (!__x.empty())
875 this->_M_transfer(__position, __x.begin(), __x.end());
879 * @doctodo
881 void
882 splice(iterator __position, list&, iterator __i)
884 iterator __j = __i;
885 ++__j;
886 if (__position == __i || __position == __j) return;
887 this->_M_transfer(__position, __i, __j);
891 * @doctodo
893 void
894 splice(iterator __position, list&, iterator __first, iterator __last)
896 if (__first != __last)
897 this->_M_transfer(__position, __first, __last);
901 * @doctodo
903 void
904 remove(const _Tp& __value);
907 * @doctodo
909 template<typename _Predicate>
910 void
911 remove_if(_Predicate);
914 * @doctodo
916 void
917 unique();
920 * @doctodo
922 template<typename _BinaryPredicate>
923 void
924 unique(_BinaryPredicate);
927 * @doctodo
929 void
930 merge(list& __x);
933 * @doctodo
935 template<typename _StrictWeakOrdering>
936 void
937 merge(list&, _StrictWeakOrdering);
940 * @doctodo
942 void
943 reverse() { __List_base_reverse(this->_M_node); }
946 * @doctodo
948 void
949 sort();
952 * @doctodo
954 template<typename _StrictWeakOrdering>
955 void
956 sort(_StrictWeakOrdering);
958 protected:
959 // Internal assign functions follow.
961 // called by the range assign to implement [23.1.1]/9
962 template<typename _Integer>
963 void
964 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
966 _M_fill_assign(static_cast<size_type>(__n),
967 static_cast<value_type>(__val));
970 // called by the range assign to implement [23.1.1]/9
971 template<typename _InputIter>
972 void
973 _M_assign_dispatch(_InputIter __first, _InputIter __last, __false_type);
975 // Called by assign(n,t), and the range assign when it turns out to be the
976 // same thing.
977 void
978 _M_fill_assign(size_type __n, const value_type& __val);
981 // Internal insert functions follow.
983 // called by the range insert to implement [23.1.1]/9
984 template<typename _Integer>
985 void
986 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
987 __true_type)
989 _M_fill_insert(__pos, static_cast<size_type>(__n),
990 static_cast<value_type>(__x));
993 // called by the range insert to implement [23.1.1]/9
994 template<typename _InputIterator>
995 void
996 _M_insert_dispatch(iterator __pos,
997 _InputIterator __first, _InputIterator __last,
998 __false_type)
1000 for ( ; __first != __last; ++__first)
1001 insert(__pos, *__first);
1004 // Called by insert(p,n,x), and the range insert when it turns out to be
1005 // the same thing.
1006 void
1007 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x)
1009 for ( ; __n > 0; --__n)
1010 insert(__pos, __x);
1014 // Moves the elements from [first,last) before position.
1015 void
1016 _M_transfer(iterator __position, iterator __first, iterator __last)
1018 if (__position != __last) {
1019 // Remove [first, last) from its old position.
1020 __last._M_node->_M_prev->_M_next = __position._M_node;
1021 __first._M_node->_M_prev->_M_next = __last._M_node;
1022 __position._M_node->_M_prev->_M_next = __first._M_node;
1024 // Splice [first, last) into its new position.
1025 _List_node_base* __tmp = __position._M_node->_M_prev;
1026 __position._M_node->_M_prev = __last._M_node->_M_prev;
1027 __last._M_node->_M_prev = __first._M_node->_M_prev;
1028 __first._M_node->_M_prev = __tmp;
1035 * @brief List equality comparison.
1036 * @param x A %list.
1037 * @param y A %list of the same type as @a x.
1038 * @return True iff the size and elements of the lists are equal.
1040 * This is an equivalence relation. It is linear in the size of the
1041 * lists. Lists are considered equivalent if their sizes are equal,
1042 * and if corresponding elements compare equal.
1044 template<typename _Tp, typename _Alloc>
1045 inline bool
1046 operator==(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
1048 typedef typename list<_Tp,_Alloc>::const_iterator const_iterator;
1049 const_iterator __end1 = __x.end();
1050 const_iterator __end2 = __y.end();
1052 const_iterator __i1 = __x.begin();
1053 const_iterator __i2 = __y.begin();
1054 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2) {
1055 ++__i1;
1056 ++__i2;
1058 return __i1 == __end1 && __i2 == __end2;
1062 * @brief List ordering relation.
1063 * @param x A %list.
1064 * @param y A %list of the same type as @a x.
1065 * @return True iff @a x is lexicographically less than @a y.
1067 * This is a total ordering relation. It is linear in the size of the
1068 * lists. The elements must be comparable with @c <.
1070 * See std::lexicographical_compare() for how the determination is made.
1072 template<typename _Tp, typename _Alloc>
1073 inline bool
1074 operator<(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
1076 return lexicographical_compare(__x.begin(), __x.end(),
1077 __y.begin(), __y.end());
1080 /// Based on operator==
1081 template<typename _Tp, typename _Alloc>
1082 inline bool
1083 operator!=(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
1084 { return !(__x == __y); }
1086 /// Based on operator<
1087 template<typename _Tp, typename _Alloc>
1088 inline bool
1089 operator>(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
1090 { return __y < __x; }
1092 /// Based on operator<
1093 template<typename _Tp, typename _Alloc>
1094 inline bool
1095 operator<=(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
1096 { return !(__y < __x); }
1098 /// Based on operator<
1099 template<typename _Tp, typename _Alloc>
1100 inline bool
1101 operator>=(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
1102 { return !(__x < __y); }
1104 /// See std::list::swap().
1105 template<typename _Tp, typename _Alloc>
1106 inline void
1107 swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
1108 { __x.swap(__y); }
1109 } // namespace std
1111 #endif /* __GLIBCPP_INTERNAL_LIST_H */