-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfibonacciEnemy.h
235 lines (204 loc) · 4.64 KB
/
fibonacciEnemy.h
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#pragma once
#include <iostream>
#include <stdio.h>
#include <cstdio>
#include "enemy.h"
//#include "utility.h"
struct nodeEnemy {
enemy* enem;
int degree;
bool marked;
nodeEnemy* prev;
nodeEnemy* next;
nodeEnemy* child;
nodeEnemy* parent;
nodeEnemy* getPrev() { return prev; }
nodeEnemy* getNext() { return next; }
nodeEnemy* getChild() { return child; }
nodeEnemy* getParent() { return parent; }
float getValue() { return enem->Priority; }
bool isMarked() { return marked; }
bool hasChildren() { return child != NULL; }
bool hasParent() { return parent != NULL; }
};
struct FibonacciHeap {
nodeEnemy* heap;
FibonacciHeap() {
heap = empty();
}
nodeEnemy* singleton(enemy* enemer)
{
nodeEnemy* n = new nodeEnemy;
n->enem = enemer;
n->prev = n->next = n;
n->degree = 0;
n->marked = false;
n->child = NULL;
n->parent = NULL;
return n;
}
nodeEnemy* t_merge(nodeEnemy* a, nodeEnemy* b)
{
if (a == NULL)return b;
if (b == NULL)return a;
if (a->enem->Priority>b->enem->Priority) {
nodeEnemy* temp = a;
a = b;
b = temp;
}
nodeEnemy* an = a->next;
nodeEnemy* bp = b->prev;
a->next = b;
b->prev = a;
an->prev = bp;
bp->next = an;
return a;
}
nodeEnemy* merge(nodeEnemy* heap1, nodeEnemy* heap2) {
heap = t_merge(heap1, heap2);
heap2 = empty();
return heap;
}
void insert(enemy* enemer) {
nodeEnemy* ret = singleton(enemer);
heap = merge(heap, ret);
//return heap;
}
nodeEnemy* empty() {
return NULL;
}
bool isEmpty() {
return heap == NULL;
}
float getMinimum() {
return heap->enem->Priority;
}
//changes in this function
enemy* removeMinimum() {
nodeEnemy* old = heap;
heap = t_removeMinimum(heap);
enemy* ret = old->enem;
delete old;
return ret;
}
void deleteAll(nodeEnemy* n) {
if (n != NULL) {
nodeEnemy* c = n;
do {
nodeEnemy* d = c;
c = c->next;
deleteAll(d->child);
delete d;
} while (c != n);
}
}
void addChild(nodeEnemy* parent, nodeEnemy* child) {
child->prev = child->next = child;
child->parent = parent;
parent->degree++;
parent->child = t_merge(parent->child, child);
}
void unMarkAndUnParentAll(nodeEnemy* n) {
if (n == NULL)return;
nodeEnemy* c = n;
do {
c->marked = false;
c->parent = NULL;
c = c->next;
} while (c != n);
}
nodeEnemy* t_removeMinimum(nodeEnemy* n) {
unMarkAndUnParentAll(n->child);
if (n->next == n) {
n = n->child;
}
else {
n->next->prev = n->prev;
n->prev->next = n->next;
n = t_merge(n->next, n->child);
}
if (n == NULL)return n;
nodeEnemy* trees[64] = { NULL };
while (true) {
if (trees[n->degree] != NULL) {
nodeEnemy* t = trees[n->degree];
if (t == n)break;
trees[n->degree] = NULL;
if (n->enem->Priority<t->enem->Priority) {
t->prev->next = t->next;
t->next->prev = t->prev;
addChild(n, t);
}
else {
t->prev->next = t->next;
t->next->prev = t->prev;
if (n->next == n) {
t->next = t->prev = t;
addChild(t, n);
n = t;
}
else {
n->prev->next = t;
n->next->prev = t;
t->next = n->next;
t->prev = n->prev;
addChild(t, n);
n = t;
}
}
continue;
}
else {
trees[n->degree] = n;
}
n = n->next;
}
nodeEnemy* min = n;
do {
if (n->enem->Priority<min->enem->Priority)min = n;
n = n->next;
} while (n != min);
return min;
}
nodeEnemy* cut(nodeEnemy* heap, nodeEnemy* n) {
if (n->next == n) {
n->parent->child = NULL;
}
else {
n->next->prev = n->prev;
n->prev->next = n->next;
n->parent->child = n->next;
}
n->next = n->prev = n;
n->marked = false;
return t_merge(heap, n);
}
nodeEnemy* decreaseKey(nodeEnemy* heap, nodeEnemy* n, float value) {
if (n->enem->Priority<value)return heap;
n->enem->Priority = value;
if (n->enem->Priority < n->parent->enem->Priority) {
heap = cut(heap, n);
nodeEnemy* parent = n->parent;
n->parent = NULL;
while (parent != NULL && parent->marked) {
heap = cut(heap, parent);
n = parent;
parent = n->parent;
n->parent = NULL;
}
if (parent != NULL && parent->parent != NULL)parent->marked = true;
}
return heap;
}
nodeEnemy* find(nodeEnemy* heap, float value) {
nodeEnemy* n = heap;
if (n == NULL)return NULL;
do {
if (n->enem->Priority == value)return n;
nodeEnemy* ret = find(n->child, value);
if (ret)return ret;
n = n->next;
} while (n != heap);
return NULL;
}
};