From d74660f7e54378be80c1a98f431de8bc6c9e854f Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sun, 5 Aug 2018 12:03:36 -0400 Subject: [PATCH] optimized --- .../calc/openset/BinaryHeapOpenSet.java | 59 ++++++++----------- 1 file changed, 26 insertions(+), 33 deletions(-) diff --git a/src/main/java/baritone/bot/pathing/calc/openset/BinaryHeapOpenSet.java b/src/main/java/baritone/bot/pathing/calc/openset/BinaryHeapOpenSet.java index dd539553..eca1803e 100644 --- a/src/main/java/baritone/bot/pathing/calc/openset/BinaryHeapOpenSet.java +++ b/src/main/java/baritone/bot/pathing/calc/openset/BinaryHeapOpenSet.java @@ -36,10 +36,9 @@ public class BinaryHeapOpenSet implements IOpenSet { array = Arrays.copyOf(array, array.length * 2); } size++; - int index = size; - value.heapPosition = index; - array[index] = value; - upHeap(index); + value.heapPosition = size; + array[size] = value; + upHeap(size); } public void update(PathNode node) { @@ -67,21 +66,28 @@ public class BinaryHeapOpenSet implements IOpenSet { } int index = 1; int smallerChild = 2; - double cost = array[index].combinedCost; + PathNode val = array[index]; + double cost = val.combinedCost; do { int right = smallerChild + 1; - double smallerChildCost = array[smallerChild].combinedCost; + PathNode smallerChildNode = array[smallerChild]; + double smallerChildCost = smallerChildNode.combinedCost; if (right <= size) { - double rightChildCost = array[right].combinedCost; + PathNode rightChildNode = array[right]; + double rightChildCost = rightChildNode.combinedCost; if (smallerChildCost > rightChildCost) { smallerChild = right; smallerChildCost = rightChildCost; + smallerChildNode = rightChildNode; } } if (cost <= smallerChildCost) { break; } - swap(index, smallerChild); + array[index] = smallerChildNode; + array[smallerChild] = val; + val.heapPosition = smallerChild; + smallerChildNode.heapPosition = index; index = smallerChild; smallerChild = index << 1; } while (smallerChild <= size); @@ -89,31 +95,18 @@ public class BinaryHeapOpenSet implements IOpenSet { } private void upHeap(int index) { - int parent = index >>> 1; - double cost = array[index].combinedCost; - while (index > 1 && array[parent].combinedCost > cost) { - swap(index, parent); - index = parent; - parent = index >>> 1; + int parentInd = index >>> 1; + PathNode val = array[index]; + double cost = val.combinedCost; + PathNode parentNode = array[parentInd]; + while (index > 1 && parentNode.combinedCost > cost) { + array[index] = parentNode; + array[parentInd] = val; + val.heapPosition = parentInd; + parentNode.heapPosition = index; + index = parentInd; + parentInd = index >>> 1; + parentNode = array[parentInd]; } } - - - /** - * Swaps the elements at the specified indices. - * - * @param index1 The first index - * @param index2 The second index - */ - protected void swap(int index1, int index2) { - //sanity checks, disabled because of performance hit - //if (array[index1].heapPosition != index1) throw new IllegalStateException(); - //if (array[index2].heapPosition != index2) throw new IllegalStateException(); - PathNode tmp = array[index1]; - array[index1] = array[index2]; - array[index2] = tmp; - tmp.heapPosition = index2; - array[index1].heapPosition = index1; - } - }