more binary heap performance optims
This commit is contained in:
parent
dff3e1efe5
commit
25663c54e7
@ -46,6 +46,7 @@ public class BinaryHeapOpenSet implements IOpenSet {
|
|||||||
upHeap(size);
|
upHeap(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void update(PathNode node) {
|
public void update(PathNode node) {
|
||||||
upHeap(node.heapPosition);
|
upHeap(node.heapPosition);
|
||||||
}
|
}
|
||||||
@ -57,17 +58,19 @@ public class BinaryHeapOpenSet implements IOpenSet {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PathNode removeLowest() {
|
public PathNode removeLowest() {
|
||||||
if (size == 0) {
|
int s = --size;
|
||||||
|
if (s < 0) {
|
||||||
|
size++; // undo invalid decrement
|
||||||
throw new IllegalStateException();
|
throw new IllegalStateException();
|
||||||
}
|
}
|
||||||
PathNode result = array[1];
|
PathNode[] arr = array;
|
||||||
PathNode val = array[size];
|
PathNode result = arr[1];
|
||||||
array[1] = val;
|
PathNode val = arr[s + 1];
|
||||||
|
arr[1] = val;
|
||||||
val.heapPosition = 1;
|
val.heapPosition = 1;
|
||||||
array[size] = null;
|
arr[s + 1] = null;
|
||||||
size--;
|
|
||||||
result.heapPosition = -1;
|
result.heapPosition = -1;
|
||||||
if (size < 2) {
|
if (s < 2) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
int index = 1;
|
int index = 1;
|
||||||
@ -75,27 +78,30 @@ public class BinaryHeapOpenSet implements IOpenSet {
|
|||||||
double cost = val.combinedCost;
|
double cost = val.combinedCost;
|
||||||
do {
|
do {
|
||||||
int right = smallerChild + 1;
|
int right = smallerChild + 1;
|
||||||
PathNode smallerChildNode = array[smallerChild];
|
PathNode smallerChildNode = arr[smallerChild];
|
||||||
double smallerChildCost = smallerChildNode.combinedCost;
|
double smallerChildCost;
|
||||||
if (right <= size) {
|
if (right <= s) {
|
||||||
PathNode rightChildNode = array[right];
|
PathNode rightChildNode = arr[right];
|
||||||
|
smallerChildCost = smallerChildNode.combinedCost;
|
||||||
double rightChildCost = rightChildNode.combinedCost;
|
double rightChildCost = rightChildNode.combinedCost;
|
||||||
if (smallerChildCost > rightChildCost) {
|
if (smallerChildCost > rightChildCost) {
|
||||||
smallerChild = right;
|
smallerChild = right;
|
||||||
smallerChildCost = rightChildCost;
|
smallerChildCost = rightChildCost;
|
||||||
smallerChildNode = rightChildNode;
|
smallerChildNode = rightChildNode;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
smallerChildCost = smallerChildNode.combinedCost;
|
||||||
}
|
}
|
||||||
if (cost <= smallerChildCost) {
|
if (cost <= smallerChildCost) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
array[index] = smallerChildNode;
|
arr[index] = smallerChildNode;
|
||||||
array[smallerChild] = val;
|
arr[smallerChild] = val;
|
||||||
val.heapPosition = smallerChild;
|
val.heapPosition = smallerChild;
|
||||||
smallerChildNode.heapPosition = index;
|
smallerChildNode.heapPosition = index;
|
||||||
index = smallerChild;
|
index = smallerChild;
|
||||||
smallerChild = index << 1;
|
smallerChild = index << 1;
|
||||||
} while (smallerChild <= size);
|
} while (smallerChild <= s);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user