Closed daleao closed 4 weeks ago
I'm not able to reproduce this issue. Specifically, I added this unit test, which passes
public readonly struct TestKey : IComparable<TestKey>
{
public readonly float Key1;
public readonly float Key2;
public TestKey(float key1, float key2)
{
Key1 = key1;
Key2 = key2;
}
public static bool operator <(TestKey left, TestKey right) => left.Key1 < right.Key1 || (left.Key1 == right.Key1 && left.Key2 < right.Key2);
public static bool operator >(TestKey left, TestKey right) => left.Key1 > right.Key1 || (left.Key1 == right.Key1 && left.Key2 > right.Key2);
public static bool operator ==(TestKey left, TestKey right) => left.Key1 == right.Key1 && left.Key2 == right.Key2;
public static bool operator !=(TestKey left, TestKey right) => !(left == right);
public int CompareTo(TestKey other)
{
return this < other ? -1 : this > other ? 1 : 0;
}
public override bool Equals(object @object)
{
return @object is TestKey key && this == key;
}
public override int GetHashCode()
{
return Key1.GetHashCode() ^ Key2.GetHashCode();
}
public override string ToString()
{
return $"{this.Key1}, {this.Key2}";
}
}
public sealed class TestNode : GenericPriorityQueueNode<TestKey>
{
public TestNode() { }
}
[Test]
public void TestQueueWithCustomKey()
{
TestNode node1 = new TestNode();
TestNode node2 = new TestNode();
TestNode node3 = new TestNode();
TestKey key1 = new TestKey(1, 0);
TestKey key2 = new TestKey(2, 0);
TestKey key3 = new TestKey(3, 0);
SimplePriorityQueue<TestNode, TestKey> queue = new SimplePriorityQueue<TestNode, TestKey>();
queue.Enqueue(node3, key3);
queue.Enqueue(node1, key1);
queue.Enqueue(node2, key2);
Assert.IsTrue(queue.IsValidQueue());
Assert.AreEqual(key1, queue.GetPriority(node1));
Assert.AreEqual(key2, queue.GetPriority(node2));
Assert.AreEqual(key3, queue.GetPriority(node3));
Assert.AreEqual(node1, queue.Dequeue());
Assert.AreEqual(node2, queue.Dequeue());
Assert.AreEqual(node3, queue.Dequeue());
}
Less importantly, using a custom node class that extends GenericPriorityQueueNode
shouldn't be necessary with SimplePriorityQueue
. It won't hurt anything, it's just unnecessary.
I'm using a
SimplePriorityQueue<TItem, TPriority>
, whereTItem : GenericPriorityQueueNode<TPriority>
andTPriority : IComparable<TPriority>
; i.e., my Node class is derived fromGenericPriorityQueueNode
and uses a custom struct as the priority.The prioritization seems to work correctly. But immediately after calling
.Enqueue(item, priority)
, thePriority
property of the queued item iis set to the default value, instead of the valuepriority
which was passed toEnqueue
.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):