Archive for the ‘D’ Category
Why complexity matters
I needed a simple queue class – so I wrote one based on java.util.Queue interface
**
* Queue Template Class
* @author: Jarek Przygódzki <jarek.przygodzki@gmail.com>
* @version 0.1
**/
class Queue(E) {
this() {
}
bool isEmpty() {
return length is 0 ;
}
/**
* Retrieves and removes the head of this queue
*/
E poll(){
auto e = elements[$-1];
elements = elements[0..$-1];
return e;
}
/**
* Retrieves, but does not remove, the head of this queue
*/
E peek() {
return elements[$-1];
}
/**
* Inserts the specified element into this queue
*/
Queue!(E) offer(E e) {
elements ~= e;
return this;
}
/**
* Number of elements in queue
*/
uint length() {
return elements.length;
}
int opApply(int delegate(ref E e) dg) {
foreach(e;elements) dg(e);
return 0;
}
private E[] elements;
}
Of course it totally screwed up Edmonds-Karp algorithm complexity. But it worked ;D