High-Speed-Priority-Queue-for-C-Sharp
High-Speed-Priority-Queue-for-C-Sharp copied to clipboard
Priority not persisted after enqueuing
I'm using a SimplePriorityQueue<TItem, TPriority>
, where TItem : GenericPriorityQueueNode<TPriority>
and TPriority : IComparable<TPriority>
; i.e., my Node class is derived from GenericPriorityQueueNode
and uses a custom struct as the priority.
The prioritization seems to work correctly. But immediately after calling .Enqueue(item, priority)
, the Priority
property of the queued item iis set to the default value, instead of the value priority
which was passed to Enqueue
.
Have I misunderstood how to use these objects? I'm implementing D* and was hoping to get the Key value from the Priority
property.
Minimal working example (included the whole Key
struct in case it's important):
public readonly struct Key(float k1, float k2) : IComparable<Key>
{
public readonly float K1 = k1;
public readonly float K2 = k2;
public static bool operator <(Key left, Key right) => left.K1 < right.K1 || (left.K1 == right.K1 && left.K2 < right.K2);
public static bool operator >(Key left, Key right) => left.K1 > right.K1 || (left.K1 == right.K1 && left.K2 > right.K2);
public static bool operator ==(Key left, Key right) => left.K1 == right.K1 && left.K2 == right.K2;
public static bool operator !=(Key left, Key right) => !(left == right);
public int CompareTo(Key other)
{
return this < other ? -1 : this > other ? 1 : 0;
}
public override bool Equals(object? @object)
{
return @object is Key key && this == key;
}
public override int GetHashCode()
{
return HashCode.Combine(this.K1, this.K2);
}
public override string ToString()
{
return $"{this.K1}, {this.K2}";
}
}
public sealed class Node : GenericPriorityQueueNode<Key>
{
public Node() { }
}
class Program
{
static void Main()
{
var queue = new SimplePriorityQueue<Node, Key>();
queue.Enqueue(new Node(), new Key(1, 1));
// inspect the queue here and notice that item 0 has Priority = Key {0, 0};
}
}