libstdc++
stl_list.h
Go to the documentation of this file.
1 // List implementation -*- C++ -*-
2 
3 // Copyright (C) 2001-2013 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /*
26  *
27  * Copyright (c) 1994
28  * Hewlett-Packard Company
29  *
30  * Permission to use, copy, modify, distribute and sell this software
31  * and its documentation for any purpose is hereby granted without fee,
32  * provided that the above copyright notice appear in all copies and
33  * that both that copyright notice and this permission notice appear
34  * in supporting documentation. Hewlett-Packard Company makes no
35  * representations about the suitability of this software for any
36  * purpose. It is provided "as is" without express or implied warranty.
37  *
38  *
39  * Copyright (c) 1996,1997
40  * Silicon Graphics Computer Systems, Inc.
41  *
42  * Permission to use, copy, modify, distribute and sell this software
43  * and its documentation for any purpose is hereby granted without fee,
44  * provided that the above copyright notice appear in all copies and
45  * that both that copyright notice and this permission notice appear
46  * in supporting documentation. Silicon Graphics makes no
47  * representations about the suitability of this software for any
48  * purpose. It is provided "as is" without express or implied warranty.
49  */
50 
51 /** @file bits/stl_list.h
52  * This is an internal header file, included by other library headers.
53  * Do not attempt to use it directly. @headername{list}
54  */
55 
56 #ifndef _STL_LIST_H
57 #define _STL_LIST_H 1
58 
59 #include <bits/concept_check.h>
60 #if __cplusplus >= 201103L
61 #include <initializer_list>
62 #endif
63 
64 namespace std _GLIBCXX_VISIBILITY(default)
65 {
66  namespace __detail
67  {
68  _GLIBCXX_BEGIN_NAMESPACE_VERSION
69 
70  // Supporting structures are split into common and templated
71  // types; the latter publicly inherits from the former in an
72  // effort to reduce code duplication. This results in some
73  // "needless" static_cast'ing later on, but it's all safe
74  // downcasting.
75 
76  /// Common part of a node in the %list.
78  {
79  _List_node_base* _M_next;
80  _List_node_base* _M_prev;
81 
82  static void
83  swap(_List_node_base& __x, _List_node_base& __y) _GLIBCXX_USE_NOEXCEPT;
84 
85  void
86  _M_transfer(_List_node_base* const __first,
87  _List_node_base* const __last) _GLIBCXX_USE_NOEXCEPT;
88 
89  void
90  _M_reverse() _GLIBCXX_USE_NOEXCEPT;
91 
92  void
93  _M_hook(_List_node_base* const __position) _GLIBCXX_USE_NOEXCEPT;
94 
95  void
96  _M_unhook() _GLIBCXX_USE_NOEXCEPT;
97  };
98 
99  _GLIBCXX_END_NAMESPACE_VERSION
100  } // namespace detail
101 
102 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
103 
104  /// An actual node in the %list.
105  template<typename _Tp>
107  {
108  ///< User's data.
109  _Tp _M_data;
110 
111 #if __cplusplus >= 201103L
112  template<typename... _Args>
113  _List_node(_Args&&... __args)
114  : __detail::_List_node_base(), _M_data(std::forward<_Args>(__args)...)
115  { }
116 #endif
117  };
118 
119  /**
120  * @brief A list::iterator.
121  *
122  * All the functions are op overloads.
123  */
124  template<typename _Tp>
126  {
127  typedef _List_iterator<_Tp> _Self;
128  typedef _List_node<_Tp> _Node;
129 
130  typedef ptrdiff_t difference_type;
132  typedef _Tp value_type;
133  typedef _Tp* pointer;
134  typedef _Tp& reference;
135 
137  : _M_node() { }
138 
139  explicit
141  : _M_node(__x) { }
142 
143  _Self
144  _M_const_cast() const
145  { return *this; }
146 
147  // Must downcast from _List_node_base to _List_node to get to _M_data.
148  reference
149  operator*() const
150  { return static_cast<_Node*>(_M_node)->_M_data; }
151 
152  pointer
153  operator->() const
154  { return std::__addressof(static_cast<_Node*>(_M_node)->_M_data); }
155 
156  _Self&
157  operator++()
158  {
159  _M_node = _M_node->_M_next;
160  return *this;
161  }
162 
163  _Self
164  operator++(int)
165  {
166  _Self __tmp = *this;
167  _M_node = _M_node->_M_next;
168  return __tmp;
169  }
170 
171  _Self&
172  operator--()
173  {
174  _M_node = _M_node->_M_prev;
175  return *this;
176  }
177 
178  _Self
179  operator--(int)
180  {
181  _Self __tmp = *this;
182  _M_node = _M_node->_M_prev;
183  return __tmp;
184  }
185 
186  bool
187  operator==(const _Self& __x) const
188  { return _M_node == __x._M_node; }
189 
190  bool
191  operator!=(const _Self& __x) const
192  { return _M_node != __x._M_node; }
193 
194  // The only member points to the %list element.
195  __detail::_List_node_base* _M_node;
196  };
197 
198  /**
199  * @brief A list::const_iterator.
200  *
201  * All the functions are op overloads.
202  */
203  template<typename _Tp>
205  {
207  typedef const _List_node<_Tp> _Node;
209 
210  typedef ptrdiff_t difference_type;
212  typedef _Tp value_type;
213  typedef const _Tp* pointer;
214  typedef const _Tp& reference;
215 
217  : _M_node() { }
218 
219  explicit
221  : _M_node(__x) { }
222 
223  _List_const_iterator(const iterator& __x)
224  : _M_node(__x._M_node) { }
225 
226  iterator
227  _M_const_cast() const
228  { return iterator(const_cast<__detail::_List_node_base*>(_M_node)); }
229 
230  // Must downcast from List_node_base to _List_node to get to
231  // _M_data.
232  reference
233  operator*() const
234  { return static_cast<_Node*>(_M_node)->_M_data; }
235 
236  pointer
237  operator->() const
238  { return std::__addressof(static_cast<_Node*>(_M_node)->_M_data); }
239 
240  _Self&
241  operator++()
242  {
243  _M_node = _M_node->_M_next;
244  return *this;
245  }
246 
247  _Self
248  operator++(int)
249  {
250  _Self __tmp = *this;
251  _M_node = _M_node->_M_next;
252  return __tmp;
253  }
254 
255  _Self&
256  operator--()
257  {
258  _M_node = _M_node->_M_prev;
259  return *this;
260  }
261 
262  _Self
263  operator--(int)
264  {
265  _Self __tmp = *this;
266  _M_node = _M_node->_M_prev;
267  return __tmp;
268  }
269 
270  bool
271  operator==(const _Self& __x) const
272  { return _M_node == __x._M_node; }
273 
274  bool
275  operator!=(const _Self& __x) const
276  { return _M_node != __x._M_node; }
277 
278  // The only member points to the %list element.
279  const __detail::_List_node_base* _M_node;
280  };
281 
282  template<typename _Val>
283  inline bool
284  operator==(const _List_iterator<_Val>& __x,
285  const _List_const_iterator<_Val>& __y)
286  { return __x._M_node == __y._M_node; }
287 
288  template<typename _Val>
289  inline bool
290  operator!=(const _List_iterator<_Val>& __x,
291  const _List_const_iterator<_Val>& __y)
292  { return __x._M_node != __y._M_node; }
293 
294 
295  /// See bits/stl_deque.h's _Deque_base for an explanation.
296  template<typename _Tp, typename _Alloc>
298  {
299  protected:
300  // NOTA BENE
301  // The stored instance is not actually of "allocator_type"'s
302  // type. Instead we rebind the type to
303  // Allocator<List_node<Tp>>, which according to [20.1.5]/4
304  // should probably be the same. List_node<Tp> is not the same
305  // size as Tp (it's two pointers larger), and specializations on
306  // Tp may go unused because List_node<Tp> is being bound
307  // instead.
308  //
309  // We put this to the test in the constructors and in
310  // get_allocator, where we use conversions between
311  // allocator_type and _Node_alloc_type. The conversion is
312  // required by table 32 in [20.1.5].
313  typedef typename _Alloc::template rebind<_List_node<_Tp> >::other
314  _Node_alloc_type;
315 
316  typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
317 
318  struct _List_impl
319  : public _Node_alloc_type
320  {
322 
323  _List_impl()
324  : _Node_alloc_type(), _M_node()
325  { }
326 
327  _List_impl(const _Node_alloc_type& __a)
328  : _Node_alloc_type(__a), _M_node()
329  { }
330 
331 #if __cplusplus >= 201103L
332  _List_impl(_Node_alloc_type&& __a)
333  : _Node_alloc_type(std::move(__a)), _M_node()
334  { }
335 #endif
336  };
337 
338  _List_impl _M_impl;
339 
341  _M_get_node()
342  { return _M_impl._Node_alloc_type::allocate(1); }
343 
344  void
345  _M_put_node(_List_node<_Tp>* __p)
346  { _M_impl._Node_alloc_type::deallocate(__p, 1); }
347 
348  public:
349  typedef _Alloc allocator_type;
350 
351  _Node_alloc_type&
352  _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
353  { return *static_cast<_Node_alloc_type*>(&_M_impl); }
354 
355  const _Node_alloc_type&
356  _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
357  { return *static_cast<const _Node_alloc_type*>(&_M_impl); }
358 
359  _Tp_alloc_type
360  _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
361  { return _Tp_alloc_type(_M_get_Node_allocator()); }
362 
363  allocator_type
364  get_allocator() const _GLIBCXX_NOEXCEPT
365  { return allocator_type(_M_get_Node_allocator()); }
366 
367  _List_base()
368  : _M_impl()
369  { _M_init(); }
370 
371  _List_base(const _Node_alloc_type& __a)
372  : _M_impl(__a)
373  { _M_init(); }
374 
375 #if __cplusplus >= 201103L
376  _List_base(_List_base&& __x)
377  : _M_impl(std::move(__x._M_get_Node_allocator()))
378  {
379  _M_init();
380  __detail::_List_node_base::swap(_M_impl._M_node, __x._M_impl._M_node);
381  }
382 #endif
383 
384  // This is what actually destroys the list.
385  ~_List_base() _GLIBCXX_NOEXCEPT
386  { _M_clear(); }
387 
388  void
389  _M_clear();
390 
391  void
392  _M_init()
393  {
394  this->_M_impl._M_node._M_next = &this->_M_impl._M_node;
395  this->_M_impl._M_node._M_prev = &this->_M_impl._M_node;
396  }
397  };
398 
399  /**
400  * @brief A standard container with linear time access to elements,
401  * and fixed time insertion/deletion at any point in the sequence.
402  *
403  * @ingroup sequences
404  *
405  * @tparam _Tp Type of element.
406  * @tparam _Alloc Allocator type, defaults to allocator<_Tp>.
407  *
408  * Meets the requirements of a <a href="tables.html#65">container</a>, a
409  * <a href="tables.html#66">reversible container</a>, and a
410  * <a href="tables.html#67">sequence</a>, including the
411  * <a href="tables.html#68">optional sequence requirements</a> with the
412  * %exception of @c at and @c operator[].
413  *
414  * This is a @e doubly @e linked %list. Traversal up and down the
415  * %list requires linear time, but adding and removing elements (or
416  * @e nodes) is done in constant time, regardless of where the
417  * change takes place. Unlike std::vector and std::deque,
418  * random-access iterators are not provided, so subscripting ( @c
419  * [] ) access is not allowed. For algorithms which only need
420  * sequential access, this lack makes no difference.
421  *
422  * Also unlike the other standard containers, std::list provides
423  * specialized algorithms %unique to linked lists, such as
424  * splicing, sorting, and in-place reversal.
425  *
426  * A couple points on memory allocation for list<Tp>:
427  *
428  * First, we never actually allocate a Tp, we allocate
429  * List_node<Tp>'s and trust [20.1.5]/4 to DTRT. This is to ensure
430  * that after elements from %list<X,Alloc1> are spliced into
431  * %list<X,Alloc2>, destroying the memory of the second %list is a
432  * valid operation, i.e., Alloc1 giveth and Alloc2 taketh away.
433  *
434  * Second, a %list conceptually represented as
435  * @code
436  * A <---> B <---> C <---> D
437  * @endcode
438  * is actually circular; a link exists between A and D. The %list
439  * class holds (as its only data member) a private list::iterator
440  * pointing to @e D, not to @e A! To get to the head of the %list,
441  * we start at the tail and move forward by one. When this member
442  * iterator's next/previous pointers refer to itself, the %list is
443  * %empty.
444  */
445  template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
446  class list : protected _List_base<_Tp, _Alloc>
447  {
448  // concept requirements
449  typedef typename _Alloc::value_type _Alloc_value_type;
450  __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
451  __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
452 
454  typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
455  typedef typename _Base::_Node_alloc_type _Node_alloc_type;
456 
457  public:
458  typedef _Tp value_type;
459  typedef typename _Tp_alloc_type::pointer pointer;
460  typedef typename _Tp_alloc_type::const_pointer const_pointer;
461  typedef typename _Tp_alloc_type::reference reference;
462  typedef typename _Tp_alloc_type::const_reference const_reference;
467  typedef size_t size_type;
468  typedef ptrdiff_t difference_type;
469  typedef _Alloc allocator_type;
470 
471  protected:
472  // Note that pointers-to-_Node's can be ctor-converted to
473  // iterator types.
474  typedef _List_node<_Tp> _Node;
475 
476  using _Base::_M_impl;
477  using _Base::_M_put_node;
478  using _Base::_M_get_node;
479  using _Base::_M_get_Tp_allocator;
480  using _Base::_M_get_Node_allocator;
481 
482  /**
483  * @param __args An instance of user data.
484  *
485  * Allocates space for a new node and constructs a copy of
486  * @a __args in it.
487  */
488 #if __cplusplus < 201103L
489  _Node*
490  _M_create_node(const value_type& __x)
491  {
492  _Node* __p = this->_M_get_node();
493  __try
494  {
495  _M_get_Tp_allocator().construct
496  (std::__addressof(__p->_M_data), __x);
497  }
498  __catch(...)
499  {
500  _M_put_node(__p);
501  __throw_exception_again;
502  }
503  return __p;
504  }
505 #else
506  template<typename... _Args>
507  _Node*
508  _M_create_node(_Args&&... __args)
509  {
510  _Node* __p = this->_M_get_node();
511  __try
512  {
513  _M_get_Node_allocator().construct(__p,
514  std::forward<_Args>(__args)...);
515  }
516  __catch(...)
517  {
518  _M_put_node(__p);
519  __throw_exception_again;
520  }
521  return __p;
522  }
523 #endif
524 
525  public:
526  // [23.2.2.1] construct/copy/destroy
527  // (assign() and get_allocator() are also listed in this section)
528  /**
529  * @brief Default constructor creates no elements.
530  */
532  : _Base() { }
533 
534  /**
535  * @brief Creates a %list with no elements.
536  * @param __a An allocator object.
537  */
538  explicit
539  list(const allocator_type& __a)
540  : _Base(_Node_alloc_type(__a)) { }
541 
542 #if __cplusplus >= 201103L
543  /**
544  * @brief Creates a %list with default constructed elements.
545  * @param __n The number of elements to initially create.
546  *
547  * This constructor fills the %list with @a __n default
548  * constructed elements.
549  */
550  explicit
551  list(size_type __n)
552  : _Base()
553  { _M_default_initialize(__n); }
554 
555  /**
556  * @brief Creates a %list with copies of an exemplar element.
557  * @param __n The number of elements to initially create.
558  * @param __value An element to copy.
559  * @param __a An allocator object.
560  *
561  * This constructor fills the %list with @a __n copies of @a __value.
562  */
563  list(size_type __n, const value_type& __value,
564  const allocator_type& __a = allocator_type())
565  : _Base(_Node_alloc_type(__a))
566  { _M_fill_initialize(__n, __value); }
567 #else
568  /**
569  * @brief Creates a %list with copies of an exemplar element.
570  * @param __n The number of elements to initially create.
571  * @param __value An element to copy.
572  * @param __a An allocator object.
573  *
574  * This constructor fills the %list with @a __n copies of @a __value.
575  */
576  explicit
577  list(size_type __n, const value_type& __value = value_type(),
578  const allocator_type& __a = allocator_type())
579  : _Base(_Node_alloc_type(__a))
580  { _M_fill_initialize(__n, __value); }
581 #endif
582 
583  /**
584  * @brief %List copy constructor.
585  * @param __x A %list of identical element and allocator types.
586  *
587  * The newly-created %list uses a copy of the allocation object used
588  * by @a __x.
589  */
590  list(const list& __x)
591  : _Base(__x._M_get_Node_allocator())
592  { _M_initialize_dispatch(__x.begin(), __x.end(), __false_type()); }
593 
594 #if __cplusplus >= 201103L
595  /**
596  * @brief %List move constructor.
597  * @param __x A %list of identical element and allocator types.
598  *
599  * The newly-created %list contains the exact contents of @a __x.
600  * The contents of @a __x are a valid, but unspecified %list.
601  */
602  list(list&& __x) noexcept
603  : _Base(std::move(__x)) { }
604 
605  /**
606  * @brief Builds a %list from an initializer_list
607  * @param __l An initializer_list of value_type.
608  * @param __a An allocator object.
609  *
610  * Create a %list consisting of copies of the elements in the
611  * initializer_list @a __l. This is linear in __l.size().
612  */
614  const allocator_type& __a = allocator_type())
615  : _Base(_Node_alloc_type(__a))
616  { _M_initialize_dispatch(__l.begin(), __l.end(), __false_type()); }
617 #endif
618 
619  /**
620  * @brief Builds a %list from a range.
621  * @param __first An input iterator.
622  * @param __last An input iterator.
623  * @param __a An allocator object.
624  *
625  * Create a %list consisting of copies of the elements from
626  * [@a __first,@a __last). This is linear in N (where N is
627  * distance(@a __first,@a __last)).
628  */
629 #if __cplusplus >= 201103L
630  template<typename _InputIterator,
631  typename = std::_RequireInputIter<_InputIterator>>
632  list(_InputIterator __first, _InputIterator __last,
633  const allocator_type& __a = allocator_type())
634  : _Base(_Node_alloc_type(__a))
635  { _M_initialize_dispatch(__first, __last, __false_type()); }
636 #else
637  template<typename _InputIterator>
638  list(_InputIterator __first, _InputIterator __last,
639  const allocator_type& __a = allocator_type())
640  : _Base(_Node_alloc_type(__a))
641  {
642  // Check whether it's an integral type. If so, it's not an iterator.
643  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
644  _M_initialize_dispatch(__first, __last, _Integral());
645  }
646 #endif
647 
648  /**
649  * No explicit dtor needed as the _Base dtor takes care of
650  * things. The _Base dtor only erases the elements, and note
651  * that if the elements themselves are pointers, the pointed-to
652  * memory is not touched in any way. Managing the pointer is
653  * the user's responsibility.
654  */
655 
656  /**
657  * @brief %List assignment operator.
658  * @param __x A %list of identical element and allocator types.
659  *
660  * All the elements of @a __x are copied, but unlike the copy
661  * constructor, the allocator object is not copied.
662  */
663  list&
664  operator=(const list& __x);
665 
666 #if __cplusplus >= 201103L
667  /**
668  * @brief %List move assignment operator.
669  * @param __x A %list of identical element and allocator types.
670  *
671  * The contents of @a __x are moved into this %list (without copying).
672  * @a __x is a valid, but unspecified %list
673  */
674  list&
676  {
677  // NB: DR 1204.
678  // NB: DR 675.
679  this->clear();
680  this->swap(__x);
681  return *this;
682  }
683 
684  /**
685  * @brief %List initializer list assignment operator.
686  * @param __l An initializer_list of value_type.
687  *
688  * Replace the contents of the %list with copies of the elements
689  * in the initializer_list @a __l. This is linear in l.size().
690  */
691  list&
693  {
694  this->assign(__l.begin(), __l.end());
695  return *this;
696  }
697 #endif
698 
699  /**
700  * @brief Assigns a given value to a %list.
701  * @param __n Number of elements to be assigned.
702  * @param __val Value to be assigned.
703  *
704  * This function fills a %list with @a __n copies of the given
705  * value. Note that the assignment completely changes the %list
706  * and that the resulting %list's size is the same as the number
707  * of elements assigned. Old data may be lost.
708  */
709  void
710  assign(size_type __n, const value_type& __val)
711  { _M_fill_assign(__n, __val); }
712 
713  /**
714  * @brief Assigns a range to a %list.
715  * @param __first An input iterator.
716  * @param __last An input iterator.
717  *
718  * This function fills a %list with copies of the elements in the
719  * range [@a __first,@a __last).
720  *
721  * Note that the assignment completely changes the %list and
722  * that the resulting %list's size is the same as the number of
723  * elements assigned. Old data may be lost.
724  */
725 #if __cplusplus >= 201103L
726  template<typename _InputIterator,
727  typename = std::_RequireInputIter<_InputIterator>>
728  void
729  assign(_InputIterator __first, _InputIterator __last)
730  { _M_assign_dispatch(__first, __last, __false_type()); }
731 #else
732  template<typename _InputIterator>
733  void
734  assign(_InputIterator __first, _InputIterator __last)
735  {
736  // Check whether it's an integral type. If so, it's not an iterator.
737  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
738  _M_assign_dispatch(__first, __last, _Integral());
739  }
740 #endif
741 
742 #if __cplusplus >= 201103L
743  /**
744  * @brief Assigns an initializer_list to a %list.
745  * @param __l An initializer_list of value_type.
746  *
747  * Replace the contents of the %list with copies of the elements
748  * in the initializer_list @a __l. This is linear in __l.size().
749  */
750  void
752  { this->assign(__l.begin(), __l.end()); }
753 #endif
754 
755  /// Get a copy of the memory allocation object.
756  allocator_type
757  get_allocator() const _GLIBCXX_NOEXCEPT
758  { return _Base::get_allocator(); }
759 
760  // iterators
761  /**
762  * Returns a read/write iterator that points to the first element in the
763  * %list. Iteration is done in ordinary element order.
764  */
765  iterator
766  begin() _GLIBCXX_NOEXCEPT
767  { return iterator(this->_M_impl._M_node._M_next); }
768 
769  /**
770  * Returns a read-only (constant) iterator that points to the
771  * first element in the %list. Iteration is done in ordinary
772  * element order.
773  */
774  const_iterator
775  begin() const _GLIBCXX_NOEXCEPT
776  { return const_iterator(this->_M_impl._M_node._M_next); }
777 
778  /**
779  * Returns a read/write iterator that points one past the last
780  * element in the %list. Iteration is done in ordinary element
781  * order.
782  */
783  iterator
784  end() _GLIBCXX_NOEXCEPT
785  { return iterator(&this->_M_impl._M_node); }
786 
787  /**
788  * Returns a read-only (constant) iterator that points one past
789  * the last element in the %list. Iteration is done in ordinary
790  * element order.
791  */
792  const_iterator
793  end() const _GLIBCXX_NOEXCEPT
794  { return const_iterator(&this->_M_impl._M_node); }
795 
796  /**
797  * Returns a read/write reverse iterator that points to the last
798  * element in the %list. Iteration is done in reverse element
799  * order.
800  */
802  rbegin() _GLIBCXX_NOEXCEPT
803  { return reverse_iterator(end()); }
804 
805  /**
806  * Returns a read-only (constant) reverse iterator that points to
807  * the last element in the %list. Iteration is done in reverse
808  * element order.
809  */
810  const_reverse_iterator
811  rbegin() const _GLIBCXX_NOEXCEPT
812  { return const_reverse_iterator(end()); }
813 
814  /**
815  * Returns a read/write reverse iterator that points to one
816  * before the first element in the %list. Iteration is done in
817  * reverse element order.
818  */
820  rend() _GLIBCXX_NOEXCEPT
821  { return reverse_iterator(begin()); }
822 
823  /**
824  * Returns a read-only (constant) reverse iterator that points to one
825  * before the first element in the %list. Iteration is done in reverse
826  * element order.
827  */
828  const_reverse_iterator
829  rend() const _GLIBCXX_NOEXCEPT
830  { return const_reverse_iterator(begin()); }
831 
832 #if __cplusplus >= 201103L
833  /**
834  * Returns a read-only (constant) iterator that points to the
835  * first element in the %list. Iteration is done in ordinary
836  * element order.
837  */
838  const_iterator
839  cbegin() const noexcept
840  { return const_iterator(this->_M_impl._M_node._M_next); }
841 
842  /**
843  * Returns a read-only (constant) iterator that points one past
844  * the last element in the %list. Iteration is done in ordinary
845  * element order.
846  */
847  const_iterator
848  cend() const noexcept
849  { return const_iterator(&this->_M_impl._M_node); }
850 
851  /**
852  * Returns a read-only (constant) reverse iterator that points to
853  * the last element in the %list. Iteration is done in reverse
854  * element order.
855  */
856  const_reverse_iterator
857  crbegin() const noexcept
858  { return const_reverse_iterator(end()); }
859 
860  /**
861  * Returns a read-only (constant) reverse iterator that points to one
862  * before the first element in the %list. Iteration is done in reverse
863  * element order.
864  */
865  const_reverse_iterator
866  crend() const noexcept
867  { return const_reverse_iterator(begin()); }
868 #endif
869 
870  // [23.2.2.2] capacity
871  /**
872  * Returns true if the %list is empty. (Thus begin() would equal
873  * end().)
874  */
875  bool
876  empty() const _GLIBCXX_NOEXCEPT
877  { return this->_M_impl._M_node._M_next == &this->_M_impl._M_node; }
878 
879  /** Returns the number of elements in the %list. */
880  size_type
881  size() const _GLIBCXX_NOEXCEPT
882  { return std::distance(begin(), end()); }
883 
884  /** Returns the size() of the largest possible %list. */
885  size_type
886  max_size() const _GLIBCXX_NOEXCEPT
887  { return _M_get_Node_allocator().max_size(); }
888 
889 #if __cplusplus >= 201103L
890  /**
891  * @brief Resizes the %list to the specified number of elements.
892  * @param __new_size Number of elements the %list should contain.
893  *
894  * This function will %resize the %list to the specified number
895  * of elements. If the number is smaller than the %list's
896  * current size the %list is truncated, otherwise default
897  * constructed elements are appended.
898  */
899  void
900  resize(size_type __new_size);
901 
902  /**
903  * @brief Resizes the %list to the specified number of elements.
904  * @param __new_size Number of elements the %list should contain.
905  * @param __x Data with which new elements should be populated.
906  *
907  * This function will %resize the %list to the specified number
908  * of elements. If the number is smaller than the %list's
909  * current size the %list is truncated, otherwise the %list is
910  * extended and new elements are populated with given data.
911  */
912  void
913  resize(size_type __new_size, const value_type& __x);
914 #else
915  /**
916  * @brief Resizes the %list to the specified number of elements.
917  * @param __new_size Number of elements the %list should contain.
918  * @param __x Data with which new elements should be populated.
919  *
920  * This function will %resize the %list to the specified number
921  * of elements. If the number is smaller than the %list's
922  * current size the %list is truncated, otherwise the %list is
923  * extended and new elements are populated with given data.
924  */
925  void
926  resize(size_type __new_size, value_type __x = value_type());
927 #endif
928 
929  // element access
930  /**
931  * Returns a read/write reference to the data at the first
932  * element of the %list.
933  */
934  reference
936  { return *begin(); }
937 
938  /**
939  * Returns a read-only (constant) reference to the data at the first
940  * element of the %list.
941  */
942  const_reference
943  front() const
944  { return *begin(); }
945 
946  /**
947  * Returns a read/write reference to the data at the last element
948  * of the %list.
949  */
950  reference
952  {
953  iterator __tmp = end();
954  --__tmp;
955  return *__tmp;
956  }
957 
958  /**
959  * Returns a read-only (constant) reference to the data at the last
960  * element of the %list.
961  */
962  const_reference
963  back() const
964  {
965  const_iterator __tmp = end();
966  --__tmp;
967  return *__tmp;
968  }
969 
970  // [23.2.2.3] modifiers
971  /**
972  * @brief Add data to the front of the %list.
973  * @param __x Data to be added.
974  *
975  * This is a typical stack operation. The function creates an
976  * element at the front of the %list and assigns the given data
977  * to it. Due to the nature of a %list this operation can be
978  * done in constant time, and does not invalidate iterators and
979  * references.
980  */
981  void
982  push_front(const value_type& __x)
983  { this->_M_insert(begin(), __x); }
984 
985 #if __cplusplus >= 201103L
986  void
987  push_front(value_type&& __x)
988  { this->_M_insert(begin(), std::move(__x)); }
989 
990  template<typename... _Args>
991  void
992  emplace_front(_Args&&... __args)
993  { this->_M_insert(begin(), std::forward<_Args>(__args)...); }
994 #endif
995 
996  /**
997  * @brief Removes first element.
998  *
999  * This is a typical stack operation. It shrinks the %list by
1000  * one. Due to the nature of a %list this operation can be done
1001  * in constant time, and only invalidates iterators/references to
1002  * the element being removed.
1003  *
1004  * Note that no data is returned, and if the first element's data
1005  * is needed, it should be retrieved before pop_front() is
1006  * called.
1007  */
1008  void
1010  { this->_M_erase(begin()); }
1011 
1012  /**
1013  * @brief Add data to the end of the %list.
1014  * @param __x Data to be added.
1015  *
1016  * This is a typical stack operation. The function creates an
1017  * element at the end of the %list and assigns the given data to
1018  * it. Due to the nature of a %list this operation can be done
1019  * in constant time, and does not invalidate iterators and
1020  * references.
1021  */
1022  void
1023  push_back(const value_type& __x)
1024  { this->_M_insert(end(), __x); }
1025 
1026 #if __cplusplus >= 201103L
1027  void
1028  push_back(value_type&& __x)
1029  { this->_M_insert(end(), std::move(__x)); }
1030 
1031  template<typename... _Args>
1032  void
1033  emplace_back(_Args&&... __args)
1034  { this->_M_insert(end(), std::forward<_Args>(__args)...); }
1035 #endif
1036 
1037  /**
1038  * @brief Removes last element.
1039  *
1040  * This is a typical stack operation. It shrinks the %list by
1041  * one. Due to the nature of a %list this operation can be done
1042  * in constant time, and only invalidates iterators/references to
1043  * the element being removed.
1044  *
1045  * Note that no data is returned, and if the last element's data
1046  * is needed, it should be retrieved before pop_back() is called.
1047  */
1048  void
1050  { this->_M_erase(iterator(this->_M_impl._M_node._M_prev)); }
1051 
1052 #if __cplusplus >= 201103L
1053  /**
1054  * @brief Constructs object in %list before specified iterator.
1055  * @param __position A const_iterator into the %list.
1056  * @param __args Arguments.
1057  * @return An iterator that points to the inserted data.
1058  *
1059  * This function will insert an object of type T constructed
1060  * with T(std::forward<Args>(args)...) before the specified
1061  * location. Due to the nature of a %list this operation can
1062  * be done in constant time, and does not invalidate iterators
1063  * and references.
1064  */
1065  template<typename... _Args>
1066  iterator
1067  emplace(const_iterator __position, _Args&&... __args);
1068 
1069  /**
1070  * @brief Inserts given value into %list before specified iterator.
1071  * @param __position A const_iterator into the %list.
1072  * @param __x Data to be inserted.
1073  * @return An iterator that points to the inserted data.
1074  *
1075  * This function will insert a copy of the given value before
1076  * the specified location. Due to the nature of a %list this
1077  * operation can be done in constant time, and does not
1078  * invalidate iterators and references.
1079  */
1080  iterator
1081  insert(const_iterator __position, const value_type& __x);
1082 #else
1083  /**
1084  * @brief Inserts given value into %list before specified iterator.
1085  * @param __position An iterator into the %list.
1086  * @param __x Data to be inserted.
1087  * @return An iterator that points to the inserted data.
1088  *
1089  * This function will insert a copy of the given value before
1090  * the specified location. Due to the nature of a %list this
1091  * operation can be done in constant time, and does not
1092  * invalidate iterators and references.
1093  */
1094  iterator
1095  insert(iterator __position, const value_type& __x);
1096 #endif
1097 
1098 #if __cplusplus >= 201103L
1099  /**
1100  * @brief Inserts given rvalue into %list before specified iterator.
1101  * @param __position A const_iterator into the %list.
1102  * @param __x Data to be inserted.
1103  * @return An iterator that points to the inserted data.
1104  *
1105  * This function will insert a copy of the given rvalue before
1106  * the specified location. Due to the nature of a %list this
1107  * operation can be done in constant time, and does not
1108  * invalidate iterators and references.
1109  */
1110  iterator
1111  insert(const_iterator __position, value_type&& __x)
1112  { return emplace(__position, std::move(__x)); }
1113 
1114  /**
1115  * @brief Inserts the contents of an initializer_list into %list
1116  * before specified const_iterator.
1117  * @param __p A const_iterator into the %list.
1118  * @param __l An initializer_list of value_type.
1119  * @return An iterator pointing to the first element inserted
1120  * (or __position).
1121  *
1122  * This function will insert copies of the data in the
1123  * initializer_list @a l into the %list before the location
1124  * specified by @a p.
1125  *
1126  * This operation is linear in the number of elements inserted and
1127  * does not invalidate iterators and references.
1128  */
1129  iterator
1131  { return this->insert(__p, __l.begin(), __l.end()); }
1132 #endif
1133 
1134 #if __cplusplus >= 201103L
1135  /**
1136  * @brief Inserts a number of copies of given data into the %list.
1137  * @param __position A const_iterator into the %list.
1138  * @param __n Number of elements to be inserted.
1139  * @param __x Data to be inserted.
1140  * @return An iterator pointing to the first element inserted
1141  * (or __position).
1142  *
1143  * This function will insert a specified number of copies of the
1144  * given data before the location specified by @a position.
1145  *
1146  * This operation is linear in the number of elements inserted and
1147  * does not invalidate iterators and references.
1148  */
1149  iterator
1150  insert(const_iterator __position, size_type __n, const value_type& __x);
1151 #else
1152  /**
1153  * @brief Inserts a number of copies of given data into the %list.
1154  * @param __position An iterator into the %list.
1155  * @param __n Number of elements to be inserted.
1156  * @param __x Data to be inserted.
1157  *
1158  * This function will insert a specified number of copies of the
1159  * given data before the location specified by @a position.
1160  *
1161  * This operation is linear in the number of elements inserted and
1162  * does not invalidate iterators and references.
1163  */
1164  void
1165  insert(iterator __position, size_type __n, const value_type& __x)
1166  {
1167  list __tmp(__n, __x, get_allocator());
1168  splice(__position, __tmp);
1169  }
1170 #endif
1171 
1172 #if __cplusplus >= 201103L
1173  /**
1174  * @brief Inserts a range into the %list.
1175  * @param __position A const_iterator into the %list.
1176  * @param __first An input iterator.
1177  * @param __last An input iterator.
1178  * @return An iterator pointing to the first element inserted
1179  * (or __position).
1180  *
1181  * This function will insert copies of the data in the range [@a
1182  * first,@a last) into the %list before the location specified by
1183  * @a position.
1184  *
1185  * This operation is linear in the number of elements inserted and
1186  * does not invalidate iterators and references.
1187  */
1188  template<typename _InputIterator,
1189  typename = std::_RequireInputIter<_InputIterator>>
1190  iterator
1191  insert(const_iterator __position, _InputIterator __first,
1192  _InputIterator __last);
1193 #else
1194  /**
1195  * @brief Inserts a range into the %list.
1196  * @param __position An iterator into the %list.
1197  * @param __first An input iterator.
1198  * @param __last An input iterator.
1199  *
1200  * This function will insert copies of the data in the range [@a
1201  * first,@a last) into the %list before the location specified by
1202  * @a position.
1203  *
1204  * This operation is linear in the number of elements inserted and
1205  * does not invalidate iterators and references.
1206  */
1207  template<typename _InputIterator>
1208  void
1209  insert(iterator __position, _InputIterator __first,
1210  _InputIterator __last)
1211  {
1212  list __tmp(__first, __last, get_allocator());
1213  splice(__position, __tmp);
1214  }
1215 #endif
1216 
1217  /**
1218  * @brief Remove element at given position.
1219  * @param __position Iterator pointing to element to be erased.
1220  * @return An iterator pointing to the next element (or end()).
1221  *
1222  * This function will erase the element at the given position and thus
1223  * shorten the %list by one.
1224  *
1225  * Due to the nature of a %list this operation can be done in
1226  * constant time, and only invalidates iterators/references to
1227  * the element being removed. The user is also cautioned that
1228  * this function only erases the element, and that if the element
1229  * is itself a pointer, the pointed-to memory is not touched in
1230  * any way. Managing the pointer is the user's responsibility.
1231  */
1232  iterator
1233 #if __cplusplus >= 201103L
1234  erase(const_iterator __position);
1235 #else
1236  erase(iterator __position);
1237 #endif
1238 
1239  /**
1240  * @brief Remove a range of elements.
1241  * @param __first Iterator pointing to the first element to be erased.
1242  * @param __last Iterator pointing to one past the last element to be
1243  * erased.
1244  * @return An iterator pointing to the element pointed to by @a last
1245  * prior to erasing (or end()).
1246  *
1247  * This function will erase the elements in the range @a
1248  * [first,last) and shorten the %list accordingly.
1249  *
1250  * This operation is linear time in the size of the range and only
1251  * invalidates iterators/references to the element being removed.
1252  * The user is also cautioned that this function only erases the
1253  * elements, and that if the elements themselves are pointers, the
1254  * pointed-to memory is not touched in any way. Managing the pointer
1255  * is the user's responsibility.
1256  */
1257  iterator
1258 #if __cplusplus >= 201103L
1260 #else
1261  erase(iterator __first, iterator __last)
1262 #endif
1263  {
1264  while (__first != __last)
1265  __first = erase(__first);
1266  return __last._M_const_cast();
1267  }
1268 
1269  /**
1270  * @brief Swaps data with another %list.
1271  * @param __x A %list of the same element and allocator types.
1272  *
1273  * This exchanges the elements between two lists in constant
1274  * time. Note that the global std::swap() function is
1275  * specialized such that std::swap(l1,l2) will feed to this
1276  * function.
1277  */
1278  void
1279  swap(list& __x)
1280  {
1281  __detail::_List_node_base::swap(this->_M_impl._M_node,
1282  __x._M_impl._M_node);
1283 
1284  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1285  // 431. Swapping containers with unequal allocators.
1286  std::__alloc_swap<typename _Base::_Node_alloc_type>::
1287  _S_do_it(_M_get_Node_allocator(), __x._M_get_Node_allocator());
1288  }
1289 
1290  /**
1291  * Erases all the elements. Note that this function only erases
1292  * the elements, and that if the elements themselves are
1293  * pointers, the pointed-to memory is not touched in any way.
1294  * Managing the pointer is the user's responsibility.
1295  */
1296  void
1297  clear() _GLIBCXX_NOEXCEPT
1298  {
1299  _Base::_M_clear();
1300  _Base::_M_init();
1301  }
1302 
1303  // [23.2.2.4] list operations
1304  /**
1305  * @brief Insert contents of another %list.
1306  * @param __position Iterator referencing the element to insert before.
1307  * @param __x Source list.
1308  *
1309  * The elements of @a __x are inserted in constant time in front of
1310  * the element referenced by @a __position. @a __x becomes an empty
1311  * list.
1312  *
1313  * Requires this != @a __x.
1314  */
1315  void
1316 #if __cplusplus >= 201103L
1317  splice(const_iterator __position, list&& __x)
1318 #else
1319  splice(iterator __position, list& __x)
1320 #endif
1321  {
1322  if (!__x.empty())
1323  {
1324  _M_check_equal_allocators(__x);
1325 
1326  this->_M_transfer(__position._M_const_cast(),
1327  __x.begin(), __x.end());
1328  }
1329  }
1330 
1331 #if __cplusplus >= 201103L
1332  void
1333  splice(const_iterator __position, list& __x)
1334  { splice(__position, std::move(__x)); }
1335 #endif
1336 
1337 #if __cplusplus >= 201103L
1338  /**
1339  * @brief Insert element from another %list.
1340  * @param __position Const_iterator referencing the element to
1341  * insert before.
1342  * @param __x Source list.
1343  * @param __i Const_iterator referencing the element to move.
1344  *
1345  * Removes the element in list @a __x referenced by @a __i and
1346  * inserts it into the current list before @a __position.
1347  */
1348  void
1349  splice(const_iterator __position, list&& __x, const_iterator __i)
1350 #else
1351  /**
1352  * @brief Insert element from another %list.
1353  * @param __position Iterator referencing the element to insert before.
1354  * @param __x Source list.
1355  * @param __i Iterator referencing the element to move.
1356  *
1357  * Removes the element in list @a __x referenced by @a __i and
1358  * inserts it into the current list before @a __position.
1359  */
1360  void
1361  splice(iterator __position, list& __x, iterator __i)
1362 #endif
1363  {
1364  iterator __j = __i._M_const_cast();
1365  ++__j;
1366  if (__position == __i || __position == __j)
1367  return;
1368 
1369  if (this != &__x)
1370  _M_check_equal_allocators(__x);
1371 
1372  this->_M_transfer(__position._M_const_cast(),
1373  __i._M_const_cast(), __j);
1374  }
1375 
1376 #if __cplusplus >= 201103L
1377  /**
1378  * @brief Insert element from another %list.
1379  * @param __position Const_iterator referencing the element to
1380  * insert before.
1381  * @param __x Source list.
1382  * @param __i Const_iterator referencing the element to move.
1383  *
1384  * Removes the element in list @a __x referenced by @a __i and
1385  * inserts it into the current list before @a __position.
1386  */
1387  void
1388  splice(const_iterator __position, list& __x, const_iterator __i)
1389  { splice(__position, std::move(__x), __i); }
1390 #endif
1391 
1392 #if __cplusplus >= 201103L
1393  /**
1394  * @brief Insert range from another %list.
1395  * @param __position Const_iterator referencing the element to
1396  * insert before.
1397  * @param __x Source list.
1398  * @param __first Const_iterator referencing the start of range in x.
1399  * @param __last Const_iterator referencing the end of range in x.
1400  *
1401  * Removes elements in the range [__first,__last) and inserts them
1402  * before @a __position in constant time.
1403  *
1404  * Undefined if @a __position is in [__first,__last).
1405  */
1406  void
1407  splice(const_iterator __position, list&& __x, const_iterator __first,
1408  const_iterator __last)
1409 #else
1410  /**
1411  * @brief Insert range from another %list.
1412  * @param __position Iterator referencing the element to insert before.
1413  * @param __x Source list.
1414  * @param __first Iterator referencing the start of range in x.
1415  * @param __last Iterator referencing the end of range in x.
1416  *
1417  * Removes elements in the range [__first,__last) and inserts them
1418  * before @a __position in constant time.
1419  *
1420  * Undefined if @a __position is in [__first,__last).
1421  */
1422  void
1423  splice(iterator __position, list& __x, iterator __first,
1424  iterator __last)
1425 #endif
1426  {
1427  if (__first != __last)
1428  {
1429  if (this != &__x)
1430  _M_check_equal_allocators(__x);
1431 
1432  this->_M_transfer(__position._M_const_cast(),
1433  __first._M_const_cast(),
1434  __last._M_const_cast());
1435  }
1436  }
1437 
1438 #if __cplusplus >= 201103L
1439  /**
1440  * @brief Insert range from another %list.
1441  * @param __position Const_iterator referencing the element to
1442  * insert before.
1443  * @param __x Source list.
1444  * @param __first Const_iterator referencing the start of range in x.
1445  * @param __last Const_iterator referencing the end of range in x.
1446  *
1447  * Removes elements in the range [__first,__last) and inserts them
1448  * before @a __position in constant time.
1449  *
1450  * Undefined if @a __position is in [__first,__last).
1451  */
1452  void
1453  splice(const_iterator __position, list& __x, const_iterator __first,
1454  const_iterator __last)
1455  { splice(__position, std::move(__x), __first, __last); }
1456 #endif
1457 
1458  /**
1459  * @brief Remove all elements equal to value.
1460  * @param __value The value to remove.
1461  *
1462  * Removes every element in the list equal to @a value.
1463  * Remaining elements stay in list order. Note that this
1464  * function only erases the elements, and that if the elements
1465  * themselves are pointers, the pointed-to memory is not
1466  * touched in any way. Managing the pointer is the user's
1467  * responsibility.
1468  */
1469  void
1470  remove(const _Tp& __value);
1471 
1472  /**
1473  * @brief Remove all elements satisfying a predicate.
1474  * @tparam _Predicate Unary predicate function or object.
1475  *
1476  * Removes every element in the list for which the predicate
1477  * returns true. Remaining elements stay in list order. Note
1478  * that this function only erases the elements, and that if the
1479  * elements themselves are pointers, the pointed-to memory is
1480  * not touched in any way. Managing the pointer is the user's
1481  * responsibility.
1482  */
1483  template<typename _Predicate>
1484  void
1485  remove_if(_Predicate);
1486 
1487  /**
1488  * @brief Remove consecutive duplicate elements.
1489  *
1490  * For each consecutive set of elements with the same value,
1491  * remove all but the first one. Remaining elements stay in
1492  * list order. Note that this function only erases the
1493  * elements, and that if the elements themselves are pointers,
1494  * the pointed-to memory is not touched in any way. Managing
1495  * the pointer is the user's responsibility.
1496  */
1497  void
1498  unique();
1499 
1500  /**
1501  * @brief Remove consecutive elements satisfying a predicate.
1502  * @tparam _BinaryPredicate Binary predicate function or object.
1503  *
1504  * For each consecutive set of elements [first,last) that
1505  * satisfy predicate(first,i) where i is an iterator in
1506  * [first,last), remove all but the first one. Remaining
1507  * elements stay in list order. Note that this function only
1508  * erases the elements, and that if the elements themselves are
1509  * pointers, the pointed-to memory is not touched in any way.
1510  * Managing the pointer is the user's responsibility.
1511  */
1512  template<typename _BinaryPredicate>
1513  void
1514  unique(_BinaryPredicate);
1515 
1516  /**
1517  * @brief Merge sorted lists.
1518  * @param __x Sorted list to merge.
1519  *
1520  * Assumes that both @a __x and this list are sorted according to
1521  * operator<(). Merges elements of @a __x into this list in
1522  * sorted order, leaving @a __x empty when complete. Elements in
1523  * this list precede elements in @a __x that are equal.
1524  */
1525 #if __cplusplus >= 201103L
1526  void
1527  merge(list&& __x);
1528 
1529  void
1530  merge(list& __x)
1531  { merge(std::move(__x)); }
1532 #else
1533  void
1534  merge(list& __x);
1535 #endif
1536 
1537  /**
1538  * @brief Merge sorted lists according to comparison function.
1539  * @tparam _StrictWeakOrdering Comparison function defining
1540  * sort order.
1541  * @param __x Sorted list to merge.
1542  * @param __comp Comparison functor.
1543  *
1544  * Assumes that both @a __x and this list are sorted according to
1545  * StrictWeakOrdering. Merges elements of @a __x into this list
1546  * in sorted order, leaving @a __x empty when complete. Elements
1547  * in this list precede elements in @a __x that are equivalent
1548  * according to StrictWeakOrdering().
1549  */
1550 #if __cplusplus >= 201103L
1551  template<typename _StrictWeakOrdering>
1552  void
1553  merge(list&& __x, _StrictWeakOrdering __comp);
1554 
1555  template<typename _StrictWeakOrdering>
1556  void
1557  merge(list& __x, _StrictWeakOrdering __comp)
1558  { merge(std::move(__x), __comp); }
1559 #else
1560  template<typename _StrictWeakOrdering>
1561  void
1562  merge(list& __x, _StrictWeakOrdering __comp);
1563 #endif
1564 
1565  /**
1566  * @brief Reverse the elements in list.
1567  *
1568  * Reverse the order of elements in the list in linear time.
1569  */
1570  void
1571  reverse() _GLIBCXX_NOEXCEPT
1572  { this->_M_impl._M_node._M_reverse(); }
1573 
1574  /**
1575  * @brief Sort the elements.
1576  *
1577  * Sorts the elements of this list in NlogN time. Equivalent
1578  * elements remain in list order.
1579  */
1580  void
1581  sort();
1582 
1583  /**
1584  * @brief Sort the elements according to comparison function.
1585  *
1586  * Sorts the elements of this list in NlogN time. Equivalent
1587  * elements remain in list order.
1588  */
1589  template<typename _StrictWeakOrdering>
1590  void
1591  sort(_StrictWeakOrdering);
1592 
1593  protected:
1594  // Internal constructor functions follow.
1595 
1596  // Called by the range constructor to implement [23.1.1]/9
1597 
1598  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1599  // 438. Ambiguity in the "do the right thing" clause
1600  template<typename _Integer>
1601  void
1602  _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1603  { _M_fill_initialize(static_cast<size_type>(__n), __x); }
1604 
1605  // Called by the range constructor to implement [23.1.1]/9
1606  template<typename _InputIterator>
1607  void
1608  _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1609  __false_type)
1610  {
1611  for (; __first != __last; ++__first)
1612 #if __cplusplus >= 201103L
1613  emplace_back(*__first);
1614 #else
1615  push_back(*__first);
1616 #endif
1617  }
1618 
1619  // Called by list(n,v,a), and the range constructor when it turns out
1620  // to be the same thing.
1621  void
1622  _M_fill_initialize(size_type __n, const value_type& __x)
1623  {
1624  for (; __n; --__n)
1625  push_back(__x);
1626  }
1627 
1628 #if __cplusplus >= 201103L
1629  // Called by list(n).
1630  void
1631  _M_default_initialize(size_type __n)
1632  {
1633  for (; __n; --__n)
1634  emplace_back();
1635  }
1636 
1637  // Called by resize(sz).
1638  void
1639  _M_default_append(size_type __n);
1640 #endif
1641 
1642  // Internal assign functions follow.
1643 
1644  // Called by the range assign to implement [23.1.1]/9
1645 
1646  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1647  // 438. Ambiguity in the "do the right thing" clause
1648  template<typename _Integer>
1649  void
1650  _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1651  { _M_fill_assign(__n, __val); }
1652 
1653  // Called by the range assign to implement [23.1.1]/9
1654  template<typename _InputIterator>
1655  void
1656  _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1657  __false_type);
1658 
1659  // Called by assign(n,t), and the range assign when it turns out
1660  // to be the same thing.
1661  void
1662  _M_fill_assign(size_type __n, const value_type& __val);
1663 
1664 
1665  // Moves the elements from [first,last) before position.
1666  void
1667  _M_transfer(iterator __position, iterator __first, iterator __last)
1668  { __position._M_node->_M_transfer(__first._M_node, __last._M_node); }
1669 
1670  // Inserts new element at position given and with value given.
1671 #if __cplusplus < 201103L
1672  void
1673  _M_insert(iterator __position, const value_type& __x)
1674  {
1675  _Node* __tmp = _M_create_node(__x);
1676  __tmp->_M_hook(__position._M_node);
1677  }
1678 #else
1679  template<typename... _Args>
1680  void
1681  _M_insert(iterator __position, _Args&&... __args)
1682  {
1683  _Node* __tmp = _M_create_node(std::forward<_Args>(__args)...);
1684  __tmp->_M_hook(__position._M_node);
1685  }
1686 #endif
1687 
1688  // Erases element at position given.
1689  void
1690  _M_erase(iterator __position)
1691  {
1692  __position._M_node->_M_unhook();
1693  _Node* __n = static_cast<_Node*>(__position._M_node);
1694 #if __cplusplus >= 201103L
1695  _M_get_Node_allocator().destroy(__n);
1696 #else
1697  _M_get_Tp_allocator().destroy(std::__addressof(__n->_M_data));
1698 #endif
1699  _M_put_node(__n);
1700  }
1701 
1702  // To implement the splice (and merge) bits of N1599.
1703  void
1704  _M_check_equal_allocators(list& __x)
1705  {
1706  if (std::__alloc_neq<typename _Base::_Node_alloc_type>::
1707  _S_do_it(_M_get_Node_allocator(), __x._M_get_Node_allocator()))
1708  __throw_runtime_error(__N("list::_M_check_equal_allocators"));
1709  }
1710  };
1711 
1712  /**
1713  * @brief List equality comparison.
1714  * @param __x A %list.
1715  * @param __y A %list of the same type as @a __x.
1716  * @return True iff the size and elements of the lists are equal.
1717  *
1718  * This is an equivalence relation. It is linear in the size of
1719  * the lists. Lists are considered equivalent if their sizes are
1720  * equal, and if corresponding elements compare equal.
1721  */
1722  template<typename _Tp, typename _Alloc>
1723  inline bool
1724  operator==(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1725  {
1726  typedef typename list<_Tp, _Alloc>::const_iterator const_iterator;
1727  const_iterator __end1 = __x.end();
1728  const_iterator __end2 = __y.end();
1729 
1730  const_iterator __i1 = __x.begin();
1731  const_iterator __i2 = __y.begin();
1732  while (__i1 != __end1 && __i2 != __end2 && *__i1 == *__i2)
1733  {
1734  ++__i1;
1735  ++__i2;
1736  }
1737  return __i1 == __end1 && __i2 == __end2;
1738  }
1739 
1740  /**
1741  * @brief List ordering relation.
1742  * @param __x A %list.
1743  * @param __y A %list of the same type as @a __x.
1744  * @return True iff @a __x is lexicographically less than @a __y.
1745  *
1746  * This is a total ordering relation. It is linear in the size of the
1747  * lists. The elements must be comparable with @c <.
1748  *
1749  * See std::lexicographical_compare() for how the determination is made.
1750  */
1751  template<typename _Tp, typename _Alloc>
1752  inline bool
1753  operator<(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1754  { return std::lexicographical_compare(__x.begin(), __x.end(),
1755  __y.begin(), __y.end()); }
1756 
1757  /// Based on operator==
1758  template<typename _Tp, typename _Alloc>
1759  inline bool
1760  operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1761  { return !(__x == __y); }
1762 
1763  /// Based on operator<
1764  template<typename _Tp, typename _Alloc>
1765  inline bool
1766  operator>(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1767  { return __y < __x; }
1768 
1769  /// Based on operator<
1770  template<typename _Tp, typename _Alloc>
1771  inline bool
1772  operator<=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1773  { return !(__y < __x); }
1774 
1775  /// Based on operator<
1776  template<typename _Tp, typename _Alloc>
1777  inline bool
1778  operator>=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
1779  { return !(__x < __y); }
1780 
1781  /// See std::list::swap().
1782  template<typename _Tp, typename _Alloc>
1783  inline void
1785  { __x.swap(__y); }
1786 
1787 _GLIBCXX_END_NAMESPACE_CONTAINER
1788 } // namespace std
1789 
1790 #endif /* _STL_LIST_H */