diff options
-rwxr-xr-x | Heap.cs | 8 | ||||
-rwxr-xr-x | PriorityQueue.cs | 8 |
2 files changed, 8 insertions, 8 deletions
@@ -40,8 +40,8 @@ namespace PriorityQueue Array[other] = temp;
}
- protected bool IsMoreSignificant(T that, T other) =>
- that.CompareTo(other) * (IsMin ? -1 : 1) > 0;
+ protected int CompareKey(T that, T other) =>
+ that.CompareTo(other) * (IsMin ? -1 : 1);
// build local heap
protected void Heapify(int index)
@@ -50,10 +50,10 @@ namespace PriorityQueue int left = 2 * index + 1;
int right = 2 * index + 2;
- if (left <= HeapSize && IsMoreSignificant(Array[left], Array[sig]))
+ if (left <= HeapSize && CompareKey(Array[left], Array[sig]) > 0)
sig = left;
- if (right <= HeapSize && IsMoreSignificant(Array[right], Array[sig]))
+ if (right <= HeapSize && CompareKey(Array[right], Array[sig]) > 0)
sig = right;
if (sig != index)
diff --git a/PriorityQueue.cs b/PriorityQueue.cs index d708f58..9dc2346 100755 --- a/PriorityQueue.cs +++ b/PriorityQueue.cs @@ -67,20 +67,20 @@ namespace PriorityQueue return sig;
}
- protected bool IsMoreSignificant(U that, U other) =>
- that.CompareTo(other) * (IsMin ? -1 : 1) > 0;
+ protected int CompareKey(U that, U other) =>
+ that.CompareTo(other) * (IsMin ? -1 : 1);
public void ChangeKey(int id, U key)
{
int index = Location[id];
// key <= Array[index].Key
- if (!IsMoreSignificant(key, Array[index].Key))
+ if (CompareKey(key, Array[index].Key) < 0)
throw new System.ArgumentException($"New key ({key}) is less significant than node {id}'s current key ({Array[index].Key}).");
Array[index].Key = key;
- while (index > 0 && IsMoreSignificant(Array[index], Array[Parent(index)]))
+ while (index > 0 && CompareKey(Array[index], Array[Parent(index)]) > 0)
{
Swap(index, Parent(index));
index = Parent(index);
|