A Generic PriorityQueue<T>

Earlier this week, I wrote a simple PriorityQueue to handle implementing a pathfinding solution with movement costs. In my initial writing, I used a special handler class, which required the end user of the PriorityQueue to create a subclass and include their object in it, but it occurred to me that this is not at all the way List, Queue or Stack work. They let you bind an object type and operate on those objects with no real interest in the actual properties of . With a little research, I adapted my PriorityQueue to work in this same manner. It still uses a “helper” class to store the objects in the queue, but you don’t need to use it, it’s all used behind the scenes in PriorityQueue.

So now, it’s a matter of simply declaring a PriorityQueue of the type of object you want to store, in the case of our game,

PriorityQueue<Waypoint> queue = new PriorityQueue<Waypoint>();

To add the the queue, simply

queue.Enqueue(waypoint, priority);

To pull the next item (based on priority) from the queue, simply use

 waypoint=queue.Dequeue();

The complete source code can be found Here

A wiki explaining its use can be found here

Privacy & Terms