GeniusOfEvil’s Blog

It is a sin to believe evil of others, but it is seldom a mistake.

Posts Tagged ‘template

Why complexity matters

leave a comment »

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

Advertisements

Written by Jarek Przygódzki

June 23, 2009 at 11:19 am

Posted in D, Geek, Programing

Tagged with , ,