This commit is contained in:
Leijurv 2018-08-06 11:48:30 -07:00
parent 25663c54e7
commit 3eef5e2972
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A

View File

@ -46,7 +46,6 @@ 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);
} }
@ -58,19 +57,17 @@ public class BinaryHeapOpenSet implements IOpenSet {
@Override @Override
public PathNode removeLowest() { public PathNode removeLowest() {
int s = --size; if (size == 0) {
if (s < 0) {
size++; // undo invalid decrement
throw new IllegalStateException(); throw new IllegalStateException();
} }
PathNode[] arr = array; PathNode result = array[1];
PathNode result = arr[1]; PathNode val = array[size];
PathNode val = arr[s + 1]; array[1] = val;
arr[1] = val;
val.heapPosition = 1; val.heapPosition = 1;
arr[s + 1] = null; array[size] = null;
size--;
result.heapPosition = -1; result.heapPosition = -1;
if (s < 2) { if (size < 2) {
return result; return result;
} }
int index = 1; int index = 1;
@ -78,30 +75,27 @@ 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 = arr[smallerChild]; PathNode smallerChildNode = array[smallerChild];
double smallerChildCost; double smallerChildCost = smallerChildNode.combinedCost;
if (right <= s) { if (right <= size) {
PathNode rightChildNode = arr[right]; PathNode rightChildNode = array[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;
} }
arr[index] = smallerChildNode; array[index] = smallerChildNode;
arr[smallerChild] = val; array[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 <= s); } while (smallerChild <= size);
return result; return result;
} }