xref: /dflybsd-src/contrib/gcc-8.0/libstdc++-v3/include/debug/list (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj// Debugging list implementation -*- C++ -*-
2*38fd1498Szrj
3*38fd1498Szrj// Copyright (C) 2003-2018 Free Software Foundation, Inc.
4*38fd1498Szrj//
5*38fd1498Szrj// This file is part of the GNU ISO C++ Library.  This library is free
6*38fd1498Szrj// software; you can redistribute it and/or modify it under the
7*38fd1498Szrj// terms of the GNU General Public License as published by the
8*38fd1498Szrj// Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj// any later version.
10*38fd1498Szrj
11*38fd1498Szrj// This library is distributed in the hope that it will be useful,
12*38fd1498Szrj// but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*38fd1498Szrj// GNU General Public License for more details.
15*38fd1498Szrj
16*38fd1498Szrj// Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj// permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj// 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj
20*38fd1498Szrj// You should have received a copy of the GNU General Public License and
21*38fd1498Szrj// a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23*38fd1498Szrj// <http://www.gnu.org/licenses/>.
24*38fd1498Szrj
25*38fd1498Szrj/** @file debug/list
26*38fd1498Szrj *  This file is a GNU debug extension to the Standard C++ Library.
27*38fd1498Szrj */
28*38fd1498Szrj
29*38fd1498Szrj#ifndef _GLIBCXX_DEBUG_LIST
30*38fd1498Szrj#define _GLIBCXX_DEBUG_LIST 1
31*38fd1498Szrj
32*38fd1498Szrj#pragma GCC system_header
33*38fd1498Szrj
34*38fd1498Szrj#include <list>
35*38fd1498Szrj#include <debug/safe_sequence.h>
36*38fd1498Szrj#include <debug/safe_container.h>
37*38fd1498Szrj#include <debug/safe_iterator.h>
38*38fd1498Szrj
39*38fd1498Szrjnamespace std _GLIBCXX_VISIBILITY(default)
40*38fd1498Szrj{
41*38fd1498Szrjnamespace __debug
42*38fd1498Szrj{
43*38fd1498Szrj  /// Class std::list with safety/checking/debug instrumentation.
44*38fd1498Szrj  template<typename _Tp, typename _Allocator = std::allocator<_Tp> >
45*38fd1498Szrj    class list
46*38fd1498Szrj    : public __gnu_debug::_Safe_container<
47*38fd1498Szrj	list<_Tp, _Allocator>, _Allocator,
48*38fd1498Szrj	__gnu_debug::_Safe_node_sequence>,
49*38fd1498Szrj      public _GLIBCXX_STD_C::list<_Tp, _Allocator>
50*38fd1498Szrj    {
51*38fd1498Szrj      typedef _GLIBCXX_STD_C::list<_Tp, _Allocator>		_Base;
52*38fd1498Szrj      typedef __gnu_debug::_Safe_container<
53*38fd1498Szrj	list, _Allocator, __gnu_debug::_Safe_node_sequence>	_Safe;
54*38fd1498Szrj
55*38fd1498Szrj      typedef typename _Base::iterator		_Base_iterator;
56*38fd1498Szrj      typedef typename _Base::const_iterator	_Base_const_iterator;
57*38fd1498Szrj      typedef __gnu_debug::_Equal_to<_Base_const_iterator>	_Equal;
58*38fd1498Szrj      typedef __gnu_debug::_Not_equal_to<_Base_const_iterator>  _Not_equal;
59*38fd1498Szrj
60*38fd1498Szrj    public:
61*38fd1498Szrj      typedef typename _Base::reference			reference;
62*38fd1498Szrj      typedef typename _Base::const_reference		const_reference;
63*38fd1498Szrj
64*38fd1498Szrj      typedef __gnu_debug::_Safe_iterator<_Base_iterator, list>
65*38fd1498Szrj							iterator;
66*38fd1498Szrj      typedef __gnu_debug::_Safe_iterator<_Base_const_iterator, list>
67*38fd1498Szrj							const_iterator;
68*38fd1498Szrj
69*38fd1498Szrj      typedef typename _Base::size_type			size_type;
70*38fd1498Szrj      typedef typename _Base::difference_type		difference_type;
71*38fd1498Szrj
72*38fd1498Szrj      typedef _Tp					value_type;
73*38fd1498Szrj      typedef _Allocator				allocator_type;
74*38fd1498Szrj      typedef typename _Base::pointer			pointer;
75*38fd1498Szrj      typedef typename _Base::const_pointer		const_pointer;
76*38fd1498Szrj      typedef std::reverse_iterator<iterator>		reverse_iterator;
77*38fd1498Szrj      typedef std::reverse_iterator<const_iterator>	const_reverse_iterator;
78*38fd1498Szrj
79*38fd1498Szrj      // 23.2.2.1 construct/copy/destroy:
80*38fd1498Szrj
81*38fd1498Szrj#if __cplusplus < 201103L
82*38fd1498Szrj      list()
83*38fd1498Szrj      : _Base() { }
84*38fd1498Szrj
85*38fd1498Szrj      list(const list& __x)
86*38fd1498Szrj      : _Base(__x) { }
87*38fd1498Szrj
88*38fd1498Szrj      ~list() { }
89*38fd1498Szrj#else
90*38fd1498Szrj      list() = default;
91*38fd1498Szrj      list(const list&) = default;
92*38fd1498Szrj      list(list&&) = default;
93*38fd1498Szrj
94*38fd1498Szrj      list(initializer_list<value_type> __l,
95*38fd1498Szrj	   const allocator_type& __a = allocator_type())
96*38fd1498Szrj      : _Base(__l, __a) { }
97*38fd1498Szrj
98*38fd1498Szrj      ~list() = default;
99*38fd1498Szrj
100*38fd1498Szrj      list(const list& __x, const allocator_type& __a)
101*38fd1498Szrj      : _Base(__x, __a) { }
102*38fd1498Szrj
103*38fd1498Szrj      list(list&& __x, const allocator_type& __a)
104*38fd1498Szrj      : _Base(std::move(__x), __a) { }
105*38fd1498Szrj#endif
106*38fd1498Szrj
107*38fd1498Szrj      explicit
108*38fd1498Szrj      list(const _Allocator& __a) _GLIBCXX_NOEXCEPT
109*38fd1498Szrj      : _Base(__a) { }
110*38fd1498Szrj
111*38fd1498Szrj#if __cplusplus >= 201103L
112*38fd1498Szrj      explicit
113*38fd1498Szrj      list(size_type __n, const allocator_type& __a = allocator_type())
114*38fd1498Szrj      : _Base(__n, __a) { }
115*38fd1498Szrj
116*38fd1498Szrj      list(size_type __n, const _Tp& __value,
117*38fd1498Szrj	   const _Allocator& __a = _Allocator())
118*38fd1498Szrj      : _Base(__n, __value, __a) { }
119*38fd1498Szrj#else
120*38fd1498Szrj      explicit
121*38fd1498Szrj      list(size_type __n, const _Tp& __value = _Tp(),
122*38fd1498Szrj	   const _Allocator& __a = _Allocator())
123*38fd1498Szrj      : _Base(__n, __value, __a) { }
124*38fd1498Szrj#endif
125*38fd1498Szrj
126*38fd1498Szrj#if __cplusplus >= 201103L
127*38fd1498Szrj      template<class _InputIterator,
128*38fd1498Szrj	       typename = std::_RequireInputIter<_InputIterator>>
129*38fd1498Szrj#else
130*38fd1498Szrj      template<class _InputIterator>
131*38fd1498Szrj#endif
132*38fd1498Szrj	list(_InputIterator __first, _InputIterator __last,
133*38fd1498Szrj	     const _Allocator& __a = _Allocator())
134*38fd1498Szrj	: _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
135*38fd1498Szrj								     __last)),
136*38fd1498Szrj		__gnu_debug::__base(__last), __a)
137*38fd1498Szrj	{ }
138*38fd1498Szrj
139*38fd1498Szrj      list(const _Base& __x)
140*38fd1498Szrj      : _Base(__x) { }
141*38fd1498Szrj
142*38fd1498Szrj#if __cplusplus < 201103L
143*38fd1498Szrj      list&
144*38fd1498Szrj      operator=(const list& __x)
145*38fd1498Szrj      {
146*38fd1498Szrj	this->_M_safe() = __x;
147*38fd1498Szrj	_M_base() = __x;
148*38fd1498Szrj	return *this;
149*38fd1498Szrj      }
150*38fd1498Szrj#else
151*38fd1498Szrj      list&
152*38fd1498Szrj      operator=(const list&) = default;
153*38fd1498Szrj
154*38fd1498Szrj      list&
155*38fd1498Szrj      operator=(list&&) = default;
156*38fd1498Szrj
157*38fd1498Szrj      list&
158*38fd1498Szrj      operator=(initializer_list<value_type> __l)
159*38fd1498Szrj      {
160*38fd1498Szrj	this->_M_invalidate_all();
161*38fd1498Szrj	_M_base() = __l;
162*38fd1498Szrj	return *this;
163*38fd1498Szrj      }
164*38fd1498Szrj
165*38fd1498Szrj      void
166*38fd1498Szrj      assign(initializer_list<value_type> __l)
167*38fd1498Szrj      {
168*38fd1498Szrj	_Base::assign(__l);
169*38fd1498Szrj	this->_M_invalidate_all();
170*38fd1498Szrj      }
171*38fd1498Szrj#endif
172*38fd1498Szrj
173*38fd1498Szrj#if __cplusplus >= 201103L
174*38fd1498Szrj      template<class _InputIterator,
175*38fd1498Szrj	       typename = std::_RequireInputIter<_InputIterator>>
176*38fd1498Szrj#else
177*38fd1498Szrj      template<class _InputIterator>
178*38fd1498Szrj#endif
179*38fd1498Szrj	void
180*38fd1498Szrj	assign(_InputIterator __first, _InputIterator __last)
181*38fd1498Szrj	{
182*38fd1498Szrj	  typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
183*38fd1498Szrj	  __glibcxx_check_valid_range2(__first, __last, __dist);
184*38fd1498Szrj
185*38fd1498Szrj	  if (__dist.second >= __gnu_debug::__dp_sign)
186*38fd1498Szrj	    _Base::assign(__gnu_debug::__unsafe(__first),
187*38fd1498Szrj			  __gnu_debug::__unsafe(__last));
188*38fd1498Szrj	  else
189*38fd1498Szrj	    _Base::assign(__first, __last);
190*38fd1498Szrj
191*38fd1498Szrj	  this->_M_invalidate_all();
192*38fd1498Szrj	}
193*38fd1498Szrj
194*38fd1498Szrj      void
195*38fd1498Szrj      assign(size_type __n, const _Tp& __t)
196*38fd1498Szrj      {
197*38fd1498Szrj	_Base::assign(__n, __t);
198*38fd1498Szrj	this->_M_invalidate_all();
199*38fd1498Szrj      }
200*38fd1498Szrj
201*38fd1498Szrj      using _Base::get_allocator;
202*38fd1498Szrj
203*38fd1498Szrj      // iterators:
204*38fd1498Szrj      iterator
205*38fd1498Szrj      begin() _GLIBCXX_NOEXCEPT
206*38fd1498Szrj      { return iterator(_Base::begin(), this); }
207*38fd1498Szrj
208*38fd1498Szrj      const_iterator
209*38fd1498Szrj      begin() const _GLIBCXX_NOEXCEPT
210*38fd1498Szrj      { return const_iterator(_Base::begin(), this); }
211*38fd1498Szrj
212*38fd1498Szrj      iterator
213*38fd1498Szrj      end() _GLIBCXX_NOEXCEPT
214*38fd1498Szrj      { return iterator(_Base::end(), this); }
215*38fd1498Szrj
216*38fd1498Szrj      const_iterator
217*38fd1498Szrj      end() const _GLIBCXX_NOEXCEPT
218*38fd1498Szrj      { return const_iterator(_Base::end(), this); }
219*38fd1498Szrj
220*38fd1498Szrj      reverse_iterator
221*38fd1498Szrj      rbegin() _GLIBCXX_NOEXCEPT
222*38fd1498Szrj      { return reverse_iterator(end()); }
223*38fd1498Szrj
224*38fd1498Szrj      const_reverse_iterator
225*38fd1498Szrj      rbegin() const _GLIBCXX_NOEXCEPT
226*38fd1498Szrj      { return const_reverse_iterator(end()); }
227*38fd1498Szrj
228*38fd1498Szrj      reverse_iterator
229*38fd1498Szrj      rend() _GLIBCXX_NOEXCEPT
230*38fd1498Szrj      { return reverse_iterator(begin()); }
231*38fd1498Szrj
232*38fd1498Szrj      const_reverse_iterator
233*38fd1498Szrj      rend() const _GLIBCXX_NOEXCEPT
234*38fd1498Szrj      { return const_reverse_iterator(begin()); }
235*38fd1498Szrj
236*38fd1498Szrj#if __cplusplus >= 201103L
237*38fd1498Szrj      const_iterator
238*38fd1498Szrj      cbegin() const noexcept
239*38fd1498Szrj      { return const_iterator(_Base::begin(), this); }
240*38fd1498Szrj
241*38fd1498Szrj      const_iterator
242*38fd1498Szrj      cend() const noexcept
243*38fd1498Szrj      { return const_iterator(_Base::end(), this); }
244*38fd1498Szrj
245*38fd1498Szrj      const_reverse_iterator
246*38fd1498Szrj      crbegin() const noexcept
247*38fd1498Szrj      { return const_reverse_iterator(end()); }
248*38fd1498Szrj
249*38fd1498Szrj      const_reverse_iterator
250*38fd1498Szrj      crend() const noexcept
251*38fd1498Szrj      { return const_reverse_iterator(begin()); }
252*38fd1498Szrj#endif
253*38fd1498Szrj
254*38fd1498Szrj      // 23.2.2.2 capacity:
255*38fd1498Szrj      using _Base::empty;
256*38fd1498Szrj      using _Base::size;
257*38fd1498Szrj      using _Base::max_size;
258*38fd1498Szrj
259*38fd1498Szrj#if __cplusplus >= 201103L
260*38fd1498Szrj      void
261*38fd1498Szrj      resize(size_type __sz)
262*38fd1498Szrj      {
263*38fd1498Szrj	this->_M_detach_singular();
264*38fd1498Szrj
265*38fd1498Szrj	// if __sz < size(), invalidate all iterators in [begin + __sz, end())
266*38fd1498Szrj	_Base_iterator __victim = _Base::begin();
267*38fd1498Szrj	_Base_iterator __end = _Base::end();
268*38fd1498Szrj	for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
269*38fd1498Szrj	  ++__victim;
270*38fd1498Szrj
271*38fd1498Szrj	for (; __victim != __end; ++__victim)
272*38fd1498Szrj	  this->_M_invalidate_if(_Equal(__victim));
273*38fd1498Szrj
274*38fd1498Szrj	__try
275*38fd1498Szrj	  {
276*38fd1498Szrj	    _Base::resize(__sz);
277*38fd1498Szrj	  }
278*38fd1498Szrj	__catch(...)
279*38fd1498Szrj	  {
280*38fd1498Szrj	    this->_M_revalidate_singular();
281*38fd1498Szrj	    __throw_exception_again;
282*38fd1498Szrj	  }
283*38fd1498Szrj      }
284*38fd1498Szrj
285*38fd1498Szrj      void
286*38fd1498Szrj      resize(size_type __sz, const _Tp& __c)
287*38fd1498Szrj      {
288*38fd1498Szrj	this->_M_detach_singular();
289*38fd1498Szrj
290*38fd1498Szrj	// if __sz < size(), invalidate all iterators in [begin + __sz, end())
291*38fd1498Szrj	_Base_iterator __victim = _Base::begin();
292*38fd1498Szrj	_Base_iterator __end = _Base::end();
293*38fd1498Szrj	for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
294*38fd1498Szrj	  ++__victim;
295*38fd1498Szrj
296*38fd1498Szrj	for (; __victim != __end; ++__victim)
297*38fd1498Szrj	  this->_M_invalidate_if(_Equal(__victim));
298*38fd1498Szrj
299*38fd1498Szrj	__try
300*38fd1498Szrj	  {
301*38fd1498Szrj	    _Base::resize(__sz, __c);
302*38fd1498Szrj	  }
303*38fd1498Szrj	__catch(...)
304*38fd1498Szrj	  {
305*38fd1498Szrj	    this->_M_revalidate_singular();
306*38fd1498Szrj	    __throw_exception_again;
307*38fd1498Szrj	  }
308*38fd1498Szrj      }
309*38fd1498Szrj#else
310*38fd1498Szrj      void
311*38fd1498Szrj      resize(size_type __sz, _Tp __c = _Tp())
312*38fd1498Szrj      {
313*38fd1498Szrj	this->_M_detach_singular();
314*38fd1498Szrj
315*38fd1498Szrj	// if __sz < size(), invalidate all iterators in [begin + __sz, end())
316*38fd1498Szrj	_Base_iterator __victim = _Base::begin();
317*38fd1498Szrj	_Base_iterator __end = _Base::end();
318*38fd1498Szrj	for (size_type __i = __sz; __victim != __end && __i > 0; --__i)
319*38fd1498Szrj	  ++__victim;
320*38fd1498Szrj
321*38fd1498Szrj	for (; __victim != __end; ++__victim)
322*38fd1498Szrj	  this->_M_invalidate_if(_Equal(__victim));
323*38fd1498Szrj
324*38fd1498Szrj	__try
325*38fd1498Szrj	  {
326*38fd1498Szrj	    _Base::resize(__sz, __c);
327*38fd1498Szrj	  }
328*38fd1498Szrj	__catch(...)
329*38fd1498Szrj	  {
330*38fd1498Szrj	    this->_M_revalidate_singular();
331*38fd1498Szrj	    __throw_exception_again;
332*38fd1498Szrj	  }
333*38fd1498Szrj      }
334*38fd1498Szrj#endif
335*38fd1498Szrj
336*38fd1498Szrj      // element access:
337*38fd1498Szrj      reference
338*38fd1498Szrj      front() _GLIBCXX_NOEXCEPT
339*38fd1498Szrj      {
340*38fd1498Szrj	__glibcxx_check_nonempty();
341*38fd1498Szrj	return _Base::front();
342*38fd1498Szrj      }
343*38fd1498Szrj
344*38fd1498Szrj      const_reference
345*38fd1498Szrj      front() const _GLIBCXX_NOEXCEPT
346*38fd1498Szrj      {
347*38fd1498Szrj	__glibcxx_check_nonempty();
348*38fd1498Szrj	return _Base::front();
349*38fd1498Szrj      }
350*38fd1498Szrj
351*38fd1498Szrj      reference
352*38fd1498Szrj      back() _GLIBCXX_NOEXCEPT
353*38fd1498Szrj      {
354*38fd1498Szrj	__glibcxx_check_nonempty();
355*38fd1498Szrj	return _Base::back();
356*38fd1498Szrj      }
357*38fd1498Szrj
358*38fd1498Szrj      const_reference
359*38fd1498Szrj      back() const _GLIBCXX_NOEXCEPT
360*38fd1498Szrj      {
361*38fd1498Szrj	__glibcxx_check_nonempty();
362*38fd1498Szrj	return _Base::back();
363*38fd1498Szrj      }
364*38fd1498Szrj
365*38fd1498Szrj      // 23.2.2.3 modifiers:
366*38fd1498Szrj      using _Base::push_front;
367*38fd1498Szrj
368*38fd1498Szrj#if __cplusplus >= 201103L
369*38fd1498Szrj      using _Base::emplace_front;
370*38fd1498Szrj#endif
371*38fd1498Szrj
372*38fd1498Szrj      void
373*38fd1498Szrj      pop_front() _GLIBCXX_NOEXCEPT
374*38fd1498Szrj      {
375*38fd1498Szrj	__glibcxx_check_nonempty();
376*38fd1498Szrj	this->_M_invalidate_if(_Equal(_Base::begin()));
377*38fd1498Szrj	_Base::pop_front();
378*38fd1498Szrj      }
379*38fd1498Szrj
380*38fd1498Szrj      using _Base::push_back;
381*38fd1498Szrj
382*38fd1498Szrj#if __cplusplus >= 201103L
383*38fd1498Szrj      using _Base::emplace_back;
384*38fd1498Szrj#endif
385*38fd1498Szrj
386*38fd1498Szrj      void
387*38fd1498Szrj      pop_back() _GLIBCXX_NOEXCEPT
388*38fd1498Szrj      {
389*38fd1498Szrj	__glibcxx_check_nonempty();
390*38fd1498Szrj	this->_M_invalidate_if(_Equal(--_Base::end()));
391*38fd1498Szrj	_Base::pop_back();
392*38fd1498Szrj      }
393*38fd1498Szrj
394*38fd1498Szrj#if __cplusplus >= 201103L
395*38fd1498Szrj      template<typename... _Args>
396*38fd1498Szrj	iterator
397*38fd1498Szrj	emplace(const_iterator __position, _Args&&... __args)
398*38fd1498Szrj	{
399*38fd1498Szrj	  __glibcxx_check_insert(__position);
400*38fd1498Szrj	  return iterator(_Base::emplace(__position.base(),
401*38fd1498Szrj					std::forward<_Args>(__args)...), this);
402*38fd1498Szrj	}
403*38fd1498Szrj#endif
404*38fd1498Szrj
405*38fd1498Szrj     iterator
406*38fd1498Szrj#if __cplusplus >= 201103L
407*38fd1498Szrj     insert(const_iterator __position, const _Tp& __x)
408*38fd1498Szrj#else
409*38fd1498Szrj     insert(iterator __position, const _Tp& __x)
410*38fd1498Szrj#endif
411*38fd1498Szrj     {
412*38fd1498Szrj       __glibcxx_check_insert(__position);
413*38fd1498Szrj       return iterator(_Base::insert(__position.base(), __x), this);
414*38fd1498Szrj     }
415*38fd1498Szrj
416*38fd1498Szrj#if __cplusplus >= 201103L
417*38fd1498Szrj      iterator
418*38fd1498Szrj      insert(const_iterator __position, _Tp&& __x)
419*38fd1498Szrj      { return emplace(__position, std::move(__x)); }
420*38fd1498Szrj
421*38fd1498Szrj      iterator
422*38fd1498Szrj      insert(const_iterator __p, initializer_list<value_type> __l)
423*38fd1498Szrj      {
424*38fd1498Szrj	__glibcxx_check_insert(__p);
425*38fd1498Szrj	return iterator(_Base::insert(__p.base(), __l), this);
426*38fd1498Szrj      }
427*38fd1498Szrj#endif
428*38fd1498Szrj
429*38fd1498Szrj#if __cplusplus >= 201103L
430*38fd1498Szrj      iterator
431*38fd1498Szrj      insert(const_iterator __position, size_type __n, const _Tp& __x)
432*38fd1498Szrj      {
433*38fd1498Szrj	__glibcxx_check_insert(__position);
434*38fd1498Szrj	return iterator(_Base::insert(__position.base(), __n, __x), this);
435*38fd1498Szrj      }
436*38fd1498Szrj#else
437*38fd1498Szrj      void
438*38fd1498Szrj      insert(iterator __position, size_type __n, const _Tp& __x)
439*38fd1498Szrj      {
440*38fd1498Szrj	__glibcxx_check_insert(__position);
441*38fd1498Szrj	_Base::insert(__position.base(), __n, __x);
442*38fd1498Szrj      }
443*38fd1498Szrj#endif
444*38fd1498Szrj
445*38fd1498Szrj#if __cplusplus >= 201103L
446*38fd1498Szrj      template<class _InputIterator,
447*38fd1498Szrj	       typename = std::_RequireInputIter<_InputIterator>>
448*38fd1498Szrj	iterator
449*38fd1498Szrj	insert(const_iterator __position, _InputIterator __first,
450*38fd1498Szrj	       _InputIterator __last)
451*38fd1498Szrj	{
452*38fd1498Szrj	  typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
453*38fd1498Szrj	  __glibcxx_check_insert_range(__position, __first, __last, __dist);
454*38fd1498Szrj	  if (__dist.second >= __gnu_debug::__dp_sign)
455*38fd1498Szrj	    return
456*38fd1498Szrj	      {
457*38fd1498Szrj		_Base::insert(__position.base(),
458*38fd1498Szrj			      __gnu_debug::__unsafe(__first),
459*38fd1498Szrj			      __gnu_debug::__unsafe(__last)),
460*38fd1498Szrj		  this
461*38fd1498Szrj	      };
462*38fd1498Szrj	  else
463*38fd1498Szrj	    return { _Base::insert(__position.base(), __first, __last), this };
464*38fd1498Szrj	}
465*38fd1498Szrj#else
466*38fd1498Szrj      template<class _InputIterator>
467*38fd1498Szrj	void
468*38fd1498Szrj	insert(iterator __position, _InputIterator __first,
469*38fd1498Szrj	       _InputIterator __last)
470*38fd1498Szrj	{
471*38fd1498Szrj	  typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
472*38fd1498Szrj	  __glibcxx_check_insert_range(__position, __first, __last, __dist);
473*38fd1498Szrj
474*38fd1498Szrj	  if (__dist.second >= __gnu_debug::__dp_sign)
475*38fd1498Szrj	    _Base::insert(__position.base(), __gnu_debug::__unsafe(__first),
476*38fd1498Szrj					     __gnu_debug::__unsafe(__last));
477*38fd1498Szrj	  else
478*38fd1498Szrj	    _Base::insert(__position.base(), __first, __last);
479*38fd1498Szrj	}
480*38fd1498Szrj#endif
481*38fd1498Szrj
482*38fd1498Szrj    private:
483*38fd1498Szrj      _Base_iterator
484*38fd1498Szrj#if __cplusplus >= 201103L
485*38fd1498Szrj      _M_erase(_Base_const_iterator __position) noexcept
486*38fd1498Szrj#else
487*38fd1498Szrj      _M_erase(_Base_iterator __position)
488*38fd1498Szrj#endif
489*38fd1498Szrj      {
490*38fd1498Szrj	this->_M_invalidate_if(_Equal(__position));
491*38fd1498Szrj	return _Base::erase(__position);
492*38fd1498Szrj      }
493*38fd1498Szrj
494*38fd1498Szrj    public:
495*38fd1498Szrj      iterator
496*38fd1498Szrj#if __cplusplus >= 201103L
497*38fd1498Szrj      erase(const_iterator __position) noexcept
498*38fd1498Szrj#else
499*38fd1498Szrj      erase(iterator __position)
500*38fd1498Szrj#endif
501*38fd1498Szrj      {
502*38fd1498Szrj	__glibcxx_check_erase(__position);
503*38fd1498Szrj	return iterator(_M_erase(__position.base()), this);
504*38fd1498Szrj      }
505*38fd1498Szrj
506*38fd1498Szrj      iterator
507*38fd1498Szrj#if __cplusplus >= 201103L
508*38fd1498Szrj      erase(const_iterator __first, const_iterator __last) noexcept
509*38fd1498Szrj#else
510*38fd1498Szrj      erase(iterator __first, iterator __last)
511*38fd1498Szrj#endif
512*38fd1498Szrj      {
513*38fd1498Szrj	// _GLIBCXX_RESOLVE_LIB_DEFECTS
514*38fd1498Szrj	// 151. can't currently clear() empty container
515*38fd1498Szrj	__glibcxx_check_erase_range(__first, __last);
516*38fd1498Szrj	for (_Base_const_iterator __victim = __first.base();
517*38fd1498Szrj	     __victim != __last.base(); ++__victim)
518*38fd1498Szrj	  {
519*38fd1498Szrj	    _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(),
520*38fd1498Szrj				  _M_message(__gnu_debug::__msg_valid_range)
521*38fd1498Szrj				  ._M_iterator(__first, "position")
522*38fd1498Szrj				  ._M_iterator(__last, "last"));
523*38fd1498Szrj	    this->_M_invalidate_if(_Equal(__victim));
524*38fd1498Szrj	  }
525*38fd1498Szrj	return iterator(_Base::erase(__first.base(), __last.base()), this);
526*38fd1498Szrj      }
527*38fd1498Szrj
528*38fd1498Szrj      void
529*38fd1498Szrj      swap(list& __x)
530*38fd1498Szrj      _GLIBCXX_NOEXCEPT_IF( noexcept(declval<_Base&>().swap(__x)) )
531*38fd1498Szrj      {
532*38fd1498Szrj	_Safe::_M_swap(__x);
533*38fd1498Szrj	_Base::swap(__x);
534*38fd1498Szrj      }
535*38fd1498Szrj
536*38fd1498Szrj      void
537*38fd1498Szrj      clear() _GLIBCXX_NOEXCEPT
538*38fd1498Szrj      {
539*38fd1498Szrj	_Base::clear();
540*38fd1498Szrj	this->_M_invalidate_all();
541*38fd1498Szrj      }
542*38fd1498Szrj
543*38fd1498Szrj      // 23.2.2.4 list operations:
544*38fd1498Szrj      void
545*38fd1498Szrj#if __cplusplus >= 201103L
546*38fd1498Szrj      splice(const_iterator __position, list&& __x) noexcept
547*38fd1498Szrj#else
548*38fd1498Szrj      splice(iterator __position, list& __x)
549*38fd1498Szrj#endif
550*38fd1498Szrj      {
551*38fd1498Szrj	_GLIBCXX_DEBUG_VERIFY(std::__addressof(__x) != this,
552*38fd1498Szrj			      _M_message(__gnu_debug::__msg_self_splice)
553*38fd1498Szrj			      ._M_sequence(*this, "this"));
554*38fd1498Szrj	this->_M_transfer_from_if(__x, _Not_equal(__x._M_base().end()));
555*38fd1498Szrj	_Base::splice(__position.base(), _GLIBCXX_MOVE(__x._M_base()));
556*38fd1498Szrj      }
557*38fd1498Szrj
558*38fd1498Szrj#if __cplusplus >= 201103L
559*38fd1498Szrj      void
560*38fd1498Szrj      splice(const_iterator __position, list& __x) noexcept
561*38fd1498Szrj      { splice(__position, std::move(__x)); }
562*38fd1498Szrj#endif
563*38fd1498Szrj
564*38fd1498Szrj      void
565*38fd1498Szrj#if __cplusplus >= 201103L
566*38fd1498Szrj      splice(const_iterator __position, list&& __x, const_iterator __i) noexcept
567*38fd1498Szrj#else
568*38fd1498Szrj      splice(iterator __position, list& __x, iterator __i)
569*38fd1498Szrj#endif
570*38fd1498Szrj      {
571*38fd1498Szrj	__glibcxx_check_insert(__position);
572*38fd1498Szrj
573*38fd1498Szrj	// We used to perform the splice_alloc check:  not anymore, redundant
574*38fd1498Szrj	// after implementing the relevant bits of N1599.
575*38fd1498Szrj
576*38fd1498Szrj	_GLIBCXX_DEBUG_VERIFY(__i._M_dereferenceable(),
577*38fd1498Szrj			      _M_message(__gnu_debug::__msg_splice_bad)
578*38fd1498Szrj			      ._M_iterator(__i, "__i"));
579*38fd1498Szrj	_GLIBCXX_DEBUG_VERIFY(__i._M_attached_to(std::__addressof(__x)),
580*38fd1498Szrj			      _M_message(__gnu_debug::__msg_splice_other)
581*38fd1498Szrj			     ._M_iterator(__i, "__i")._M_sequence(__x, "__x"));
582*38fd1498Szrj
583*38fd1498Szrj	// _GLIBCXX_RESOLVE_LIB_DEFECTS
584*38fd1498Szrj	// 250. splicing invalidates iterators
585*38fd1498Szrj	this->_M_transfer_from_if(__x, _Equal(__i.base()));
586*38fd1498Szrj	_Base::splice(__position.base(), _GLIBCXX_MOVE(__x._M_base()),
587*38fd1498Szrj		      __i.base());
588*38fd1498Szrj      }
589*38fd1498Szrj
590*38fd1498Szrj#if __cplusplus >= 201103L
591*38fd1498Szrj      void
592*38fd1498Szrj      splice(const_iterator __position, list& __x, const_iterator __i) noexcept
593*38fd1498Szrj      { splice(__position, std::move(__x), __i); }
594*38fd1498Szrj#endif
595*38fd1498Szrj
596*38fd1498Szrj      void
597*38fd1498Szrj#if __cplusplus >= 201103L
598*38fd1498Szrj      splice(const_iterator __position, list&& __x, const_iterator __first,
599*38fd1498Szrj	     const_iterator __last) noexcept
600*38fd1498Szrj#else
601*38fd1498Szrj      splice(iterator __position, list& __x, iterator __first,
602*38fd1498Szrj	     iterator __last)
603*38fd1498Szrj#endif
604*38fd1498Szrj      {
605*38fd1498Szrj	__glibcxx_check_insert(__position);
606*38fd1498Szrj	__glibcxx_check_valid_range(__first, __last);
607*38fd1498Szrj	_GLIBCXX_DEBUG_VERIFY(__first._M_attached_to(std::__addressof(__x)),
608*38fd1498Szrj			      _M_message(__gnu_debug::__msg_splice_other)
609*38fd1498Szrj			      ._M_sequence(__x, "x")
610*38fd1498Szrj			      ._M_iterator(__first, "first"));
611*38fd1498Szrj
612*38fd1498Szrj	// We used to perform the splice_alloc check:  not anymore, redundant
613*38fd1498Szrj	// after implementing the relevant bits of N1599.
614*38fd1498Szrj
615*38fd1498Szrj	for (_Base_const_iterator __tmp = __first.base();
616*38fd1498Szrj	     __tmp != __last.base(); ++__tmp)
617*38fd1498Szrj	  {
618*38fd1498Szrj	    _GLIBCXX_DEBUG_VERIFY(__tmp != _Base::end(),
619*38fd1498Szrj				  _M_message(__gnu_debug::__msg_valid_range)
620*38fd1498Szrj				  ._M_iterator(__first, "first")
621*38fd1498Szrj				  ._M_iterator(__last, "last"));
622*38fd1498Szrj	    _GLIBCXX_DEBUG_VERIFY(std::__addressof(__x) != this
623*38fd1498Szrj				  || __tmp != __position.base(),
624*38fd1498Szrj				_M_message(__gnu_debug::__msg_splice_overlap)
625*38fd1498Szrj				  ._M_iterator(__tmp, "position")
626*38fd1498Szrj				  ._M_iterator(__first, "first")
627*38fd1498Szrj				  ._M_iterator(__last, "last"));
628*38fd1498Szrj	    // _GLIBCXX_RESOLVE_LIB_DEFECTS
629*38fd1498Szrj	    // 250. splicing invalidates iterators
630*38fd1498Szrj	    this->_M_transfer_from_if(__x, _Equal(__tmp));
631*38fd1498Szrj	  }
632*38fd1498Szrj
633*38fd1498Szrj	_Base::splice(__position.base(), _GLIBCXX_MOVE(__x._M_base()),
634*38fd1498Szrj		      __first.base(), __last.base());
635*38fd1498Szrj      }
636*38fd1498Szrj
637*38fd1498Szrj#if __cplusplus >= 201103L
638*38fd1498Szrj      void
639*38fd1498Szrj      splice(const_iterator __position, list& __x,
640*38fd1498Szrj	     const_iterator __first, const_iterator __last) noexcept
641*38fd1498Szrj      { splice(__position, std::move(__x), __first, __last); }
642*38fd1498Szrj#endif
643*38fd1498Szrj
644*38fd1498Szrj      void
645*38fd1498Szrj      remove(const _Tp& __value)
646*38fd1498Szrj      {
647*38fd1498Szrj	for (_Base_iterator __x = _Base::begin(); __x != _Base::end(); )
648*38fd1498Szrj	  {
649*38fd1498Szrj	    if (*__x == __value)
650*38fd1498Szrj	      __x = _M_erase(__x);
651*38fd1498Szrj	    else
652*38fd1498Szrj	      ++__x;
653*38fd1498Szrj	  }
654*38fd1498Szrj      }
655*38fd1498Szrj
656*38fd1498Szrj      template<class _Predicate>
657*38fd1498Szrj	void
658*38fd1498Szrj	remove_if(_Predicate __pred)
659*38fd1498Szrj	{
660*38fd1498Szrj	  for (_Base_iterator __x = _Base::begin(); __x != _Base::end(); )
661*38fd1498Szrj	    {
662*38fd1498Szrj	      if (__pred(*__x))
663*38fd1498Szrj		__x = _M_erase(__x);
664*38fd1498Szrj	      else
665*38fd1498Szrj		++__x;
666*38fd1498Szrj	    }
667*38fd1498Szrj	}
668*38fd1498Szrj
669*38fd1498Szrj      void
670*38fd1498Szrj      unique()
671*38fd1498Szrj      {
672*38fd1498Szrj	_Base_iterator __first = _Base::begin();
673*38fd1498Szrj	_Base_iterator __last = _Base::end();
674*38fd1498Szrj	if (__first == __last)
675*38fd1498Szrj	  return;
676*38fd1498Szrj	_Base_iterator __next = __first; ++__next;
677*38fd1498Szrj	while (__next != __last)
678*38fd1498Szrj	  {
679*38fd1498Szrj	    if (*__first == *__next)
680*38fd1498Szrj	      __next = _M_erase(__next);
681*38fd1498Szrj	    else
682*38fd1498Szrj	      __first = __next++;
683*38fd1498Szrj	  }
684*38fd1498Szrj      }
685*38fd1498Szrj
686*38fd1498Szrj      template<class _BinaryPredicate>
687*38fd1498Szrj	void
688*38fd1498Szrj	unique(_BinaryPredicate __binary_pred)
689*38fd1498Szrj	{
690*38fd1498Szrj	  _Base_iterator __first = _Base::begin();
691*38fd1498Szrj	  _Base_iterator __last = _Base::end();
692*38fd1498Szrj	  if (__first == __last)
693*38fd1498Szrj	    return;
694*38fd1498Szrj	  _Base_iterator __next = __first; ++__next;
695*38fd1498Szrj	  while (__next != __last)
696*38fd1498Szrj	    {
697*38fd1498Szrj	      if (__binary_pred(*__first, *__next))
698*38fd1498Szrj		__next = _M_erase(__next);
699*38fd1498Szrj	      else
700*38fd1498Szrj		__first = __next++;
701*38fd1498Szrj	    }
702*38fd1498Szrj	}
703*38fd1498Szrj
704*38fd1498Szrj      void
705*38fd1498Szrj#if __cplusplus >= 201103L
706*38fd1498Szrj      merge(list&& __x)
707*38fd1498Szrj#else
708*38fd1498Szrj      merge(list& __x)
709*38fd1498Szrj#endif
710*38fd1498Szrj      {
711*38fd1498Szrj	// _GLIBCXX_RESOLVE_LIB_DEFECTS
712*38fd1498Szrj	// 300. list::merge() specification incomplete
713*38fd1498Szrj	if (this != std::__addressof(__x))
714*38fd1498Szrj	  {
715*38fd1498Szrj	    __glibcxx_check_sorted(_Base::begin(), _Base::end());
716*38fd1498Szrj	    __glibcxx_check_sorted(__x.begin().base(), __x.end().base());
717*38fd1498Szrj	    this->_M_transfer_from_if(__x, _Not_equal(__x._M_base().end()));
718*38fd1498Szrj	    _Base::merge(_GLIBCXX_MOVE(__x._M_base()));
719*38fd1498Szrj	  }
720*38fd1498Szrj      }
721*38fd1498Szrj
722*38fd1498Szrj#if __cplusplus >= 201103L
723*38fd1498Szrj      void
724*38fd1498Szrj      merge(list& __x)
725*38fd1498Szrj      { merge(std::move(__x)); }
726*38fd1498Szrj#endif
727*38fd1498Szrj
728*38fd1498Szrj      template<class _Compare>
729*38fd1498Szrj	void
730*38fd1498Szrj#if __cplusplus >= 201103L
731*38fd1498Szrj	merge(list&& __x, _Compare __comp)
732*38fd1498Szrj#else
733*38fd1498Szrj	merge(list& __x, _Compare __comp)
734*38fd1498Szrj#endif
735*38fd1498Szrj	{
736*38fd1498Szrj	  // _GLIBCXX_RESOLVE_LIB_DEFECTS
737*38fd1498Szrj	  // 300. list::merge() specification incomplete
738*38fd1498Szrj	  if (this != std::__addressof(__x))
739*38fd1498Szrj	    {
740*38fd1498Szrj	      __glibcxx_check_sorted_pred(_Base::begin(), _Base::end(),
741*38fd1498Szrj					  __comp);
742*38fd1498Szrj	      __glibcxx_check_sorted_pred(__x.begin().base(), __x.end().base(),
743*38fd1498Szrj					  __comp);
744*38fd1498Szrj	      this->_M_transfer_from_if(__x, _Not_equal(__x._M_base().end()));
745*38fd1498Szrj	      _Base::merge(_GLIBCXX_MOVE(__x._M_base()), __comp);
746*38fd1498Szrj	    }
747*38fd1498Szrj	}
748*38fd1498Szrj
749*38fd1498Szrj#if __cplusplus >= 201103L
750*38fd1498Szrj      template<typename _Compare>
751*38fd1498Szrj	void
752*38fd1498Szrj	merge(list& __x, _Compare __comp)
753*38fd1498Szrj	{ merge(std::move(__x), __comp); }
754*38fd1498Szrj#endif
755*38fd1498Szrj
756*38fd1498Szrj      void
757*38fd1498Szrj      sort() { _Base::sort(); }
758*38fd1498Szrj
759*38fd1498Szrj      template<typename _StrictWeakOrdering>
760*38fd1498Szrj	void
761*38fd1498Szrj	sort(_StrictWeakOrdering __pred) { _Base::sort(__pred); }
762*38fd1498Szrj
763*38fd1498Szrj      using _Base::reverse;
764*38fd1498Szrj
765*38fd1498Szrj      _Base&
766*38fd1498Szrj      _M_base() _GLIBCXX_NOEXCEPT	{ return *this; }
767*38fd1498Szrj
768*38fd1498Szrj      const _Base&
769*38fd1498Szrj      _M_base() const _GLIBCXX_NOEXCEPT	{ return *this; }
770*38fd1498Szrj    };
771*38fd1498Szrj
772*38fd1498Szrj#if __cpp_deduction_guides >= 201606
773*38fd1498Szrj  template<typename _InputIterator, typename _ValT
774*38fd1498Szrj	     = typename iterator_traits<_InputIterator>::value_type,
775*38fd1498Szrj	   typename _Allocator = allocator<_ValT>,
776*38fd1498Szrj	   typename = _RequireInputIter<_InputIterator>,
777*38fd1498Szrj	   typename = _RequireAllocator<_Allocator>>
778*38fd1498Szrj    list(_InputIterator, _InputIterator, _Allocator = _Allocator())
779*38fd1498Szrj      -> list<_ValT, _Allocator>;
780*38fd1498Szrj#endif
781*38fd1498Szrj
782*38fd1498Szrj  template<typename _Tp, typename _Alloc>
783*38fd1498Szrj    inline bool
784*38fd1498Szrj    operator==(const list<_Tp, _Alloc>& __lhs,
785*38fd1498Szrj	       const list<_Tp, _Alloc>& __rhs)
786*38fd1498Szrj    { return __lhs._M_base() == __rhs._M_base(); }
787*38fd1498Szrj
788*38fd1498Szrj  template<typename _Tp, typename _Alloc>
789*38fd1498Szrj    inline bool
790*38fd1498Szrj    operator!=(const list<_Tp, _Alloc>& __lhs,
791*38fd1498Szrj	       const list<_Tp, _Alloc>& __rhs)
792*38fd1498Szrj    { return __lhs._M_base() != __rhs._M_base(); }
793*38fd1498Szrj
794*38fd1498Szrj  template<typename _Tp, typename _Alloc>
795*38fd1498Szrj    inline bool
796*38fd1498Szrj    operator<(const list<_Tp, _Alloc>& __lhs,
797*38fd1498Szrj	      const list<_Tp, _Alloc>& __rhs)
798*38fd1498Szrj    { return __lhs._M_base() < __rhs._M_base(); }
799*38fd1498Szrj
800*38fd1498Szrj  template<typename _Tp, typename _Alloc>
801*38fd1498Szrj    inline bool
802*38fd1498Szrj    operator<=(const list<_Tp, _Alloc>& __lhs,
803*38fd1498Szrj	       const list<_Tp, _Alloc>& __rhs)
804*38fd1498Szrj    { return __lhs._M_base() <= __rhs._M_base(); }
805*38fd1498Szrj
806*38fd1498Szrj  template<typename _Tp, typename _Alloc>
807*38fd1498Szrj    inline bool
808*38fd1498Szrj    operator>=(const list<_Tp, _Alloc>& __lhs,
809*38fd1498Szrj	       const list<_Tp, _Alloc>& __rhs)
810*38fd1498Szrj    { return __lhs._M_base() >= __rhs._M_base(); }
811*38fd1498Szrj
812*38fd1498Szrj  template<typename _Tp, typename _Alloc>
813*38fd1498Szrj    inline bool
814*38fd1498Szrj    operator>(const list<_Tp, _Alloc>& __lhs,
815*38fd1498Szrj	      const list<_Tp, _Alloc>& __rhs)
816*38fd1498Szrj    { return __lhs._M_base() > __rhs._M_base(); }
817*38fd1498Szrj
818*38fd1498Szrj  template<typename _Tp, typename _Alloc>
819*38fd1498Szrj    inline void
820*38fd1498Szrj    swap(list<_Tp, _Alloc>& __lhs, list<_Tp, _Alloc>& __rhs)
821*38fd1498Szrj    _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
822*38fd1498Szrj    { __lhs.swap(__rhs); }
823*38fd1498Szrj
824*38fd1498Szrj} // namespace __debug
825*38fd1498Szrj} // namespace std
826*38fd1498Szrj
827*38fd1498Szrjnamespace __gnu_debug
828*38fd1498Szrj{
829*38fd1498Szrj#ifndef _GLIBCXX_USE_CXX11_ABI
830*38fd1498Szrj  // If not using C++11 list::size() is not in O(1) so we do not use it.
831*38fd1498Szrj  template<typename _Tp, typename _Alloc>
832*38fd1498Szrj    struct _Sequence_traits<std::__debug::list<_Tp, _Alloc> >
833*38fd1498Szrj    {
834*38fd1498Szrj      typedef typename std::__debug::list<_Tp, _Alloc>::iterator _It;
835*38fd1498Szrj
836*38fd1498Szrj      static typename _Distance_traits<_It>::__type
837*38fd1498Szrj      _S_size(const std::__debug::list<_Tp, _Alloc>& __seq)
838*38fd1498Szrj      {
839*38fd1498Szrj	return __seq.empty()
840*38fd1498Szrj	  ? std::make_pair(0, __dp_exact) : std::make_pair(1, __dp_equality);
841*38fd1498Szrj      }
842*38fd1498Szrj    };
843*38fd1498Szrj#endif
844*38fd1498Szrj
845*38fd1498Szrj#ifndef _GLIBCXX_DEBUG_PEDANTIC
846*38fd1498Szrj  template<class _Tp, class _Alloc>
847*38fd1498Szrj    struct _Insert_range_from_self_is_safe<std::__debug::list<_Tp, _Alloc> >
848*38fd1498Szrj    { enum { __value = 1 }; };
849*38fd1498Szrj#endif
850*38fd1498Szrj}
851*38fd1498Szrj
852*38fd1498Szrj#endif
853