linker-map.gnu: Verbose comments, clean up spacing.
[gcc.git] / libstdc++-v3 / include / bits / stl_list.h
1 // List implementation -*- C++ -*-
2
3 // Copyright (C) 2001, 2002 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 __GLIBCPP_INTERNAL_LIST_H
62 #define __GLIBCPP_INTERNAL_LIST_H
63
64 #include <bits/concept_check.h>
65
66 namespace 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
80 /// @if maint An actual node in the %list. @endif
81 template<typename _Tp>
82 struct _List_node : public _List_node_base
83 {
84 _Tp _M_data; ///< User's data.
85 };
86
87
88 /**
89 * @if maint
90 * @brief Common part of a list::iterator.
91 *
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
96 */
97 struct _List_iterator_base
98 {
99 typedef size_t size_type;
100 typedef ptrdiff_t difference_type;
101 typedef bidirectional_iterator_tag iterator_category;
102
103 /// The only member points to the %list element.
104 _List_node_base* _M_node;
105
106 _List_iterator_base(_List_node_base* __x)
107 : _M_node(__x)
108 { }
109
110 _List_iterator_base()
111 { }
112
113 /// Walk the %list forward.
114 void
115 _M_incr()
116 { _M_node = _M_node->_M_next; }
117
118 /// Walk the %list backward.
119 void
120 _M_decr()
121 { _M_node = _M_node->_M_prev; }
122
123 bool
124 operator==(const _List_iterator_base& __x) const
125 { return _M_node == __x._M_node; }
126
127 bool
128 operator!=(const _List_iterator_base& __x) const
129 { return _M_node != __x._M_node; }
130 };
131
132 /**
133 * @brief A list::iterator.
134 *
135 * In addition to being used externally, a list holds one of these
136 * internally, pointing to the sequence of data.
137 *
138 * @if maint
139 * All the functions are op overloads.
140 * @endif
141 */
142 template<typename _Tp, typename _Ref, typename _Ptr>
143 struct _List_iterator : public _List_iterator_base
144 {
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;
148
149 typedef _Tp value_type;
150 typedef _Ptr pointer;
151 typedef _Ref reference;
152 typedef _List_node<_Tp> _Node;
153
154 _List_iterator(_Node* __x)
155 : _List_iterator_base(__x)
156 { }
157
158 _List_iterator()
159 { }
160
161 _List_iterator(const iterator& __x)
162 : _List_iterator_base(__x._M_node)
163 { }
164
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.
169
170 pointer
171 operator->() const
172 { return &(operator*()); }
173
174 _Self&
175 operator++()
176 {
177 this->_M_incr();
178 return *this;
179 }
180
181 _Self
182 operator++(int)
183 {
184 _Self __tmp = *this;
185 this->_M_incr();
186 return __tmp;
187 }
188
189 _Self&
190 operator--()
191 {
192 this->_M_decr();
193 return *this;
194 }
195
196 _Self
197 operator--(int)
198 {
199 _Self __tmp = *this;
200 this->_M_decr();
201 return __tmp;
202 }
203 };
204
205
206 /// @if maint Primary default version. @endif
207 /**
208 * @if maint
209 * See bits/stl_deque.h's _Deque_alloc_base for an explanation.
210 * @endif
211 */
212 template<typename _Tp, typename _Allocator, bool _IsStatic>
213 class _List_alloc_base
214 {
215 public:
216 typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type
217 allocator_type;
218
219 allocator_type
220 get_allocator() const { return _M_node_allocator; }
221
222 _List_alloc_base(const allocator_type& __a)
223 : _M_node_allocator(__a)
224 { }
225
226 protected:
227 _List_node<_Tp>*
228 _M_get_node()
229 { return _M_node_allocator.allocate(1); }
230
231 void
232 _M_put_node(_List_node<_Tp>* __p)
233 { _M_node_allocator.deallocate(__p, 1); }
234
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.
241 //
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.
244 //
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;
249
250 _List_node<_Tp>* _M_node;
251 };
252
253 /// @if maint Specialization for instanceless allocators. @endif
254 template<typename _Tp, typename _Allocator>
255 class _List_alloc_base<_Tp, _Allocator, true>
256 {
257 public:
258 typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type
259 allocator_type;
260
261 allocator_type
262 get_allocator() const { return allocator_type(); }
263
264 _List_alloc_base(const allocator_type&)
265 { }
266
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;
272
273 _List_node<_Tp>*
274 _M_get_node()
275 { return _Alloc_type::allocate(1); }
276
277 void
278 _M_put_node(_List_node<_Tp>* __p)
279 { _Alloc_type::deallocate(__p, 1); }
280
281 _List_node<_Tp>* _M_node;
282 };
283
284
285 /**
286 * @if maint
287 * See bits/stl_deque.h's _Deque_base for an explanation.
288 * @endif
289 */
290 template <typename _Tp, typename _Alloc>
291 class _List_base
292 : public _List_alloc_base<_Tp, _Alloc,
293 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
294 {
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;
300
301 _List_base(const allocator_type& __a)
302 : _Base(__a)
303 {
304 _M_node = _M_get_node();
305 _M_node->_M_next = _M_node;
306 _M_node->_M_prev = _M_node;
307 }
308
309 // This is what actually destroys the list.
310 ~_List_base()
311 {
312 __clear();
313 _M_put_node(_M_node);
314 }
315
316 void
317 __clear();
318 };
319
320
321 /**
322 * @brief A standard container with linear time access to elements, and
323 * fixed time insertion/deletion at any point in the sequence.
324 *
325 * @ingroup Containers
326 * @ingroup Sequences
327 *
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[].
333 *
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.
340 *
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.
344 *
345 * @if maint
346 * A couple points on memory allocation for list<Tp>:
347 *
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.
353 *
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
364 */
365 template<typename _Tp, typename _Alloc = allocator<_Tp> >
366 class list : protected _List_base<_Tp, _Alloc>
367 {
368 // concept requirements
369 __glibcpp_class_requires(_Tp, _SGIAssignableConcept)
370
371 typedef _List_base<_Tp, _Alloc> _Base;
372
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 reverse_iterator<const_iterator> const_reverse_iterator;
380 typedef 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;
386
387 protected:
388 // Note that pointers-to-_Node's can be ctor-converted to iterator types.
389 typedef _List_node<_Tp> _Node;
390
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
396 */
397 using _Base::_M_node;
398 using _Base::_M_put_node;
399 using _Base::_M_get_node;
400
401 /**
402 * @if maint
403 * @param x An instance of user data.
404 *
405 * Allocates space for a new node and constructs a copy of @a x in it.
406 * @endif
407 */
408 _Node*
409 _M_create_node(const value_type& __x)
410 {
411 _Node* __p = _M_get_node();
412 try {
413 _Construct(&__p->_M_data, __x);
414 }
415 catch(...)
416 {
417 _M_put_node(__p);
418 __throw_exception_again;
419 }
420 return __p;
421 }
422
423 /**
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
428 */
429 _Node*
430 _M_create_node()
431 {
432 _Node* __p = _M_get_node();
433 try {
434 _Construct(&__p->_M_data);
435 }
436 catch(...)
437 {
438 _M_put_node(__p);
439 __throw_exception_again;
440 }
441 return __p;
442 }
443
444 public:
445 // [23.2.2.1] construct/copy/destroy
446 // (assign() and get_allocator() are also listed in this section)
447 /**
448 * @brief Default constructor creates no elements.
449 */
450 explicit
451 list(const allocator_type& __a = allocator_type())
452 : _Base(__a) { }
453
454 /**
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.
458 *
459 * This constructor fills the %list with @a n copies of @a value.
460 */
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); }
465
466 /**
467 * @brief Create a %list with default elements.
468 * @param n The number of elements to initially create.
469 *
470 * This constructor fills the %list with @a n copies of a
471 * default-constructed element.
472 */
473 explicit
474 list(size_type __n)
475 : _Base(allocator_type())
476 { this->insert(begin(), __n, value_type()); }
477
478 /**
479 * @brief %List copy constructor.
480 * @param x A %list of identical element and allocator types.
481 *
482 * The newly-created %list uses a copy of the allocation object used
483 * by @a x.
484 */
485 list(const list& __x)
486 : _Base(__x.get_allocator())
487 { this->insert(begin(), __x.begin(), __x.end()); }
488
489 /**
490 * @brief Builds a %list from a range.
491 * @param first An input iterator.
492 * @param last An input iterator.
493 *
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)).
496 *
497 * @if maint
498 * We don't need any dispatching tricks here, because insert does all of
499 * that anyway.
500 * @endif
501 */
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); }
507
508 /**
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.
512 */
513 ~list() { }
514
515 /**
516 * @brief %List assignment operator.
517 * @param x A %list of identical element and allocator types.
518 *
519 * All the elements of @a x are copied, but unlike the copy constructor,
520 * the allocator object is not copied.
521 */
522 list&
523 operator=(const list& __x);
524
525 /**
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.
529 *
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.
534 */
535 void
536 assign(size_type __n, const value_type& __val) { _M_fill_assign(__n, __val); }
537
538 /**
539 * @brief Assigns a range to a %list.
540 * @param first An input iterator.
541 * @param last An input iterator.
542 *
543 * This function fills a %list with copies of the elements in the
544 * range [first,last).
545 *
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.
549 */
550 template<typename _InputIterator>
551 void
552 assign(_InputIterator __first, _InputIterator __last)
553 {
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());
557 }
558
559 /// Get a copy of the memory allocation object.
560 allocator_type
561 get_allocator() const { return _Base::get_allocator(); }
562
563 // iterators
564 /**
565 * Returns a read/write iterator that points to the first element in the
566 * %list. Iteration is done in ordinary element order.
567 */
568 iterator
569 begin() { return static_cast<_Node*>(_M_node->_M_next); }
570
571 /**
572 * Returns a read-only (constant) iterator that points to the first element
573 * in the %list. Iteration is done in ordinary element order.
574 */
575 const_iterator
576 begin() const { return static_cast<_Node*>(_M_node->_M_next); }
577
578 /**
579 * Returns a read/write iterator that points one past the last element in
580 * the %list. Iteration is done in ordinary element order.
581 */
582 iterator
583 end() { return _M_node; }
584
585 /**
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.
588 */
589 const_iterator
590 end() const { return _M_node; }
591
592 /**
593 * Returns a read/write reverse iterator that points to the last element in
594 * the %list. Iteration is done in reverse element order.
595 */
596 reverse_iterator
597 rbegin() { return reverse_iterator(end()); }
598
599 /**
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.
602 */
603 const_reverse_iterator
604 rbegin() const { return const_reverse_iterator(end()); }
605
606 /**
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.
610 */
611 reverse_iterator
612 rend() { return reverse_iterator(begin()); }
613
614 /**
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.
618 */
619 const_reverse_iterator
620 rend() const
621 { return const_reverse_iterator(begin()); }
622
623 // [23.2.2.2] capacity
624 /**
625 * Returns true if the %list is empty. (Thus begin() would equal end().)
626 */
627 bool
628 empty() const { return _M_node->_M_next == _M_node; }
629
630 /** Returns the number of elements in the %list. */
631 size_type
632 size() const { return distance(begin(), end()); }
633
634 /** Returns the size() of the largest possible %list. */
635 size_type
636 max_size() const { return size_type(-1); }
637
638 /**
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.
642 *
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.
647 */
648 void
649 resize(size_type __new_size, const value_type& __x);
650
651 /**
652 * @brief Resizes the %list to the specified number of elements.
653 * @param new_size Number of elements the %list should contain.
654 *
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.
659 */
660 void
661 resize(size_type __new_size) { this->resize(__new_size, value_type()); }
662
663 // element access
664 /**
665 * Returns a read/write reference to the data at the first element of the
666 * %list.
667 */
668 reference
669 front() { return *begin(); }
670
671 /**
672 * Returns a read-only (constant) reference to the data at the first
673 * element of the %list.
674 */
675 const_reference
676 front() const { return *begin(); }
677
678 /**
679 * Returns a read/write reference to the data at the last element of the
680 * %list.
681 */
682 reference
683 back() { return *(--end()); }
684
685 /**
686 * Returns a read-only (constant) reference to the data at the last
687 * element of the %list.
688 */
689 const_reference
690 back() const { return *(--end()); }
691
692 // [23.2.2.3] modifiers
693 /**
694 * @brief Add data to the front of the %list.
695 * @param x Data to be added.
696 *
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.
701 */
702 void
703 push_front(const value_type& __x) { this->insert(begin(), __x); }
704
705 #ifdef _GLIBCPP_DEPRECATED
706 /**
707 * @brief Add data to the front of the %list.
708 *
709 * This is a typical stack operation. The function creates a
710 * default-constructed element at the front of the %list. Due to the
711 * nature of a %list this operation can be done in constant time. You
712 * should consider using push_front(value_type()) instead.
713 *
714 * @note This was deprecated in 3.2 and will be removed in 3.4. You must
715 * define @c _GLIBCPP_DEPRECATED to make this visible in 3.2; see
716 * c++config.h.
717 */
718 void
719 push_front() { this->insert(begin(), value_type()); }
720 #endif
721
722 /**
723 * @brief Removes first element.
724 *
725 * This is a typical stack operation. It shrinks the %list by one.
726 * Due to the nature of a %list this operation can be done in constant
727 * time, and only invalidates iterators/references to the element being
728 * removed.
729 *
730 * Note that no data is returned, and if the first element's data is
731 * needed, it should be retrieved before pop_front() is called.
732 */
733 void
734 pop_front() { this->erase(begin()); }
735
736 /**
737 * @brief Add data to the end of the %list.
738 * @param x Data to be added.
739 *
740 * This is a typical stack operation. The function creates an element at
741 * the end of the %list and assigns the given data to it. Due to the
742 * nature of a %list this operation can be done in constant time, and
743 * does not invalidate iterators and references.
744 */
745 void
746 push_back(const value_type& __x) { this->insert(end(), __x); }
747
748 #ifdef _GLIBCPP_DEPRECATED
749 /**
750 * @brief Add data to the end of the %list.
751 *
752 * This is a typical stack operation. The function creates a
753 * default-constructed element at the end of the %list. Due to the nature
754 * of a %list this operation can be done in constant time. You should
755 * consider using push_back(value_type()) instead.
756 *
757 * @note This was deprecated in 3.2 and will be removed in 3.4. You must
758 * define @c _GLIBCPP_DEPRECATED to make this visible in 3.2; see
759 * c++config.h.
760 */
761 void
762 push_back() { this->insert(end(), value_type()); }
763 #endif
764
765 /**
766 * @brief Removes last element.
767 *
768 * This is a typical stack operation. It shrinks the %list by one.
769 * Due to the nature of a %list this operation can be done in constant
770 * time, and only invalidates iterators/references to the element being
771 * removed.
772 *
773 * Note that no data is returned, and if the last element's data is
774 * needed, it should be retrieved before pop_back() is called.
775 */
776 void
777 pop_back()
778 {
779 iterator __tmp = end();
780 this->erase(--__tmp);
781 }
782
783 /**
784 * @brief Inserts given value into %list before specified iterator.
785 * @param position An iterator into the %list.
786 * @param x Data to be inserted.
787 * @return An iterator that points to the inserted data.
788 *
789 * This function will insert a copy of the given value before the specified
790 * location.
791 * Due to the nature of a %list this operation can be done in constant
792 * time, and does not invalidate iterators and references.
793 */
794 iterator
795 insert(iterator __position, const value_type& __x);
796
797 #ifdef _GLIBCPP_DEPRECATED
798 /**
799 * @brief Inserts an element into the %list.
800 * @param position An iterator into the %list.
801 * @return An iterator that points to the inserted element.
802 *
803 * This function will insert a default-constructed element before the
804 * specified location. You should consider using
805 * insert(position,value_type()) instead.
806 * Due to the nature of a %list this operation can be done in constant
807 * time, and does not invalidate iterators and references.
808 *
809 * @note This was deprecated in 3.2 and will be removed in 3.4. You must
810 * define @c _GLIBCPP_DEPRECATED to make this visible in 3.2; see
811 * c++config.h.
812 */
813 iterator
814 insert(iterator __position) { return insert(__position, value_type()); }
815 #endif
816
817 /**
818 * @brief Inserts a number of copies of given data into the %list.
819 * @param position An iterator into the %list.
820 * @param n Number of elements to be inserted.
821 * @param x Data to be inserted.
822 *
823 * This function will insert a specified number of copies of the given data
824 * before the location specified by @a position.
825 *
826 * Due to the nature of a %list this operation can be done in constant
827 * time, and does not invalidate iterators and references.
828 */
829 void
830 insert(iterator __pos, size_type __n, const value_type& __x)
831 { _M_fill_insert(__pos, __n, __x); }
832
833 /**
834 * @brief Inserts a range into the %list.
835 * @param pos An iterator into the %list.
836 * @param first An input iterator.
837 * @param last An input iterator.
838 *
839 * This function will insert copies of the data in the range [first,last)
840 * into the %list before the location specified by @a pos.
841 *
842 * Due to the nature of a %list this operation can be done in constant
843 * time, and does not invalidate iterators and references.
844 */
845 template<typename _InputIterator>
846 void
847 insert(iterator __pos, _InputIterator __first, _InputIterator __last)
848 {
849 // Check whether it's an integral type. If so, it's not an iterator.
850 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
851 _M_insert_dispatch(__pos, __first, __last, _Integral());
852 }
853
854 /**
855 * @brief Remove element at given position.
856 * @param position Iterator pointing to element to be erased.
857 * @return An iterator pointing to the next element (or end()).
858 *
859 * This function will erase the element at the given position and thus
860 * shorten the %list by one.
861 *
862 * Due to the nature of a %list this operation can be done in constant
863 * time, and only invalidates iterators/references to the element being
864 * removed.
865 * The user is also cautioned that
866 * this function only erases the element, and that if the element is itself
867 * a pointer, the pointed-to memory is not touched in any way. Managing
868 * the pointer is the user's responsibilty.
869 */
870 iterator
871 erase(iterator __position);
872
873 /**
874 * @brief Remove a range of elements.
875 * @param first Iterator pointing to the first element to be erased.
876 * @param last Iterator pointing to one past the last element to be
877 * erased.
878 * @return An iterator pointing to the element pointed to by @a last
879 * prior to erasing (or end()).
880 *
881 * This function will erase the elements in the range [first,last) and
882 * shorten the %list accordingly.
883 *
884 * Due to the nature of a %list this operation can be done in constant
885 * time, and only invalidates iterators/references to the element being
886 * removed.
887 * The user is also cautioned that
888 * this function only erases the elements, and that if the elements
889 * themselves are pointers, the pointed-to memory is not touched in any
890 * way. Managing the pointer is the user's responsibilty.
891 */
892 iterator
893 erase(iterator __first, iterator __last)
894 {
895 while (__first != __last)
896 erase(__first++);
897 return __last;
898 }
899
900 /**
901 * @brief Swaps data with another %list.
902 * @param x A %list of the same element and allocator types.
903 *
904 * This exchanges the elements between two lists in constant time.
905 * (It is only swapping a single pointer, so it should be quite fast.)
906 * Note that the global std::swap() function is specialized such that
907 * std::swap(l1,l2) will feed to this function.
908 */
909 void
910 swap(list& __x) { std::swap(_M_node, __x._M_node); }
911
912 /**
913 * Erases all the elements. Note that this function only erases the
914 * elements, and that if the elements themselves are pointers, the
915 * pointed-to memory is not touched in any way. Managing the pointer is
916 * the user's responsibilty.
917 */
918 void
919 clear() { _Base::__clear(); }
920
921 // [23.2.2.4] list operations
922 /**
923 * @doctodo
924 */
925 void
926 splice(iterator __position, list& __x)
927 {
928 if (!__x.empty())
929 this->_M_transfer(__position, __x.begin(), __x.end());
930 }
931
932 /**
933 * @doctodo
934 */
935 void
936 splice(iterator __position, list&, iterator __i)
937 {
938 iterator __j = __i;
939 ++__j;
940 if (__position == __i || __position == __j) return;
941 this->_M_transfer(__position, __i, __j);
942 }
943
944 /**
945 * @doctodo
946 */
947 void
948 splice(iterator __position, list&, iterator __first, iterator __last)
949 {
950 if (__first != __last)
951 this->_M_transfer(__position, __first, __last);
952 }
953
954 /**
955 * @doctodo
956 */
957 void
958 remove(const _Tp& __value);
959
960 /**
961 * @doctodo
962 */
963 template<typename _Predicate>
964 void
965 remove_if(_Predicate);
966
967 /**
968 * @doctodo
969 */
970 void
971 unique();
972
973 /**
974 * @doctodo
975 */
976 template<typename _BinaryPredicate>
977 void
978 unique(_BinaryPredicate);
979
980 /**
981 * @doctodo
982 */
983 void
984 merge(list& __x);
985
986 /**
987 * @doctodo
988 */
989 template<typename _StrictWeakOrdering>
990 void
991 merge(list&, _StrictWeakOrdering);
992
993 /**
994 * @doctodo
995 */
996 void
997 reverse() { __List_base_reverse(this->_M_node); }
998
999 /**
1000 * @doctodo
1001 */
1002 void
1003 sort();
1004
1005 /**
1006 * @doctodo
1007 */
1008 template<typename _StrictWeakOrdering>
1009 void
1010 sort(_StrictWeakOrdering);
1011
1012 protected:
1013 // Internal assign functions follow.
1014
1015 // called by the range assign to implement [23.1.1]/9
1016 template<typename _Integer>
1017 void
1018 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1019 {
1020 _M_fill_assign(static_cast<size_type>(__n),
1021 static_cast<value_type>(__val));
1022 }
1023
1024 // called by the range assign to implement [23.1.1]/9
1025 template<typename _InputIter>
1026 void
1027 _M_assign_dispatch(_InputIter __first, _InputIter __last, __false_type);
1028
1029 // Called by assign(n,t), and the range assign when it turns out to be the
1030 // same thing.
1031 void
1032 _M_fill_assign(size_type __n, const value_type& __val);
1033
1034
1035 // Internal insert functions follow.
1036
1037 // called by the range insert to implement [23.1.1]/9
1038 template<typename _Integer>
1039 void
1040 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __x,
1041 __true_type)
1042 {
1043 _M_fill_insert(__pos, static_cast<size_type>(__n),
1044 static_cast<value_type>(__x));
1045 }
1046
1047 // called by the range insert to implement [23.1.1]/9
1048 template<typename _InputIterator>
1049 void
1050 _M_insert_dispatch(iterator __pos,
1051 _InputIterator __first, _InputIterator __last,
1052 __false_type)
1053 {
1054 for ( ; __first != __last; ++__first)
1055 insert(__pos, *__first);
1056 }
1057
1058 // Called by insert(p,n,x), and the range insert when it turns out to be
1059 // the same thing.
1060 void
1061 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x)
1062 {
1063 for ( ; __n > 0; --__n)
1064 insert(__pos, __x);
1065 }
1066
1067
1068 // Moves the elements from [first,last) before position.
1069 void
1070 _M_transfer(iterator __position, iterator __first, iterator __last)
1071 {
1072 if (__position != __last) {
1073 // Remove [first, last) from its old position.
1074 __last._M_node->_M_prev->_M_next = __position._M_node;
1075 __first._M_node->_M_prev->_M_next = __last._M_node;
1076 __position._M_node->_M_prev->_M_next = __first._M_node;
1077
1078 // Splice [first, last) into its new position.
1079 _List_node_base* __tmp = __position._M_node->_M_prev;
1080 __position._M_node->_M_prev = __last._M_node->_M_prev;
1081 __last._M_node->_M_prev = __first._M_node->_M_prev;
1082 __first._M_node->_M_prev = __tmp;
1083 }
1084 }
1085 };
1086
1087
1088 /**
1089 * @brief List equality comparison.
1090 * @param x A %list.
1091 * @param y A %list of the same type as @a x.
1092 * @return True iff the size and elements of the lists are equal.
1093 *
1094 * This is an equivalence relation. It is linear in the size of the
1095 * lists. Lists are considered equivalent if their sizes are equal,
1096 * and if corresponding elements compare equal.
1097 */
1098 template<typename _Tp, typename _Alloc>
1099 inline bool
1100 operator==(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
1101 {
1102 typedef typename list<_Tp,_Alloc>::const_iterator const_iterator;
1103 const_iterator __end1 = __x.end();
1104 const_iterator __end2 = __y.end();
1105
1106 const_iterator __i1 = __x.begin();
1107 const_iterator __i2 = __y.begin();
1108 while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2) {
1109 ++__i1;
1110 ++__i2;
1111 }
1112 return __i1 == __end1 && __i2 == __end2;
1113 }
1114
1115 /**
1116 * @brief List ordering relation.
1117 * @param x A %list.
1118 * @param y A %list of the same type as @a x.
1119 * @return True iff @a x is lexographically less than @a y.
1120 *
1121 * This is a total ordering relation. It is linear in the size of the
1122 * lists. The elements must be comparable with @c <.
1123 *
1124 * See std::lexographical_compare() for how the determination is made.
1125 */
1126 template<typename _Tp, typename _Alloc>
1127 inline bool
1128 operator<(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
1129 {
1130 return lexicographical_compare(__x.begin(), __x.end(),
1131 __y.begin(), __y.end());
1132 }
1133
1134 /// Based on operator==
1135 template<typename _Tp, typename _Alloc>
1136 inline bool
1137 operator!=(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
1138 { return !(__x == __y); }
1139
1140 /// Based on operator<
1141 template<typename _Tp, typename _Alloc>
1142 inline bool
1143 operator>(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
1144 { return __y < __x; }
1145
1146 /// Based on operator<
1147 template<typename _Tp, typename _Alloc>
1148 inline bool
1149 operator<=(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
1150 { return !(__y < __x); }
1151
1152 /// Based on operator<
1153 template<typename _Tp, typename _Alloc>
1154 inline bool
1155 operator>=(const list<_Tp,_Alloc>& __x, const list<_Tp,_Alloc>& __y)
1156 { return !(__x < __y); }
1157
1158 /// See std::list::swap().
1159 template<typename _Tp, typename _Alloc>
1160 inline void
1161 swap(list<_Tp, _Alloc>& __x, list<_Tp, _Alloc>& __y)
1162 { __x.swap(__y); }
1163 } // namespace std
1164
1165 #endif /* __GLIBCPP_INTERNAL_LIST_H */