xref: /NextBSD/contrib/libstdc++/include/bits/stl_deque.h (revision eb1a5f8de9f7ea602c373a710f531abbf81141c4)
1 // Deque implementation -*- C++ -*-
2 
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11 
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING.  If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
21 
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction.  Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License.  This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30 
31 /*
32  *
33  * Copyright (c) 1994
34  * Hewlett-Packard Company
35  *
36  * Permission to use, copy, modify, distribute and sell this software
37  * and its documentation for any purpose is hereby granted without fee,
38  * provided that the above copyright notice appear in all copies and
39  * that both that copyright notice and this permission notice appear
40  * in supporting documentation.  Hewlett-Packard Company makes no
41  * representations about the suitability of this software for any
42  * purpose.  It is provided "as is" without express or implied warranty.
43  *
44  *
45  * Copyright (c) 1997
46  * Silicon Graphics Computer Systems, Inc.
47  *
48  * Permission to use, copy, modify, distribute and sell this software
49  * and its documentation for any purpose is hereby granted without fee,
50  * provided that the above copyright notice appear in all copies and
51  * that both that copyright notice and this permission notice appear
52  * in supporting documentation.  Silicon Graphics makes no
53  * representations about the suitability of this software for any
54  * purpose.  It is provided "as is" without express or implied warranty.
55  */
56 
57 /** @file stl_deque.h
58  *  This is an internal header file, included by other library headers.
59  *  You should not attempt to use it directly.
60  */
61 
62 #ifndef _DEQUE_H
63 #define _DEQUE_H 1
64 
65 #include <bits/concept_check.h>
66 #include <bits/stl_iterator_base_types.h>
67 #include <bits/stl_iterator_base_funcs.h>
68 
_GLIBCXX_BEGIN_NESTED_NAMESPACE(std,_GLIBCXX_STD)69 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD)
70 
71   /**
72    *  @if maint
73    *  @brief This function controls the size of memory nodes.
74    *  @param  size  The size of an element.
75    *  @return   The number (not byte size) of elements per node.
76    *
77    *  This function started off as a compiler kludge from SGI, but seems to
78    *  be a useful wrapper around a repeated constant expression.  The '512' is
79    *  tuneable (and no other code needs to change), but no investigation has
80    *  been done since inheriting the SGI code.
81    *  @endif
82   */
83   inline size_t
84   __deque_buf_size(size_t __size)
85   { return __size < 512 ? size_t(512 / __size) : size_t(1); }
86 
87 
88   /**
89    *  @brief A deque::iterator.
90    *
91    *  Quite a bit of intelligence here.  Much of the functionality of
92    *  deque is actually passed off to this class.  A deque holds two
93    *  of these internally, marking its valid range.  Access to
94    *  elements is done as offsets of either of those two, relying on
95    *  operator overloading in this class.
96    *
97    *  @if maint
98    *  All the functions are op overloads except for _M_set_node.
99    *  @endif
100   */
101   template<typename _Tp, typename _Ref, typename _Ptr>
102     struct _Deque_iterator
103     {
104       typedef _Deque_iterator<_Tp, _Tp&, _Tp*>             iterator;
105       typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
106 
_S_buffer_size_Deque_iterator107       static size_t _S_buffer_size()
108       { return __deque_buf_size(sizeof(_Tp)); }
109 
110       typedef std::random_access_iterator_tag iterator_category;
111       typedef _Tp                             value_type;
112       typedef _Ptr                            pointer;
113       typedef _Ref                            reference;
114       typedef size_t                          size_type;
115       typedef ptrdiff_t                       difference_type;
116       typedef _Tp**                           _Map_pointer;
117       typedef _Deque_iterator                 _Self;
118 
119       _Tp* _M_cur;
120       _Tp* _M_first;
121       _Tp* _M_last;
122       _Map_pointer _M_node;
123 
_Deque_iterator_Deque_iterator124       _Deque_iterator(_Tp* __x, _Map_pointer __y)
125       : _M_cur(__x), _M_first(*__y),
126         _M_last(*__y + _S_buffer_size()), _M_node(__y) {}
127 
_Deque_iterator_Deque_iterator128       _Deque_iterator() : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) {}
129 
_Deque_iterator_Deque_iterator130       _Deque_iterator(const iterator& __x)
131       : _M_cur(__x._M_cur), _M_first(__x._M_first),
132         _M_last(__x._M_last), _M_node(__x._M_node) {}
133 
134       reference
135       operator*() const
136       { return *_M_cur; }
137 
138       pointer
139       operator->() const
140       { return _M_cur; }
141 
142       _Self&
143       operator++()
144       {
145 	++_M_cur;
146 	if (_M_cur == _M_last)
147 	  {
148 	    _M_set_node(_M_node + 1);
149 	    _M_cur = _M_first;
150 	  }
151 	return *this;
152       }
153 
154       _Self
155       operator++(int)
156       {
157 	_Self __tmp = *this;
158 	++*this;
159 	return __tmp;
160       }
161 
162       _Self&
163       operator--()
164       {
165 	if (_M_cur == _M_first)
166 	  {
167 	    _M_set_node(_M_node - 1);
168 	    _M_cur = _M_last;
169 	  }
170 	--_M_cur;
171 	return *this;
172       }
173 
174       _Self
175       operator--(int)
176       {
177 	_Self __tmp = *this;
178 	--*this;
179 	return __tmp;
180       }
181 
182       _Self&
183       operator+=(difference_type __n)
184       {
185 	const difference_type __offset = __n + (_M_cur - _M_first);
186 	if (__offset >= 0 && __offset < difference_type(_S_buffer_size()))
187 	  _M_cur += __n;
188 	else
189 	  {
190 	    const difference_type __node_offset =
191 	      __offset > 0 ? __offset / difference_type(_S_buffer_size())
192 	                   : -difference_type((-__offset - 1)
193 					      / _S_buffer_size()) - 1;
194 	    _M_set_node(_M_node + __node_offset);
195 	    _M_cur = _M_first + (__offset - __node_offset
196 				 * difference_type(_S_buffer_size()));
197 	  }
198 	return *this;
199       }
200 
201       _Self
202       operator+(difference_type __n) const
203       {
204 	_Self __tmp = *this;
205 	return __tmp += __n;
206       }
207 
208       _Self&
209       operator-=(difference_type __n)
210       { return *this += -__n; }
211 
212       _Self
213       operator-(difference_type __n) const
214       {
215 	_Self __tmp = *this;
216 	return __tmp -= __n;
217       }
218 
219       reference
220       operator[](difference_type __n) const
221       { return *(*this + __n); }
222 
223       /** @if maint
224        *  Prepares to traverse new_node.  Sets everything except
225        *  _M_cur, which should therefore be set by the caller
226        *  immediately afterwards, based on _M_first and _M_last.
227        *  @endif
228        */
229       void
_M_set_node_Deque_iterator230       _M_set_node(_Map_pointer __new_node)
231       {
232 	_M_node = __new_node;
233 	_M_first = *__new_node;
234 	_M_last = _M_first + difference_type(_S_buffer_size());
235       }
236     };
237 
238   // Note: we also provide overloads whose operands are of the same type in
239   // order to avoid ambiguous overload resolution when std::rel_ops operators
240   // are in scope (for additional details, see libstdc++/3628)
241   template<typename _Tp, typename _Ref, typename _Ptr>
242     inline bool
243     operator==(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
244 	       const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
245     { return __x._M_cur == __y._M_cur; }
246 
247   template<typename _Tp, typename _RefL, typename _PtrL,
248 	   typename _RefR, typename _PtrR>
249     inline bool
250     operator==(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
251 	       const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
252     { return __x._M_cur == __y._M_cur; }
253 
254   template<typename _Tp, typename _Ref, typename _Ptr>
255     inline bool
256     operator!=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
257 	       const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
258     { return !(__x == __y); }
259 
260   template<typename _Tp, typename _RefL, typename _PtrL,
261 	   typename _RefR, typename _PtrR>
262     inline bool
263     operator!=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
264 	       const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
265     { return !(__x == __y); }
266 
267   template<typename _Tp, typename _Ref, typename _Ptr>
268     inline bool
269     operator<(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
270 	      const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
271     { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
272                                           : (__x._M_node < __y._M_node); }
273 
274   template<typename _Tp, typename _RefL, typename _PtrL,
275 	   typename _RefR, typename _PtrR>
276     inline bool
277     operator<(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
278 	      const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
279     { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
280 	                                  : (__x._M_node < __y._M_node); }
281 
282   template<typename _Tp, typename _Ref, typename _Ptr>
283     inline bool
284     operator>(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
285 	      const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
286     { return __y < __x; }
287 
288   template<typename _Tp, typename _RefL, typename _PtrL,
289 	   typename _RefR, typename _PtrR>
290     inline bool
291     operator>(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
292 	      const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
293     { return __y < __x; }
294 
295   template<typename _Tp, typename _Ref, typename _Ptr>
296     inline bool
297     operator<=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
298 	       const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
299     { return !(__y < __x); }
300 
301   template<typename _Tp, typename _RefL, typename _PtrL,
302 	   typename _RefR, typename _PtrR>
303     inline bool
304     operator<=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
305 	       const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
306     { return !(__y < __x); }
307 
308   template<typename _Tp, typename _Ref, typename _Ptr>
309     inline bool
310     operator>=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
311 	       const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
312     { return !(__x < __y); }
313 
314   template<typename _Tp, typename _RefL, typename _PtrL,
315 	   typename _RefR, typename _PtrR>
316     inline bool
317     operator>=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
318 	       const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
319     { return !(__x < __y); }
320 
321   // _GLIBCXX_RESOLVE_LIB_DEFECTS
322   // According to the resolution of DR179 not only the various comparison
323   // operators but also operator- must accept mixed iterator/const_iterator
324   // parameters.
325   template<typename _Tp, typename _Ref, typename _Ptr>
326     inline typename _Deque_iterator<_Tp, _Ref, _Ptr>::difference_type
327     operator-(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
328 	      const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
329     {
330       return typename _Deque_iterator<_Tp, _Ref, _Ptr>::difference_type
331 	(_Deque_iterator<_Tp, _Ref, _Ptr>::_S_buffer_size())
332 	* (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
333 	+ (__y._M_last - __y._M_cur);
334     }
335 
336   template<typename _Tp, typename _RefL, typename _PtrL,
337 	   typename _RefR, typename _PtrR>
338     inline typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
339     operator-(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
340 	      const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
341     {
342       return typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
343 	(_Deque_iterator<_Tp, _RefL, _PtrL>::_S_buffer_size())
344 	* (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
345 	+ (__y._M_last - __y._M_cur);
346     }
347 
348   template<typename _Tp, typename _Ref, typename _Ptr>
349     inline _Deque_iterator<_Tp, _Ref, _Ptr>
350     operator+(ptrdiff_t __n, const _Deque_iterator<_Tp, _Ref, _Ptr>& __x)
351     { return __x + __n; }
352 
353   template<typename _Tp>
354     void
355     fill(const _Deque_iterator<_Tp, _Tp&, _Tp*>& __first,
356 	 const _Deque_iterator<_Tp, _Tp&, _Tp*>& __last, const _Tp& __value);
357 
358   /**
359    *  @if maint
360    *  Deque base class.  This class provides the unified face for %deque's
361    *  allocation.  This class's constructor and destructor allocate and
362    *  deallocate (but do not initialize) storage.  This makes %exception
363    *  safety easier.
364    *
365    *  Nothing in this class ever constructs or destroys an actual Tp element.
366    *  (Deque handles that itself.)  Only/All memory management is performed
367    *  here.
368    *  @endif
369   */
370   template<typename _Tp, typename _Alloc>
371     class _Deque_base
372     {
373     public:
374       typedef _Alloc                  allocator_type;
375 
376       allocator_type
get_allocator()377       get_allocator() const
378       { return allocator_type(_M_get_Tp_allocator()); }
379 
380       typedef _Deque_iterator<_Tp, _Tp&, _Tp*>             iterator;
381       typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
382 
_Deque_base()383       _Deque_base()
384       : _M_impl()
385       { _M_initialize_map(0); }
386 
_Deque_base(const allocator_type & __a,size_t __num_elements)387       _Deque_base(const allocator_type& __a, size_t __num_elements)
388       : _M_impl(__a)
389       { _M_initialize_map(__num_elements); }
390 
_Deque_base(const allocator_type & __a)391       _Deque_base(const allocator_type& __a)
392       : _M_impl(__a)
393       { }
394 
395       ~_Deque_base();
396 
397     protected:
398       //This struct encapsulates the implementation of the std::deque
399       //standard container and at the same time makes use of the EBO
400       //for empty allocators.
401       typedef typename _Alloc::template rebind<_Tp*>::other _Map_alloc_type;
402 
403       typedef typename _Alloc::template rebind<_Tp>::other  _Tp_alloc_type;
404 
405       struct _Deque_impl
406       : public _Tp_alloc_type
407       {
408 	_Tp** _M_map;
409 	size_t _M_map_size;
410 	iterator _M_start;
411 	iterator _M_finish;
412 
_Deque_impl_Deque_impl413 	_Deque_impl()
414 	: _Tp_alloc_type(), _M_map(0), _M_map_size(0),
415 	  _M_start(), _M_finish()
416 	{ }
417 
_Deque_impl_Deque_impl418 	_Deque_impl(const _Tp_alloc_type& __a)
419 	: _Tp_alloc_type(__a), _M_map(0), _M_map_size(0),
420 	  _M_start(), _M_finish()
421 	{ }
422       };
423 
424       _Tp_alloc_type&
_M_get_Tp_allocator()425       _M_get_Tp_allocator()
426       { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); }
427 
428       const _Tp_alloc_type&
_M_get_Tp_allocator()429       _M_get_Tp_allocator() const
430       { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
431 
432       _Map_alloc_type
_M_get_map_allocator()433       _M_get_map_allocator() const
434       { return _Map_alloc_type(_M_get_Tp_allocator()); }
435 
436       _Tp*
_M_allocate_node()437       _M_allocate_node()
438       {
439 	return _M_impl._Tp_alloc_type::allocate(__deque_buf_size(sizeof(_Tp)));
440       }
441 
442       void
_M_deallocate_node(_Tp * __p)443       _M_deallocate_node(_Tp* __p)
444       {
445 	_M_impl._Tp_alloc_type::deallocate(__p, __deque_buf_size(sizeof(_Tp)));
446       }
447 
448       _Tp**
_M_allocate_map(size_t __n)449       _M_allocate_map(size_t __n)
450       { return _M_get_map_allocator().allocate(__n); }
451 
452       void
_M_deallocate_map(_Tp ** __p,size_t __n)453       _M_deallocate_map(_Tp** __p, size_t __n)
454       { _M_get_map_allocator().deallocate(__p, __n); }
455 
456     protected:
457       void _M_initialize_map(size_t);
458       void _M_create_nodes(_Tp** __nstart, _Tp** __nfinish);
459       void _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish);
460       enum { _S_initial_map_size = 8 };
461 
462       _Deque_impl _M_impl;
463     };
464 
465   template<typename _Tp, typename _Alloc>
466     _Deque_base<_Tp, _Alloc>::
~_Deque_base()467     ~_Deque_base()
468     {
469       if (this->_M_impl._M_map)
470 	{
471 	  _M_destroy_nodes(this->_M_impl._M_start._M_node,
472 			   this->_M_impl._M_finish._M_node + 1);
473 	  _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
474 	}
475     }
476 
477   /**
478    *  @if maint
479    *  @brief Layout storage.
480    *  @param  num_elements  The count of T's for which to allocate space
481    *                        at first.
482    *  @return   Nothing.
483    *
484    *  The initial underlying memory layout is a bit complicated...
485    *  @endif
486   */
487   template<typename _Tp, typename _Alloc>
488     void
489     _Deque_base<_Tp, _Alloc>::
_M_initialize_map(size_t __num_elements)490     _M_initialize_map(size_t __num_elements)
491     {
492       const size_t __num_nodes = (__num_elements/ __deque_buf_size(sizeof(_Tp))
493 				  + 1);
494 
495       this->_M_impl._M_map_size = std::max((size_t) _S_initial_map_size,
496 					   size_t(__num_nodes + 2));
497       this->_M_impl._M_map = _M_allocate_map(this->_M_impl._M_map_size);
498 
499       // For "small" maps (needing less than _M_map_size nodes), allocation
500       // starts in the middle elements and grows outwards.  So nstart may be
501       // the beginning of _M_map, but for small maps it may be as far in as
502       // _M_map+3.
503 
504       _Tp** __nstart = (this->_M_impl._M_map
505 			+ (this->_M_impl._M_map_size - __num_nodes) / 2);
506       _Tp** __nfinish = __nstart + __num_nodes;
507 
508       try
509 	{ _M_create_nodes(__nstart, __nfinish); }
510       catch(...)
511 	{
512 	  _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
513 	  this->_M_impl._M_map = 0;
514 	  this->_M_impl._M_map_size = 0;
515 	  __throw_exception_again;
516 	}
517 
518       this->_M_impl._M_start._M_set_node(__nstart);
519       this->_M_impl._M_finish._M_set_node(__nfinish - 1);
520       this->_M_impl._M_start._M_cur = _M_impl._M_start._M_first;
521       this->_M_impl._M_finish._M_cur = (this->_M_impl._M_finish._M_first
522 					+ __num_elements
523 					% __deque_buf_size(sizeof(_Tp)));
524     }
525 
526   template<typename _Tp, typename _Alloc>
527     void
528     _Deque_base<_Tp, _Alloc>::
_M_create_nodes(_Tp ** __nstart,_Tp ** __nfinish)529     _M_create_nodes(_Tp** __nstart, _Tp** __nfinish)
530     {
531       _Tp** __cur;
532       try
533 	{
534 	  for (__cur = __nstart; __cur < __nfinish; ++__cur)
535 	    *__cur = this->_M_allocate_node();
536 	}
537       catch(...)
538 	{
539 	  _M_destroy_nodes(__nstart, __cur);
540 	  __throw_exception_again;
541 	}
542     }
543 
544   template<typename _Tp, typename _Alloc>
545     void
546     _Deque_base<_Tp, _Alloc>::
_M_destroy_nodes(_Tp ** __nstart,_Tp ** __nfinish)547     _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish)
548     {
549       for (_Tp** __n = __nstart; __n < __nfinish; ++__n)
550 	_M_deallocate_node(*__n);
551     }
552 
553   /**
554    *  @brief  A standard container using fixed-size memory allocation and
555    *  constant-time manipulation of elements at either end.
556    *
557    *  @ingroup Containers
558    *  @ingroup Sequences
559    *
560    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
561    *  <a href="tables.html#66">reversible container</a>, and a
562    *  <a href="tables.html#67">sequence</a>, including the
563    *  <a href="tables.html#68">optional sequence requirements</a>.
564    *
565    *  In previous HP/SGI versions of deque, there was an extra template
566    *  parameter so users could control the node size.  This extension turned
567    *  out to violate the C++ standard (it can be detected using template
568    *  template parameters), and it was removed.
569    *
570    *  @if maint
571    *  Here's how a deque<Tp> manages memory.  Each deque has 4 members:
572    *
573    *  - Tp**        _M_map
574    *  - size_t      _M_map_size
575    *  - iterator    _M_start, _M_finish
576    *
577    *  map_size is at least 8.  %map is an array of map_size
578    *  pointers-to-"nodes".  (The name %map has nothing to do with the
579    *  std::map class, and "nodes" should not be confused with
580    *  std::list's usage of "node".)
581    *
582    *  A "node" has no specific type name as such, but it is referred
583    *  to as "node" in this file.  It is a simple array-of-Tp.  If Tp
584    *  is very large, there will be one Tp element per node (i.e., an
585    *  "array" of one).  For non-huge Tp's, node size is inversely
586    *  related to Tp size: the larger the Tp, the fewer Tp's will fit
587    *  in a node.  The goal here is to keep the total size of a node
588    *  relatively small and constant over different Tp's, to improve
589    *  allocator efficiency.
590    *
591    *  Not every pointer in the %map array will point to a node.  If
592    *  the initial number of elements in the deque is small, the
593    *  /middle/ %map pointers will be valid, and the ones at the edges
594    *  will be unused.  This same situation will arise as the %map
595    *  grows: available %map pointers, if any, will be on the ends.  As
596    *  new nodes are created, only a subset of the %map's pointers need
597    *  to be copied "outward".
598    *
599    *  Class invariants:
600    * - For any nonsingular iterator i:
601    *    - i.node points to a member of the %map array.  (Yes, you read that
602    *      correctly:  i.node does not actually point to a node.)  The member of
603    *      the %map array is what actually points to the node.
604    *    - i.first == *(i.node)    (This points to the node (first Tp element).)
605    *    - i.last  == i.first + node_size
606    *    - i.cur is a pointer in the range [i.first, i.last).  NOTE:
607    *      the implication of this is that i.cur is always a dereferenceable
608    *      pointer, even if i is a past-the-end iterator.
609    * - Start and Finish are always nonsingular iterators.  NOTE: this
610    * means that an empty deque must have one node, a deque with <N
611    * elements (where N is the node buffer size) must have one node, a
612    * deque with N through (2N-1) elements must have two nodes, etc.
613    * - For every node other than start.node and finish.node, every
614    * element in the node is an initialized object.  If start.node ==
615    * finish.node, then [start.cur, finish.cur) are initialized
616    * objects, and the elements outside that range are uninitialized
617    * storage.  Otherwise, [start.cur, start.last) and [finish.first,
618    * finish.cur) are initialized objects, and [start.first, start.cur)
619    * and [finish.cur, finish.last) are uninitialized storage.
620    * - [%map, %map + map_size) is a valid, non-empty range.
621    * - [start.node, finish.node] is a valid range contained within
622    *   [%map, %map + map_size).
623    * - A pointer in the range [%map, %map + map_size) points to an allocated
624    *   node if and only if the pointer is in the range
625    *   [start.node, finish.node].
626    *
627    *  Here's the magic:  nothing in deque is "aware" of the discontiguous
628    *  storage!
629    *
630    *  The memory setup and layout occurs in the parent, _Base, and the iterator
631    *  class is entirely responsible for "leaping" from one node to the next.
632    *  All the implementation routines for deque itself work only through the
633    *  start and finish iterators.  This keeps the routines simple and sane,
634    *  and we can use other standard algorithms as well.
635    *  @endif
636   */
637   template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
638     class deque : protected _Deque_base<_Tp, _Alloc>
639     {
640       // concept requirements
641       typedef typename _Alloc::value_type        _Alloc_value_type;
642       __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
643       __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
644 
645       typedef _Deque_base<_Tp, _Alloc>           _Base;
646       typedef typename _Base::_Tp_alloc_type	 _Tp_alloc_type;
647 
648     public:
649       typedef _Tp                                        value_type;
650       typedef typename _Tp_alloc_type::pointer           pointer;
651       typedef typename _Tp_alloc_type::const_pointer     const_pointer;
652       typedef typename _Tp_alloc_type::reference         reference;
653       typedef typename _Tp_alloc_type::const_reference   const_reference;
654       typedef typename _Base::iterator                   iterator;
655       typedef typename _Base::const_iterator             const_iterator;
656       typedef std::reverse_iterator<const_iterator>      const_reverse_iterator;
657       typedef std::reverse_iterator<iterator>            reverse_iterator;
658       typedef size_t                             size_type;
659       typedef ptrdiff_t                          difference_type;
660       typedef _Alloc                             allocator_type;
661 
662     protected:
663       typedef pointer*                           _Map_pointer;
664 
_S_buffer_size()665       static size_t _S_buffer_size()
666       { return __deque_buf_size(sizeof(_Tp)); }
667 
668       // Functions controlling memory layout, and nothing else.
669       using _Base::_M_initialize_map;
670       using _Base::_M_create_nodes;
671       using _Base::_M_destroy_nodes;
672       using _Base::_M_allocate_node;
673       using _Base::_M_deallocate_node;
674       using _Base::_M_allocate_map;
675       using _Base::_M_deallocate_map;
676       using _Base::_M_get_Tp_allocator;
677 
678       /** @if maint
679        *  A total of four data members accumulated down the heirarchy.
680        *  May be accessed via _M_impl.*
681        *  @endif
682        */
683       using _Base::_M_impl;
684 
685     public:
686       // [23.2.1.1] construct/copy/destroy
687       // (assign() and get_allocator() are also listed in this section)
688       /**
689        *  @brief  Default constructor creates no elements.
690        */
deque()691       deque()
692       : _Base() { }
693 
694       explicit
deque(const allocator_type & __a)695       deque(const allocator_type& __a)
696       : _Base(__a, 0) {}
697 
698       /**
699        *  @brief  Create a %deque with copies of an exemplar element.
700        *  @param  n  The number of elements to initially create.
701        *  @param  value  An element to copy.
702        *
703        *  This constructor fills the %deque with @a n copies of @a value.
704        */
705       explicit
706       deque(size_type __n, const value_type& __value = value_type(),
707 	    const allocator_type& __a = allocator_type())
_Base(__a,__n)708       : _Base(__a, __n)
709       { _M_fill_initialize(__value); }
710 
711       /**
712        *  @brief  %Deque copy constructor.
713        *  @param  x  A %deque of identical element and allocator types.
714        *
715        *  The newly-created %deque uses a copy of the allocation object used
716        *  by @a x.
717        */
deque(const deque & __x)718       deque(const deque& __x)
719       : _Base(__x._M_get_Tp_allocator(), __x.size())
720       { std::__uninitialized_copy_a(__x.begin(), __x.end(),
721 				    this->_M_impl._M_start,
722 				    _M_get_Tp_allocator()); }
723 
724       /**
725        *  @brief  Builds a %deque from a range.
726        *  @param  first  An input iterator.
727        *  @param  last  An input iterator.
728        *
729        *  Create a %deque consisting of copies of the elements from [first,
730        *  last).
731        *
732        *  If the iterators are forward, bidirectional, or random-access, then
733        *  this will call the elements' copy constructor N times (where N is
734        *  distance(first,last)) and do no memory reallocation.  But if only
735        *  input iterators are used, then this will do at most 2N calls to the
736        *  copy constructor, and logN memory reallocations.
737        */
738       template<typename _InputIterator>
739         deque(_InputIterator __first, _InputIterator __last,
740 	      const allocator_type& __a = allocator_type())
_Base(__a)741 	: _Base(__a)
742         {
743 	  // Check whether it's an integral type.  If so, it's not an iterator.
744 	  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
745 	  _M_initialize_dispatch(__first, __last, _Integral());
746 	}
747 
748       /**
749        *  The dtor only erases the elements, and note that if the elements
750        *  themselves are pointers, the pointed-to memory is not touched in any
751        *  way.  Managing the pointer is the user's responsibilty.
752        */
~deque()753       ~deque()
754       { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
755 
756       /**
757        *  @brief  %Deque assignment operator.
758        *  @param  x  A %deque of identical element and allocator types.
759        *
760        *  All the elements of @a x are copied, but unlike the copy constructor,
761        *  the allocator object is not copied.
762        */
763       deque&
764       operator=(const deque& __x);
765 
766       /**
767        *  @brief  Assigns a given value to a %deque.
768        *  @param  n  Number of elements to be assigned.
769        *  @param  val  Value to be assigned.
770        *
771        *  This function fills a %deque with @a n copies of the given
772        *  value.  Note that the assignment completely changes the
773        *  %deque and that the resulting %deque's size is the same as
774        *  the number of elements assigned.  Old data may be lost.
775        */
776       void
assign(size_type __n,const value_type & __val)777       assign(size_type __n, const value_type& __val)
778       { _M_fill_assign(__n, __val); }
779 
780       /**
781        *  @brief  Assigns a range to a %deque.
782        *  @param  first  An input iterator.
783        *  @param  last   An input iterator.
784        *
785        *  This function fills a %deque with copies of the elements in the
786        *  range [first,last).
787        *
788        *  Note that the assignment completely changes the %deque and that the
789        *  resulting %deque's size is the same as the number of elements
790        *  assigned.  Old data may be lost.
791        */
792       template<typename _InputIterator>
793         void
assign(_InputIterator __first,_InputIterator __last)794         assign(_InputIterator __first, _InputIterator __last)
795         {
796 	  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
797 	  _M_assign_dispatch(__first, __last, _Integral());
798 	}
799 
800       /// Get a copy of the memory allocation object.
801       allocator_type
get_allocator()802       get_allocator() const
803       { return _Base::get_allocator(); }
804 
805       // iterators
806       /**
807        *  Returns a read/write iterator that points to the first element in the
808        *  %deque.  Iteration is done in ordinary element order.
809        */
810       iterator
begin()811       begin()
812       { return this->_M_impl._M_start; }
813 
814       /**
815        *  Returns a read-only (constant) iterator that points to the first
816        *  element in the %deque.  Iteration is done in ordinary element order.
817        */
818       const_iterator
begin()819       begin() const
820       { return this->_M_impl._M_start; }
821 
822       /**
823        *  Returns a read/write iterator that points one past the last
824        *  element in the %deque.  Iteration is done in ordinary
825        *  element order.
826        */
827       iterator
end()828       end()
829       { return this->_M_impl._M_finish; }
830 
831       /**
832        *  Returns a read-only (constant) iterator that points one past
833        *  the last element in the %deque.  Iteration is done in
834        *  ordinary element order.
835        */
836       const_iterator
end()837       end() const
838       { return this->_M_impl._M_finish; }
839 
840       /**
841        *  Returns a read/write reverse iterator that points to the
842        *  last element in the %deque.  Iteration is done in reverse
843        *  element order.
844        */
845       reverse_iterator
rbegin()846       rbegin()
847       { return reverse_iterator(this->_M_impl._M_finish); }
848 
849       /**
850        *  Returns a read-only (constant) reverse iterator that points
851        *  to the last element in the %deque.  Iteration is done in
852        *  reverse element order.
853        */
854       const_reverse_iterator
rbegin()855       rbegin() const
856       { return const_reverse_iterator(this->_M_impl._M_finish); }
857 
858       /**
859        *  Returns a read/write reverse iterator that points to one
860        *  before the first element in the %deque.  Iteration is done
861        *  in reverse element order.
862        */
863       reverse_iterator
rend()864       rend()
865       { return reverse_iterator(this->_M_impl._M_start); }
866 
867       /**
868        *  Returns a read-only (constant) reverse iterator that points
869        *  to one before the first element in the %deque.  Iteration is
870        *  done in reverse element order.
871        */
872       const_reverse_iterator
rend()873       rend() const
874       { return const_reverse_iterator(this->_M_impl._M_start); }
875 
876       // [23.2.1.2] capacity
877       /**  Returns the number of elements in the %deque.  */
878       size_type
size()879       size() const
880       { return this->_M_impl._M_finish - this->_M_impl._M_start; }
881 
882       /**  Returns the size() of the largest possible %deque.  */
883       size_type
max_size()884       max_size() const
885       { return _M_get_Tp_allocator().max_size(); }
886 
887       /**
888        *  @brief  Resizes the %deque to the specified number of elements.
889        *  @param  new_size  Number of elements the %deque should contain.
890        *  @param  x  Data with which new elements should be populated.
891        *
892        *  This function will %resize the %deque to the specified
893        *  number of elements.  If the number is smaller than the
894        *  %deque's current size the %deque is truncated, otherwise the
895        *  %deque is extended and new elements are populated with given
896        *  data.
897        */
898       void
899       resize(size_type __new_size, value_type __x = value_type())
900       {
901 	const size_type __len = size();
902 	if (__new_size < __len)
903 	  _M_erase_at_end(this->_M_impl._M_start + difference_type(__new_size));
904 	else
905 	  insert(this->_M_impl._M_finish, __new_size - __len, __x);
906       }
907 
908       /**
909        *  Returns true if the %deque is empty.  (Thus begin() would
910        *  equal end().)
911        */
912       bool
empty()913       empty() const
914       { return this->_M_impl._M_finish == this->_M_impl._M_start; }
915 
916       // element access
917       /**
918        *  @brief Subscript access to the data contained in the %deque.
919        *  @param n The index of the element for which data should be
920        *  accessed.
921        *  @return  Read/write reference to data.
922        *
923        *  This operator allows for easy, array-style, data access.
924        *  Note that data access with this operator is unchecked and
925        *  out_of_range lookups are not defined. (For checked lookups
926        *  see at().)
927        */
928       reference
929       operator[](size_type __n)
930       { return this->_M_impl._M_start[difference_type(__n)]; }
931 
932       /**
933        *  @brief Subscript access to the data contained in the %deque.
934        *  @param n The index of the element for which data should be
935        *  accessed.
936        *  @return  Read-only (constant) reference to data.
937        *
938        *  This operator allows for easy, array-style, data access.
939        *  Note that data access with this operator is unchecked and
940        *  out_of_range lookups are not defined. (For checked lookups
941        *  see at().)
942        */
943       const_reference
944       operator[](size_type __n) const
945       { return this->_M_impl._M_start[difference_type(__n)]; }
946 
947     protected:
948       /// @if maint Safety check used only from at().  @endif
949       void
_M_range_check(size_type __n)950       _M_range_check(size_type __n) const
951       {
952 	if (__n >= this->size())
953 	  __throw_out_of_range(__N("deque::_M_range_check"));
954       }
955 
956     public:
957       /**
958        *  @brief  Provides access to the data contained in the %deque.
959        *  @param n The index of the element for which data should be
960        *  accessed.
961        *  @return  Read/write reference to data.
962        *  @throw  std::out_of_range  If @a n is an invalid index.
963        *
964        *  This function provides for safer data access.  The parameter
965        *  is first checked that it is in the range of the deque.  The
966        *  function throws out_of_range if the check fails.
967        */
968       reference
at(size_type __n)969       at(size_type __n)
970       {
971 	_M_range_check(__n);
972 	return (*this)[__n];
973       }
974 
975       /**
976        *  @brief  Provides access to the data contained in the %deque.
977        *  @param n The index of the element for which data should be
978        *  accessed.
979        *  @return  Read-only (constant) reference to data.
980        *  @throw  std::out_of_range  If @a n is an invalid index.
981        *
982        *  This function provides for safer data access.  The parameter is first
983        *  checked that it is in the range of the deque.  The function throws
984        *  out_of_range if the check fails.
985        */
986       const_reference
at(size_type __n)987       at(size_type __n) const
988       {
989 	_M_range_check(__n);
990 	return (*this)[__n];
991       }
992 
993       /**
994        *  Returns a read/write reference to the data at the first
995        *  element of the %deque.
996        */
997       reference
front()998       front()
999       { return *begin(); }
1000 
1001       /**
1002        *  Returns a read-only (constant) reference to the data at the first
1003        *  element of the %deque.
1004        */
1005       const_reference
front()1006       front() const
1007       { return *begin(); }
1008 
1009       /**
1010        *  Returns a read/write reference to the data at the last element of the
1011        *  %deque.
1012        */
1013       reference
back()1014       back()
1015       {
1016 	iterator __tmp = end();
1017 	--__tmp;
1018 	return *__tmp;
1019       }
1020 
1021       /**
1022        *  Returns a read-only (constant) reference to the data at the last
1023        *  element of the %deque.
1024        */
1025       const_reference
back()1026       back() const
1027       {
1028 	const_iterator __tmp = end();
1029 	--__tmp;
1030 	return *__tmp;
1031       }
1032 
1033       // [23.2.1.2] modifiers
1034       /**
1035        *  @brief  Add data to the front of the %deque.
1036        *  @param  x  Data to be added.
1037        *
1038        *  This is a typical stack operation.  The function creates an
1039        *  element at the front of the %deque and assigns the given
1040        *  data to it.  Due to the nature of a %deque this operation
1041        *  can be done in constant time.
1042        */
1043       void
push_front(const value_type & __x)1044       push_front(const value_type& __x)
1045       {
1046 	if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
1047 	  {
1048 	    this->_M_impl.construct(this->_M_impl._M_start._M_cur - 1, __x);
1049 	    --this->_M_impl._M_start._M_cur;
1050 	  }
1051 	else
1052 	  _M_push_front_aux(__x);
1053       }
1054 
1055       /**
1056        *  @brief  Add data to the end of the %deque.
1057        *  @param  x  Data to be added.
1058        *
1059        *  This is a typical stack operation.  The function creates an
1060        *  element at the end of the %deque and assigns the given data
1061        *  to it.  Due to the nature of a %deque this operation can be
1062        *  done in constant time.
1063        */
1064       void
push_back(const value_type & __x)1065       push_back(const value_type& __x)
1066       {
1067 	if (this->_M_impl._M_finish._M_cur
1068 	    != this->_M_impl._M_finish._M_last - 1)
1069 	  {
1070 	    this->_M_impl.construct(this->_M_impl._M_finish._M_cur, __x);
1071 	    ++this->_M_impl._M_finish._M_cur;
1072 	  }
1073 	else
1074 	  _M_push_back_aux(__x);
1075       }
1076 
1077       /**
1078        *  @brief  Removes first element.
1079        *
1080        *  This is a typical stack operation.  It shrinks the %deque by one.
1081        *
1082        *  Note that no data is returned, and if the first element's data is
1083        *  needed, it should be retrieved before pop_front() is called.
1084        */
1085       void
pop_front()1086       pop_front()
1087       {
1088 	if (this->_M_impl._M_start._M_cur
1089 	    != this->_M_impl._M_start._M_last - 1)
1090 	  {
1091 	    this->_M_impl.destroy(this->_M_impl._M_start._M_cur);
1092 	    ++this->_M_impl._M_start._M_cur;
1093 	  }
1094 	else
1095 	  _M_pop_front_aux();
1096       }
1097 
1098       /**
1099        *  @brief  Removes last element.
1100        *
1101        *  This is a typical stack operation.  It shrinks the %deque by one.
1102        *
1103        *  Note that no data is returned, and if the last element's data is
1104        *  needed, it should be retrieved before pop_back() is called.
1105        */
1106       void
pop_back()1107       pop_back()
1108       {
1109 	if (this->_M_impl._M_finish._M_cur
1110 	    != this->_M_impl._M_finish._M_first)
1111 	  {
1112 	    --this->_M_impl._M_finish._M_cur;
1113 	    this->_M_impl.destroy(this->_M_impl._M_finish._M_cur);
1114 	  }
1115 	else
1116 	  _M_pop_back_aux();
1117       }
1118 
1119       /**
1120        *  @brief  Inserts given value into %deque before specified iterator.
1121        *  @param  position  An iterator into the %deque.
1122        *  @param  x  Data to be inserted.
1123        *  @return  An iterator that points to the inserted data.
1124        *
1125        *  This function will insert a copy of the given value before the
1126        *  specified location.
1127        */
1128       iterator
1129       insert(iterator __position, const value_type& __x);
1130 
1131       /**
1132        *  @brief  Inserts a number of copies of given data into the %deque.
1133        *  @param  position  An iterator into the %deque.
1134        *  @param  n  Number of elements to be inserted.
1135        *  @param  x  Data to be inserted.
1136        *
1137        *  This function will insert a specified number of copies of the given
1138        *  data before the location specified by @a position.
1139        */
1140       void
insert(iterator __position,size_type __n,const value_type & __x)1141       insert(iterator __position, size_type __n, const value_type& __x)
1142       { _M_fill_insert(__position, __n, __x); }
1143 
1144       /**
1145        *  @brief  Inserts a range into the %deque.
1146        *  @param  position  An iterator into the %deque.
1147        *  @param  first  An input iterator.
1148        *  @param  last   An input iterator.
1149        *
1150        *  This function will insert copies of the data in the range
1151        *  [first,last) into the %deque before the location specified
1152        *  by @a pos.  This is known as "range insert."
1153        */
1154       template<typename _InputIterator>
1155         void
insert(iterator __position,_InputIterator __first,_InputIterator __last)1156         insert(iterator __position, _InputIterator __first,
1157 	       _InputIterator __last)
1158         {
1159 	  // Check whether it's an integral type.  If so, it's not an iterator.
1160 	  typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1161 	  _M_insert_dispatch(__position, __first, __last, _Integral());
1162 	}
1163 
1164       /**
1165        *  @brief  Remove element at given position.
1166        *  @param  position  Iterator pointing to element to be erased.
1167        *  @return  An iterator pointing to the next element (or end()).
1168        *
1169        *  This function will erase the element at the given position and thus
1170        *  shorten the %deque by one.
1171        *
1172        *  The user is cautioned that
1173        *  this function only erases the element, and that if the element is
1174        *  itself a pointer, the pointed-to memory is not touched in any way.
1175        *  Managing the pointer is the user's responsibilty.
1176        */
1177       iterator
1178       erase(iterator __position);
1179 
1180       /**
1181        *  @brief  Remove a range of elements.
1182        *  @param  first  Iterator pointing to the first element to be erased.
1183        *  @param  last  Iterator pointing to one past the last element to be
1184        *                erased.
1185        *  @return  An iterator pointing to the element pointed to by @a last
1186        *           prior to erasing (or end()).
1187        *
1188        *  This function will erase the elements in the range [first,last) and
1189        *  shorten the %deque accordingly.
1190        *
1191        *  The user is cautioned that
1192        *  this function only erases the elements, and that if the elements
1193        *  themselves are pointers, the pointed-to memory is not touched in any
1194        *  way.  Managing the pointer is the user's responsibilty.
1195        */
1196       iterator
1197       erase(iterator __first, iterator __last);
1198 
1199       /**
1200        *  @brief  Swaps data with another %deque.
1201        *  @param  x  A %deque of the same element and allocator types.
1202        *
1203        *  This exchanges the elements between two deques in constant time.
1204        *  (Four pointers, so it should be quite fast.)
1205        *  Note that the global std::swap() function is specialized such that
1206        *  std::swap(d1,d2) will feed to this function.
1207        */
1208       void
swap(deque & __x)1209       swap(deque& __x)
1210       {
1211 	std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
1212 	std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
1213 	std::swap(this->_M_impl._M_map, __x._M_impl._M_map);
1214 	std::swap(this->_M_impl._M_map_size, __x._M_impl._M_map_size);
1215 
1216 	// _GLIBCXX_RESOLVE_LIB_DEFECTS
1217 	// 431. Swapping containers with unequal allocators.
1218 	std::__alloc_swap<_Tp_alloc_type>::_S_do_it(_M_get_Tp_allocator(),
1219 						    __x._M_get_Tp_allocator());
1220       }
1221 
1222       /**
1223        *  Erases all the elements.  Note that this function only erases the
1224        *  elements, and that if the elements themselves are pointers, the
1225        *  pointed-to memory is not touched in any way.  Managing the pointer is
1226        *  the user's responsibilty.
1227        */
1228       void
clear()1229       clear()
1230       { _M_erase_at_end(begin()); }
1231 
1232     protected:
1233       // Internal constructor functions follow.
1234 
1235       // called by the range constructor to implement [23.1.1]/9
1236       template<typename _Integer>
1237         void
_M_initialize_dispatch(_Integer __n,_Integer __x,__true_type)1238         _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1239         {
1240 	  _M_initialize_map(__n);
1241 	  _M_fill_initialize(__x);
1242 	}
1243 
1244       // called by the range constructor to implement [23.1.1]/9
1245       template<typename _InputIterator>
1246         void
_M_initialize_dispatch(_InputIterator __first,_InputIterator __last,__false_type)1247         _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1248 			       __false_type)
1249         {
1250 	  typedef typename std::iterator_traits<_InputIterator>::
1251 	    iterator_category _IterCategory;
1252 	  _M_range_initialize(__first, __last, _IterCategory());
1253 	}
1254 
1255       // called by the second initialize_dispatch above
1256       //@{
1257       /**
1258        *  @if maint
1259        *  @brief Fills the deque with whatever is in [first,last).
1260        *  @param  first  An input iterator.
1261        *  @param  last  An input iterator.
1262        *  @return   Nothing.
1263        *
1264        *  If the iterators are actually forward iterators (or better), then the
1265        *  memory layout can be done all at once.  Else we move forward using
1266        *  push_back on each value from the iterator.
1267        *  @endif
1268        */
1269       template<typename _InputIterator>
1270         void
1271         _M_range_initialize(_InputIterator __first, _InputIterator __last,
1272 			    std::input_iterator_tag);
1273 
1274       // called by the second initialize_dispatch above
1275       template<typename _ForwardIterator>
1276         void
1277         _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
1278 			    std::forward_iterator_tag);
1279       //@}
1280 
1281       /**
1282        *  @if maint
1283        *  @brief Fills the %deque with copies of value.
1284        *  @param  value  Initial value.
1285        *  @return   Nothing.
1286        *  @pre _M_start and _M_finish have already been initialized,
1287        *  but none of the %deque's elements have yet been constructed.
1288        *
1289        *  This function is called only when the user provides an explicit size
1290        *  (with or without an explicit exemplar value).
1291        *  @endif
1292        */
1293       void
1294       _M_fill_initialize(const value_type& __value);
1295 
1296       // Internal assign functions follow.  The *_aux functions do the actual
1297       // assignment work for the range versions.
1298 
1299       // called by the range assign to implement [23.1.1]/9
1300       template<typename _Integer>
1301         void
_M_assign_dispatch(_Integer __n,_Integer __val,__true_type)1302         _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1303         {
1304 	  _M_fill_assign(static_cast<size_type>(__n),
1305 			 static_cast<value_type>(__val));
1306 	}
1307 
1308       // called by the range assign to implement [23.1.1]/9
1309       template<typename _InputIterator>
1310         void
_M_assign_dispatch(_InputIterator __first,_InputIterator __last,__false_type)1311         _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1312 			   __false_type)
1313         {
1314 	  typedef typename std::iterator_traits<_InputIterator>::
1315 	    iterator_category _IterCategory;
1316 	  _M_assign_aux(__first, __last, _IterCategory());
1317 	}
1318 
1319       // called by the second assign_dispatch above
1320       template<typename _InputIterator>
1321         void
1322         _M_assign_aux(_InputIterator __first, _InputIterator __last,
1323 		      std::input_iterator_tag);
1324 
1325       // called by the second assign_dispatch above
1326       template<typename _ForwardIterator>
1327         void
_M_assign_aux(_ForwardIterator __first,_ForwardIterator __last,std::forward_iterator_tag)1328         _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1329 		      std::forward_iterator_tag)
1330         {
1331 	  const size_type __len = std::distance(__first, __last);
1332 	  if (__len > size())
1333 	    {
1334 	      _ForwardIterator __mid = __first;
1335 	      std::advance(__mid, size());
1336 	      std::copy(__first, __mid, begin());
1337 	      insert(end(), __mid, __last);
1338 	    }
1339 	  else
1340 	    _M_erase_at_end(std::copy(__first, __last, begin()));
1341 	}
1342 
1343       // Called by assign(n,t), and the range assign when it turns out
1344       // to be the same thing.
1345       void
_M_fill_assign(size_type __n,const value_type & __val)1346       _M_fill_assign(size_type __n, const value_type& __val)
1347       {
1348 	if (__n > size())
1349 	  {
1350 	    std::fill(begin(), end(), __val);
1351 	    insert(end(), __n - size(), __val);
1352 	  }
1353 	else
1354 	  {
1355 	    _M_erase_at_end(begin() + difference_type(__n));
1356 	    std::fill(begin(), end(), __val);
1357 	  }
1358       }
1359 
1360       //@{
1361       /**
1362        *  @if maint
1363        *  @brief Helper functions for push_* and pop_*.
1364        *  @endif
1365        */
1366       void _M_push_back_aux(const value_type&);
1367 
1368       void _M_push_front_aux(const value_type&);
1369 
1370       void _M_pop_back_aux();
1371 
1372       void _M_pop_front_aux();
1373       //@}
1374 
1375       // Internal insert functions follow.  The *_aux functions do the actual
1376       // insertion work when all shortcuts fail.
1377 
1378       // called by the range insert to implement [23.1.1]/9
1379       template<typename _Integer>
1380         void
_M_insert_dispatch(iterator __pos,_Integer __n,_Integer __x,__true_type)1381         _M_insert_dispatch(iterator __pos,
1382 			   _Integer __n, _Integer __x, __true_type)
1383         {
1384 	  _M_fill_insert(__pos, static_cast<size_type>(__n),
1385 			 static_cast<value_type>(__x));
1386 	}
1387 
1388       // called by the range insert to implement [23.1.1]/9
1389       template<typename _InputIterator>
1390         void
_M_insert_dispatch(iterator __pos,_InputIterator __first,_InputIterator __last,__false_type)1391         _M_insert_dispatch(iterator __pos,
1392 			   _InputIterator __first, _InputIterator __last,
1393 			   __false_type)
1394         {
1395 	  typedef typename std::iterator_traits<_InputIterator>::
1396 	    iterator_category _IterCategory;
1397           _M_range_insert_aux(__pos, __first, __last, _IterCategory());
1398 	}
1399 
1400       // called by the second insert_dispatch above
1401       template<typename _InputIterator>
1402         void
1403         _M_range_insert_aux(iterator __pos, _InputIterator __first,
1404 			    _InputIterator __last, std::input_iterator_tag);
1405 
1406       // called by the second insert_dispatch above
1407       template<typename _ForwardIterator>
1408         void
1409         _M_range_insert_aux(iterator __pos, _ForwardIterator __first,
1410 			    _ForwardIterator __last, std::forward_iterator_tag);
1411 
1412       // Called by insert(p,n,x), and the range insert when it turns out to be
1413       // the same thing.  Can use fill functions in optimal situations,
1414       // otherwise passes off to insert_aux(p,n,x).
1415       void
1416       _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
1417 
1418       // called by insert(p,x)
1419       iterator
1420       _M_insert_aux(iterator __pos, const value_type& __x);
1421 
1422       // called by insert(p,n,x) via fill_insert
1423       void
1424       _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);
1425 
1426       // called by range_insert_aux for forward iterators
1427       template<typename _ForwardIterator>
1428         void
1429         _M_insert_aux(iterator __pos,
1430 		      _ForwardIterator __first, _ForwardIterator __last,
1431 		      size_type __n);
1432 
1433 
1434       // Internal erase functions follow.
1435 
1436       void
1437       _M_destroy_data_aux(iterator __first, iterator __last);
1438 
1439       void
_M_destroy_data_dispatch(iterator,iterator,__true_type)1440       _M_destroy_data_dispatch(iterator, iterator, __true_type) { }
1441 
1442       void
_M_destroy_data_dispatch(iterator __first,iterator __last,__false_type)1443       _M_destroy_data_dispatch(iterator __first, iterator __last, __false_type)
1444       { _M_destroy_data_aux(__first, __last); }
1445 
1446       // Called by ~deque().
1447       // NB: Doesn't deallocate the nodes.
1448       template<typename _Alloc1>
1449         void
_M_destroy_data(iterator __first,iterator __last,const _Alloc1 &)1450         _M_destroy_data(iterator __first, iterator __last, const _Alloc1&)
1451         { _M_destroy_data_aux(__first, __last); }
1452 
1453       void
_M_destroy_data(iterator __first,iterator __last,const std::allocator<_Tp> &)1454       _M_destroy_data(iterator __first, iterator __last,
1455 		      const std::allocator<_Tp>&)
1456       {
1457 	typedef typename std::__is_scalar<value_type>::__type
1458 	  _Has_trivial_destructor;
1459 	_M_destroy_data_dispatch(__first, __last, _Has_trivial_destructor());
1460       }
1461 
1462       // Called by erase(q1, q2).
1463       void
_M_erase_at_begin(iterator __pos)1464       _M_erase_at_begin(iterator __pos)
1465       {
1466 	_M_destroy_data(begin(), __pos, _M_get_Tp_allocator());
1467 	_M_destroy_nodes(this->_M_impl._M_start._M_node, __pos._M_node);
1468 	this->_M_impl._M_start = __pos;
1469       }
1470 
1471       // Called by erase(q1, q2), resize(), clear(), _M_assign_aux,
1472       // _M_fill_assign, operator=.
1473       void
_M_erase_at_end(iterator __pos)1474       _M_erase_at_end(iterator __pos)
1475       {
1476 	_M_destroy_data(__pos, end(), _M_get_Tp_allocator());
1477 	_M_destroy_nodes(__pos._M_node + 1,
1478 			 this->_M_impl._M_finish._M_node + 1);
1479 	this->_M_impl._M_finish = __pos;
1480       }
1481 
1482       //@{
1483       /**
1484        *  @if maint
1485        *  @brief Memory-handling helpers for the previous internal insert
1486        *         functions.
1487        *  @endif
1488        */
1489       iterator
_M_reserve_elements_at_front(size_type __n)1490       _M_reserve_elements_at_front(size_type __n)
1491       {
1492 	const size_type __vacancies = this->_M_impl._M_start._M_cur
1493 	                              - this->_M_impl._M_start._M_first;
1494 	if (__n > __vacancies)
1495 	  _M_new_elements_at_front(__n - __vacancies);
1496 	return this->_M_impl._M_start - difference_type(__n);
1497       }
1498 
1499       iterator
_M_reserve_elements_at_back(size_type __n)1500       _M_reserve_elements_at_back(size_type __n)
1501       {
1502 	const size_type __vacancies = (this->_M_impl._M_finish._M_last
1503 				       - this->_M_impl._M_finish._M_cur) - 1;
1504 	if (__n > __vacancies)
1505 	  _M_new_elements_at_back(__n - __vacancies);
1506 	return this->_M_impl._M_finish + difference_type(__n);
1507       }
1508 
1509       void
1510       _M_new_elements_at_front(size_type __new_elements);
1511 
1512       void
1513       _M_new_elements_at_back(size_type __new_elements);
1514       //@}
1515 
1516 
1517       //@{
1518       /**
1519        *  @if maint
1520        *  @brief Memory-handling helpers for the major %map.
1521        *
1522        *  Makes sure the _M_map has space for new nodes.  Does not
1523        *  actually add the nodes.  Can invalidate _M_map pointers.
1524        *  (And consequently, %deque iterators.)
1525        *  @endif
1526        */
1527       void
1528       _M_reserve_map_at_back(size_type __nodes_to_add = 1)
1529       {
1530 	if (__nodes_to_add + 1 > this->_M_impl._M_map_size
1531 	    - (this->_M_impl._M_finish._M_node - this->_M_impl._M_map))
1532 	  _M_reallocate_map(__nodes_to_add, false);
1533       }
1534 
1535       void
1536       _M_reserve_map_at_front(size_type __nodes_to_add = 1)
1537       {
1538 	if (__nodes_to_add > size_type(this->_M_impl._M_start._M_node
1539 				       - this->_M_impl._M_map))
1540 	  _M_reallocate_map(__nodes_to_add, true);
1541       }
1542 
1543       void
1544       _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);
1545       //@}
1546     };
1547 
1548 
1549   /**
1550    *  @brief  Deque equality comparison.
1551    *  @param  x  A %deque.
1552    *  @param  y  A %deque of the same type as @a x.
1553    *  @return  True iff the size and elements of the deques are equal.
1554    *
1555    *  This is an equivalence relation.  It is linear in the size of the
1556    *  deques.  Deques are considered equivalent if their sizes are equal,
1557    *  and if corresponding elements compare equal.
1558   */
1559   template<typename _Tp, typename _Alloc>
1560     inline bool
1561     operator==(const deque<_Tp, _Alloc>& __x,
1562                          const deque<_Tp, _Alloc>& __y)
1563     { return __x.size() == __y.size()
1564              && std::equal(__x.begin(), __x.end(), __y.begin()); }
1565 
1566   /**
1567    *  @brief  Deque ordering relation.
1568    *  @param  x  A %deque.
1569    *  @param  y  A %deque of the same type as @a x.
1570    *  @return  True iff @a x is lexicographically less than @a y.
1571    *
1572    *  This is a total ordering relation.  It is linear in the size of the
1573    *  deques.  The elements must be comparable with @c <.
1574    *
1575    *  See std::lexicographical_compare() for how the determination is made.
1576   */
1577   template<typename _Tp, typename _Alloc>
1578     inline bool
1579     operator<(const deque<_Tp, _Alloc>& __x,
1580 	      const deque<_Tp, _Alloc>& __y)
1581     { return std::lexicographical_compare(__x.begin(), __x.end(),
1582 					  __y.begin(), __y.end()); }
1583 
1584   /// Based on operator==
1585   template<typename _Tp, typename _Alloc>
1586     inline bool
1587     operator!=(const deque<_Tp, _Alloc>& __x,
1588 	       const deque<_Tp, _Alloc>& __y)
1589     { return !(__x == __y); }
1590 
1591   /// Based on operator<
1592   template<typename _Tp, typename _Alloc>
1593     inline bool
1594     operator>(const deque<_Tp, _Alloc>& __x,
1595 	      const deque<_Tp, _Alloc>& __y)
1596     { return __y < __x; }
1597 
1598   /// Based on operator<
1599   template<typename _Tp, typename _Alloc>
1600     inline bool
1601     operator<=(const deque<_Tp, _Alloc>& __x,
1602 	       const deque<_Tp, _Alloc>& __y)
1603     { return !(__y < __x); }
1604 
1605   /// Based on operator<
1606   template<typename _Tp, typename _Alloc>
1607     inline bool
1608     operator>=(const deque<_Tp, _Alloc>& __x,
1609 	       const deque<_Tp, _Alloc>& __y)
1610     { return !(__x < __y); }
1611 
1612   /// See std::deque::swap().
1613   template<typename _Tp, typename _Alloc>
1614     inline void
swap(deque<_Tp,_Alloc> & __x,deque<_Tp,_Alloc> & __y)1615     swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y)
1616     { __x.swap(__y); }
1617 
1618 _GLIBCXX_END_NESTED_NAMESPACE
1619 
1620 #endif /* _DEQUE_H */
1621