Advertisement
SebastianLague

A* Pathfinding Tutorial 04 (Heap)

Dec 24th, 2014
4,244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4.  
  5. public class Heap<T> where T : IHeapItem<T> {
  6.    
  7.     T[] items;
  8.     int currentItemCount;
  9.    
  10.     public Heap(int maxHeapSize) {
  11.         items = new T[maxHeapSize];
  12.     }
  13.    
  14.     public void Add(T item) {
  15.         item.HeapIndex = currentItemCount;
  16.         items[currentItemCount] = item;
  17.         SortUp(item);
  18.         currentItemCount++;
  19.     }
  20.  
  21.     public T RemoveFirst() {
  22.         T firstItem = items[0];
  23.         currentItemCount--;
  24.         items[0] = items[currentItemCount];
  25.         items[0].HeapIndex = 0;
  26.         SortDown(items[0]);
  27.         return firstItem;
  28.     }
  29.  
  30.     public void UpdateItem(T item) {
  31.         SortUp(item);
  32.     }
  33.  
  34.     public int Count {
  35.         get {
  36.             return currentItemCount;
  37.         }
  38.     }
  39.  
  40.     public bool Contains(T item) {
  41.         return Equals(items[item.HeapIndex], item);
  42.     }
  43.  
  44.     void SortDown(T item) {
  45.         while (true) {
  46.             int childIndexLeft = item.HeapIndex * 2 + 1;
  47.             int childIndexRight = item.HeapIndex * 2 + 2;
  48.             int swapIndex = 0;
  49.  
  50.             if (childIndexLeft < currentItemCount) {
  51.                 swapIndex = childIndexLeft;
  52.  
  53.                 if (childIndexRight < currentItemCount) {
  54.                     if (items[childIndexLeft].CompareTo(items[childIndexRight]) < 0) {
  55.                         swapIndex = childIndexRight;
  56.                     }
  57.                 }
  58.  
  59.                 if (item.CompareTo(items[swapIndex]) < 0) {
  60.                     Swap (item,items[swapIndex]);
  61.                 }
  62.                 else {
  63.                     return;
  64.                 }
  65.  
  66.             }
  67.             else {
  68.                 return;
  69.             }
  70.         }
  71.     }
  72.    
  73.     void SortUp(T item) {
  74.         int parentIndex = (item.HeapIndex-1)/2;
  75.        
  76.         while (true) {
  77.             T parentItem = items[parentIndex];
  78.             if (item.CompareTo(parentItem) > 0) {
  79.                 Swap (item,parentItem);
  80.             }
  81.             else {
  82.                 break;
  83.             }
  84.             parentIndex = (item.HeapIndex-1)/2;
  85.         }
  86.     }
  87.    
  88.     void Swap(T itemA, T itemB) {
  89.         items[itemA.HeapIndex] = itemB;
  90.         items[itemB.HeapIndex] = itemA;
  91.         int itemAIndex = itemA.HeapIndex;
  92.         itemA.HeapIndex = itemB.HeapIndex;
  93.         itemB.HeapIndex = itemAIndex;
  94.     }
  95. }
  96.  
  97. public interface IHeapItem<T> : IComparable<T> {
  98.     int HeapIndex {
  99.         get;
  100.         set;
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement