forked from LIJI32/SameBoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHFAnnotatedTree.m
432 lines (364 loc) · 13.9 KB
/
HFAnnotatedTree.m
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//
// HFAnnotatedTree.m
// HexFiend_2
//
// Copyright 2010 ridiculous_fish. All rights reserved.
//
#import "HFAnnotatedTree.h"
#if NDEBUG
#define VERIFY_INTEGRITY() do { } while (0)
#else
#define VERIFY_INTEGRITY() [self verifyIntegrity]
#endif
/* HFAnnotatedTree is an AA tree. */
static unsigned long long null_annotater(id left, id right) { USE(left); USE(right); return 0; }
static void skew(HFAnnotatedTreeNode *node, HFAnnotatedTree *tree);
static BOOL split(HFAnnotatedTreeNode *oldparent, HFAnnotatedTree *tree);
static void rebalanceAfterLeafAdd(HFAnnotatedTreeNode *n, HFAnnotatedTree *tree);
static void delete(HFAnnotatedTreeNode *n, HFAnnotatedTree *tree);
static void verify_integrity(HFAnnotatedTreeNode *n);
static HFAnnotatedTreeNode *next_node(HFAnnotatedTreeNode *node);
static void insert(HFAnnotatedTreeNode *root, HFAnnotatedTreeNode *node, HFAnnotatedTree *tree);
static inline HFAnnotatedTreeNode *get_parent(HFAnnotatedTreeNode *node);
static inline HFAnnotatedTreeNode *get_root(HFAnnotatedTree *tree);
static inline HFAnnotatedTreeNode *create_root(void);
static inline HFAnnotatedTreeAnnotaterFunction_t get_annotater(HFAnnotatedTree *tree);
static void reannotate(HFAnnotatedTreeNode *node, HFAnnotatedTree *tree);
static HFAnnotatedTreeNode *first_node(HFAnnotatedTreeNode *node);
static HFAnnotatedTreeNode *left_child(HFAnnotatedTreeNode *node);
static HFAnnotatedTreeNode *right_child(HFAnnotatedTreeNode *node);
@implementation HFAnnotatedTree
- (instancetype)initWithAnnotater:(HFAnnotatedTreeAnnotaterFunction_t)annot {
self = [super init];
annotater = annot ? annot : null_annotater;
/* root is always an HFAnnotatedTreeNode with a left child but no right child */
root = create_root();
return self;
}
- (void)dealloc {
[root release];
[super dealloc];
}
- (id)rootNode {
return root;
}
- (id)firstNode {
return first_node(root);
}
- (id)mutableCopyWithZone:(NSZone *)zone {
HFAnnotatedTree *copied = [[[self class] alloc] init];
copied->annotater = annotater;
[copied->root release];
copied->root = [root mutableCopyWithZone:zone];
return copied;
}
- (BOOL)isEmpty {
/* We're empty if our root has no children. */
return left_child(root) == nil && right_child(root) == nil;
}
- (void)insertNode:(HFAnnotatedTreeNode *)node {
HFASSERT(node != nil);
HFASSERT(get_parent(node) == nil);
/* Insert into the root */
insert(root, [node retain], self);
VERIFY_INTEGRITY();
}
- (void)removeNode:(HFAnnotatedTreeNode *)node {
HFASSERT(node != nil);
HFASSERT(get_parent(node) != nil);
delete(node, self);
[node release];
VERIFY_INTEGRITY();
}
#if ! NDEBUG
- (void)verifyIntegrity {
[root verifyIntegrity];
[root verifyAnnotation:annotater];
}
#endif
static HFAnnotatedTreeNode *get_root(HFAnnotatedTree *tree) {
return tree->root;
}
static HFAnnotatedTreeAnnotaterFunction_t get_annotater(HFAnnotatedTree *tree) {
return tree->annotater;
}
@end
@implementation HFAnnotatedTreeNode
- (void)dealloc {
[left release];
[right release];
[super dealloc];
}
- (NSComparisonResult)compare:(HFAnnotatedTreeNode *)node {
USE(node);
UNIMPLEMENTED();
}
- (id)nextNode {
return next_node(self);
}
- (id)leftNode { return left; }
- (id)rightNode { return right; }
- (id)parentNode { return parent; }
- (id)mutableCopyWithZone:(NSZone *)zone {
HFAnnotatedTreeNode *copied = [[[self class] alloc] init];
if (left) {
copied->left = [left mutableCopyWithZone:zone];
copied->left->parent = copied;
}
if (right) {
copied->right = [right mutableCopyWithZone:zone];
copied->right->parent = copied;
}
copied->level = level;
copied->annotation = annotation;
return copied;
}
static HFAnnotatedTreeNode *left_child(HFAnnotatedTreeNode *node) {
return node->left;
}
static HFAnnotatedTreeNode *right_child(HFAnnotatedTreeNode *node) {
return node->right;
}
static HFAnnotatedTreeNode *create_root(void) {
HFAnnotatedTreeNode *result = [[HFAnnotatedTreeNode alloc] init];
result->level = UINT_MAX; //the root has a huge level
return result;
}
static void reannotate(HFAnnotatedTreeNode *node, HFAnnotatedTree *tree) {
HFASSERT(node != nil);
HFASSERT(tree != nil);
const HFAnnotatedTreeAnnotaterFunction_t annotater = get_annotater(tree);
node->annotation = annotater(node->left, node->right);
}
static void insert(HFAnnotatedTreeNode *root, HFAnnotatedTreeNode *node, HFAnnotatedTree *tree) {
/* Insert node at the proper place in the tree. root is the root node, and we always insert to the left of root */
BOOL left = YES;
HFAnnotatedTreeNode *parentNode = root, *currentChild;
/* Descend the tree until we find where to insert */
while ((currentChild = (left ? parentNode->left : parentNode->right)) != nil) {
parentNode = currentChild;
left = ([parentNode compare:node] >= 0); //if parentNode is larger than the child, then the child goes to the left of node
}
/* Now insert, potentially unbalancing the tree */
if (left) {
parentNode->left = node;
}
else {
parentNode->right = node;
}
/* Tell our node about its new parent */
node->parent = parentNode;
/* Rebalance and update annotations */
rebalanceAfterLeafAdd(node, tree);
}
static void skew(HFAnnotatedTreeNode *oldparent, HFAnnotatedTree *tree) {
HFAnnotatedTreeNode *newp = oldparent->left;
if (oldparent->parent->left == oldparent) {
/* oldparent is the left child of its parent. Substitute in our left child. */
oldparent->parent->left = newp;
}
else {
/* oldparent is the right child of its parent. Substitute in our left child. */
oldparent->parent->right = newp;
}
/* Tell the child about its new parent */
newp->parent = oldparent->parent;
/* Adopt its right child as our left child, and tell it about its new parent */
oldparent->left = newp->right;
if (oldparent->left) oldparent->left->parent = oldparent;
/* We are now the right child of the new parent */
newp->right = oldparent;
oldparent->parent = newp;
/* If we're now a leaf, our level is 1. Otherwise, it's one more than the level of our child. */
oldparent->level = oldparent->left ? oldparent->left->level + 1 : 1;
/* oldparent and newp both had their children changed, so need to be reannotated */
reannotate(oldparent, tree);
reannotate(newp, tree);
}
static BOOL split(HFAnnotatedTreeNode *oldparent, HFAnnotatedTree *tree) {
HFAnnotatedTreeNode *newp = oldparent->right;
if (newp && newp->right && newp->right->level == oldparent->level) {
if (oldparent->parent->left == oldparent) oldparent->parent->left = newp;
else oldparent->parent->right = newp;
newp->parent = oldparent->parent;
oldparent->parent = newp;
oldparent->right = newp->left;
if (oldparent->right) oldparent->right->parent = oldparent;
newp->left = oldparent;
newp->level = oldparent->level + 1;
/* oldparent and newp both had their children changed, so need to be reannotated */
reannotate(oldparent, tree);
reannotate(newp, tree);
return YES;
}
return NO;
}
static void rebalanceAfterLeafAdd(HFAnnotatedTreeNode *node, HFAnnotatedTree *tree) { // n is a node that has just been inserted and is now a leaf node.
node->level = 1;
node->left = nil;
node->right = nil;
reannotate(node, tree);
HFAnnotatedTreeNode * const root = get_root(tree);
HFAnnotatedTreeNode *probe;
for (probe = node->parent; probe != root; probe = probe->parent) {
reannotate(probe, tree);
// At this point probe->parent->level == probe->level
if (probe->level != (probe->left ? probe->left->level + 1 : 1)) {
// At this point the tree is correct, except (AA2) for n->parent
skew(probe, tree);
// We handle it (a left add) by changing it into a right add using Skew
// If the original add was to the left side of a node that is on the
// right side of a horisontal link, probe now points to the rights side
// of the second horisontal link, which is correct.
// However if the original add was to the left of node with a horizontal
// link, we must get to the right side of the second link.
if (!probe->right || probe->level != probe->right->level) probe = probe->parent;
}
if (! split(probe->parent, tree)) break;
}
while (probe) {
reannotate(probe, tree);
probe = probe->parent;
}
}
static void delete(HFAnnotatedTreeNode *n, HFAnnotatedTree *tree) { // If n is not a leaf, we first swap it out with the leaf node that just
// precedes it.
HFAnnotatedTreeNode *leaf = n, *tmp;
if (n->left) {
/* Descend the right subtree of our left child, to get the closest predecessor */
for (leaf = n->left; leaf->right; leaf = leaf->right) {}
// When we stop, leaf has no 'right' child so it cannot have a left one
}
else if (n->right) {
/* We have no children that precede us, but we have a child after us, so use our closest successor */
leaf = n->right;
}
/* tmp is either the parent who loses the child, or tmp is our right subtree. Either way, we will have to reduce its level. */
tmp = leaf->parent == n ? leaf : leaf->parent;
/* Tell leaf's parent to forget about leaf */
if (leaf->parent->left == leaf) {
leaf->parent->left = NULL;
}
else {
leaf->parent->right = NULL;
}
reannotate(leaf->parent, tree);
if (n != leaf) {
/* Replace ourself as our parent's child with leaf */
if (n->parent->left == n) n->parent->left = leaf;
else n->parent->right = leaf;
/* Leaf's parent is our parent */
leaf->parent = n->parent;
/* Our left and right children are now leaf's left and right children */
if (n->left) n->left->parent = leaf;
leaf->left = n->left;
if (n->right) n->right->parent = leaf;
leaf->right = n->right;
/* Leaf's level is our level */
leaf->level = n->level;
}
/* Since we adopted n's children, transferring the retain, tell n to forget about them so it doesn't release them */
n->left = nil;
n->right = nil;
// free (n);
HFAnnotatedTreeNode * const root = get_root(tree);
while (tmp != root) {
reannotate(tmp, tree);
// One of tmp's childern had its level reduced
if (tmp->level > (tmp->left ? tmp->left->level + 1 : 1)) { // AA2 failed
tmp->level--;
if (split(tmp, tree)) {
if (split(tmp, tree)) skew(tmp->parent->parent, tree);
break;
}
tmp = tmp->parent;
}
else if (tmp->level <= (tmp->right ? tmp->right->level + 1 : 1)){
break;
}
else { // AA3 failed
skew(tmp, tree);
//if (tmp->right) tmp->right->level = tmp->right->left ? tmp->right->left->level + 1 : 1;
if (tmp->level > tmp->parent->level) {
skew(tmp, tree);
split(tmp->parent->parent, tree);
break;
}
tmp = tmp->parent->parent;
}
}
while (tmp) {
reannotate(tmp, tree);
tmp = tmp->parent;
}
}
static HFAnnotatedTreeNode *next_node(HFAnnotatedTreeNode *node) {
/* Return the next in-order node */
HFAnnotatedTreeNode *result;
if (node->right) {
/* We have a right child, which is after us. Descend its left subtree. */
result = node->right;
while (result->left) {
result = result->left;
}
}
else {
/* We have no right child. If we are our parent's left child, then our parent is after us. Otherwise, we're our parent's right child and it was before us, so ascend while we're the parent's right child. */
result = node;
while (result->parent && result->parent->right == result) {
result = result->parent;
}
/* Now result is the left child of the parent (or has NULL parents), so its parent is the next node */
result = result->parent;
}
/* Don't return the root */
if (result != nil && result->parent == nil) {
result = next_node(result);
}
return result;
}
static HFAnnotatedTreeNode *first_node(HFAnnotatedTreeNode *node) {
/* Return the first node */
HFAnnotatedTreeNode *result = nil, *cursor = node->left;
while (cursor) {
/* Descend the left subtree */
result = cursor;
cursor = cursor->left;
}
return result;
}
static HFAnnotatedTreeNode *get_parent(HFAnnotatedTreeNode *node) {
HFASSERT(node != nil);
return node->parent;
}
static void __attribute__((unused))verify_integrity(HFAnnotatedTreeNode *n) {
HFASSERT(!n->left || n->left->parent == n);
HFASSERT(!n->right || n->right->parent == n);
HFASSERT(!next_node(n) || [n compare:next_node(n)] <= 0);
HFASSERT(!n->parent || n->parent->level >= n->level);
if (n->parent == nil) {
/* root node */
HFASSERT(n->level == UINT_MAX);
}
else {
/* non-root node */
HFASSERT(n->level == (n->left == NULL ? 1 : n->left->level + 1));
HFASSERT((n->level <= 1) || (n->right && n->level - n->right->level <= 1));
}
HFASSERT(!n->parent || !n->parent->parent ||
n->parent->parent->level > n->level);
}
#if ! NDEBUG
- (void)verifyIntegrity {
[left verifyIntegrity];
[right verifyIntegrity];
verify_integrity(self);
}
- (void)verifyAnnotation:(HFAnnotatedTreeAnnotaterFunction_t)annotater {
[left verifyAnnotation:annotater];
[right verifyAnnotation:annotater];
unsigned long long expectedAnnotation = annotater(left, right);
HFASSERT(annotation == expectedAnnotation);
}
#endif
@end