123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- #pragma warning disable 162
- #pragma warning disable 429
- #define DECREASE_KEY
- namespace Pathfinding {
-
-
-
-
-
-
-
-
-
-
-
-
- public class BinaryHeap {
-
- public int numberOfItems;
-
- public float growthFactor = 2;
-
-
-
-
-
- const int D = 4;
-
-
-
-
-
-
- const bool SortGScores = true;
- public const ushort NotInHeap = 0xFFFF;
-
- private Tuple[] heap;
-
- public bool isEmpty {
- get {
- return numberOfItems <= 0;
- }
- }
-
- private struct Tuple {
- public PathNode node;
- public uint F;
- public Tuple (uint f, PathNode node) {
- this.F = f;
- this.node = node;
- }
- }
-
-
-
-
- static int RoundUpToNextMultipleMod1 (int v) {
-
- return v + (4 - ((v-1) % D)) % D;
- }
-
- public BinaryHeap (int capacity) {
-
-
-
- capacity = RoundUpToNextMultipleMod1(capacity);
- heap = new Tuple[capacity];
- numberOfItems = 0;
- }
-
- public void Clear () {
- #if DECREASE_KEY
-
-
- for (int i = 0; i < numberOfItems; i++) {
- heap[i].node.heapIndex = NotInHeap;
- }
- #endif
- numberOfItems = 0;
- }
- internal PathNode GetNode (int i) {
- return heap[i].node;
- }
- internal void SetF (int i, uint f) {
- heap[i].F = f;
- }
-
- void Expand () {
-
- int newSize = System.Math.Max(heap.Length+4, System.Math.Min(65533, (int)System.Math.Round(heap.Length*growthFactor)));
-
-
-
- newSize = RoundUpToNextMultipleMod1(newSize);
-
-
-
-
- if (newSize > (1<<16) - 2) {
- throw new System.Exception("Binary Heap Size really large (>65534). A heap size this large is probably the cause of pathfinding running in an infinite loop. ");
- }
- var newHeap = new Tuple[newSize];
- heap.CopyTo(newHeap, 0);
- #if ASTARDEBUG
- UnityEngine.Debug.Log("Resizing binary heap to "+newSize);
- #endif
- heap = newHeap;
- }
-
- public void Add (PathNode node) {
- if (node == null) throw new System.ArgumentNullException("node");
- #if DECREASE_KEY
-
- if (node.heapIndex != NotInHeap) {
- DecreaseKey(heap[node.heapIndex], node.heapIndex);
- return;
- }
- #endif
- if (numberOfItems == heap.Length) {
- Expand();
- }
- DecreaseKey(new Tuple(0, node), (ushort)numberOfItems);
- numberOfItems++;
- }
- void DecreaseKey (Tuple node, ushort index) {
-
-
-
- int bubbleIndex = index;
-
- uint nodeF = node.F = node.node.F;
- uint nodeG = node.node.G;
- while (bubbleIndex != 0) {
-
- int parentIndex = (bubbleIndex-1) / D;
- if (nodeF < heap[parentIndex].F || (SortGScores && nodeF == heap[parentIndex].F && nodeG > heap[parentIndex].node.G)) {
-
-
-
- heap[bubbleIndex] = heap[parentIndex];
- #if DECREASE_KEY
- heap[bubbleIndex].node.heapIndex = (ushort)bubbleIndex;
- #endif
- bubbleIndex = parentIndex;
- } else {
- break;
- }
- }
- heap[bubbleIndex] = node;
- #if DECREASE_KEY
- node.node.heapIndex = (ushort)bubbleIndex;
- #endif
- }
-
- public PathNode Remove () {
- PathNode returnItem = heap[0].node;
- #if DECREASE_KEY
- returnItem.heapIndex = NotInHeap;
- #endif
- numberOfItems--;
- if (numberOfItems == 0) return returnItem;
-
- var swapItem = heap[numberOfItems];
- var swapItemG = swapItem.node.G;
- int swapIndex = 0, parent;
-
- while (true) {
- parent = swapIndex;
- uint swapF = swapItem.F;
- int pd = parent * D + 1;
-
-
-
- if (pd <= numberOfItems) {
-
-
- uint f0 = heap[pd+0].F;
- uint f1 = heap[pd+1].F;
- uint f2 = heap[pd+2].F;
- uint f3 = heap[pd+3].F;
-
-
-
-
-
- if (pd+0 < numberOfItems && (f0 < swapF || (SortGScores && f0 == swapF && heap[pd+0].node.G < swapItemG))) {
- swapF = f0;
- swapIndex = pd+0;
- }
- if (pd+1 < numberOfItems && (f1 < swapF || (SortGScores && f1 == swapF && heap[pd+1].node.G < (swapIndex == parent ? swapItemG : heap[swapIndex].node.G)))) {
- swapF = f1;
- swapIndex = pd+1;
- }
- if (pd+2 < numberOfItems && (f2 < swapF || (SortGScores && f2 == swapF && heap[pd+2].node.G < (swapIndex == parent ? swapItemG : heap[swapIndex].node.G)))) {
- swapF = f2;
- swapIndex = pd+2;
- }
- if (pd+3 < numberOfItems && (f3 < swapF || (SortGScores && f3 == swapF && heap[pd+3].node.G < (swapIndex == parent ? swapItemG : heap[swapIndex].node.G)))) {
- swapIndex = pd+3;
- }
- }
-
-
-
- if (parent != swapIndex) {
- heap[parent] = heap[swapIndex];
- #if DECREASE_KEY
- heap[parent].node.heapIndex = (ushort)parent;
- #endif
- } else {
- break;
- }
- }
-
- heap[swapIndex] = swapItem;
- #if DECREASE_KEY
- swapItem.node.heapIndex = (ushort)swapIndex;
- #endif
-
-
- return returnItem;
- }
- void Validate () {
- for (int i = 1; i < numberOfItems; i++) {
- int parentIndex = (i-1)/D;
- if (heap[parentIndex].F > heap[i].F) {
- throw new System.Exception("Invalid state at " + i + ":" + parentIndex + " ( " + heap[parentIndex].F + " > " + heap[i].F + " ) ");
- }
- #if DECREASE_KEY
- if (heap[i].node.heapIndex != i) {
- throw new System.Exception("Invalid heap index");
- }
- #endif
- }
- }
-
-
-
-
- public void Rebuild () {
- #if ASTARDEBUG
- int changes = 0;
- #endif
- for (int i = 2; i < numberOfItems; i++) {
- int bubbleIndex = i;
- var node = heap[i];
- uint nodeF = node.F;
- while (bubbleIndex != 1) {
- int parentIndex = bubbleIndex / D;
- if (nodeF < heap[parentIndex].F) {
- heap[bubbleIndex] = heap[parentIndex];
- #if DECREASE_KEY
- heap[bubbleIndex].node.heapIndex = (ushort)bubbleIndex;
- #endif
- heap[parentIndex] = node;
- #if DECREASE_KEY
- heap[parentIndex].node.heapIndex = (ushort)parentIndex;
- #endif
- bubbleIndex = parentIndex;
- #if ASTARDEBUG
- changes++;
- #endif
- } else {
- break;
- }
- }
- }
- #if ASTARDEBUG
- UnityEngine.Debug.Log("+++ Rebuilt Heap - "+changes+" changes +++");
- #endif
- }
- }
- }
|