| 1 |
/* Copyright (c) 2013-2019, David Anderson |
| 2 |
All rights reserved. |
| 3 |
|
| 4 |
Redistribution and use in source and binary forms, with |
| 5 |
or without modification, are permitted provided that the |
| 6 |
following conditions are met: |
| 7 |
|
| 8 |
Redistributions of source code must retain the above |
| 9 |
copyright notice, this list of conditions and the following |
| 10 |
disclaimer. |
| 11 |
|
| 12 |
Redistributions in binary form must reproduce the above |
| 13 |
copyright notice, this list of conditions and the following |
| 14 |
disclaimer in the documentation and/or other materials |
| 15 |
provided with the distribution. |
| 16 |
|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 18 |
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 19 |
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 |
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 22 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 24 |
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 26 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 28 |
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 29 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
|
| 31 |
*/ |
| 32 |
|
| 33 |
/* The interfaces follow tsearch (See the Single |
| 34 |
Unix Specification) but the implementation is |
| 35 |
written without reference to the source of any |
| 36 |
version of tsearch. |
| 37 |
|
| 38 |
See http://www.prevanders.net/tsearch.html |
| 39 |
for information and an example of use. |
| 40 |
|
| 41 |
Based on Knuth, chapter 6.2.2 |
| 42 |
And based on chapter 6.2.3 Balanced Trees (sometimes |
| 43 |
call AVL trees) Algorithm A and the sketch on deletion. |
| 44 |
|
| 45 |
The wikipedia page on AVL trees is also quite useful. |
| 46 |
|
| 47 |
A Key equation is: |
| 48 |
bal-factor-node-k = |
| 49 |
height-left-subtree - height-right-subtree |
| 50 |
We don't know the absolute height, but we do know the |
| 51 |
balance factor of the pointed-to subtrees (-1,0, or 1). |
| 52 |
And we always know if we are adding or deleting a node. |
| 53 |
|
| 54 |
Since dwarfdump does not use the tdump facility |
| 55 |
of tsearchbal.c, we never define BUILD_TDUMP |
| 56 |
*/ |
| 57 |
|
| 58 |
#include <config.h> |
| 59 |
#include <stdio.h> /* fprintf() printf() sprintf() */ |
| 60 |
#include <stddef.h> /* NULL size_t */ |
| 61 |
#include <stdlib.h> /* calloc() free() malloc() */ |
| 62 |
|
| 63 |
#ifdef HAVE_STDINT_H |
| 64 |
#include <stdint.h> /* uintptr_t */ |
| 65 |
#endif /* HAVE_STDINT_H */ |
| 66 |
|
| 67 |
#include "libdwarf.h" |
| 68 |
#include "libdwarf_private.h" |
| 69 |
#include "dd_tsearchbal.h" |
| 70 |
|
| 71 |
#define IMPLEMENTD15 1 |
| 72 |
|
| 73 |
#ifdef DW_CHECK_CONSISTENCY |
| 74 |
struct ts_entry; |
| 75 |
void |
| 76 |
ddlocal_dwarf_check_balance(struct ts_entry *head,int finalprefix); |
| 77 |
#endif |
| 78 |
|
| 79 |
/* HEAD is a special record. rlink points to root node. |
| 80 |
As of September 2022, the following is true: The keypointer |
| 81 |
and balance and llink fields of the HEAD node are not used. |
| 82 |
The treedepth field is not used except in HEAD. |
| 83 |
|
| 84 |
In earlier versions, llink was used as an integer |
| 85 |
representing treedepth in HEAD and a pointer elsewhere. |
| 86 |
This lead to certain casts and pointer subtractions in |
| 87 |
setting tree depth that a recent compiler identified |
| 88 |
as having undefined reault in C. Knuth had no compiiler |
| 89 |
concerns because his algorithms are not in C! |
| 90 |
*/ |
| 91 |
|
| 92 |
struct ts_entry { |
| 93 |
/* Keyptr points to a pointer to a record the user saved, the |
| 94 |
user record contains the user's key itself |
| 95 |
and perhaps more. We will request free, |
| 96 |
so const void * is not quite right. */ |
| 97 |
void *keyptr; |
| 98 |
/* treedepth is only set in the HEAD node, |
| 99 |
and in this implementation head->llink is |
| 100 |
always zero. */ |
| 101 |
int treedepth; |
| 102 |
/* balance only used in the tree proper, not in HEAD */ |
| 103 |
int balance; /* Knuth 6.2.3 algorithm A */ |
| 104 |
struct ts_entry * llink; |
| 105 |
struct ts_entry * rlink; |
| 106 |
}; |
| 107 |
|
| 108 |
#ifdef BUILD_TDUMP |
| 109 |
static void |
| 110 |
printlevel(int level) |
| 111 |
{ |
| 112 |
int len = 0; |
| 113 |
int targetlen = 4 + level; |
| 114 |
int shownlen = 0; |
| 115 |
char number[40]; |
| 116 |
/* This is a safe sprintf. No need for esb here. */ |
| 117 |
len = sprintf(number,"<%d>",level); |
| 118 |
printf("%s",number); |
| 119 |
shownlen = len; |
| 120 |
while(shownlen < targetlen) { |
| 121 |
putchar(' '); |
| 122 |
++shownlen; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/* For debugging, mainly. |
| 127 |
We print the tree with the head node unnumbered |
| 128 |
and the root node called level 0. |
| 129 |
In Knuth algorithms where we have p[k] when |
| 130 |
k is zero k refers to the head node. Handy |
| 131 |
as then the root node is not special at all. |
| 132 |
But here it just looks better as shown, perhaps. |
| 133 |
|
| 134 |
The ordering here is so that if you turned an output |
| 135 |
page with the left side at the top |
| 136 |
then the tree sort of just shows up nicely in |
| 137 |
what most think of as a normal way. |
| 138 |
*/ |
| 139 |
static void |
| 140 |
tdump_inner(struct ts_entry *t, |
| 141 |
char *(keyprint)(const void *), |
| 142 |
const char *descr, int level) |
| 143 |
{ |
| 144 |
const char * keyv = ""; |
| 145 |
if (!t) { |
| 146 |
return; |
| 147 |
} |
| 148 |
tdump_inner(t->rlink,keyprint,"right",level+1); |
| 149 |
|
| 150 |
printlevel(level); |
| 151 |
if (t->keyptr) { |
| 152 |
keyv = keyprint(t->keyptr); |
| 153 |
} |
| 154 |
printf("0x%08" DW_PR_DUx " <keyptr 0x%08" DW_PR_DUx "> " |
| 155 |
"<%s %s> <bal %3d> " |
| 156 |
"<l 0x%08" DW_PR_DUx "> <r 0x%08" DW_PR_DUx "> " |
| 157 |
"%s\n", |
| 158 |
(Dwarf_Unsigned)(uintptr_t)t, |
| 159 |
(Dwarf_Unsigned)(uintptr_t)t->keyptr, |
| 160 |
t->keyptr?"key ":"null", |
| 161 |
keyv, |
| 162 |
t->balance, |
| 163 |
(Dwarf_Unsigned)(uintptr_t)t->llink, |
| 164 |
(Dwarf_Unsigned)(uintptr_t)t->rlink, |
| 165 |
descr); |
| 166 |
tdump_inner(t->llink,keyprint,"left ",level+1); |
| 167 |
} |
| 168 |
#endif /* BUILD_TDUMP */ |
| 169 |
#ifdef DW_CHECK_CONSISTENCY |
| 170 |
/* Checking that a tree (or sub tree) is in balance. |
| 171 |
Only meaningful for balanced trees. |
| 172 |
Returns the depth. |
| 173 |
*/ |
| 174 |
int |
| 175 |
dd_local_check_balance_inner(struct ts_entry *t,int level, |
| 176 |
int maxdepth, |
| 177 |
int *founderror, |
| 178 |
const char *prefix) |
| 179 |
{ |
| 180 |
int l = 0; |
| 181 |
int r = 0; |
| 182 |
if (level > maxdepth) { |
| 183 |
printf("%s Likely internal erroneous link loop, " |
| 184 |
"got to depth %d.\n", |
| 185 |
prefix,level); |
| 186 |
exit(EXIT_FAILURE); |
| 187 |
} |
| 188 |
if (!t) { |
| 189 |
return 0; |
| 190 |
} |
| 191 |
if (!t->llink && !t->rlink) { |
| 192 |
if (t->balance != 0) { |
| 193 |
printf("%s: Balance at 0x%" DW_PR_DUx |
| 194 |
" should be 0 is %d.\n", |
| 195 |
prefix, |
| 196 |
(Dwarf_Unsigned)(uintptr_t)t, |
| 197 |
t->balance); |
| 198 |
(*founderror)++; |
| 199 |
} |
| 200 |
return 1; |
| 201 |
} |
| 202 |
l = dd_local_check_balance_inner(t->llink,level+1,maxdepth, |
| 203 |
founderror,prefix); |
| 204 |
r = dd_local_check_balance_inner(t->rlink,level+1,maxdepth, |
| 205 |
founderror,prefix); |
| 206 |
if (l ==r && t->balance != 0) { |
| 207 |
printf("%s Balance at 0x%" DW_PR_DUx |
| 208 |
" d should be 0 is %d.\n", |
| 209 |
prefix, |
| 210 |
(Dwarf_Unsigned)(uintptr_t)t, |
| 211 |
t->balance); |
| 212 |
(*founderror)++; |
| 213 |
return l+1; |
| 214 |
} |
| 215 |
if (l > r) { |
| 216 |
if ((l-r) != 1) { |
| 217 |
printf("%s depth mismatch at 0x%" DW_PR_DUx |
| 218 |
" l %d r %d.\n", |
| 219 |
prefix, |
| 220 |
(Dwarf_Unsigned)(uintptr_t)t, |
| 221 |
l,r); |
| 222 |
(*founderror)++; |
| 223 |
} |
| 224 |
if (t->balance != -1) { |
| 225 |
printf("%s Balance at 0x%" DW_PR_DUx |
| 226 |
" should be -1 is %d.\n", |
| 227 |
prefix, |
| 228 |
(Dwarf_Unsigned)(uintptr_t)t, |
| 229 |
t->balance); |
| 230 |
(*founderror)++; |
| 231 |
} |
| 232 |
return l+1; |
| 233 |
} |
| 234 |
if (r != l) { |
| 235 |
if ((r-l) != 1) { |
| 236 |
printf("%s depth mismatch at 0x%" DW_PR_DUx |
| 237 |
" r %d l %d.\n", |
| 238 |
prefix, |
| 239 |
(Dwarf_Unsigned)(uintptr_t)t, |
| 240 |
r,l); |
| 241 |
(*founderror)++; |
| 242 |
} |
| 243 |
if (t->balance != 1) { |
| 244 |
printf("%s Balance at 0x%" DW_PR_DUx |
| 245 |
" should be 1 is %d.\n", |
| 246 |
prefix, |
| 247 |
(Dwarf_Unsigned)(uintptr_t)t, |
| 248 |
t->balance); |
| 249 |
(*founderror)++; |
| 250 |
} |
| 251 |
} else { |
| 252 |
if (t->balance != 0) { |
| 253 |
printf("%s Balance at 0x%" DW_PR_DUx |
| 254 |
" should be 0 is %d.\n", |
| 255 |
prefix, |
| 256 |
(Dwarf_Unsigned)(uintptr_t)t, |
| 257 |
t->balance); |
| 258 |
(*founderror)++; |
| 259 |
} |
| 260 |
} |
| 261 |
return r+1; |
| 262 |
} |
| 263 |
|
| 264 |
void |
| 265 |
ddlocal_dwarf_check_balance(struct ts_entry *head,int finalprefix) |
| 266 |
{ |
| 267 |
const char *prefix = 0; |
| 268 |
int maxdepth = 0; |
| 269 |
size_t headdepth = 0; |
| 270 |
int errcount = 0; |
| 271 |
int depth = 0; |
| 272 |
struct ts_entry*root = 0; |
| 273 |
if (finalprefix) { |
| 274 |
prefix = "BalanceError:"; |
| 275 |
} else { |
| 276 |
prefix = "BalanceWarn:"; |
| 277 |
} |
| 278 |
|
| 279 |
if (!head) { |
| 280 |
printf("%s check balance null tree ptr\n",prefix); |
| 281 |
return; |
| 282 |
} |
| 283 |
root = head->rlink; |
| 284 |
headdepth = head->treedepth; |
| 285 |
if (!root) { |
| 286 |
printf("%s check balance null tree ptr\n",prefix); |
| 287 |
return; |
| 288 |
} |
| 289 |
|
| 290 |
maxdepth = headdepth+10; |
| 291 |
/* Counting in levels, not level number of top level. */ |
| 292 |
headdepth++; |
| 293 |
depth = ddlocal_dwarf_check_balance_inner(root,depth,maxdepth, |
| 294 |
&errcount,prefix); |
| 295 |
if (depth != headdepth) { |
| 296 |
printf("%s Head node says depth %lu, it is really %d\n", |
| 297 |
prefix, |
| 298 |
(unsigned long)headdepth, |
| 299 |
depth); |
| 300 |
++errcount; |
| 301 |
} |
| 302 |
if (errcount) { |
| 303 |
printf("%s error count %d\n",prefix,errcount); |
| 304 |
} |
| 305 |
return; |
| 306 |
} |
| 307 |
#endif |
| 308 |
|
| 309 |
#ifdef BUILD_TDUMP |
| 310 |
/* Dumping the tree to stdout. */ |
| 311 |
void |
| 312 |
dwarf_tdump(const void*headp_in, |
| 313 |
char *(*keyprint)(const void *), |
| 314 |
const char *msg) |
| 315 |
{ |
| 316 |
const struct ts_entry *head = (const struct ts_entry *)headp_in; |
| 317 |
struct ts_entry *root = 0; |
| 318 |
size_t headdepth = 0; |
| 319 |
if (!head) { |
| 320 |
printf("dumptree null tree ptr : %s\n",msg); |
| 321 |
return; |
| 322 |
} |
| 323 |
headdepth = head->treedepth; |
| 324 |
printf("dumptree head ptr : 0x%08" DW_PR_DUx |
| 325 |
" tree-depth %d: %s\n", |
| 326 |
(Dwarf_Unsigned)(uintptr_t)head, |
| 327 |
(int)headdepth, |
| 328 |
msg); |
| 329 |
root = head->rlink; |
| 330 |
if (!root) { |
| 331 |
printf("Empty tree\n"); |
| 332 |
return; |
| 333 |
} |
| 334 |
tdump_inner(root,keyprint,"top",0); |
| 335 |
} |
| 336 |
#endif /* BUILD_TDUMP */ |
| 337 |
static void |
| 338 |
setlink(struct ts_entry*t,int a,struct ts_entry *x) |
| 339 |
{ |
| 340 |
if (a < 0) { |
| 341 |
t->llink = x; |
| 342 |
} else { |
| 343 |
t->rlink = x; |
| 344 |
} |
| 345 |
} |
| 346 |
static struct ts_entry* |
| 347 |
getlink(struct ts_entry*t,int a) |
| 348 |
{ |
| 349 |
if (a < 0) { |
| 350 |
return(t->llink); |
| 351 |
} |
| 352 |
return(t->rlink); |
| 353 |
} |
| 354 |
|
| 355 |
static struct ts_entry * |
| 356 |
allocate_ts_entry(const void *key) |
| 357 |
{ |
| 358 |
struct ts_entry *e = (struct ts_entry *) |
| 359 |
malloc(sizeof(struct ts_entry)); |
| 360 |
if (!e) { |
| 361 |
return NULL; |
| 362 |
} |
| 363 |
/* We will eventually ask it be freed, so |
| 364 |
being const void * in is not quite right. */ |
| 365 |
e->keyptr = (void *)key; |
| 366 |
e->balance = 0; |
| 367 |
e->treedepth = 0; |
| 368 |
e->llink = 0; |
| 369 |
e->rlink = 0; |
| 370 |
return e; |
| 371 |
} |
| 372 |
|
| 373 |
/* Knuth step T5, the insert. */ |
| 374 |
static struct ts_entry * |
| 375 |
tsearch_insert_k(const void *key,int kc, |
| 376 |
struct ts_entry *p) |
| 377 |
{ |
| 378 |
struct ts_entry *q = allocate_ts_entry(key); |
| 379 |
if (!q) { |
| 380 |
/* out of memory */ |
| 381 |
return NULL; |
| 382 |
} |
| 383 |
setlink(p,kc,q); |
| 384 |
/* Non-NULL means inserted. */ |
| 385 |
return q; |
| 386 |
} |
| 387 |
|
| 388 |
/* Knuth step T5. */ |
| 389 |
static struct ts_entry * |
| 390 |
tsearch_do_insert(const void *key, |
| 391 |
int kc, |
| 392 |
int * inserted, |
| 393 |
struct ts_entry* p) |
| 394 |
{ |
| 395 |
struct ts_entry *q = 0; |
| 396 |
q = tsearch_insert_k(key,kc,p); |
| 397 |
if (q) { |
| 398 |
*inserted = 1; |
| 399 |
} |
| 400 |
return q; |
| 401 |
} |
| 402 |
|
| 403 |
/* Algorithm A of Knuth 6.2.3, balanced tree. |
| 404 |
key is pointer to a user data area containing the key |
| 405 |
and possibly more. |
| 406 |
|
| 407 |
We could recurse on this routine, but instead we |
| 408 |
iterate (like Knuth does, but using for (;;) instead |
| 409 |
of go-to. */ |
| 410 |
static struct ts_entry * |
| 411 |
tsearch_inner( const void *key, struct ts_entry* head, |
| 412 |
int (*compar)(const void *, const void *), |
| 413 |
int*inserted) |
| 414 |
{ |
| 415 |
/* t points to parent of p */ |
| 416 |
struct ts_entry *t = head; |
| 417 |
/* p moves down tree, p starts as root. */ |
| 418 |
struct ts_entry *p = head->rlink; |
| 419 |
/* s points where rebalancing may be needed. */ |
| 420 |
struct ts_entry *s = p; |
| 421 |
struct ts_entry *r = 0; |
| 422 |
struct ts_entry *q = 0; |
| 423 |
|
| 424 |
int a = 0; |
| 425 |
int kc = 0; |
| 426 |
for (;;) { |
| 427 |
/* A2. */ |
| 428 |
kc = compar(key,p->keyptr); |
| 429 |
if (kc) { |
| 430 |
/* A3 and A4 handled here. */ |
| 431 |
q = getlink(p,kc); |
| 432 |
if (!q) { |
| 433 |
/* Does step A5. */ |
| 434 |
q = tsearch_do_insert(key,kc,inserted,p); |
| 435 |
if (!q) { |
| 436 |
/* Out of memory. */ |
| 437 |
return q; |
| 438 |
} |
| 439 |
break; /* to A5. */ |
| 440 |
} |
| 441 |
if (q->balance) { |
| 442 |
t = p; |
| 443 |
/* t is no longer a pointer to HEAD */ |
| 444 |
s = q; |
| 445 |
} |
| 446 |
p = q; |
| 447 |
continue; |
| 448 |
} |
| 449 |
/* K = KEY(P) in Knuth. */ |
| 450 |
/* kc == 0, we found the entry we search for. */ |
| 451 |
return p; |
| 452 |
} |
| 453 |
/* A5: work already done. */ |
| 454 |
|
| 455 |
/* A6: */ |
| 456 |
{ |
| 457 |
/* Balance factors on nodes betwen S and Q need to be |
| 458 |
changed from zero to +-1 */ |
| 459 |
int kc2 = compar(key,s->keyptr); |
| 460 |
if (kc2 < 0) { |
| 461 |
a = -1; |
| 462 |
} else { |
| 463 |
a = 1; |
| 464 |
} |
| 465 |
r = p = getlink(s,a); |
| 466 |
while (p != q) { |
| 467 |
int kc3 = compar(key,p->keyptr); |
| 468 |
if (kc3 < 0) { |
| 469 |
p->balance = -1; |
| 470 |
p = p->llink; |
| 471 |
} else if (kc3 > 0) { |
| 472 |
p->balance = 1; |
| 473 |
p = p->rlink; |
| 474 |
} else { |
| 475 |
/* ASSERT: p == q */ |
| 476 |
break; |
| 477 |
} |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
/* A7: */ |
| 482 |
{ |
| 483 |
if (! s->balance) { |
| 484 |
/* Tree has grown higher. */ |
| 485 |
s->balance = a; |
| 486 |
head->treedepth++; |
| 487 |
return q; |
| 488 |
} |
| 489 |
if (s->balance == -a) { |
| 490 |
/* Tree is more balanced */ |
| 491 |
s->balance = 0; |
| 492 |
return q; |
| 493 |
} |
| 494 |
if (s->balance == a) { |
| 495 |
/* Rebalance. */ |
| 496 |
if (r->balance == a) { |
| 497 |
/* single rotation, step A8. */ |
| 498 |
p = r; |
| 499 |
setlink(s,a,getlink(r,-a)); |
| 500 |
setlink(r,-a,s); |
| 501 |
s->balance = 0; |
| 502 |
r->balance = 0; |
| 503 |
} else if (r->balance == -a) { |
| 504 |
/* double rotation, step A9. */ |
| 505 |
p = getlink(r,-a); |
| 506 |
setlink(r,-a,getlink(p,a)); |
| 507 |
setlink(p,a,r); |
| 508 |
setlink(s,a,getlink(p,-a)); |
| 509 |
setlink(p,-a,s); |
| 510 |
if (p->balance == a) { |
| 511 |
s->balance = -a; |
| 512 |
r->balance = 0; |
| 513 |
} else if (p->balance == 0) { |
| 514 |
s->balance = 0; |
| 515 |
r->balance = 0; |
| 516 |
} else if (p->balance == -a) { |
| 517 |
s->balance = 0; |
| 518 |
r->balance = a; |
| 519 |
} |
| 520 |
p->balance = 0; |
| 521 |
} else { |
| 522 |
fprintf(stderr," ERROR Impossible balanced " |
| 523 |
"tree situation!\n"); |
| 524 |
/* Impossible. Cannot be here. */ |
| 525 |
exit(EXIT_FAILURE); |
| 526 |
} |
| 527 |
} else { |
| 528 |
fprintf(stderr,"ERROR Impossible balanced " |
| 529 |
"tree situation!!\n"); |
| 530 |
/* Impossible. Cannot be here. */ |
| 531 |
exit(EXIT_FAILURE); |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
/* A10: */ |
| 536 |
if (s == t->rlink) { |
| 537 |
/* ASSERT: t may or may not be HEAD */ |
| 538 |
t->rlink = p; |
| 539 |
} else { |
| 540 |
/* ASSERT: t is not HEAD */ |
| 541 |
t->llink = p; |
| 542 |
} |
| 543 |
#ifdef DW_CHECK_CONSISTENCY |
| 544 |
ddlocal_dwarf_check_balance(head,1); |
| 545 |
#endif |
| 546 |
return q; |
| 547 |
} |
| 548 |
/* Search and, if missing, insert. */ |
| 549 |
void * |
| 550 |
dwarf_tsearch(const void *key, void **headin, |
| 551 |
int (*compar)(const void *, const void *)) |
| 552 |
{ |
| 553 |
struct ts_entry **headp = (struct ts_entry **)headin; |
| 554 |
struct ts_entry *head = 0; |
| 555 |
struct ts_entry *r = 0; |
| 556 |
int inserted = 0; |
| 557 |
|
| 558 |
if (!headp) { |
| 559 |
return NULL; |
| 560 |
} |
| 561 |
head = *headp; |
| 562 |
if (!head) { |
| 563 |
struct ts_entry *root = 0; |
| 564 |
head = allocate_ts_entry(0); |
| 565 |
if (!head) { |
| 566 |
return NULL; |
| 567 |
} |
| 568 |
root = allocate_ts_entry(key); |
| 569 |
if (!root) { |
| 570 |
free(head); |
| 571 |
return NULL; |
| 572 |
} |
| 573 |
head->rlink = root; |
| 574 |
/* head->llink is used for the depth, as a count */ |
| 575 |
/* head points to the special head node ... */ |
| 576 |
*headin = head; |
| 577 |
return (void *)(&(root->keyptr)); |
| 578 |
} |
| 579 |
r = tsearch_inner(key,head,compar,&inserted); |
| 580 |
if (!r) { |
| 581 |
return NULL; |
| 582 |
} |
| 583 |
return (void *)&(r->keyptr); |
| 584 |
} |
| 585 |
|
| 586 |
/* Search without insert. */ |
| 587 |
void * |
| 588 |
dwarf_tfind(const void *key, void *const*rootp, |
| 589 |
int (*compar)(const void *, const void *)) |
| 590 |
{ |
| 591 |
struct ts_entry * const *phead = (struct ts_entry * const*)rootp; |
| 592 |
struct ts_entry *head = 0; |
| 593 |
struct ts_entry *p = 0; |
| 594 |
if (!phead) { |
| 595 |
return NULL; |
| 596 |
} |
| 597 |
head = *phead; |
| 598 |
if (!head) { |
| 599 |
return NULL; |
| 600 |
} |
| 601 |
p = head->rlink; |
| 602 |
while(p) { |
| 603 |
int kc = compar(key,p->keyptr); |
| 604 |
if (!kc) { |
| 605 |
return (void *)(&(p->keyptr)); |
| 606 |
} |
| 607 |
p = getlink(p,kc); |
| 608 |
} |
| 609 |
return NULL; |
| 610 |
} |
| 611 |
|
| 612 |
/* Used for an array of records used in the deletion code. |
| 613 |
k == 0 for the special head node which is never matched by |
| 614 |
input. |
| 615 |
k == 1 etc. |
| 616 |
*/ |
| 617 |
struct pkrecord { |
| 618 |
struct ts_entry *pk; |
| 619 |
int ak; /* Is -1 or +1 */ |
| 620 |
}; |
| 621 |
|
| 622 |
/* Here we rearrange the tree so the node p to be deleted |
| 623 |
is a node with a null left link. With that done |
| 624 |
we can fix pkarray and then we can use the pkarray |
| 625 |
to rebalance. |
| 626 |
It's a bit long, so we refactor out the code from |
| 627 |
where it is called. |
| 628 |
The rearrangement is Algorithm 6.2.2D in Knuth. |
| 629 |
PRECONDITION: p,p->rlink, pp non-null. |
| 630 |
RETURNS: new high index of pkarray. |
| 631 |
*/ |
| 632 |
|
| 633 |
static unsigned |
| 634 |
rearrange_tree_so_p_llink_null( struct pkrecord * pkarray, |
| 635 |
unsigned k, |
| 636 |
struct ts_entry *r, |
| 637 |
struct ts_entry *p, |
| 638 |
struct ts_entry *pp, |
| 639 |
int ppak) |
| 640 |
{ |
| 641 |
struct ts_entry *s = 0; |
| 642 |
unsigned k2 = 0; /* indexing pkarray */ |
| 643 |
int pbalance = p->balance; |
| 644 |
|
| 645 |
/* Step D3 */ |
| 646 |
/* Since we are going to modify the tree by |
| 647 |
movement of a node down the tree a ways, |
| 648 |
we need to build pkarray with the (not yet |
| 649 |
found) new next node, in pkarray[k], not |
| 650 |
p. |
| 651 |
The deletion will be of p, but by then |
| 652 |
p will be moved in the tree so it has a null left link. |
| 653 |
P's possibly-non-null right link |
| 654 |
*/ |
| 655 |
k2 = k; |
| 656 |
k2++; |
| 657 |
r = p->rlink; |
| 658 |
pkarray[k2].pk = r; |
| 659 |
pkarray[k2].ak = -1; |
| 660 |
s = r->llink; |
| 661 |
/* Move down and left to get a null llink. */ |
| 662 |
while (s->llink) { |
| 663 |
k2++; |
| 664 |
r = s; |
| 665 |
s = r->llink; |
| 666 |
pkarray[k2].pk = r; |
| 667 |
pkarray[k2].ak = -1; |
| 668 |
} |
| 669 |
/* Now we move S up in place (in the tree) |
| 670 |
of the node P we will delete. |
| 671 |
and p replaces s. |
| 672 |
Finally winding up with a newly shaped balanced tree. |
| 673 |
*/ |
| 674 |
{ |
| 675 |
struct ts_entry *tmp = 0; |
| 676 |
int sbalance = s->balance; |
| 677 |
s->llink = p->llink; |
| 678 |
r->llink = p; |
| 679 |
p->llink = 0; |
| 680 |
tmp = p->rlink; |
| 681 |
p->rlink = s->rlink; |
| 682 |
s->rlink = tmp; |
| 683 |
setlink(pp,ppak,s); |
| 684 |
s->balance = pbalance; |
| 685 |
p->balance = sbalance; |
| 686 |
/* Now the tree is rearranged and still in balance. */ |
| 687 |
|
| 688 |
/* Replace the previous k position entry with S. |
| 689 |
We trace the right link off of the moved S node. */ |
| 690 |
pkarray[k].pk = s; |
| 691 |
pkarray[k].ak = 1; |
| 692 |
|
| 693 |
r->llink = p->rlink; |
| 694 |
/* Now p is out of the tree and we start |
| 695 |
the rebalance at r. pkarray Index k2. */ |
| 696 |
} |
| 697 |
/* Step D4 */ |
| 698 |
free(p); |
| 699 |
return k2; |
| 700 |
} |
| 701 |
|
| 702 |
/* Returns deleted node parent unless the head changed. |
| 703 |
Returns NULL if wanted node not found or the tree |
| 704 |
is now empty or the head node changed. |
| 705 |
Sets *did_delete if it found and deleted a node. |
| 706 |
Sets *tree_empty if there are no more user nodes present. |
| 707 |
*/ |
| 708 |
static struct ts_entry * |
| 709 |
tdelete_inner(const void *key, |
| 710 |
struct ts_entry *head, |
| 711 |
int (*compar)(const void *, const void *), |
| 712 |
int *tree_empty, |
| 713 |
int *did_delete) |
| 714 |
{ |
| 715 |
struct ts_entry *p = 0; |
| 716 |
struct ts_entry *pp = 0; |
| 717 |
struct pkrecord * pkarray = 0; |
| 718 |
size_t depth = head->treedepth; |
| 719 |
unsigned k = 0; |
| 720 |
|
| 721 |
/* Allocate extra, head is on the stack we create |
| 722 |
here and the depth might increase. */ |
| 723 |
depth = depth + 4; |
| 724 |
pkarray = calloc(depth, sizeof(struct pkrecord)); |
| 725 |
if (!pkarray) { |
| 726 |
/* Malloc fails, we could abort... */ |
| 727 |
return NULL; |
| 728 |
} |
| 729 |
k = 0; |
| 730 |
pkarray[k].pk=head; |
| 731 |
pkarray[k].ak=1; |
| 732 |
p = head->rlink; |
| 733 |
while(p) { |
| 734 |
int kc = 0; |
| 735 |
k++; |
| 736 |
kc = compar(key,p->keyptr); |
| 737 |
pkarray[k].pk = p; |
| 738 |
pkarray[k].ak = kc; |
| 739 |
if (!kc) { |
| 740 |
break; |
| 741 |
} |
| 742 |
p = getlink(p,kc); |
| 743 |
} |
| 744 |
if (!p) { |
| 745 |
/* Node to delete never found. */ |
| 746 |
free(pkarray); |
| 747 |
return NULL; |
| 748 |
} |
| 749 |
|
| 750 |
{ |
| 751 |
struct ts_entry *t = 0; |
| 752 |
struct ts_entry *r = 0; |
| 753 |
int ppak = 0; |
| 754 |
|
| 755 |
p = pkarray[k].pk; |
| 756 |
pp = pkarray[k-1].pk; |
| 757 |
ppak = pkarray[k-1].ak; |
| 758 |
|
| 759 |
/* Found a match. p to be deleted. */ |
| 760 |
t = p; |
| 761 |
*did_delete = 1; |
| 762 |
if (!t->rlink) { |
| 763 |
if (k == 1 && !t->llink) { |
| 764 |
*tree_empty = 1; |
| 765 |
/* upper level will fix up head node. */ |
| 766 |
free(t); |
| 767 |
free(pkarray); |
| 768 |
return NULL; |
| 769 |
} |
| 770 |
/* t->llink might be NULL. */ |
| 771 |
setlink(pp,ppak,t->llink); |
| 772 |
/* ASSERT: t->llink NULL or t->llink |
| 773 |
has no children, balance zero and balance |
| 774 |
of t->llink not changing. */ |
| 775 |
k--; |
| 776 |
/* Step D4. */ |
| 777 |
free(t); |
| 778 |
goto balance; |
| 779 |
} |
| 780 |
#ifdef IMPLEMENTD15 |
| 781 |
/* Step D1.5 */ |
| 782 |
if (!t->llink) { |
| 783 |
setlink(pp,ppak,t->rlink); |
| 784 |
/* we change the left link off ak */ |
| 785 |
k--; |
| 786 |
/* Step D4. */ |
| 787 |
free(t); |
| 788 |
goto balance; |
| 789 |
} |
| 790 |
#endif /* IMPLEMENTD15 */ |
| 791 |
/* Step D2 */ |
| 792 |
r = t->rlink; |
| 793 |
if (!r->llink) { |
| 794 |
/* We decrease the height of the right tree. */ |
| 795 |
r->llink = t->llink; |
| 796 |
setlink(pp,ppak,r); |
| 797 |
pkarray[k].pk = r; |
| 798 |
pkarray[k].ak = 1; |
| 799 |
/* The following essential line not mentioned |
| 800 |
in Knuth AFAICT. */ |
| 801 |
r->balance = t->balance; |
| 802 |
/* Step D4. */ |
| 803 |
free(t); |
| 804 |
goto balance; |
| 805 |
} |
| 806 |
/* Step D3, we rearrange the tree |
| 807 |
and pkarray so the balance step can work. |
| 808 |
step D2 is insufficient so not done. */ |
| 809 |
k = rearrange_tree_so_p_llink_null(pkarray,k, |
| 810 |
r,p,pp,ppak); |
| 811 |
goto balance; |
| 812 |
} |
| 813 |
/* Now use pkarray decide if rebalancing |
| 814 |
needed and, if needed, to rebalance. |
| 815 |
k here matches l-1 in Knuth. */ |
| 816 |
balance: |
| 817 |
{ |
| 818 |
unsigned k2 = k; |
| 819 |
/* We do not want a test in the for () itself. */ |
| 820 |
for ( ; 1 ; k2--) { |
| 821 |
struct ts_entry *pk = 0; |
| 822 |
int ak = 0; |
| 823 |
int bk = 0; |
| 824 |
if (k2 == 0) { |
| 825 |
/* decreased in height */ |
| 826 |
head->treedepth--; |
| 827 |
goto cleanup; |
| 828 |
} |
| 829 |
pk = pkarray[k2].pk; |
| 830 |
if (!pk) { |
| 831 |
/* Nothing here to work with. Move up. */ |
| 832 |
continue; |
| 833 |
} |
| 834 |
ak = pkarray[k2].ak; |
| 835 |
bk = pk->balance; |
| 836 |
if (bk == ak) { |
| 837 |
pk->balance = 0; |
| 838 |
continue; |
| 839 |
} |
| 840 |
if (bk == 0) { |
| 841 |
pk->balance = -ak; |
| 842 |
goto cleanup; |
| 843 |
} |
| 844 |
/* ASSERT: bk == -ak. We |
| 845 |
will use bk == adel here (just below). */ |
| 846 |
/* Rebalancing required. Here we use (1) and (2) |
| 847 |
in 6.2.3 to adjust the nodes */ |
| 848 |
{ |
| 849 |
/* Rebalance. We use s for what |
| 850 |
is called A in Knuth Case 1, Case 2 |
| 851 |
page 461. r For what is called B. |
| 852 |
So the link movement logic looks similar |
| 853 |
to the tsearch insert case.*/ |
| 854 |
struct ts_entry *r = 0; |
| 855 |
struct ts_entry *s = 0; |
| 856 |
struct ts_entry *pa = 0; |
| 857 |
int pak = 0; |
| 858 |
int adel = -ak; |
| 859 |
|
| 860 |
s = pk; |
| 861 |
r = getlink(s,adel); |
| 862 |
pa = pkarray[k2-1].pk; |
| 863 |
pak = pkarray[k2-1].ak; |
| 864 |
if (r->balance == adel) { |
| 865 |
/* case 1. */ |
| 866 |
setlink(s,adel,getlink(r,-adel)); |
| 867 |
setlink(r,-adel,s); |
| 868 |
|
| 869 |
/* A10 in tsearch. */ |
| 870 |
setlink(pa,pak,r); |
| 871 |
s->balance = 0; |
| 872 |
r->balance = 0; |
| 873 |
continue; |
| 874 |
} else if (r->balance == -adel) { |
| 875 |
/* case 2 */ |
| 876 |
/* x plays the role of p in step A9 */ |
| 877 |
struct ts_entry*x = getlink(r,-adel); |
| 878 |
setlink(r,-adel,getlink(x,adel)); |
| 879 |
setlink(x,adel,r); |
| 880 |
setlink(s,adel,getlink(x,-adel)); |
| 881 |
setlink(x,-adel,s); |
| 882 |
|
| 883 |
/* A10 in tsearch. */ |
| 884 |
setlink(pa,pak,x); |
| 885 |
if (x->balance == adel) { |
| 886 |
s->balance = -adel; |
| 887 |
r->balance = 0; |
| 888 |
} else if (x->balance == 0) { |
| 889 |
s->balance = 0; |
| 890 |
r->balance = 0; |
| 891 |
} else if (x->balance == -adel) { |
| 892 |
s->balance = 0; |
| 893 |
r->balance = adel; |
| 894 |
} |
| 895 |
x->balance = 0; |
| 896 |
continue; |
| 897 |
} else { |
| 898 |
/* r->balance == 0 case 3 |
| 899 |
we do a single rotation and |
| 900 |
we are done. */ |
| 901 |
setlink(s,adel,getlink(r,-adel)); |
| 902 |
setlink(r,-adel,s); |
| 903 |
setlink(pa,pak,r); |
| 904 |
r->balance = -adel; |
| 905 |
/*s->balance = r->balance = 0; */ |
| 906 |
goto cleanup; |
| 907 |
} |
| 908 |
} |
| 909 |
} |
| 910 |
} |
| 911 |
cleanup: |
| 912 |
free(pkarray); |
| 913 |
#ifdef DW_CHECK_CONSISTENCY |
| 914 |
ddlocal_dwarf_check_balance(head,1); |
| 915 |
#endif |
| 916 |
return pp; |
| 917 |
} |
| 918 |
|
| 919 |
void * |
| 920 |
dwarf_tdelete(const void *key, void **rootp, |
| 921 |
int (*compar)(const void *, const void *)) |
| 922 |
{ |
| 923 |
struct ts_entry **phead = (struct ts_entry **)rootp; |
| 924 |
struct ts_entry *head = 0; |
| 925 |
/* If a leaf is found, we have to null a parent link |
| 926 |
or the root */ |
| 927 |
struct ts_entry * parentp = 0; |
| 928 |
int tree_empty = 0; |
| 929 |
int did_delete = 0; |
| 930 |
|
| 931 |
if (!phead) { |
| 932 |
return NULL; |
| 933 |
} |
| 934 |
head = *phead; |
| 935 |
if (!head) { |
| 936 |
return NULL; |
| 937 |
} |
| 938 |
if (!head->rlink) { |
| 939 |
return NULL; |
| 940 |
} |
| 941 |
parentp = tdelete_inner(key,head,compar,&tree_empty,&did_delete); |
| 942 |
if (tree_empty) { |
| 943 |
head->rlink = 0; |
| 944 |
head->llink = 0; |
| 945 |
free(head); |
| 946 |
*phead = 0; |
| 947 |
return NULL; |
| 948 |
} |
| 949 |
/* ASSERT: head->rlink non-null. */ |
| 950 |
if (did_delete) { |
| 951 |
if (!parentp) { |
| 952 |
parentp = head->rlink; |
| 953 |
} |
| 954 |
return (void *)(&(parentp->keyptr)); |
| 955 |
} |
| 956 |
/* Not deleted */ |
| 957 |
return NULL; |
| 958 |
} |
| 959 |
|
| 960 |
static void |
| 961 |
dd_local_twalk_inner(struct ts_entry *p, |
| 962 |
void (*action)(const void *nodep, |
| 963 |
const DW_VISIT which, const int depth), |
| 964 |
unsigned level) |
| 965 |
{ |
| 966 |
if (!p->llink && !p->rlink) { |
| 967 |
action((const void *)(&(p->keyptr)),dwarf_leaf,level); |
| 968 |
return; |
| 969 |
} |
| 970 |
action((const void *)(&(p->keyptr)),dwarf_preorder,level); |
| 971 |
if (p->llink) { |
| 972 |
dd_local_twalk_inner(p->llink,action,level+1); |
| 973 |
} |
| 974 |
action((const void *)(&(p->keyptr)),dwarf_postorder,level); |
| 975 |
if (p->rlink) { |
| 976 |
dd_local_twalk_inner(p->rlink,action,level+1); |
| 977 |
} |
| 978 |
action((const void *)(&(p->keyptr)),dwarf_endorder,level); |
| 979 |
} |
| 980 |
|
| 981 |
void |
| 982 |
dwarf_twalk(const void *rootp, |
| 983 |
void (*action)(const void *nodep, |
| 984 |
const DW_VISIT which, const int depth)) |
| 985 |
{ |
| 986 |
const struct ts_entry *head = (const struct ts_entry *)rootp; |
| 987 |
struct ts_entry *root = 0; |
| 988 |
if (!head) { |
| 989 |
return; |
| 990 |
} |
| 991 |
root = head->rlink; |
| 992 |
if (!root) { |
| 993 |
return; |
| 994 |
} |
| 995 |
/* Get to actual tree. */ |
| 996 |
dd_local_twalk_inner(root,action,0); |
| 997 |
} |
| 998 |
|
| 999 |
static void |
| 1000 |
dd_local_tdestroy_inner(struct ts_entry*p, |
| 1001 |
void (*free_node)(void *nodep), |
| 1002 |
int depth) |
| 1003 |
{ |
| 1004 |
if (p->llink) { |
| 1005 |
dd_local_tdestroy_inner(p->llink,free_node,depth+1); |
| 1006 |
p->llink = 0; |
| 1007 |
} |
| 1008 |
if (p->rlink) { |
| 1009 |
dd_local_tdestroy_inner(p->rlink,free_node,depth+1); |
| 1010 |
p->rlink = 0; |
| 1011 |
} |
| 1012 |
free_node((void *)p->keyptr); |
| 1013 |
free(p); |
| 1014 |
} |
| 1015 |
|
| 1016 |
/* Walk the tree, freeing all space in the tree |
| 1017 |
and calling the user's callback function on each node. |
| 1018 |
|
| 1019 |
It is up to the caller to zero out anything pointing to |
| 1020 |
head (ie, that has the value rootp holds) after this |
| 1021 |
returns. |
| 1022 |
*/ |
| 1023 |
void |
| 1024 |
dwarf_tdestroy(void *rootp, void (*free_node)(void *nodep)) |
| 1025 |
{ |
| 1026 |
struct ts_entry *head = (struct ts_entry *)rootp; |
| 1027 |
struct ts_entry *root = 0; |
| 1028 |
if (!head) { |
| 1029 |
return; |
| 1030 |
} |
| 1031 |
root = head->rlink; |
| 1032 |
if (root) { |
| 1033 |
dd_local_tdestroy_inner(root,free_node,0); |
| 1034 |
} |
| 1035 |
free(head); |
| 1036 |
} |