How are Deques implemented? I've read that they use arrays of arrays. Is that right?
Maybe something like this?
Code:Block<T> base[8]; template<typename T> struct Block { T* data; int size; };
How are Deques implemented? I've read that they use arrays of arrays. Is that right?
Maybe something like this?
Code:Block<T> base[8]; template<typename T> struct Block { T* data; int size; };
There's no defined way of implementing a deque, but yes, an array of fixed-sized arrays is often how it's done. That way any individual index can be determined by some quick arithmetic and constant time array access. In your example the Block size is not fixed, but in most implementations I would imagine the block size is fixed and the array of blocks is dynamic.
Ok. Thanks.
Is there any default size for a block (I'm thinking eight)?
It depends on the implementation. I'm not sure if there is a commonly used size. I would imagine the blocks would be much larger than 8, though. You can try to look through your standard library's source code to see (the documentation might also mention it, but that's not likely). You could also probably figure it out with some code that outputs the memory location of elements in a deque.
You can implement a deque however you like, it's an abstract data type. I used a linked-list for a deque last time I had to implement one myself.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
>> I used a linked-list for a deque last time I had to implement one myself.
You're referring to a double-ended queue in general rather than the deque interface specified by the standard, right?
Does the standard say ANYTHING about how it should be implemented [other than the interface it must follow]?
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
If I'm not wrong it needs constant-time random access, something that would be impossible to achieve with nothing more than a linked list.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
There are complexity guarantees; with a linked list random access does not seem possible.Does the standard say ANYTHING about how it should be implemented [other than the interface it must follow]?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
>> You're referring to a double-ended queue in general rather than the deque interface specified by the standard, right?
FYI, when I said that, I was actually asking iMalc who mentioned implementing a deque with a linked list.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"