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_frontoperation 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::sortprovides similar interface for sorting bothstd::vectorandstd::deque. How are different algoritms for different data structures implemented? Using a template specialization? If so, whystd::listcan't be sorted usingstd::sort? Or, maybe,std::sortdoesn't care about internal structure of this containers and just uses iterators and methods, that are similar in bothstd::vectorandstd::deque(likeoperator[],size(), etc)? This idea sounds reasonable and an answer to "why can'tstd::sortsortstd::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.