Heap.cs 3.06 KB
Newer Older
Zac Olsen's avatar
Zac Olsen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

public class Heap {

    Tile[] heapTiles;
    int itemCount;

    //replace openList with Heap

    //add heapIndex, compareTo to tile class
    //compare fCost of node then hCost
    //item.compareTo(item2) should return pos if item is less than item2, neg if item is greater than item2

    //create heap
    public Heap(int maxSize)
    {
        heapTiles = new Tile[maxSize];
    }

    //add item into heap and sort it into position
    public void Add(Tile item)
    {
        item.heapIndex = itemCount;
        heapTiles[itemCount] = item;
        SortUp(item);
        itemCount++;
    }

    //remove first tile and resort heap
    public Tile RemoveFirst()
    {
        Tile first = heapTiles[0];
        itemCount--;
        heapTiles[0] = heapTiles[itemCount];
        heapTiles[0].heapIndex = 0;
        SortDown(heapTiles[0]);
        return first;
    }

Zac Olsen's avatar
Zac Olsen committed
43 44 45 46 47 48 49
	public void Remove (Tile item){

		heapTiles [item.heapIndex].fitness = float.MinValue;
		UpdateItem (item);
		RemoveFirst ();
	}

Zac Olsen's avatar
Zac Olsen committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    //resort item position
    public void UpdateItem(Tile item)
    {
        SortUp(item);
    }

    
    public int Count
    {
        get
        {
            return itemCount;
        }
    }

    //does heap contain item
    public bool Contains(Tile item)
    {
        return Equals(heapTiles[item.heapIndex], item);
    }

    //sort tile lower into heap
    void SortDown(Tile item)
    {
        while (true)
        {
            int leftChildIndex = item.heapIndex * 2 + 1;
            int rightChildIndex = (item.heapIndex * 2) + 2;
            int swapIndex = 0;

            if (leftChildIndex < itemCount)
            {
                swapIndex = leftChildIndex;

                if (rightChildIndex < itemCount)
                {
                    if (heapTiles[leftChildIndex].CompareTo(heapTiles[rightChildIndex]) < 0)
                    {
                        swapIndex = rightChildIndex;
                    }
                }

                if (item.CompareTo(heapTiles[swapIndex]) < 0)
                {
                    Swap(item, heapTiles[swapIndex]);
                }
                else
                {
                    return;
                }
            }
            else
            {
                return;
            }
        }
    }

    //sort item higher into heap
    void SortUp(Tile item)
    {
        int indexOfParent = (item.heapIndex - 1) / 2;

        while (true)
        {
            Tile parent = heapTiles[indexOfParent];

            if (item.CompareTo(parent) > 0)
            {
                Swap(item, parent);
            }
            else
            {
                break;
            }
            indexOfParent = (item.heapIndex - 1) / 2;
        }
    }

    //swap tiles
    void Swap(Tile item1, Tile item2)
    {
        heapTiles[item1.heapIndex] = item2;
        heapTiles[item2.heapIndex] = item1;
        int tempIndex = item1.heapIndex;
        item1.heapIndex = item2.heapIndex;
        item2.heapIndex = tempIndex;
    }
}