1 // Deque implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
33 * Hewlett-Packard Company
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
45 * Silicon Graphics Computer Systems, Inc.
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
64 #include <bits/concept_check.h>
65 #include <bits/stl_iterator_base_types.h>
66 #include <bits/stl_iterator_base_funcs.h>
68 namespace _GLIBCXX_STD
72 * @brief This function controls the size of memory nodes.
73 * @param size The size of an element.
74 * @return The number (not byte size) of elements per node.
76 * This function started off as a compiler kludge from SGI, but seems to
77 * be a useful wrapper around a repeated constant expression. The '512' is
78 * tuneable (and no other code needs to change), but no investigation has
79 * been done since inheriting the SGI code.
83 __deque_buf_size(size_t __size
)
84 { return __size
< 512 ? size_t(512 / __size
) : size_t(1); }
88 * @brief A deque::iterator.
90 * Quite a bit of intelligence here. Much of the functionality of deque is
91 * actually passed off to this class. A deque holds two of these internally,
92 * marking its valid range. Access to elements is done as offsets of either
93 * of those two, relying on operator overloading in this class.
96 * All the functions are op overloads except for _M_set_node.
99 template<typename _Tp
, typename _Ref
, typename _Ptr
>
100 struct _Deque_iterator
102 typedef _Deque_iterator
<_Tp
, _Tp
&, _Tp
*> iterator
;
103 typedef _Deque_iterator
<_Tp
, const _Tp
&, const _Tp
*> const_iterator
;
105 static size_t _S_buffer_size()
106 { return __deque_buf_size(sizeof(_Tp
)); }
108 typedef random_access_iterator_tag iterator_category
;
109 typedef _Tp value_type
;
110 typedef _Ptr pointer
;
111 typedef _Ref reference
;
112 typedef size_t size_type
;
113 typedef ptrdiff_t difference_type
;
114 typedef _Tp
** _Map_pointer
;
115 typedef _Deque_iterator _Self
;
120 _Map_pointer _M_node
;
122 _Deque_iterator(_Tp
* __x
, _Map_pointer __y
)
123 : _M_cur(__x
), _M_first(*__y
),
124 _M_last(*__y
+ _S_buffer_size()), _M_node(__y
) {}
126 _Deque_iterator() : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) {}
128 _Deque_iterator(const iterator
& __x
)
129 : _M_cur(__x
._M_cur
), _M_first(__x
._M_first
),
130 _M_last(__x
._M_last
), _M_node(__x
._M_node
) {}
144 if (_M_cur
== _M_last
)
146 _M_set_node(_M_node
+ 1);
163 if (_M_cur
== _M_first
)
165 _M_set_node(_M_node
- 1);
181 operator+=(difference_type __n
)
183 const difference_type __offset
= __n
+ (_M_cur
- _M_first
);
184 if (__offset
>= 0 && __offset
< difference_type(_S_buffer_size()))
188 const difference_type __node_offset
=
189 __offset
> 0 ? __offset
/ difference_type(_S_buffer_size())
190 : -difference_type((-__offset
- 1)
191 / _S_buffer_size()) - 1;
192 _M_set_node(_M_node
+ __node_offset
);
193 _M_cur
= _M_first
+ (__offset
- __node_offset
194 * difference_type(_S_buffer_size()));
200 operator+(difference_type __n
) const
207 operator-=(difference_type __n
)
208 { return *this += -__n
; }
211 operator-(difference_type __n
) const
218 operator[](difference_type __n
) const
219 { return *(*this + __n
); }
222 * Prepares to traverse new_node. Sets everything except _M_cur, which
223 * should therefore be set by the caller immediately afterwards, based on
224 * _M_first and _M_last.
228 _M_set_node(_Map_pointer __new_node
)
230 _M_node
= __new_node
;
231 _M_first
= *__new_node
;
232 _M_last
= _M_first
+ difference_type(_S_buffer_size());
236 // Note: we also provide overloads whose operands are of the same type in
237 // order to avoid ambiguous overload resolution when std::rel_ops operators
238 // are in scope (for additional details, see libstdc++/3628)
239 template<typename _Tp
, typename _Ref
, typename _Ptr
>
241 operator==(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
242 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
243 { return __x
._M_cur
== __y
._M_cur
; }
245 template<typename _Tp
, typename _RefL
, typename _PtrL
,
246 typename _RefR
, typename _PtrR
>
248 operator==(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
249 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
250 { return __x
._M_cur
== __y
._M_cur
; }
252 template<typename _Tp
, typename _Ref
, typename _Ptr
>
254 operator!=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
255 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
256 { return !(__x
== __y
); }
258 template<typename _Tp
, typename _RefL
, typename _PtrL
,
259 typename _RefR
, typename _PtrR
>
261 operator!=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
262 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
263 { return !(__x
== __y
); }
265 template<typename _Tp
, typename _Ref
, typename _Ptr
>
267 operator<(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
268 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
269 { return (__x
._M_node
== __y
._M_node
) ? (__x
._M_cur
< __y
._M_cur
)
270 : (__x
._M_node
< __y
._M_node
); }
272 template<typename _Tp
, typename _RefL
, typename _PtrL
,
273 typename _RefR
, typename _PtrR
>
275 operator<(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
276 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
277 { return (__x
._M_node
== __y
._M_node
) ? (__x
._M_cur
< __y
._M_cur
)
278 : (__x
._M_node
< __y
._M_node
); }
280 template<typename _Tp
, typename _Ref
, typename _Ptr
>
282 operator>(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
283 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
284 { return __y
< __x
; }
286 template<typename _Tp
, typename _RefL
, typename _PtrL
,
287 typename _RefR
, typename _PtrR
>
289 operator>(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
290 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
291 { return __y
< __x
; }
293 template<typename _Tp
, typename _Ref
, typename _Ptr
>
295 operator<=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
296 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
297 { return !(__y
< __x
); }
299 template<typename _Tp
, typename _RefL
, typename _PtrL
,
300 typename _RefR
, typename _PtrR
>
302 operator<=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
303 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
304 { return !(__y
< __x
); }
306 template<typename _Tp
, typename _Ref
, typename _Ptr
>
308 operator>=(const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
,
309 const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __y
)
310 { return !(__x
< __y
); }
312 template<typename _Tp
, typename _RefL
, typename _PtrL
,
313 typename _RefR
, typename _PtrR
>
315 operator>=(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
316 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
317 { return !(__x
< __y
); }
319 // _GLIBCXX_RESOLVE_LIB_DEFECTS
320 // According to the resolution of DR179 not only the various comparison
321 // operators but also operator- must accept mixed iterator/const_iterator
323 template<typename _Tp
, typename _RefL
, typename _PtrL
,
324 typename _RefR
, typename _PtrR
>
325 inline typename _Deque_iterator
<_Tp
, _RefL
, _PtrL
>::difference_type
326 operator-(const _Deque_iterator
<_Tp
, _RefL
, _PtrL
>& __x
,
327 const _Deque_iterator
<_Tp
, _RefR
, _PtrR
>& __y
)
329 return typename _Deque_iterator
<_Tp
, _RefL
, _PtrL
>::difference_type
330 (_Deque_iterator
<_Tp
, _RefL
, _PtrL
>::_S_buffer_size())
331 * (__x
._M_node
- __y
._M_node
- 1) + (__x
._M_cur
- __x
._M_first
)
332 + (__y
._M_last
- __y
._M_cur
);
335 template<typename _Tp
, typename _Ref
, typename _Ptr
>
336 inline _Deque_iterator
<_Tp
, _Ref
, _Ptr
>
337 operator+(ptrdiff_t __n
, const _Deque_iterator
<_Tp
, _Ref
, _Ptr
>& __x
)
338 { return __x
+ __n
; }
342 * Deque base class. This class provides the unified face for %deque's
343 * allocation. This class's constructor and destructor allocate and
344 * deallocate (but do not initialize) storage. This makes %exception
347 * Nothing in this class ever constructs or destroys an actual Tp element.
348 * (Deque handles that itself.) Only/All memory management is performed
352 template<typename _Tp
, typename _Alloc
>
356 typedef _Alloc allocator_type
;
359 get_allocator() const
360 { return *static_cast<const _Alloc
*>(&this->_M_impl
); }
362 typedef _Deque_iterator
<_Tp
,_Tp
&,_Tp
*> iterator
;
363 typedef _Deque_iterator
<_Tp
,const _Tp
&,const _Tp
*> const_iterator
;
365 _Deque_base(const allocator_type
& __a
, size_t __num_elements
)
367 { _M_initialize_map(__num_elements
); }
369 _Deque_base(const allocator_type
& __a
)
376 //This struct encapsulates the implementation of the std::deque
377 //standard container and at the same time makes use of the EBO
378 //for empty allocators.
386 _Deque_impl(const _Alloc
& __a
)
387 : _Alloc(__a
), _M_map(0), _M_map_size(0), _M_start(), _M_finish()
391 typedef typename
_Alloc::template rebind
<_Tp
*>::other _Map_alloc_type
;
392 _Map_alloc_type
_M_get_map_allocator() const
393 { return _Map_alloc_type(this->get_allocator()); }
397 { return _M_impl
._Alloc::allocate(__deque_buf_size(sizeof(_Tp
))); }
400 _M_deallocate_node(_Tp
* __p
)
401 { _M_impl
._Alloc::deallocate(__p
, __deque_buf_size(sizeof(_Tp
))); }
404 _M_allocate_map(size_t __n
)
405 { return _M_get_map_allocator().allocate(__n
); }
408 _M_deallocate_map(_Tp
** __p
, size_t __n
)
409 { _M_get_map_allocator().deallocate(__p
, __n
); }
412 void _M_initialize_map(size_t);
413 void _M_create_nodes(_Tp
** __nstart
, _Tp
** __nfinish
);
414 void _M_destroy_nodes(_Tp
** __nstart
, _Tp
** __nfinish
);
415 enum { _S_initial_map_size
= 8 };
420 template<typename _Tp
, typename _Alloc
>
421 _Deque_base
<_Tp
,_Alloc
>::~_Deque_base()
423 if (this->_M_impl
._M_map
)
425 _M_destroy_nodes(this->_M_impl
._M_start
._M_node
, this->_M_impl
._M_finish
._M_node
+ 1);
426 _M_deallocate_map(this->_M_impl
._M_map
, this->_M_impl
._M_map_size
);
432 * @brief Layout storage.
433 * @param num_elements The count of T's for which to allocate space
437 * The initial underlying memory layout is a bit complicated...
440 template<typename _Tp
, typename _Alloc
>
442 _Deque_base
<_Tp
,_Alloc
>::_M_initialize_map(size_t __num_elements
)
444 size_t __num_nodes
= __num_elements
/ __deque_buf_size(sizeof(_Tp
)) + 1;
446 this->_M_impl
._M_map_size
= std::max((size_t) _S_initial_map_size
,
448 this->_M_impl
._M_map
= _M_allocate_map(this->_M_impl
._M_map_size
);
450 // For "small" maps (needing less than _M_map_size nodes), allocation
451 // starts in the middle elements and grows outwards. So nstart may be
452 // the beginning of _M_map, but for small maps it may be as far in as
455 _Tp
** __nstart
= this->_M_impl
._M_map
+ (this->_M_impl
._M_map_size
- __num_nodes
) / 2;
456 _Tp
** __nfinish
= __nstart
+ __num_nodes
;
459 { _M_create_nodes(__nstart
, __nfinish
); }
462 _M_deallocate_map(this->_M_impl
._M_map
, this->_M_impl
._M_map_size
);
463 this->_M_impl
._M_map
= 0;
464 this->_M_impl
._M_map_size
= 0;
465 __throw_exception_again
;
468 this->_M_impl
._M_start
._M_set_node(__nstart
);
469 this->_M_impl
._M_finish
._M_set_node(__nfinish
- 1);
470 this->_M_impl
._M_start
._M_cur
= _M_impl
._M_start
._M_first
;
471 this->_M_impl
._M_finish
._M_cur
= this->_M_impl
._M_finish
._M_first
+ __num_elements
472 % __deque_buf_size(sizeof(_Tp
));
475 template<typename _Tp
, typename _Alloc
>
477 _Deque_base
<_Tp
,_Alloc
>::_M_create_nodes(_Tp
** __nstart
, _Tp
** __nfinish
)
482 for (__cur
= __nstart
; __cur
< __nfinish
; ++__cur
)
483 *__cur
= this->_M_allocate_node();
487 _M_destroy_nodes(__nstart
, __cur
);
488 __throw_exception_again
;
492 template<typename _Tp
, typename _Alloc
>
494 _Deque_base
<_Tp
,_Alloc
>::_M_destroy_nodes(_Tp
** __nstart
, _Tp
** __nfinish
)
496 for (_Tp
** __n
= __nstart
; __n
< __nfinish
; ++__n
)
497 _M_deallocate_node(*__n
);
501 * @brief A standard container using fixed-size memory allocation and
502 * constant-time manipulation of elements at either end.
504 * @ingroup Containers
507 * Meets the requirements of a <a href="tables.html#65">container</a>, a
508 * <a href="tables.html#66">reversible container</a>, and a
509 * <a href="tables.html#67">sequence</a>, including the
510 * <a href="tables.html#68">optional sequence requirements</a>.
512 * In previous HP/SGI versions of deque, there was an extra template
513 * parameter so users could control the node size. This extension turned
514 * out to violate the C++ standard (it can be detected using template
515 * template parameters), and it was removed.
518 * Here's how a deque<Tp> manages memory. Each deque has 4 members:
521 * - size_t _M_map_size
522 * - iterator _M_start, _M_finish
524 * map_size is at least 8. %map is an array of map_size pointers-to-"nodes".
525 * (The name %map has nothing to do with the std::map class, and "nodes"
526 * should not be confused with std::list's usage of "node".)
528 * A "node" has no specific type name as such, but it is referred to as
529 * "node" in this file. It is a simple array-of-Tp. If Tp is very large,
530 * there will be one Tp element per node (i.e., an "array" of one).
531 * For non-huge Tp's, node size is inversely related to Tp size: the
532 * larger the Tp, the fewer Tp's will fit in a node. The goal here is to
533 * keep the total size of a node relatively small and constant over different
534 * Tp's, to improve allocator efficiency.
536 * **** As I write this, the nodes are /not/ allocated using the high-speed
537 * memory pool. There are 20 hours left in the year; perhaps I can fix
540 * Not every pointer in the %map array will point to a node. If the initial
541 * number of elements in the deque is small, the /middle/ %map pointers will
542 * be valid, and the ones at the edges will be unused. This same situation
543 * will arise as the %map grows: available %map pointers, if any, will be on
544 * the ends. As new nodes are created, only a subset of the %map's pointers
545 * need to be copied "outward".
548 * - For any nonsingular iterator i:
549 * - i.node points to a member of the %map array. (Yes, you read that
550 * correctly: i.node does not actually point to a node.) The member of
551 * the %map array is what actually points to the node.
552 * - i.first == *(i.node) (This points to the node (first Tp element).)
553 * - i.last == i.first + node_size
554 * - i.cur is a pointer in the range [i.first, i.last). NOTE:
555 * the implication of this is that i.cur is always a dereferenceable
556 * pointer, even if i is a past-the-end iterator.
557 * - Start and Finish are always nonsingular iterators. NOTE: this means that
558 * an empty deque must have one node, a deque with <N elements (where N is
559 * the node buffer size) must have one node, a deque with N through (2N-1)
560 * elements must have two nodes, etc.
561 * - For every node other than start.node and finish.node, every element in
562 * the node is an initialized object. If start.node == finish.node, then
563 * [start.cur, finish.cur) are initialized objects, and the elements outside
564 * that range are uninitialized storage. Otherwise, [start.cur, start.last)
565 * and [finish.first, finish.cur) are initialized objects, and [start.first,
566 * start.cur) and [finish.cur, finish.last) are uninitialized storage.
567 * - [%map, %map + map_size) is a valid, non-empty range.
568 * - [start.node, finish.node] is a valid range contained within
569 * [%map, %map + map_size).
570 * - A pointer in the range [%map, %map + map_size) points to an allocated
571 * node if and only if the pointer is in the range
572 * [start.node, finish.node].
574 * Here's the magic: nothing in deque is "aware" of the discontiguous
577 * The memory setup and layout occurs in the parent, _Base, and the iterator
578 * class is entirely responsible for "leaping" from one node to the next.
579 * All the implementation routines for deque itself work only through the
580 * start and finish iterators. This keeps the routines simple and sane,
581 * and we can use other standard algorithms as well.
584 template<typename _Tp
, typename _Alloc
= allocator
<_Tp
> >
585 class deque
: protected _Deque_base
<_Tp
, _Alloc
>
587 // concept requirements
588 __glibcxx_class_requires(_Tp
, _SGIAssignableConcept
)
590 typedef _Deque_base
<_Tp
, _Alloc
> _Base
;
593 typedef _Tp value_type
;
594 typedef value_type
* pointer
;
595 typedef const value_type
* const_pointer
;
596 typedef typename
_Base::iterator iterator
;
597 typedef typename
_Base::const_iterator const_iterator
;
598 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
599 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
600 typedef value_type
& reference
;
601 typedef const value_type
& const_reference
;
602 typedef size_t size_type
;
603 typedef ptrdiff_t difference_type
;
604 typedef typename
_Base::allocator_type allocator_type
;
607 typedef pointer
* _Map_pointer
;
609 static size_t _S_buffer_size()
610 { return __deque_buf_size(sizeof(_Tp
)); }
612 // Functions controlling memory layout, and nothing else.
613 using _Base::_M_initialize_map
;
614 using _Base::_M_create_nodes
;
615 using _Base::_M_destroy_nodes
;
616 using _Base::_M_allocate_node
;
617 using _Base::_M_deallocate_node
;
618 using _Base::_M_allocate_map
;
619 using _Base::_M_deallocate_map
;
622 * A total of four data members accumulated down the heirarchy.
623 * May be accessed via _M_impl.*
626 using _Base::_M_impl
;
629 // [23.2.1.1] construct/copy/destroy
630 // (assign() and get_allocator() are also listed in this section)
632 * @brief Default constructor creates no elements.
635 deque(const allocator_type
& __a
= allocator_type())
639 * @brief Create a %deque with copies of an exemplar element.
640 * @param n The number of elements to initially create.
641 * @param value An element to copy.
643 * This constructor fills the %deque with @a n copies of @a value.
645 deque(size_type __n
, const value_type
& __value
,
646 const allocator_type
& __a
= allocator_type())
648 { _M_fill_initialize(__value
); }
651 * @brief Create a %deque with default elements.
652 * @param n The number of elements to initially create.
654 * This constructor fills the %deque with @a n copies of a
655 * default-constructed element.
659 : _Base(allocator_type(), __n
)
660 { _M_fill_initialize(value_type()); }
663 * @brief %Deque copy constructor.
664 * @param x A %deque of identical element and allocator types.
666 * The newly-created %deque uses a copy of the allocation object used
669 deque(const deque
& __x
)
670 : _Base(__x
.get_allocator(), __x
.size())
671 { std::uninitialized_copy(__x
.begin(), __x
.end(), this->_M_impl
._M_start
); }
674 * @brief Builds a %deque from a range.
675 * @param first An input iterator.
676 * @param last An input iterator.
678 * Create a %deque consisting of copies of the elements from [first,
681 * If the iterators are forward, bidirectional, or random-access, then
682 * this will call the elements' copy constructor N times (where N is
683 * distance(first,last)) and do no memory reallocation. But if only
684 * input iterators are used, then this will do at most 2N calls to the
685 * copy constructor, and logN memory reallocations.
687 template<typename _InputIterator
>
688 deque(_InputIterator __first
, _InputIterator __last
,
689 const allocator_type
& __a
= allocator_type())
692 // Check whether it's an integral type. If so, it's not an iterator.
693 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
694 _M_initialize_dispatch(__first
, __last
, _Integral());
698 * The dtor only erases the elements, and note that if the elements
699 * themselves are pointers, the pointed-to memory is not touched in any
700 * way. Managing the pointer is the user's responsibilty.
703 { std::_Destroy(this->_M_impl
._M_start
, this->_M_impl
._M_finish
); }
706 * @brief %Deque assignment operator.
707 * @param x A %deque of identical element and allocator types.
709 * All the elements of @a x are copied, but unlike the copy constructor,
710 * the allocator object is not copied.
713 operator=(const deque
& __x
);
716 * @brief Assigns a given value to a %deque.
717 * @param n Number of elements to be assigned.
718 * @param val Value to be assigned.
720 * This function fills a %deque with @a n copies of the given value.
721 * Note that the assignment completely changes the %deque and that the
722 * resulting %deque's size is the same as the number of elements assigned.
723 * Old data may be lost.
726 assign(size_type __n
, const value_type
& __val
)
727 { _M_fill_assign(__n
, __val
); }
730 * @brief Assigns a range to a %deque.
731 * @param first An input iterator.
732 * @param last An input iterator.
734 * This function fills a %deque with copies of the elements in the
735 * range [first,last).
737 * Note that the assignment completely changes the %deque and that the
738 * resulting %deque's size is the same as the number of elements
739 * assigned. Old data may be lost.
741 template<typename _InputIterator
>
743 assign(_InputIterator __first
, _InputIterator __last
)
745 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
746 _M_assign_dispatch(__first
, __last
, _Integral());
749 /// Get a copy of the memory allocation object.
751 get_allocator() const
752 { return _Base::get_allocator(); }
756 * Returns a read/write iterator that points to the first element in the
757 * %deque. Iteration is done in ordinary element order.
761 { return this->_M_impl
._M_start
; }
764 * Returns a read-only (constant) iterator that points to the first
765 * element in the %deque. Iteration is done in ordinary element order.
769 { return this->_M_impl
._M_start
; }
772 * Returns a read/write iterator that points one past the last element in
773 * the %deque. Iteration is done in ordinary element order.
777 { return this->_M_impl
._M_finish
; }
780 * Returns a read-only (constant) iterator that points one past the last
781 * element in the %deque. Iteration is done in ordinary element order.
785 { return this->_M_impl
._M_finish
; }
788 * Returns a read/write reverse iterator that points to the last element
789 * in the %deque. Iteration is done in reverse element order.
793 { return reverse_iterator(this->_M_impl
._M_finish
); }
796 * Returns a read-only (constant) reverse iterator that points to the
797 * last element in the %deque. Iteration is done in reverse element
800 const_reverse_iterator
802 { return const_reverse_iterator(this->_M_impl
._M_finish
); }
805 * Returns a read/write reverse iterator that points to one before the
806 * first element in the %deque. Iteration is done in reverse element
810 rend() { return reverse_iterator(this->_M_impl
._M_start
); }
813 * Returns a read-only (constant) reverse iterator that points to one
814 * before the first element in the %deque. Iteration is done in reverse
817 const_reverse_iterator
819 { return const_reverse_iterator(this->_M_impl
._M_start
); }
821 // [23.2.1.2] capacity
822 /** Returns the number of elements in the %deque. */
825 { return this->_M_impl
._M_finish
- this->_M_impl
._M_start
; }
827 /** Returns the size() of the largest possible %deque. */
830 { return size_type(-1); }
833 * @brief Resizes the %deque to the specified number of elements.
834 * @param new_size Number of elements the %deque should contain.
835 * @param x Data with which new elements should be populated.
837 * This function will %resize the %deque to the specified number of
838 * elements. If the number is smaller than the %deque's current size the
839 * %deque is truncated, otherwise the %deque is extended and new elements
840 * are populated with given data.
843 resize(size_type __new_size
, const value_type
& __x
)
845 const size_type __len
= size();
846 if (__new_size
< __len
)
847 erase(this->_M_impl
._M_start
+ __new_size
, this->_M_impl
._M_finish
);
849 insert(this->_M_impl
._M_finish
, __new_size
- __len
, __x
);
853 * @brief Resizes the %deque to the specified number of elements.
854 * @param new_size Number of elements the %deque should contain.
856 * This function will resize the %deque to the specified number of
857 * elements. If the number is smaller than the %deque's current size the
858 * %deque is truncated, otherwise the %deque is extended and new elements
859 * are default-constructed.
862 resize(size_type new_size
)
863 { resize(new_size
, value_type()); }
866 * Returns true if the %deque is empty. (Thus begin() would equal end().)
870 { return this->_M_impl
._M_finish
== this->_M_impl
._M_start
; }
874 * @brief Subscript access to the data contained in the %deque.
875 * @param n The index of the element for which data should be accessed.
876 * @return Read/write reference to data.
878 * This operator allows for easy, array-style, data access.
879 * Note that data access with this operator is unchecked and out_of_range
880 * lookups are not defined. (For checked lookups see at().)
883 operator[](size_type __n
)
884 { return this->_M_impl
._M_start
[difference_type(__n
)]; }
887 * @brief Subscript access to the data contained in the %deque.
888 * @param n The index of the element for which data should be accessed.
889 * @return Read-only (constant) reference to data.
891 * This operator allows for easy, array-style, data access.
892 * Note that data access with this operator is unchecked and out_of_range
893 * lookups are not defined. (For checked lookups see at().)
896 operator[](size_type __n
) const
897 { return this->_M_impl
._M_start
[difference_type(__n
)]; }
900 /// @if maint Safety check used only from at(). @endif
902 _M_range_check(size_type __n
) const
904 if (__n
>= this->size())
905 __throw_out_of_range(__N("deque::_M_range_check"));
910 * @brief Provides access to the data contained in the %deque.
911 * @param n The index of the element for which data should be accessed.
912 * @return Read/write reference to data.
913 * @throw std::out_of_range If @a n is an invalid index.
915 * This function provides for safer data access. The parameter is first
916 * checked that it is in the range of the deque. The function throws
917 * out_of_range if the check fails.
921 { _M_range_check(__n
); return (*this)[__n
]; }
924 * @brief Provides access to the data contained in the %deque.
925 * @param n The index of the element for which data should be accessed.
926 * @return Read-only (constant) reference to data.
927 * @throw std::out_of_range If @a n is an invalid index.
929 * This function provides for safer data access. The parameter is first
930 * checked that it is in the range of the deque. The function throws
931 * out_of_range if the check fails.
934 at(size_type __n
) const
941 * Returns a read/write reference to the data at the first element of the
946 { return *this->_M_impl
._M_start
; }
949 * Returns a read-only (constant) reference to the data at the first
950 * element of the %deque.
954 { return *this->_M_impl
._M_start
; }
957 * Returns a read/write reference to the data at the last element of the
963 iterator __tmp
= this->_M_impl
._M_finish
;
969 * Returns a read-only (constant) reference to the data at the last
970 * element of the %deque.
975 const_iterator __tmp
= this->_M_impl
._M_finish
;
980 // [23.2.1.2] modifiers
982 * @brief Add data to the front of the %deque.
983 * @param x Data to be added.
985 * This is a typical stack operation. The function creates an element at
986 * the front of the %deque and assigns the given data to it. Due to the
987 * nature of a %deque this operation can be done in constant time.
990 push_front(const value_type
& __x
)
992 if (this->_M_impl
._M_start
._M_cur
!= this->_M_impl
._M_start
._M_first
)
994 std::_Construct(this->_M_impl
._M_start
._M_cur
- 1, __x
);
995 --this->_M_impl
._M_start
._M_cur
;
998 _M_push_front_aux(__x
);
1002 * @brief Add data to the end of the %deque.
1003 * @param x Data to be added.
1005 * This is a typical stack operation. The function creates an element at
1006 * the end of the %deque and assigns the given data to it. Due to the
1007 * nature of a %deque this operation can be done in constant time.
1010 push_back(const value_type
& __x
)
1012 if (this->_M_impl
._M_finish
._M_cur
!= this->_M_impl
._M_finish
._M_last
- 1)
1014 std::_Construct(this->_M_impl
._M_finish
._M_cur
, __x
);
1015 ++this->_M_impl
._M_finish
._M_cur
;
1018 _M_push_back_aux(__x
);
1022 * @brief Removes first element.
1024 * This is a typical stack operation. It shrinks the %deque by one.
1026 * Note that no data is returned, and if the first element's data is
1027 * needed, it should be retrieved before pop_front() is called.
1032 if (this->_M_impl
._M_start
._M_cur
!= this->_M_impl
._M_start
._M_last
- 1)
1034 std::_Destroy(this->_M_impl
._M_start
._M_cur
);
1035 ++this->_M_impl
._M_start
._M_cur
;
1042 * @brief Removes last element.
1044 * This is a typical stack operation. It shrinks the %deque by one.
1046 * Note that no data is returned, and if the last element's data is
1047 * needed, it should be retrieved before pop_back() is called.
1052 if (this->_M_impl
._M_finish
._M_cur
!= this->_M_impl
._M_finish
._M_first
)
1054 --this->_M_impl
._M_finish
._M_cur
;
1055 std::_Destroy(this->_M_impl
._M_finish
._M_cur
);
1062 * @brief Inserts given value into %deque before specified iterator.
1063 * @param position An iterator into the %deque.
1064 * @param x Data to be inserted.
1065 * @return An iterator that points to the inserted data.
1067 * This function will insert a copy of the given value before the
1068 * specified location.
1071 insert(iterator position
, const value_type
& __x
);
1074 * @brief Inserts a number of copies of given data into the %deque.
1075 * @param position An iterator into the %deque.
1076 * @param n Number of elements to be inserted.
1077 * @param x Data to be inserted.
1079 * This function will insert a specified number of copies of the given
1080 * data before the location specified by @a position.
1083 insert(iterator __position
, size_type __n
, const value_type
& __x
)
1084 { _M_fill_insert(__position
, __n
, __x
); }
1087 * @brief Inserts a range into the %deque.
1088 * @param position An iterator into the %deque.
1089 * @param first An input iterator.
1090 * @param last An input iterator.
1092 * This function will insert copies of the data in the range [first,last)
1093 * into the %deque before the location specified by @a pos. This is
1094 * known as "range insert."
1096 template<typename _InputIterator
>
1098 insert(iterator __position
, _InputIterator __first
,
1099 _InputIterator __last
)
1101 // Check whether it's an integral type. If so, it's not an iterator.
1102 typedef typename _Is_integer
<_InputIterator
>::_Integral _Integral
;
1103 _M_insert_dispatch(__position
, __first
, __last
, _Integral());
1107 * @brief Remove element at given position.
1108 * @param position Iterator pointing to element to be erased.
1109 * @return An iterator pointing to the next element (or end()).
1111 * This function will erase the element at the given position and thus
1112 * shorten the %deque by one.
1114 * The user is cautioned that
1115 * this function only erases the element, and that if the element is
1116 * itself a pointer, the pointed-to memory is not touched in any way.
1117 * Managing the pointer is the user's responsibilty.
1120 erase(iterator __position
);
1123 * @brief Remove a range of elements.
1124 * @param first Iterator pointing to the first element to be erased.
1125 * @param last Iterator pointing to one past the last element to be
1127 * @return An iterator pointing to the element pointed to by @a last
1128 * prior to erasing (or end()).
1130 * This function will erase the elements in the range [first,last) and
1131 * shorten the %deque accordingly.
1133 * The user is cautioned that
1134 * this function only erases the elements, and that if the elements
1135 * themselves are pointers, the pointed-to memory is not touched in any
1136 * way. Managing the pointer is the user's responsibilty.
1139 erase(iterator __first
, iterator __last
);
1142 * @brief Swaps data with another %deque.
1143 * @param x A %deque of the same element and allocator types.
1145 * This exchanges the elements between two deques in constant time.
1146 * (Four pointers, so it should be quite fast.)
1147 * Note that the global std::swap() function is specialized such that
1148 * std::swap(d1,d2) will feed to this function.
1153 std::swap(this->_M_impl
._M_start
, __x
._M_impl
._M_start
);
1154 std::swap(this->_M_impl
._M_finish
, __x
._M_impl
._M_finish
);
1155 std::swap(this->_M_impl
._M_map
, __x
._M_impl
._M_map
);
1156 std::swap(this->_M_impl
._M_map_size
, __x
._M_impl
._M_map_size
);
1160 * Erases all the elements. Note that this function only erases the
1161 * elements, and that if the elements themselves are pointers, the
1162 * pointed-to memory is not touched in any way. Managing the pointer is
1163 * the user's responsibilty.
1168 // Internal constructor functions follow.
1170 // called by the range constructor to implement [23.1.1]/9
1171 template<typename _Integer
>
1173 _M_initialize_dispatch(_Integer __n
, _Integer __x
, __true_type
)
1175 _M_initialize_map(__n
);
1176 _M_fill_initialize(__x
);
1179 // called by the range constructor to implement [23.1.1]/9
1180 template<typename _InputIterator
>
1182 _M_initialize_dispatch(_InputIterator __first
, _InputIterator __last
,
1185 typedef typename iterator_traits
<_InputIterator
>::iterator_category
1187 _M_range_initialize(__first
, __last
, _IterCategory());
1190 // called by the second initialize_dispatch above
1194 * @brief Fills the deque with whatever is in [first,last).
1195 * @param first An input iterator.
1196 * @param last An input iterator.
1199 * If the iterators are actually forward iterators (or better), then the
1200 * memory layout can be done all at once. Else we move forward using
1201 * push_back on each value from the iterator.
1204 template<typename _InputIterator
>
1206 _M_range_initialize(_InputIterator __first
, _InputIterator __last
,
1207 input_iterator_tag
);
1209 // called by the second initialize_dispatch above
1210 template<typename _ForwardIterator
>
1212 _M_range_initialize(_ForwardIterator __first
, _ForwardIterator __last
,
1213 forward_iterator_tag
);
1218 * @brief Fills the %deque with copies of value.
1219 * @param value Initial value.
1221 * @pre _M_start and _M_finish have already been initialized, but none of
1222 * the %deque's elements have yet been constructed.
1224 * This function is called only when the user provides an explicit size
1225 * (with or without an explicit exemplar value).
1229 _M_fill_initialize(const value_type
& __value
);
1231 // Internal assign functions follow. The *_aux functions do the actual
1232 // assignment work for the range versions.
1234 // called by the range assign to implement [23.1.1]/9
1235 template<typename _Integer
>
1237 _M_assign_dispatch(_Integer __n
, _Integer __val
, __true_type
)
1239 _M_fill_assign(static_cast<size_type
>(__n
),
1240 static_cast<value_type
>(__val
));
1243 // called by the range assign to implement [23.1.1]/9
1244 template<typename _InputIterator
>
1246 _M_assign_dispatch(_InputIterator __first
, _InputIterator __last
,
1249 typedef typename iterator_traits
<_InputIterator
>::iterator_category
1251 _M_assign_aux(__first
, __last
, _IterCategory());
1254 // called by the second assign_dispatch above
1255 template<typename _InputIterator
>
1257 _M_assign_aux(_InputIterator __first
, _InputIterator __last
,
1258 input_iterator_tag
);
1260 // called by the second assign_dispatch above
1261 template<typename _ForwardIterator
>
1263 _M_assign_aux(_ForwardIterator __first
, _ForwardIterator __last
,
1264 forward_iterator_tag
)
1266 const size_type __len
= std::distance(__first
, __last
);
1269 _ForwardIterator __mid
= __first
;
1270 std::advance(__mid
, size());
1271 std::copy(__first
, __mid
, begin());
1272 insert(end(), __mid
, __last
);
1275 erase(std::copy(__first
, __last
, begin()), end());
1278 // Called by assign(n,t), and the range assign when it turns out to be the
1281 _M_fill_assign(size_type __n
, const value_type
& __val
)
1285 std::fill(begin(), end(), __val
);
1286 insert(end(), __n
- size(), __val
);
1290 erase(begin() + __n
, end());
1291 std::fill(begin(), end(), __val
);
1298 * @brief Helper functions for push_* and pop_*.
1301 void _M_push_back_aux(const value_type
&);
1302 void _M_push_front_aux(const value_type
&);
1303 void _M_pop_back_aux();
1304 void _M_pop_front_aux();
1307 // Internal insert functions follow. The *_aux functions do the actual
1308 // insertion work when all shortcuts fail.
1310 // called by the range insert to implement [23.1.1]/9
1311 template<typename _Integer
>
1313 _M_insert_dispatch(iterator __pos
,
1314 _Integer __n
, _Integer __x
, __true_type
)
1316 _M_fill_insert(__pos
, static_cast<size_type
>(__n
),
1317 static_cast<value_type
>(__x
));
1320 // called by the range insert to implement [23.1.1]/9
1321 template<typename _InputIterator
>
1323 _M_insert_dispatch(iterator __pos
,
1324 _InputIterator __first
, _InputIterator __last
,
1327 typedef typename iterator_traits
<_InputIterator
>::iterator_category
1329 _M_range_insert_aux(__pos
, __first
, __last
, _IterCategory());
1332 // called by the second insert_dispatch above
1333 template<typename _InputIterator
>
1335 _M_range_insert_aux(iterator __pos
, _InputIterator __first
,
1336 _InputIterator __last
, input_iterator_tag
);
1338 // called by the second insert_dispatch above
1339 template<typename _ForwardIterator
>
1341 _M_range_insert_aux(iterator __pos
, _ForwardIterator __first
,
1342 _ForwardIterator __last
, forward_iterator_tag
);
1344 // Called by insert(p,n,x), and the range insert when it turns out to be
1345 // the same thing. Can use fill functions in optimal situations,
1346 // otherwise passes off to insert_aux(p,n,x).
1348 _M_fill_insert(iterator __pos
, size_type __n
, const value_type
& __x
);
1350 // called by insert(p,x)
1352 _M_insert_aux(iterator __pos
, const value_type
& __x
);
1354 // called by insert(p,n,x) via fill_insert
1356 _M_insert_aux(iterator __pos
, size_type __n
, const value_type
& __x
);
1358 // called by range_insert_aux for forward iterators
1359 template<typename _ForwardIterator
>
1361 _M_insert_aux(iterator __pos
,
1362 _ForwardIterator __first
, _ForwardIterator __last
,
1368 * @brief Memory-handling helpers for the previous internal insert
1373 _M_reserve_elements_at_front(size_type __n
)
1375 const size_type __vacancies
= this->_M_impl
._M_start
._M_cur
1376 - this->_M_impl
._M_start
._M_first
;
1377 if (__n
> __vacancies
)
1378 _M_new_elements_at_front(__n
- __vacancies
);
1379 return this->_M_impl
._M_start
- difference_type(__n
);
1383 _M_reserve_elements_at_back(size_type __n
)
1385 const size_type __vacancies
= (this->_M_impl
._M_finish
._M_last
1386 - this->_M_impl
._M_finish
._M_cur
) - 1;
1387 if (__n
> __vacancies
)
1388 _M_new_elements_at_back(__n
- __vacancies
);
1389 return this->_M_impl
._M_finish
+ difference_type(__n
);
1393 _M_new_elements_at_front(size_type __new_elements
);
1396 _M_new_elements_at_back(size_type __new_elements
);
1403 * @brief Memory-handling helpers for the major %map.
1405 * Makes sure the _M_map has space for new nodes. Does not actually add
1406 * the nodes. Can invalidate _M_map pointers. (And consequently, %deque
1411 _M_reserve_map_at_back (size_type __nodes_to_add
= 1)
1413 if (__nodes_to_add
+ 1 > this->_M_impl
._M_map_size
1414 - (this->_M_impl
._M_finish
._M_node
- this->_M_impl
._M_map
))
1415 _M_reallocate_map(__nodes_to_add
, false);
1419 _M_reserve_map_at_front (size_type __nodes_to_add
= 1)
1421 if (__nodes_to_add
> size_type(this->_M_impl
._M_start
._M_node
- this->_M_impl
._M_map
))
1422 _M_reallocate_map(__nodes_to_add
, true);
1426 _M_reallocate_map(size_type __nodes_to_add
, bool __add_at_front
);
1432 * @brief Deque equality comparison.
1433 * @param x A %deque.
1434 * @param y A %deque of the same type as @a x.
1435 * @return True iff the size and elements of the deques are equal.
1437 * This is an equivalence relation. It is linear in the size of the
1438 * deques. Deques are considered equivalent if their sizes are equal,
1439 * and if corresponding elements compare equal.
1441 template<typename _Tp
, typename _Alloc
>
1443 operator==(const deque
<_Tp
, _Alloc
>& __x
,
1444 const deque
<_Tp
, _Alloc
>& __y
)
1445 { return __x
.size() == __y
.size()
1446 && std::equal(__x
.begin(), __x
.end(), __y
.begin()); }
1449 * @brief Deque ordering relation.
1450 * @param x A %deque.
1451 * @param y A %deque of the same type as @a x.
1452 * @return True iff @a x is lexicographically less than @a y.
1454 * This is a total ordering relation. It is linear in the size of the
1455 * deques. The elements must be comparable with @c <.
1457 * See std::lexicographical_compare() for how the determination is made.
1459 template<typename _Tp
, typename _Alloc
>
1461 operator<(const deque
<_Tp
, _Alloc
>& __x
,
1462 const deque
<_Tp
, _Alloc
>& __y
)
1463 { return lexicographical_compare(__x
.begin(), __x
.end(),
1464 __y
.begin(), __y
.end()); }
1466 /// Based on operator==
1467 template<typename _Tp
, typename _Alloc
>
1469 operator!=(const deque
<_Tp
, _Alloc
>& __x
,
1470 const deque
<_Tp
, _Alloc
>& __y
)
1471 { return !(__x
== __y
); }
1473 /// Based on operator<
1474 template<typename _Tp
, typename _Alloc
>
1476 operator>(const deque
<_Tp
, _Alloc
>& __x
,
1477 const deque
<_Tp
, _Alloc
>& __y
)
1478 { return __y
< __x
; }
1480 /// Based on operator<
1481 template<typename _Tp
, typename _Alloc
>
1483 operator<=(const deque
<_Tp
, _Alloc
>& __x
,
1484 const deque
<_Tp
, _Alloc
>& __y
)
1485 { return !(__y
< __x
); }
1487 /// Based on operator<
1488 template<typename _Tp
, typename _Alloc
>
1490 operator>=(const deque
<_Tp
, _Alloc
>& __x
,
1491 const deque
<_Tp
, _Alloc
>& __y
)
1492 { return !(__x
< __y
); }
1494 /// See std::deque::swap().
1495 template<typename _Tp
, typename _Alloc
>
1497 swap(deque
<_Tp
,_Alloc
>& __x
, deque
<_Tp
,_Alloc
>& __y
)
1501 #endif /* _DEQUE_H */