aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/BinaryHeap.cpp
blob: 3a3d1c1e65f0751f680427da8ffa79f20a1f3da5 (plain)
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
43
44
45
46
47
48
49
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "stdafx.h"
#include "Node.h"
#include "System.h"
#include "BasicTypeContainers.h"
#include "BinaryHeap.h"

// 4J Jev, add common ctor code.
void BinaryHeap::_init()
{
	heap = NodeArray(1024);
	sizeVar = 0;
}

BinaryHeap::BinaryHeap()
{
	_init();
}

BinaryHeap::~BinaryHeap()
{
	delete[] heap.data;
}

Node *BinaryHeap::insert(Node *node)
{
    /* if (node->heapIdx >=0) throw new IllegalStateException("OW KNOWS!"); 4J Jev, removed try/catch */

    // Expand if necessary.
	if (sizeVar == heap.length)
    {
        NodeArray newHeap = NodeArray(sizeVar << 1);

        System::arraycopy(heap, 0, &newHeap, 0, sizeVar);

		delete[] heap.data;
        heap = newHeap;
    }

    // Insert at end and bubble up.
    heap[sizeVar] = node;
    node->heapIdx = sizeVar;
    upHeap(sizeVar++);

    return node;
}

void BinaryHeap::clear()
{
    sizeVar = 0;
}

Node *BinaryHeap::peek()
{
    return heap[0];
}

Node *BinaryHeap::pop()
{
    Node *popped = heap[0];
    heap[0] = heap[--sizeVar];
    heap[sizeVar] = NULL;
    if (sizeVar > 0) downHeap(0);
    popped->heapIdx=-1;
    return popped;
}

void BinaryHeap::remove(Node *node)
{
    // This is what node.heapIdx is for.
    heap[node->heapIdx] = heap[--sizeVar];
    heap[sizeVar] = NULL;
    if (sizeVar > node->heapIdx)
    {
        if (heap[node->heapIdx]->f < node->f)
        {
            upHeap(node->heapIdx);
        }
        else
        {
            downHeap(node->heapIdx);
        }
    }
    // Just as a precaution: should make stuff blow up if the node is abused.
    node->heapIdx = -1;
}

void BinaryHeap::changeCost(Node *node, float newCost)
{
    float oldCost = node->f;
    node->f = newCost;
    if (newCost < oldCost)
    {
        upHeap(node->heapIdx);
    }
    else
    {
        downHeap(node->heapIdx);
    }
}

int BinaryHeap::size()
{
    return sizeVar;
}

void BinaryHeap::upHeap(int idx)
{
    Node *node = heap[idx];
    float cost = node->f;
    while (idx > 0)
    {
        int parentIdx = (idx - 1) >> 1;
        Node *parent = heap[parentIdx];
        if (cost < parent->f)
        {
            heap[idx] = parent;
            parent->heapIdx = idx;
            idx = parentIdx;
        }
        else break;
    }
    heap[idx] = node;
    node->heapIdx = idx;
}

void BinaryHeap::downHeap(int idx)
{
    Node *node = heap[idx];
    float cost = node->f;

    while (true)
    {
        int leftIdx = 1 + (idx << 1);
        int rightIdx = leftIdx + 1;

        if (leftIdx >= sizeVar) break;

        // We definitely have a left child.
        Node *leftNode = heap[leftIdx];
        float leftCost = leftNode->f;
        // We may have a right child.
        Node *rightNode;
        float rightCost;

        if (rightIdx >= sizeVar)
        {
            // Only need to compare with left.
            rightNode = NULL;
            rightCost = Float::POSITIVE_INFINITY;
        }
        else
        {
            rightNode = heap[rightIdx];
            rightCost = rightNode->f;
        }

        // Find the smallest of the three costs: the corresponding node
        // should be the parent.
        if (leftCost < rightCost)
        {
            if (leftCost < cost)
            {
                heap[idx] = leftNode;
                leftNode->heapIdx = idx;
                idx = leftIdx;
            }
            else break;
        }
        else
        {
            if (rightCost < cost)
            {
                heap[idx] = rightNode;
                rightNode->heapIdx = idx;
                idx = rightIdx;
            }
            else break;
        }
    }

    heap[idx] = node;
    node->heapIdx = idx;
}

bool BinaryHeap::isEmpty()
{
    return sizeVar==0;
}