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