Commit da3ff3416821d277d925802fd56802b9e6964070
1 parent
5e1c8f73
use tree macros in memory management tree
Showing
1 changed file
with
34 additions
and
183 deletions
... | ... | @@ -54,8 +54,7 @@ |
54 | 54 | #include <unistd.h> |
55 | 55 | |
56 | 56 | #include "tr/memory.h" |
57 | - | |
58 | -enum rbColor {rbBlack=1, rbRed=2}; | |
57 | +#include "tr/tree_macros.h" | |
59 | 58 | |
60 | 59 | |
61 | 60 | struct memSegment |
... | ... | @@ -64,7 +63,9 @@ struct memSegment |
64 | 63 | size_t size; |
65 | 64 | void * ptr; |
66 | 65 | |
67 | - enum rbColor color; | |
66 | + TR_rbColor color; | |
67 | + | |
68 | + struct memSegment * data; | |
68 | 69 | |
69 | 70 | struct memSegment * next; |
70 | 71 | struct memSegment * last; |
... | ... | @@ -84,6 +85,8 @@ newElement(size_t size) |
84 | 85 | element->size = size; |
85 | 86 | element->ptr = (void*)element + sizeof(struct memSegment); |
86 | 87 | |
88 | + element->data = element; | |
89 | + | |
87 | 90 | element->next = NULL; |
88 | 91 | element->last = NULL; |
89 | 92 | |
... | ... | @@ -121,133 +124,6 @@ findElement(struct memSegment * tree, size_t size) |
121 | 124 | return fitting; |
122 | 125 | } |
123 | 126 | |
124 | -/* | |
125 | - * function to get specific elements needed for | |
126 | - * rb handling, grandparent, uncle and sibbling | |
127 | - */ | |
128 | -static | |
129 | -struct memSegment * | |
130 | -grandparent(struct memSegment * node) | |
131 | -{ | |
132 | - if (NULL != node && NULL != node->parent) { | |
133 | - return node->parent->parent; | |
134 | - } | |
135 | - | |
136 | - return NULL; | |
137 | -} | |
138 | - | |
139 | -static | |
140 | -struct memSegment * | |
141 | -uncle(struct memSegment * node) | |
142 | -{ | |
143 | - struct memSegment * gp = grandparent(node); | |
144 | - | |
145 | - if (NULL == gp) { | |
146 | - return NULL; | |
147 | - } | |
148 | - | |
149 | - if (node->parent == gp->left) { | |
150 | - return gp->right; | |
151 | - } | |
152 | - | |
153 | - return gp->left; | |
154 | -} | |
155 | - | |
156 | -static | |
157 | -struct memSegment * | |
158 | -sibling(struct memSegment * node) | |
159 | -{ | |
160 | - if (NULL == node) { | |
161 | - return NULL; | |
162 | - } | |
163 | - | |
164 | - if (NULL == node->parent->left || node == node->parent->left) { | |
165 | - return node->parent->right; | |
166 | - } else { | |
167 | - return node->parent->left; | |
168 | - } | |
169 | -} | |
170 | - | |
171 | -/* | |
172 | - * tree modifications...needed for rb handling. | |
173 | - */ | |
174 | -static | |
175 | -void | |
176 | -rotateLeft(struct memSegment ** tree, struct memSegment * node) | |
177 | -{ | |
178 | - struct memSegment * rightChild = node->right; | |
179 | - struct memSegment * rcLeftSub = node->right->left; | |
180 | - | |
181 | - rightChild->left = node; | |
182 | - rightChild->parent = node->parent; | |
183 | - node->right = rcLeftSub; | |
184 | - if (NULL != rcLeftSub) { | |
185 | - rcLeftSub->parent = node; | |
186 | - } | |
187 | - | |
188 | - if (node->parent) { | |
189 | - if (node->parent->left == node) { | |
190 | - node->parent->left = rightChild; | |
191 | - } else { | |
192 | - node->parent->right = rightChild; | |
193 | - } | |
194 | - } else { | |
195 | - *tree = rightChild; | |
196 | - } | |
197 | - | |
198 | - node->parent = rightChild; | |
199 | -} | |
200 | - | |
201 | -static | |
202 | -void | |
203 | -rotateRight(struct memSegment ** tree, struct memSegment * node) | |
204 | -{ | |
205 | - struct memSegment * leftChild = node->left; | |
206 | - struct memSegment * lcRightSub = node->left->right; | |
207 | - | |
208 | - leftChild->right = node; | |
209 | - leftChild->parent = node->parent; | |
210 | - node->left = lcRightSub; | |
211 | - if (NULL != lcRightSub) { | |
212 | - lcRightSub->parent = node; | |
213 | - } | |
214 | - | |
215 | - if (node->parent) { | |
216 | - if (node->parent->left == node) { | |
217 | - node->parent->left = leftChild; | |
218 | - } else { | |
219 | - node->parent->right = leftChild; | |
220 | - } | |
221 | - } else { | |
222 | - *tree = leftChild; | |
223 | - } | |
224 | - | |
225 | - node->parent = leftChild; | |
226 | -} | |
227 | - | |
228 | -static | |
229 | -void | |
230 | -replaceNode( | |
231 | - struct memSegment ** tree, | |
232 | - struct memSegment * node1, | |
233 | - struct memSegment * node2) | |
234 | -{ | |
235 | - if (NULL != node1->parent) { | |
236 | - if (node1 == node1->parent->left) { | |
237 | - node1->parent->left = node2; | |
238 | - } else { | |
239 | - node1->parent->right = node2; | |
240 | - } | |
241 | - } else { | |
242 | - *tree = node2; | |
243 | - } | |
244 | - | |
245 | - if (NULL != node2) { | |
246 | - node2->parent = node1->parent; | |
247 | - } | |
248 | -} | |
249 | - | |
250 | - | |
251 | 127 | /** |
252 | 128 | * insert element in tree |
253 | 129 | */ |
... | ... | @@ -324,8 +200,8 @@ insertElement(struct memSegment ** tree, struct memSegment * element) |
324 | 200 | } |
325 | 201 | |
326 | 202 | // case 3 |
327 | - u = uncle(node); | |
328 | - g = grandparent(node); | |
203 | + u = TR_TREE_UNCLE(node); | |
204 | + g = TR_TREE_GRANDPARENT(node); | |
329 | 205 | |
330 | 206 | if (u != NULL && u->color == rbRed) { |
331 | 207 | node->parent->color = rbBlack; |
... | ... | @@ -338,24 +214,24 @@ insertElement(struct memSegment ** tree, struct memSegment * element) |
338 | 214 | |
339 | 215 | // case 4 |
340 | 216 | if (node == node->parent->right && node->parent == g->left) { |
341 | - rotateLeft(tree, node->parent); | |
217 | + TR_TREE_ROTATE(left, tree, node->parent); | |
342 | 218 | node = node->left; |
343 | 219 | } else if (node == node->parent->left && node->parent == g->right) { |
344 | 220 | |
345 | - rotateRight(tree, node->parent); | |
221 | + TR_TREE_ROTATE(right, tree, node->parent); | |
346 | 222 | node = node->right; |
347 | 223 | } |
348 | 224 | |
349 | 225 | // case 5 |
350 | - g = grandparent(node); | |
226 | + g = TR_TREE_GRANDPARENT(node); | |
351 | 227 | |
352 | 228 | node->parent->color = rbBlack; |
353 | 229 | g->color = rbRed; |
354 | 230 | |
355 | 231 | if (node == node->parent->left) { |
356 | - rotateRight(tree, g); | |
232 | + TR_TREE_ROTATE(right, tree, g); | |
357 | 233 | } else { |
358 | - rotateLeft(tree, g); | |
234 | + TR_TREE_ROTATE(left, tree, g); | |
359 | 235 | } |
360 | 236 | |
361 | 237 | // we're done.. |
... | ... | @@ -366,36 +242,6 @@ insertElement(struct memSegment ** tree, struct memSegment * element) |
366 | 242 | return new_node; |
367 | 243 | } |
368 | 244 | |
369 | -/** | |
370 | - * delete element from tree | |
371 | - * here multiple functions are involved.... | |
372 | - * ======================================================================= | |
373 | - */ | |
374 | -/** | |
375 | - * find minimum of the right subtree aka leftmost leaf of right subtree | |
376 | - * aka left in-order successor. | |
377 | - * We return the parent of the element in the out argument parent. | |
378 | - * This can be NULL wenn calling. | |
379 | - * | |
380 | - * 2: *successor = {size = 80, ptr = 0x603ae0, color = rbRed, parent = 0x603160, | |
381 | - * left = 0x0, right = 0x0} | |
382 | - * 1: *node = {size = 70, ptr = 0x603a60, color = rbBlack, parent = 0x603070, | |
383 | - * left = 0x6030e0, right = 0x6031e0} | |
384 | - * | |
385 | - */ | |
386 | -static | |
387 | -struct memSegment * | |
388 | -findInOrderSuccessor(struct memSegment * tree) | |
389 | -{ | |
390 | - struct memSegment * node = tree->right; | |
391 | - | |
392 | - while (NULL != node->left) { | |
393 | - node = node->left; | |
394 | - } | |
395 | - | |
396 | - return node; | |
397 | -} | |
398 | - | |
399 | 245 | static |
400 | 246 | struct memSegment * |
401 | 247 | deleteElement(struct memSegment ** tree, struct memSegment * element) |
... | ... | @@ -456,14 +302,19 @@ deleteElement(struct memSegment ** tree, struct memSegment * element) |
456 | 302 | |
457 | 303 | // case 1: two children |
458 | 304 | if (NULL != node->left && NULL != node->right) { |
459 | - struct memSegment * successor = findInOrderSuccessor(node); | |
305 | + struct memSegment * successor; | |
306 | + struct memSegment * tmpparent; | |
307 | + struct memSegment * tmpleft; | |
308 | + struct memSegment * tmpright; | |
309 | + TR_rbColor tmpcolor; | |
460 | 310 | |
461 | - enum rbColor tmpcolor = successor->color; | |
462 | - struct memSegment * tmpparent = successor->parent; | |
463 | - struct memSegment * tmpleft = successor->left; | |
464 | - struct memSegment * tmpright = successor->right; | |
311 | + TR_TREE_INORDER_SUCC(node, successor); | |
312 | + tmpparent = successor->parent; | |
313 | + tmpleft = successor->left; | |
314 | + tmpright = successor->right; | |
315 | + tmpcolor = successor->color; | |
465 | 316 | |
466 | - replaceNode(tree, node, successor); | |
317 | + TR_TREE_REPLACE_NODE(tree, node, successor); | |
467 | 318 | |
468 | 319 | successor->color = node->color; |
469 | 320 | successor->left = node->left; |
... | ... | @@ -486,7 +337,7 @@ deleteElement(struct memSegment ** tree, struct memSegment * element) |
486 | 337 | |
487 | 338 | // Precondition: n has at most one non-null child. |
488 | 339 | child = (NULL == node->right) ? node->left : node->right; |
489 | - replaceNode(tree, node, child); | |
340 | + TR_TREE_REPLACE_NODE(tree, node, child); | |
490 | 341 | |
491 | 342 | // delete one child case |
492 | 343 | // TODO this is overly complex as simply derived from the function... |
... | ... | @@ -518,7 +369,7 @@ deleteElement(struct memSegment ** tree, struct memSegment * element) |
518 | 369 | } |
519 | 370 | |
520 | 371 | // case 2 |
521 | - s = sibling(node); | |
372 | + s = TR_TREE_SIBLING(node); | |
522 | 373 | |
523 | 374 | if (NULL != s && s->color == rbRed) { |
524 | 375 | node->parent->color = rbRed; |
... | ... | @@ -530,13 +381,13 @@ deleteElement(struct memSegment ** tree, struct memSegment * element) |
530 | 381 | * null we must be left, even if its set to NULL previously |
531 | 382 | */ |
532 | 383 | if (NULL != node->parent->right && node != node->parent->right) { |
533 | - rotateLeft(tree, node->parent); | |
384 | + TR_TREE_ROTATE(left, tree, node->parent); | |
534 | 385 | } else { |
535 | - rotateRight(tree, node->parent); | |
386 | + TR_TREE_ROTATE(right, tree, node->parent); | |
536 | 387 | } |
537 | 388 | } |
538 | 389 | |
539 | - s = sibling(node); | |
390 | + s = TR_TREE_SIBLING(node); | |
540 | 391 | // case 3 / 4 |
541 | 392 | if (NULL == s || ((s->color == rbBlack) && |
542 | 393 | (NULL == s->left || s->left->color == rbBlack) && |
... | ... | @@ -577,7 +428,7 @@ deleteElement(struct memSegment ** tree, struct memSegment * element) |
577 | 428 | s->color = rbRed; |
578 | 429 | s->left->color = rbBlack; |
579 | 430 | |
580 | - rotateRight(tree, s); | |
431 | + TR_TREE_ROTATE(right, tree, s); | |
581 | 432 | } else if ((node == node->parent->right) && |
582 | 433 | (NULL == s->left || s->left->color == rbBlack) && |
583 | 434 | (NULL != s->right && s->right->color == rbRed)) { |
... | ... | @@ -585,11 +436,11 @@ deleteElement(struct memSegment ** tree, struct memSegment * element) |
585 | 436 | s->color = rbRed; |
586 | 437 | s->right->color = rbBlack; |
587 | 438 | |
588 | - rotateLeft(tree, s); | |
439 | + TR_TREE_ROTATE(left, tree, s); | |
589 | 440 | } |
590 | 441 | } |
591 | 442 | |
592 | - s = sibling(node); | |
443 | + s = TR_TREE_SIBLING(node); | |
593 | 444 | // case 6 |
594 | 445 | if (NULL != s) { |
595 | 446 | s->color = node->parent->color; |
... | ... | @@ -607,12 +458,12 @@ deleteElement(struct memSegment ** tree, struct memSegment * element) |
607 | 458 | if (NULL != s->right) { |
608 | 459 | s->right->color = rbBlack; |
609 | 460 | } |
610 | - rotateLeft(tree, node->parent); | |
461 | + TR_TREE_ROTATE(left, tree, node->parent); | |
611 | 462 | } else { |
612 | 463 | if (NULL != s->left) { |
613 | 464 | s->left->color = rbBlack; |
614 | 465 | } |
615 | - rotateRight(tree, node->parent); | |
466 | + TR_TREE_ROTATE(right, tree, node->parent); | |
616 | 467 | } |
617 | 468 | } |
618 | 469 | ... | ... |
Please
register
or
login
to post a comment