| 1 | package net.minecraft.world.level.pathfinder;
|
| 2 |
|
| 3 | import java.util.Arrays;
|
| 4 |
|
| 5 | public class BinaryHeap {
|
| 6 | private Node[] heap = new Node[128];
|
| 7 | private int size;
|
| 8 |
|
| 9 | public Node insert(final Node node) {
|
| 10 | if (node.heapIdx >= 0) {
|
| 11 | throw new IllegalStateException("OW KNOWS!");
|
| 12 | }
|
| 13 |
|
| 14 | if (this.size == this.heap.length) {
|
| 15 | Node[] newHeap = new Node[this.size << 1];
|
| 16 | System.arraycopy(this.heap, 0, newHeap, 0, this.size);
|
| 17 | this.heap = newHeap;
|
| 18 | }
|
| 19 |
|
| 20 | this.heap[this.size] = node;
|
| 21 | node.heapIdx = this.size;
|
| 22 | this.upHeap(this.size++);
|
| 23 | return node;
|
| 24 | }
|
| 25 |
|
| 26 | public void clear() {
|
| 27 | this.size = 0;
|
| 28 | }
|
| 29 |
|
| 30 | public Node peek() {
|
| 31 | return this.heap[0];
|
| 32 | }
|
| 33 |
|
| 34 | public Node pop() {
|
| 35 | Node popped = this.heap[0];
|
| 36 | this.heap[0] = this.heap[--this.size];
|
| 37 | this.heap[this.size] = null;
|
| 38 | if (this.size > 0) {
|
| 39 | this.downHeap(0);
|
| 40 | }
|
| 41 |
|
| 42 | popped.heapIdx = -1;
|
| 43 | return popped;
|
| 44 | }
|
| 45 |
|
| 46 | public void remove(final Node node) {
|
| 47 | this.heap[node.heapIdx] = this.heap[--this.size];
|
| 48 | this.heap[this.size] = null;
|
| 49 | if (this.size > node.heapIdx) {
|
| 50 | if (this.heap[node.heapIdx].f < node.f) {
|
| 51 | this.upHeap(node.heapIdx);
|
| 52 | } else {
|
| 53 | this.downHeap(node.heapIdx);
|
| 54 | }
|
| 55 | }
|
| 56 |
|
| 57 | node.heapIdx = -1;
|
| 58 | }
|
| 59 |
|
| 60 | public void changeCost(final Node node, final float newCost) {
|
| 61 | float oldCost = node.f;
|
| 62 | node.f = newCost;
|
| 63 | if (newCost < oldCost) {
|
| 64 | this.upHeap(node.heapIdx);
|
| 65 | } else {
|
| 66 | this.downHeap(node.heapIdx);
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | public int size() {
|
| 71 | return this.size;
|
| 72 | }
|
| 73 |
|
| 74 | private void upHeap(int idx) {
|
| 75 | Node node = this.heap[idx];
|
| 76 | float cost = node.f;
|
| 77 |
|
| 78 | while (idx > 0) {
|
| 79 | int parentIdx = idx - 1 >> 1;
|
| 80 | Node parent = this.heap[parentIdx];
|
| 81 | if (!(cost < parent.f)) {
|
| 82 | break;
|
| 83 | }
|
| 84 |
|
| 85 | this.heap[idx] = parent;
|
| 86 | parent.heapIdx = idx;
|
| 87 | idx = parentIdx;
|
| 88 | }
|
| 89 |
|
| 90 | this.heap[idx] = node;
|
| 91 | node.heapIdx = idx;
|
| 92 | }
|
| 93 |
|
| 94 | private void downHeap(int idx) {
|
| 95 | Node node = this.heap[idx];
|
| 96 | float cost = node.f;
|
| 97 |
|
| 98 | while (true) {
|
| 99 | int leftIdx = 1 + (idx << 1);
|
| 100 | int rightIdx = leftIdx + 1;
|
| 101 | if (leftIdx >= this.size) {
|
| 102 | break;
|
| 103 | }
|
| 104 |
|
| 105 | Node leftNode = this.heap[leftIdx];
|
| 106 | float leftCost = leftNode.f;
|
| 107 | Node rightNode;
|
| 108 | float rightCost;
|
| 109 | if (rightIdx >= this.size) {
|
| 110 | rightNode = null;
|
| 111 | rightCost = Float.POSITIVE_INFINITY;
|
| 112 | } else {
|
| 113 | rightNode = this.heap[rightIdx];
|
| 114 | rightCost = rightNode.f;
|
| 115 | }
|
| 116 |
|
| 117 | if (leftCost < rightCost) {
|
| 118 | if (!(leftCost < cost)) {
|
| 119 | break;
|
| 120 | }
|
| 121 |
|
| 122 | this.heap[idx] = leftNode;
|
| 123 | leftNode.heapIdx = idx;
|
| 124 | idx = leftIdx;
|
| 125 | } else {
|
| 126 | if (!(rightCost < cost)) {
|
| 127 | break;
|
| 128 | }
|
| 129 |
|
| 130 | this.heap[idx] = rightNode;
|
| 131 | rightNode.heapIdx = idx;
|
| 132 | idx = rightIdx;
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | this.heap[idx] = node;
|
| 137 | node.heapIdx = idx;
|
| 138 | }
|
| 139 |
|
| 140 | public boolean isEmpty() {
|
| 141 | return this.size == 0;
|
| 142 | }
|
| 143 |
|
| 144 | public Node[] getHeap() {
|
| 145 | return Arrays.copyOf(this.heap, this.size);
|
| 146 | }
|
| 147 | }
|
| 148 |
|