On Friday, 2 November 2012 14:20:23 UTC+2, Werner wrote:
> On Friday, November 2, 2012 2:04:35 PM UTC+2, bintom wrote:
> > On Friday, November 2, 2012 4:59:54 PM UTC+5:30, Victor Bazarov wrote:
> > > On 11/2/2012 6:06 AM, bintom wrote:
> > >
> > > > Is there any difference between a circular queue and a double-ended queue (deque)?
> > >
> > > >
> > >
> > >
> > >
> > > Yes.
> > >
> > >
> > >
> > > V
> > >
> > > --
> > >
> > > I do not respond to top-posted replies, please don't ask
> >
> > Thanks Victor,
> >
> > Now I need somebody else to tell me that difference ...
> >
> > Thanks in advance.
>
> There is a good article concerning both these data structures
> in wikipedia. The one is called Circular Buffer (to my knowledge
> the same as what you refer to as circular queue). The other
> deque/double-ended queue.
The difference is still that ring buffer has *fixed* size. So there are no growth above limit and so there are two implementations when limit is reached:
1) discarding: push to one end of full buffer silently discards an element from other end.
2) refusing: push to full buffer fails.
Deque grows and shrinks dynamically so ends are not related to each other. If lots are pushed and few are popped long enough then it is also "refusing": you get bad_alloc inevitably into your face.
> A circular buffer is typically used (IME) to buffer data
> in communication layers. Writes and reads only take place in
> one direction (refer to the wiki article).
I have seen that the discarding circular buffer is used for storing such data where old data is least important and recent data is most important. So the contents of such circular buffer can be viewed as The Most Recent things + some history. Things are pushed to one end and never popped. Typically diagnostics, alarms, faults, oscilloscope-like data.
Refusing circular buffer however is better than deque in all situations where sane limits for length of queue are not too large. It is considerably faster, does not fragment heap and timely refusal (before the memory is full) is very handy.
> Deques may grow in both directions, but can only grow from
> the ends. You can read/write from the beginning or end.
Circular buffer does not also have any limitations of direction. Growing is limited with fixed size, that is it.