source: lost-perception/astar/BinaryHeap.java

Last change on this file was ebd3538, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 6 years ago

Initial commit. This codebase for the Lost Perception game was created by decompiling code from a jar file.

  • Property mode set to 100644
File size: 3.0 KB
Line 
1package astar;
2
3import java.util.ArrayList;
4
5public class BinaryHeap
6{
7 public ArrayList<AStarNode> arr;
8
9 public BinaryHeap() {
10 this.arr = new ArrayList<AStarNode>();
11 }
12
13 private int index(final int i) {
14 return i - 1;
15 }
16
17 public boolean isEmpty() {
18 return this.arr.size() == 0;
19 }
20
21 public void add(final AStarNode c) {
22 this.arr.add(c);
23 int pos = this.arr.size();
24 c.openListPos = pos;
25 while (this.index(pos) > 0 && this.arr.get(this.index(pos)).compareTo((AStarNode)this.arr.get(this.index(pos / 2))) < 0) {
26 final AStarNode temp = this.arr.get(this.index(pos / 2));
27 this.arr.set(this.index(pos / 2), this.arr.get(this.index(pos)));
28 this.arr.set(this.index(pos), temp);
29 this.arr.get(this.index(pos)).openListPos = pos;
30 pos /= 2;
31 }
32 this.arr.get(this.index(pos)).openListPos = pos;
33 }
34
35 public AStarNode removeSmallest() {
36 final AStarNode smallest = this.arr.get(0);
37 this.arr.set(0, this.arr.get(this.arr.size() - 1));
38 this.arr.get(0).openListPos = 1;
39 this.arr.remove(this.arr.size() - 1);
40 int pos = 1;
41 boolean done = false;
42 while (this.index(pos) < this.arr.size() && !done) {
43 int smallestIndex = -1;
44 if (this.index(pos * 2) < this.arr.size() && this.arr.get(this.index(pos * 2)).compareTo((AStarNode)this.arr.get(this.index(pos))) < 0) {
45 smallestIndex = pos * 2;
46 }
47 if (this.index(pos * 2 + 1) < this.arr.size() && this.arr.get(this.index(pos * 2 + 1)).compareTo((AStarNode)this.arr.get(this.index(pos))) < 0 && (smallestIndex == -1 || this.arr.get(this.index(pos * 2 + 1)).compareTo((AStarNode)this.arr.get(this.index(pos * 2))) < 0)) {
48 smallestIndex = pos * 2 + 1;
49 }
50 if (smallestIndex == -1) {
51 done = true;
52 }
53 else {
54 final AStarNode temp = this.arr.get(this.index(pos));
55 this.arr.set(this.index(pos), this.arr.get(this.index(smallestIndex)));
56 this.arr.set(this.index(smallestIndex), temp);
57 this.arr.get(this.index(pos)).openListPos = pos;
58 this.arr.get(this.index(smallestIndex)).openListPos = smallestIndex;
59 pos = smallestIndex;
60 }
61 }
62 return smallest;
63 }
64
65 public void update(final AStarNode o) {
66 int pos = o.openListPos;
67 for (int parent = pos / 2; parent != 0 && this.arr.get(this.index(pos)).compareTo((AStarNode)this.arr.get(this.index(parent))) < 0; pos = parent, parent /= 2) {
68 final AStarNode temp = this.arr.get(this.index(parent));
69 this.arr.set(this.index(parent), this.arr.get(this.index(pos)));
70 this.arr.set(this.index(pos), temp);
71 this.arr.get(this.index(pos)).openListPos = pos;
72 }
73 this.arr.get(this.index(pos)).openListPos = pos;
74 }
75}
Note: See TracBrowser for help on using the repository browser.