473,398 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

deque implementation?

According to SGI's STL reference, a deque:

Supports amortized constant time random access (like a vector)
Allows amortized constant time insertions at the beginning and end

One way you could implement this would be to allocate a chunk of memory
and start in the middle rather than in the beginning, but then deque
would have a reserve() function. So how is deque usually implemented?

Thanks,
-Peter

Nov 22 '05 #1
3 7977
Peter Ammon wrote:
According to SGI's STL reference, a deque:

Supports amortized constant time random access (like a vector)
Allows amortized constant time insertions at the beginning and end

One way you could implement this would be to allocate a chunk of memory
and start in the middle rather than in the beginning, but then deque
would have a reserve() function. So how is deque usually implemented?


No, it is usually an array of arrays. One end grows from 0 to x and the
other grows from x to 0.

......<=
=======
=======
=>.....
Jonathan

Nov 22 '05 #2

Jonathan Mcdougall wrote:
Peter Ammon wrote:
According to SGI's STL reference, a deque:

Supports amortized constant time random access (like a vector)
Allows amortized constant time insertions at the beginning and end

One way you could implement this would be to allocate a chunk of memory
and start in the middle rather than in the beginning, but then deque
would have a reserve() function. So how is deque usually implemented?


No, it is usually an array of arrays. One end grows from 0 to x and the
other grows from x to 0.

.....<=
=======
=======
=>.....


Thanks for your reply, but I am still confused. How do you manage this
array of subarrays? You need the ability to insert a subarray at
either end with good performance to maintain the deque's performance
contract. What data structure allows you to insert stuff at either end
with good performance? A deque, of course. You can see my confusion.

To put it another way: Let's say a deque is a naive array of arrays;
how long is each of these subarrays? If each is a fixed length, say,
L, then inserting L elements into the beginning of a deque of N
elements will require allocating a new subarray, moving every subarray
pointer in the outer array forward by one, and then inserting the new
subarray at the beginning. That second step requires you to copy N/L
pointers to insert L elements; the work per element is therefore
N/L**L, which is O(N). But a deque is supposed to have O(1)
insertions.

-Peter

Nov 22 '05 #3
On 2005-11-17, Peter Ammon <pa****@gmail.com> wrote:

Jonathan Mcdougall wrote:
Peter Ammon wrote:
> According to SGI's STL reference, a deque:
>
> Supports amortized constant time random access (like a vector)
> Allows amortized constant time insertions at the beginning and end
>
> One way you could implement this would be to allocate a chunk of memory
> and start in the middle rather than in the beginning, but then deque
> would have a reserve() function. So how is deque usually implemented?
No, it is usually an array of arrays. One end grows from 0 to x and the
other grows from x to 0.

.....<=
=======
=======
=>.....


Thanks for your reply, but I am still confused. How do you manage this
array of subarrays? You need the ability to insert a subarray at
either end with good performance to maintain the deque's performance
contract. What data structure allows you to insert stuff at either end
with good performance? A deque, of course. You can see my confusion.

To put it another way: Let's say a deque is a naive array of
arrays; how long is each of these subarrays? If each is a
fixed length, say, L, then inserting L elements into the
beginning of a deque of N elements will require allocating a
new subarray, moving every subarray pointer in the outer array
forward by one, and then inserting the new subarray at the
beginning.


Yes, and the memory will be allocated in such a way that this
seldom happens, as with a vector.
That second step requires you to copy N/L pointers to insert L
elements; the work per element is therefore N/L**L, which is
O(N). But a deque is supposed to have O(1) insertions.


O(1) amortized time. I think that's how they put it. Memory
allocation won't have to take place very often with a good
implementation.

A good and simple approach doubles the size whenever capacity
fills up. Starting from zero capacity, it requires only 14
allocations for 10000 calls to push_front. So log2(N) times out
of N, performance is O(N). That's a very small ratio.

--
Neil Cerutti
Nov 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: chris | last post by:
Hello, I've recently been trying to understand the various structures supplied by c++, and the one I find most confusing is deque. One quick question about this. It seems most implementations...
9
by: R.Z. | last post by:
i was wondering whether it pays off in terms of memory use to maintain lots of empty deques (it would be convenient for my algorithms but memory use is more important). and does the size of a deque...
5
by: Russell Warren | last post by:
Does anyone have an easier/faster/better way of popping from the middle of a deque than this? class mydeque(deque): def popmiddle(self, pos): self.rotate(-pos) ret = self.popleft()...
9
by: toton | last post by:
Hi, I am looking for a circular buffer solution ( a fixed buffer) , where the elements can be pushed back & removed front. It looks stl deque can be a solution. my question is, 1) circular...
7
by: DevNull | last post by:
Hello everyone, I decided to pick c++ back up after not really having used it much in 10 years. Just as a point of refference, there was no standard C++ last time I used regularly. Anyways...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
8
by: t | last post by:
The stack container adaptor uses deque as its default underlying sequential container. I don't understand why since we only add elements and remove elements from one side of a stack. Why isn't...
1
by: Donos | last post by:
Hello I have the following declaration in my code, ****************************************************************** struct mHandleObj { HANDLE h; };
29
by: NvrBst | last post by:
I've read a bit online seeing that two writes are not safe, which I understand, but would 1 thread push()'ing and 1 thread pop()'ing be thread-safe? Basically my situation is the follows: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.