Click here to Skip to main content
15,887,596 members
Articles / Programming Languages / C#
Article

A Dequeue - also called a ring-buffer

Rate me:
Please Sign up or sign in to vote.
1.19/5 (24 votes)
29 Mar 2004 68.7K   1.1K   15   7
Another addition to the System.Collections - a ring buffer, more sophisticated than ArrayList or Queue.

Introduction

Dequeue means 'double-ended-queue'. All who remember the good old STL days will know exactly what it is. For all the others: it's a System.Collections-compatible class implementing a queue that supports Enqueue and Dequeue at both ends.

Usage

The main functions (in addition to all the System.Collections-brabble) are:

  • EnqueueHead, EnqueueTail for adding to the DQ
  • DequeueHead, DequeueTail for taking from the DQ
  • EnqueueHeadRange, EnqueueTailRange for adding a collection to the DQ (the order is preserved)

The DQ allows enumerations and indexed access. For that, you need to know that the 'head' of the DQ is always the element with index 0, the tail is always the element with the index (Count-1). Enumeration is in that order as well.

EnqueueHead/Tail- and DequeueHead/Tail-operations are O(1)-complex, except when the buffer has to be grown, which will take O(n) steps (naturally). So you can see that the buffer is faster than an ArrayList and more flexible than a Queue.

Example

C#
Dequeue D = new Dequeue();
D.EnqueueTail("The");
D.EnqueueTail("big");
D.EnqueueTail("brown");
D.EnqueueTail("fox");
// now D is [The big brown fox ]
Dequeue D2 = new Dequeue();
D2.EnqueueHead("dog.");
D2.EnqueueHead("lazy");
D2.EnqueueHead("the");
D2.EnqueueHead("over");
D2.EnqueueHead("jumped");
// now D2 is [jumped over the lazy dog. ]
Dequeue D3 = new Dequeue();
D3.EnqueueTailRange(D2);
D3.EnqueueHeadRange(D);
// now D3 is [The big brown fox jumped over the lazy dog. ]

History

  • 30.3.04 - Inserted.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Germany Germany
I did my diploma in Dresden and Sydney where I dealt with algorithms, agents and other cool AI stuff. Now I moved to Frankfurt to work on my PhD dealing with software structures for artificial intelligence systems. If I can, I do things in C# and ASP.NET, but if I have to, my C++, Java and SQL are not that bad.
Long Live .NET.

Comments and Discussions

 
GeneralMy vote of 1 Pin
zecanard22-Oct-10 6:11
zecanard22-Oct-10 6:11 
Generalbug found Pin
Guggsdu13-Sep-04 0:01
Guggsdu13-Sep-04 0:01 
GeneralRe: bug found Pin
metalmonkey1-Jul-10 18:03
metalmonkey1-Jul-10 18:03 
Generalnot a ring buffer Pin
wyldeling30-Mar-04 9:19
wyldeling30-Mar-04 9:19 
GeneralRe: not a ring buffer Pin
BenDi30-Mar-04 9:57
BenDi30-Mar-04 9:57 
well, i guess youre right with the name but thats really not something to argue about.
The ring-buffer thing is more a reference to the implementation. In fact, it only describes the way the DQ uses the (linear) buffer - meaning it writes elements 'around' the linear buffer. Call it a self-extending ring-buffer if you wish. But you are right of course, no elements are ever overridden (behold!).
GeneralRe: not a ring buffer Pin
Anonymous18-Jun-04 16:21
Anonymous18-Jun-04 16:21 
GeneralRe: not a ring buffer Pin
swampmonster25-Jul-08 14:01
swampmonster25-Jul-08 14:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.