Below i will give you the core code of deque, mainly about three parts:
iterator
How to construct a deque
1. iterator(__deque_iterator)
The main problem of iterator is, when ++, -- iterator, it may skip to other chunk(if it pointer to edge of chunk). For example, there are three data chunks: chunk 1,chunk 2,chunk 3.
The pointer1 pointers to the begin of chunk 2, when operator --pointer it will pointer to the end of chunk 1, so as to the pointer2.
Below I will give the main function of __deque_iterator:
Firstly, skip to any chunk:
voidset_node(map_pointer new_node){
node = new_node;
first =*new_node;
last = first +chunk_size();}
Note that, the chunk_size() function which compute the chunk size, you can think of it returns 8 for simplify here.
Then push_back again, it will invoke allocate new chunk:
push_back(3)
If we push_front, it will allocate new chunk before the prev start
Note when push_back element into deque, if all the maps and chunks are filled, it will cause allocate new map, and adjust chunks.But the above code may be enough for you to understand deque.
c++ - What really is a deque in STL?
https://ift.tt/r2WCY7U
From overview, you can think
deque
as adouble-ended queue
The datas in
deque
are stored by chuncks of fixed size vector, which arepointered by a
map
(which is also a chunk of vector, but its size may change)The main part code of the
deque iterator
is as below:The main part code of the
deque
is as below:Below i will give you the core code of
deque
, mainly about three parts:iterator
How to construct a
deque
1. iterator(
__deque_iterator
)The main problem of iterator is, when ++, -- iterator, it may skip to other chunk(if it pointer to edge of chunk). For example, there are three data chunks:
chunk 1
,chunk 2
,chunk 3
.The
pointer1
pointers to the begin ofchunk 2
, when operator--pointer
it will pointer to the end ofchunk 1
, so as to thepointer2
.Below I will give the main function of
__deque_iterator
:Firstly, skip to any chunk:
Note that, the
chunk_size()
function which compute the chunk size, you can think of it returns 8 for simplify here.operator*
get the data in the chunkoperator++, --
// prefix forms of increment
iterator skip n steps / random access2. How to construct a
deque
common function of
deque
Let's assume
i_deque
has 20 int elements0~19
whose chunk size is 8, and now push_back 3 elements (0, 1, 2) toi_deque
:It's internal structure like below:
Then push_back again, it will invoke allocate new chunk:
If we
push_front
, it will allocate new chunk before the prevstart
Note when
push_back
element into deque, if all the maps and chunks are filled, it will cause allocate new map, and adjust chunks.But the above code may be enough for you to understanddeque
.via Stack Overflow https://ift.tt/TwkB7Ni
October 23, 2024 at 11:15AM