www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Queue

reply lurker <lurker mailinator.com> writes:
Is there a Queue in Phobos? I can only see a SList on the online documentation.

Any plans to add other containers?

Thanks.
Aug 10 2010
next sibling parent Jonathan M Davis <jmdavisprog gmail.com> writes:
On Tuesday, August 10, 2010 16:05:29 lurker wrote:
 Is there a Queue in Phobos? I can only see a SList on the online
 documentation.
 
 Any plans to add other containers?
 
 Thanks.
std.container is quite new. More containers will be added. Likely any basic container that you can think of will eventually be in std.container in one form or another. It's just a bit sparse, since it's new. At least we now have _something_. Before the last release, std.container didn't even exist. - Jonathan M Davis
Aug 10 2010
prev sibling parent Trass3r <un known.com> writes:
 Is there a Queue in Phobos? I can only see a SList on the online  
 documentation.
Is this really a coincidence? ^^ I recently opened the very same thread in D.learn. There seems to be none yet. --- I found some very old queue code with doubling strategy. Improvements welcome :D module queue; class Queue(T) { private: T[] data; uint head=0; uint tail=0; public: this(uint size) { data.length=size; } this() { data.length=16; } void enqueue(T elem) { uint tt = (tail+1) % data.length; if (tt != head) // free { data[tail]=elem; tail=tt; } else // double it { uint m = data.length; T[] newdata; newdata.length=m * 2; for(uint i=0; i<m-2; i++) newdata[i] = data[(head+i)%m]; // copy entries data=newdata; head=0; tail=m-1; data[tail]=elem; tail++; } } T first() in { assert(hasElements()); } body { return data[head]; } T dequeue() in { assert(hasElements()); } body { T tmp = data[head]; head = (head+1)%data.length; return tmp; } bool hasElements() {return head!=tail;} // hasElements property }
Aug 10 2010