views:

224

answers:

3

I'm implementing Dijkstra's on a board of tiles. I want to store all the tiles in a Priority Queue, sorted by their distance from the starting location. In Java, this would be something like:

Queue<Point> pq = new PriorityQueue<Point>(new Comparator() { /* sort by distance from start */ });

What would the equivalent by in C# XNA? C# has a PriorityQueue class, but that only works for IComparable objects, which Point objects are not.

+1  A: 

I think the easy solution is to implement your own class for storing a node and make it comparable (by implementing the IComparable interface).

Jan
A: 

It is very likely that the PriorityQueue implementation will use a heap internally, which needs an ordering among its elements. In fact, the contract seems to require it, even if you don't care about the implementation (you should care, but not assume what it is, though).

So if Point does not already implement IComparable, you need to derive/contain it and do it yourself.

Moron
The API for PriorityQueue does indeed state that it uses a heap internally.
Kevin
+1  A: 

As you can't derive from the struct Point, you could do a minimal implementation of a IComparable ComparablePoint class, with a Point as a composite member.

Luhmann