GNU g++  v5.2.1
GNU Standard C++
stl_deque.h File Reference

This is an internal header file, included by other library headers. More...

Include dependency graph for stl_deque.h:

Macros

#define _GLIBCXX_DEQUE_BUF_SIZE   512
 

Functions

namespace std _GLIBCXX_VISIBILITY (default)
 

Detailed Description

This is an internal header file, included by other library headers.

Do not attempt to use it directly. {deque}

Macro Definition Documentation

#define _GLIBCXX_DEQUE_BUF_SIZE   512

Referenced by _GLIBCXX_VISIBILITY().

Function Documentation

namespace std _GLIBCXX_VISIBILITY ( default  )

This function controls the size of memory nodes.

Parameters
__sizeThe size of an element.
Returns
The number (not byte size) of elements per node.

This function started off as a compiler kludge from SGI, but seems to be a useful wrapper around a repeated constant expression. The 512 is tunable (and no other code needs to change), but no investigation has been done since inheriting the SGI code. Touch _GLIBCXX_DEQUE_BUF_SIZE only if you know what you are doing, however: changing it breaks the binary compatibility!!

A deque::iterator.

Quite a bit of intelligence here. Much of the functionality of deque is actually passed off to this class. A deque holds two of these internally, marking its valid range. Access to elements is done as offsets of either of those two, relying on operator overloading in this class.

All the functions are op overloads except for _M_set_node.

Prepares to traverse new_node. Sets everything except _M_cur, which should therefore be set by the caller immediately afterwards, based on _M_first and _M_last.

Deque base class. This class provides the unified face for deque's allocation. This class's constructor and destructor allocate and deallocate (but do not initialize) storage. This makes exception safety easier.

Nothing in this class ever constructs or destroys an actual Tp element. (Deque handles that itself.) Only/All memory management is performed here.

Layout storage.

Parameters
__num_elementsThe count of T's for which to allocate space at first.
Returns
Nothing.

The initial underlying memory layout is a bit complicated...

A standard container using fixed-size memory allocation and constant-time manipulation of elements at either end.

Template Parameters
_TpType of element.
_AllocAllocator type, defaults to allocator<_Tp>.

Meets the requirements of a container, a reversible container, and a sequence, including the optional sequence requirements.

In previous HP/SGI versions of deque, there was an extra template parameter so users could control the node size. This extension turned out to violate the C++ standard (it can be detected using template template parameters), and it was removed.

Here's how a deque<Tp> manages memory. Each deque has 4 members:

  • Tp** _M_map
  • size_t _M_map_size
  • iterator _M_start, _M_finish

map_size is at least 8. map is an array of map_size pointers-to-nodes. (The name map has nothing to do with the std::map class, and nodes should not be confused with std::list's usage of node.)

A node has no specific type name as such, but it is referred to as node in this file. It is a simple array-of-Tp. If Tp is very large, there will be one Tp element per node (i.e., an array of one). For non-huge Tp's, node size is inversely related to Tp size: the larger the Tp, the fewer Tp's will fit in a node. The goal here is to keep the total size of a node relatively small and constant over different Tp's, to improve allocator efficiency.

Not every pointer in the map array will point to a node. If the initial number of elements in the deque is small, the /middle/ map pointers will be valid, and the ones at the edges will be unused. This same situation will arise as the map grows: available map pointers, if any, will be on the ends. As new nodes are created, only a subset of the map's pointers need to be copied outward.

Class invariants:

  • For any nonsingular iterator i:
    • i.node points to a member of the map array. (Yes, you read that correctly: i.node does not actually point to a node.) The member of the map array is what actually points to the node.
    • i.first == *(i.node) (This points to the node (first Tp element).)
    • i.last == i.first + node_size
    • i.cur is a pointer in the range [i.first, i.last). NOTE: the implication of this is that i.cur is always a dereferenceable pointer, even if i is a past-the-end iterator.
  • Start and Finish are always nonsingular iterators. NOTE: this means that an empty deque must have one node, a deque with <N elements (where N is the node buffer size) must have one node, a deque with N through (2N-1) elements must have two nodes, etc.
  • For every node other than start.node and finish.node, every element in the node is an initialized object. If start.node == finish.node, then [start.cur, finish.cur) are initialized objects, and the elements outside that range are uninitialized storage. Otherwise, [start.cur, start.last) and [finish.first, finish.cur) are initialized objects, and [start.first, start.cur) and [finish.cur, finish.last) are uninitialized storage.
  • [map, map + map_size) is a valid, non-empty range.
  • [start.node, finish.node] is a valid range contained within [map, map + map_size).
  • A pointer in the range [map, map + map_size) points to an allocated node if and only if the pointer is in the range [start.node, finish.node].

    Here's the magic: nothing in deque is aware of the discontiguous storage!

    The memory setup and layout occurs in the parent, _Base, and the iterator class is entirely responsible for leaping from one node to the next. All the implementation routines for deque itself work only through the start and finish iterators. This keeps the routines simple and sane, and we can use other standard algorithms as well.

A total of four data members accumulated down the hierarchy. May be accessed via _M_impl.*

Creates a deque with no elements.

Creates a deque with no elements.

Parameters
__aAn allocator object.

Creates a deque with copies of an exemplar element.

Parameters
__nThe number of elements to initially create.
__valueAn element to copy.
__aAn allocator.

This constructor fills the deque with __n copies of __value.

Deque copy constructor.

Parameters
__xA deque of identical element and allocator types.

The newly-created deque uses a copy of the allocation object used by __x.

Builds a deque from a range.

Parameters
__firstAn input iterator.
__lastAn input iterator.
__aAn allocator object.

Create a deque consisting of copies of the elements from [__first, __last).

If the iterators are forward, bidirectional, or random-access, then this will call the elements' copy constructor N times (where N is distance(__first,__last)) and do no memory reallocation. But if only input iterators are used, then this will do at most 2N calls to the copy constructor, and logN memory reallocations.

The dtor only erases the elements, and note that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility.

Deque assignment operator.

Parameters
__xA deque of identical element and allocator types.

All the elements of x are copied, but unlike the copy constructor, the allocator object is not copied.

Assigns a given value to a deque.

Parameters
__nNumber of elements to be assigned.
__valValue to be assigned.

This function fills a deque with n copies of the given value. Note that the assignment completely changes the deque and that the resulting deque's size is the same as the number of elements assigned. Old data may be lost.

Assigns a range to a deque.

Parameters
__firstAn input iterator.
__lastAn input iterator.

This function fills a deque with copies of the elements in the range [__first,__last).

Note that the assignment completely changes the deque and that the resulting deque's size is the same as the number of elements assigned. Old data may be lost.

Get a copy of the memory allocation object.

Returns a read/write iterator that points to the first element in the deque. Iteration is done in ordinary element order.

Returns a read-only (constant) iterator that points to the first element in the deque. Iteration is done in ordinary element order.

Returns a read/write iterator that points one past the last element in the deque. Iteration is done in ordinary element order.

Returns a read-only (constant) iterator that points one past the last element in the deque. Iteration is done in ordinary element order.

Returns a read/write reverse iterator that points to the last element in the deque. Iteration is done in reverse element order.

Returns a read-only (constant) reverse iterator that points to the last element in the deque. Iteration is done in reverse element order.

Returns a read/write reverse iterator that points to one before the first element in the deque. Iteration is done in reverse element order.

Returns a read-only (constant) reverse iterator that points to one before the first element in the deque. Iteration is done in reverse element order.

Returns the number of elements in the deque.

Returns the size() of the largest possible deque.

Resizes the deque to the specified number of elements.

Parameters
__new_sizeNumber of elements the deque should contain.
__xData with which new elements should be populated.

This function will resize the deque to the specified number of elements. If the number is smaller than the deque's current size the deque is truncated, otherwise the deque is extended and new elements are populated with given data.

Returns true if the deque is empty. (Thus begin() would equal end().)

Subscript access to the data contained in the deque.

Parameters
__nThe index of the element for which data should be accessed.
Returns
Read/write reference to data.

This operator allows for easy, array-style, data access. Note that data access with this operator is unchecked and out_of_range lookups are not defined. (For checked lookups see at().)

Subscript access to the data contained in the deque.

Parameters
__nThe index of the element for which data should be accessed.
Returns
Read-only (constant) reference to data.

This operator allows for easy, array-style, data access. Note that data access with this operator is unchecked and out_of_range lookups are not defined. (For checked lookups see at().)

Safety check used only from at().

Provides access to the data contained in the deque.

Parameters
__nThe index of the element for which data should be accessed.
Returns
Read/write reference to data.
Exceptions
std::out_of_rangeIf __n is an invalid index.

This function provides for safer data access. The parameter is first checked that it is in the range of the deque. The function throws out_of_range if the check fails.

Provides access to the data contained in the deque.

Parameters
__nThe index of the element for which data should be accessed.
Returns
Read-only (constant) reference to data.
Exceptions
std::out_of_rangeIf __n is an invalid index.

This function provides for safer data access. The parameter is first checked that it is in the range of the deque. The function throws out_of_range if the check fails.

Returns a read/write reference to the data at the first element of the deque.

Returns a read-only (constant) reference to the data at the first element of the deque.

Returns a read/write reference to the data at the last element of the deque.

Returns a read-only (constant) reference to the data at the last element of the deque.

Add data to the front of the deque.

Parameters
__xData to be added.

This is a typical stack operation. The function creates an element at the front of the deque and assigns the given data to it. Due to the nature of a deque this operation can be done in constant time.

Add data to the end of the deque.

Parameters
__xData to be added.

This is a typical stack operation. The function creates an element at the end of the deque and assigns the given data to it. Due to the nature of a deque this operation can be done in constant time.

Removes first element.

This is a typical stack operation. It shrinks the deque by one.

Note that no data is returned, and if the first element's data is needed, it should be retrieved before pop_front() is called.

Removes last element.

This is a typical stack operation. It shrinks the deque by one.

Note that no data is returned, and if the last element's data is needed, it should be retrieved before pop_back() is called.

Inserts given value into deque before specified iterator.

Parameters
__positionAn iterator into the deque.
__xData to be inserted.
Returns
An iterator that points to the inserted data.

This function will insert a copy of the given value before the specified location.

Inserts a number of copies of given data into the deque.

Parameters
__positionAn iterator into the deque.
__nNumber of elements to be inserted.
__xData to be inserted.

This function will insert a specified number of copies of the given data before the location specified by __position.

Inserts a range into the deque.

Parameters
__positionAn iterator into the deque.
__firstAn input iterator.
__lastAn input iterator.

This function will insert copies of the data in the range [__first,__last) into the deque before the location specified by __position. This is known as range insert.

Remove element at given position.

Parameters
__positionIterator pointing to element to be erased.
Returns
An iterator pointing to the next element (or end()).

This function will erase the element at the given position and thus shorten the deque by one.

The user is cautioned that this function only erases the element, and that if the element is itself a pointer, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility.

Remove a range of elements.

Parameters
__firstIterator pointing to the first element to be erased.
__lastIterator pointing to one past the last element to be erased.
Returns
An iterator pointing to the element pointed to by last prior to erasing (or end()).

This function will erase the elements in the range [__first,__last) and shorten the deque accordingly.

The user is cautioned that this function only erases the elements, and that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility.

Swaps data with another deque.

Parameters
__xA deque of the same element and allocator types.

This exchanges the elements between two deques in constant time. (Four pointers, so it should be quite fast.) Note that the global std::swap() function is specialized such that std::swap(d1,d2) will feed to this function.

Erases all the elements. Note that this function only erases the elements, and that if the elements themselves are pointers, the pointed-to memory is not touched in any way. Managing the pointer is the user's responsibility.

Fills the deque with whatever is in [first,last).

Parameters
__firstAn input iterator.
__lastAn input iterator.
Returns
Nothing.

If the iterators are actually forward iterators (or better), then the memory layout can be done all at once. Else we move forward using push_back on each value from the iterator.

Fills the deque with copies of value.

Parameters
__valueInitial value.
Returns
Nothing.
Precondition
_M_start and _M_finish have already been initialized, but none of the deque's elements have yet been constructed.

This function is called only when the user provides an explicit size (with or without an explicit exemplar value).

Helper functions for push_* and pop_*.

Memory-handling helpers for the previous internal insert functions.

Memory-handling helpers for the major map.

Makes sure the _M_map has space for new nodes. Does not actually add the nodes. Can invalidate _M_map pointers. (And consequently, deque iterators.)

Deque equality comparison.

Parameters
__xA deque.
__yA deque of the same type as __x.
Returns
True iff the size and elements of the deques are equal.

This is an equivalence relation. It is linear in the size of the deques. Deques are considered equivalent if their sizes are equal, and if corresponding elements compare equal.

Deque ordering relation.

Parameters
__xA deque.
__yA deque of the same type as __x.
Returns
True iff x is lexicographically less than __y.

This is a total ordering relation. It is linear in the size of the deques. The elements must be comparable with <.

See std::lexicographical_compare() for how the determination is made.

Based on operator==

Based on operator<

Based on operator<

Based on operator<

See std::deque::swap().

References __catch, __glibcxx_class_requires, __glibcxx_class_requires2, __gnu_profile::__size(), __throw_exception_again, __try, _GLIBCXX_DEQUE_BUF_SIZE, __gnu_parallel::max(), std::__exception_ptr::operator!=(), __gnu_debug::operator+(), __gnu_debug::operator-(), std::__exception_ptr::operator==(), __gnu_debug::operator>(), __gnu_debug::operator>=(), and std::__exception_ptr::swap().

Here is the call graph for this function: