Not so far I've learned how std::deque
is implemented under the hood, and discovered that it's something like an array of pointers to n-byte arrays, where the data is actually stored. So now I have a couple of questions related to deques.
A picture that describes my current knowledge about it's structure:
The questions are:
When a push_front
operation is being performed and there is no free space in Data Block 0, a new Data Block is allocated on heap and a pointer to this freshly allocated memory is inserted into 'Map' array like in ordinary array -- in O(number_of_blocks) time, yes?
How to sort this beast? Can't imagine anything better then copy all the data into array, sort it, and then put it back. But this approach requires O(n) auxiliary memory... But! std::sort
provides similar interface for sorting both std::vector
and std::deque
. How are different algoritms for different data structures implemented? Using a template specialization? If so, why std::list
can't be sorted using std::sort
?
Or, maybe, std::sort
doesn't care about internal structure of this containers and just uses iterators and methods, that are similar in both std::vector
and std::deque
(like operator[]
, size()
, etc)? This idea sounds reasonable and an answer to "why can't std::sort
sort std::list
?" becomes evident.
How are the sizes of data blocks being chosen? You'll say "It's implementation dependent", but please tell more about different implementations and motivation behind solutions.
Need clarifications here. Thanks.
The math behind them not withstanding, std::deque has random access iterators, and as such any in-place sorting algorithm will be able to sort the data within using that functionality and not requiring O(N) additional storage. std::list doesn't provide random access iteration, and thus is not std::sort -able.
The std::sort is a sorting function that uses the Introsort algorithm and have the complexity of O(N log(N)) where N= std::distance(first, last) since C++11 and the order of equal elements is not guaranteed to be preserved[3]. The gcc-libstdc++ also uses Introsort algorithm.
Dequeue or Double Ended Queue is a generalized version of Queue data structure that allows insert and delete at both ends. insert_at_beg(): inserts an item at the front of Dequeue. insert_at_end(): inserts an item at the rear of Dequeue. delete_fr_beg(): Deletes an item from front of Dequeue.
A deque is generally implemented as a collection of memory blocks. These memory blocks contains the elements at contiguous locations. When we create a deque object it internally allocates a memory block to store the elements at contigious location.
To answer to your first question, yes, that's pretty much how it works. One can note that this approach can be extended into a multi-level hierarchical structure. But practical implementations usually stick to two-level structure, exactly as shown in your picture.
For your second question, if you are talking about std::sort
, then std::sort
works without any knowledge about the mechanics of the actual container. If works on a range of random-access iterators. Since std::deque
's iterators are random-access iterators, std::sort
can be applied to std::deque
. And one can actually argue that random access to elements of such data structure is rather efficient. It is not as efficient as random access in a vector, but it is still pretty efficient to make sense in the context of std::sort
.
You cannot use std::sort
with std::list
because std::list
's iterators are not random access iterators. If you wish, you can implement your own trivial (slow) version of random-access iterator for std::list
. You will be able to apply std::sort
to a range of such iterators and thus sort an std::list
with std::sort
. But for obvious reasons it will be prohibitively inefficient.
In case of std::deque
random access iterators are more than adequately efficient.
I'm not prepared to answer the third question. In fact I wouldn't be surprised to find out that these sizes are chosen empirically, based on a bunch of experiments. And, of course, there's probably no "one size fits all" solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With