baritone/src/main/java/baritone/pathing/calc/openset/BinaryHeapOpenSet.java

134 lines
3.8 KiB
Java
Raw Normal View History

2018-08-08 03:16:53 +00:00
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
2018-08-08 03:16:53 +00:00
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
2018-08-08 03:16:53 +00:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
2018-08-08 03:16:53 +00:00
*
* You should have received a copy of the GNU Lesser General Public License
2018-08-08 03:16:53 +00:00
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
2018-08-22 20:15:56 +00:00
package baritone.pathing.calc.openset;
2018-08-03 20:31:33 +00:00
2018-08-22 20:15:56 +00:00
import baritone.pathing.calc.PathNode;
import java.util.Arrays;
2018-08-06 01:17:04 +00:00
/**
* A binary heap implementation of an open set. This is the one used in the AStarPathFinder.
*
* @author leijurv
*/
2018-08-29 22:35:41 +00:00
public final class BinaryHeapOpenSet implements IOpenSet {
2018-08-03 20:31:33 +00:00
/**
* The initial capacity of the heap (2^10)
*/
private static final int INITIAL_CAPACITY = 1024;
2018-08-03 20:31:33 +00:00
/**
* The array backing the heap
*/
private PathNode[] array;
2018-08-03 20:31:33 +00:00
/**
* The size of the heap
*/
private int size;
public BinaryHeapOpenSet() {
this(INITIAL_CAPACITY);
}
public BinaryHeapOpenSet(int size) {
this.size = 0;
this.array = new PathNode[size];
}
2018-08-28 23:15:24 +00:00
public int size() {
return size;
}
2018-08-03 20:31:33 +00:00
@Override
2018-08-19 16:37:35 +00:00
public final void insert(PathNode value) {
if (size >= array.length - 1) {
array = Arrays.copyOf(array, array.length * 2);
}
size++;
2018-08-05 16:03:36 +00:00
value.heapPosition = size;
array[size] = value;
2018-08-19 16:37:35 +00:00
update(value);
}
2018-08-19 16:37:35 +00:00
@Override
2018-08-19 16:39:52 +00:00
public final void update(PathNode val) {
int index = val.heapPosition;
2018-08-19 16:37:35 +00:00
int parentInd = index >>> 1;
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];
}
}
2018-08-03 20:31:33 +00:00
@Override
2018-08-19 16:37:35 +00:00
public final boolean isEmpty() {
return size == 0;
}
2018-08-03 20:31:33 +00:00
@Override
2018-08-19 16:37:35 +00:00
public final PathNode removeLowest() {
2018-08-06 18:48:30 +00:00
if (size == 0) {
throw new IllegalStateException();
}
2018-08-06 18:48:30 +00:00
PathNode result = array[1];
PathNode val = array[size];
array[1] = val;
2018-08-05 21:16:16 +00:00
val.heapPosition = 1;
2018-08-06 18:48:30 +00:00
array[size] = null;
size--;
result.heapPosition = -1;
2018-08-06 18:48:30 +00:00
if (size < 2) {
2018-08-05 15:50:23 +00:00
return result;
}
2018-08-05 15:50:23 +00:00
int index = 1;
int smallerChild = 2;
2018-08-05 16:03:36 +00:00
double cost = val.combinedCost;
2018-08-05 15:50:23 +00:00
do {
2018-08-06 18:48:30 +00:00
PathNode smallerChildNode = array[smallerChild];
double smallerChildCost = smallerChildNode.combinedCost;
2018-08-29 23:13:14 +00:00
if (smallerChild < size) {
PathNode rightChildNode = array[smallerChild + 1];
2018-08-05 16:03:36 +00:00
double rightChildCost = rightChildNode.combinedCost;
2018-08-05 15:50:23 +00:00
if (smallerChildCost > rightChildCost) {
2018-08-29 23:13:14 +00:00
smallerChild++;
2018-08-05 15:50:23 +00:00
smallerChildCost = rightChildCost;
2018-08-05 16:03:36 +00:00
smallerChildNode = rightChildNode;
2018-08-05 15:50:23 +00:00
}
}
2018-08-05 15:50:23 +00:00
if (cost <= smallerChildCost) {
break;
}
2018-08-06 18:48:30 +00:00
array[index] = smallerChildNode;
array[smallerChild] = val;
2018-08-05 16:03:36 +00:00
val.heapPosition = smallerChild;
smallerChildNode.heapPosition = index;
index = smallerChild;
2018-08-29 23:13:14 +00:00
} while ((smallerChild <<= 1) <= size);
2018-08-05 15:50:23 +00:00
return result;
}
}