In-depth analysis of deque container implementations

Source: Internet
Author: User

deque Introduction

The deque is a continuous storage space for bidirectional openings. Although the continuity of storage space, but this continuity is only superficial, in fact, its memory is dynamically allocated, it is allocated a piece of dynamic storage on the heap, each dynamic storage area itself is continuous, deque its own mechanism of this piece of storage to connect the virtual.

It inserts an element for the first time and dynamically allocates 512 bytes of space by default, and when that 512 bytes of space is exhausted, it dynamically allocates its own additional 512-byte space, and then it is connected to the virtual. This design of deque makes it a much more complex architecture, algorithm, and iterator design than vector. It has a performance loss ratio of vector, which is a few orders of magnitude difference. Therefore, deque should be used with caution.

deque Data Structure

Logical data Structures:

                 

Figure 1 Logical Structure

Actual data structure:

Figure 2 Actual structure

Data Structure Implementation
Template<class _ty, class _ax>class deque{private:_mapptr _map;//pointer to array of pointers to Blockssize_type _M apsize;//size of Map Arraysize_type _myoff;//offset of initial elementsize_type _mysize;//current length of sequence};
first, the deque of the central control devicewe have found a _MAP member variable here. Note that this is not a map in STL. This _map is a pointer to a special memory address that holds the first address of all 512 bytes of memory space that points to the deque dynamic request. The deque first holds a pointer in a small sequential space order, and then each of these sequential pointers points to the 512-byte continuity space used to actually hold the data. When _map points to a space that is not enough to hold the memory pointer, it will find a larger space for continuity, then copy the pointer one after another and destroy the old space. Using this data structure, deque can easily simulate its own storage area is the illusion of continuous space, and can realize the function of two- way insertion and deletion, we look at the deque data structure diagram, 3 shows.
Figure 3 Internal implementation structure         a small contiguous space referred to by the _MAP is the central controller of the deque, where each element (called a node here, node) is a pointer to another segment (larger) contiguous linear space, called a buffer. The buffer is the main storage space of the deque. The STL allows us to specify the buffer size, and the default value of 0 means that the bytes buffer will be used.
when the controller utilization rate is already full, it is necessary to find a larger space to map, configuration strategy Reallocate_map (), the new map can accommodate more nodes, that is, more buffers, 4 is to expand the new space, The new buffer is based on the original, there is no copy operation, this extension avoids the "Reconfigure, copy, release" Reincarnation, the cost is complex iterator rackstructure.
Figure 4 How to expand the new space implementationThe way to extend the new space from deque, you can see that deque does not have capacity () and does not require Reserce (size_type N). This is because the deque is composed of dynamically allocated contiguous spaces that can be added to a new space at any time. It doesn't have to be like a vector that "re-allocates twice times as much space as the old space, then copies the elements and frees up the old space." Second, the deque iteratorin STL, we can get the value of the iterator by means of dereference, in order to be able to get the exact position of an element from "deque internal structure", then the deque iterator should have how structure, we can consider the following points. First, you need to know where the element is located in which buffer, and secondly, once the iterator is forward and backward it is possible to jump to the first or the next buffer, in order to determine the condition of jumping need to know, the current element in the buffer of the end-to-end pointer. Finally, ifforward or backward must jump to the next or previous buffer. To be able to jump correctly, the deque must always have control center (map), and through map can know the jumping buffer. so the following parameters need to be defined in the iterator, as shown in iterator 5 of the deque:
  • Pointer to current element
  • The starting pointer of the buffer in which the current element is located,
  • points to the map middle finger pointer to the address of the region.
&NBSP;&NBSP
                & nbsp                                /  Figure 5 Iterator

deque to the user to block the complex memory management operations, we are usually in accordance with the "logical structure" approach, that is, the dynamic insertion, deletion of a one-dimensional array, greatly simplifying the user's use, 6 shows. In fact , the biggest task of deque is to manage the continuous space of these segments, maintain the illusion of its overall continuity , and provide the interface of random access.

Figure 6 Deque<string>

In-depth analysis of deque container implementations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.