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