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 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
|
/* nl-symbol.c --- symbol handling routines for newLISP
Copyright (C) 2016 Lutz Mueller
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "newlisp.h"
#include "protos.h"
#ifdef MAC_OSX
#include <sys/mman.h>
#endif
#define str2cmp(s1,s2) \
(( (*(unsigned char *)(s1) << 8) | *((unsigned char *)(s1) + 1) ) - \
( (*(unsigned char *)(s2) << 8) | *((unsigned char *)(s2) + 1) ) )
extern CELL * cellMemory;
extern SYMBOL * trueSymbol;
extern SYMBOL * orSymbol;
SYMBOL * findInsertSymbol(char * key, int forceCreation);
int deleteSymbol(char * key);
void deleteContextSymbols(CELL * cell, int checkReferences);
void changeContextCells(SYMBOL * contextPtr);
CELL dumpSymbol(char * name);
void collectSymbols(SYMBOL * sPtr, CELL * symbolList);
void collectSymbolAssocs(SYMBOL * sPtr, CELL * assocList);
static SYMBOL * root; /* root symbol derived from context */
/* --------- return a list of all symbols in a context -------------- */
CELL * p_symbols(CELL * params)
{
SYMBOL * context;
CELL * symbolList;
symbolList = getCell(CELL_EXPRESSION);
if(params->type == CELL_NIL)
context = currentContext;
else
getContext(params, &context);
if(context) /* check in case we are in debug mode */
collectSymbols((SYMBOL *)((CELL *)context->contents)->aux, symbolList);
return(symbolList);
}
void collectSymbols(SYMBOL * sPtr, CELL * symbolList)
{
CELL * cell;
if(sPtr != NIL_SYM && sPtr != NULL)
{
collectSymbols(sPtr->left, symbolList);
if(symbolList->contents == (UINT)nilCell)
{
symbolList->contents = (UINT)stuffSymbol(sPtr);
symbolList->aux = symbolList->contents;
}
else
{
cell = (CELL *)symbolList->aux;
cell->next = stuffSymbol(sPtr);
symbolList->aux = (UINT)cell->next;
}
collectSymbols(sPtr->right, symbolList);
}
}
CELL * associationsFromTree(SYMBOL * context)
{
CELL * assocsList;
assocsList = getCell(CELL_EXPRESSION);
collectSymbolAssocs((SYMBOL *)((CELL *)context->contents)->aux, assocsList);
return(assocsList);
}
/* only for symbols starting with underscore character _ */
void collectSymbolAssocs(SYMBOL * sPtr, CELL * assocList)
{
CELL * cell;
if(sPtr != NIL_SYM && sPtr != NULL)
{
collectSymbolAssocs(sPtr->left, assocList);
if(*sPtr->name == '_')
{
cell = makeCell(CELL_EXPRESSION, (UINT)stuffString(sPtr->name + 1));
((CELL *)cell->contents)->next = copyCell((CELL *)sPtr->contents);
if(assocList->contents == (UINT)nilCell)
assocList->contents = (UINT)cell;
else
((CELL *)assocList->aux)->next = cell;
assocList->aux = (UINT)cell;
}
collectSymbolAssocs(sPtr->right, assocList);
}
}
SYMBOL * lookupSymbol(char * token, SYMBOL * context)
{
root = (SYMBOL *)((CELL *)context->contents)->aux;
return(findInsertSymbol(token, LOOKUP_ONLY));
}
SYMBOL * makeSafeSymbol(CELL * cell, SYMBOL * context, int flag)
{
char * token;
UINT number;
if(isNumber(cell->type))
{
token = alloca(32);
getIntegerExt(cell, &number, FALSE);
#ifndef EMSCRIPTEN
snprintf(token, 31, "_%"PRIdPTR, number);
#else
snprintf(token, 31, "_%"PRIdPTR, (int)number);
#endif
}
else
{
token = alloca(cell->aux + 1);
*token = '_';
memcpy(token + 1, (char *)cell->contents, cell->aux);
}
if(flag)
return(translateCreateSymbol(token, CELL_NIL, context, TRUE));
root = (SYMBOL *)((CELL *)context->contents)->aux;
return(findInsertSymbol(token, LOOKUP_ONLY));
}
/*
if forceFlag is TRUE then
create the symbol, if not found in the context
specified in that context
else
if not found try to inherit from MAIN as a global
or primitive, else create it in context specified
*/
SYMBOL * translateCreateSymbol
(char * token, int type, SYMBOL * context, int forceFlag)
{
SYMBOL * sPtr;
CELL * cell = NULL;
size_t len;
cell = (CELL *)context->contents;
root = (SYMBOL *)cell->aux;
if(forceFlag)
sPtr = findInsertSymbol(token, FORCE_CREATION);
else /* try to inherit from MAIN, if not here create in current context */
{
sPtr = findInsertSymbol(token, LOOKUP_ONLY);
if(sPtr == NULL)
{
if(context != mainContext)
{
root = (SYMBOL *)((CELL *)mainContext->contents)->aux;
sPtr = findInsertSymbol(token, LOOKUP_ONLY);
/* since 7.2.7 only inherit primitives and other globals */
if(sPtr != NULL && !(sPtr->flags & SYMBOL_GLOBAL))
{
if(symbolType(sPtr) != CELL_CONTEXT
|| (SYMBOL *)((CELL*)sPtr->contents)->contents != sPtr)
sPtr = NULL;
}
root = (SYMBOL *)cell->aux;
}
if(sPtr == NULL)
sPtr = findInsertSymbol(token, FORCE_CREATION);
}
}
/* the symbol existed already, return */
if(sPtr->contents != 0)
return(sPtr);
/* root might have changed, after symbol insertion */
cell->aux = (UINT)root;
/* a new symbol has been allocated by findInsertSymbol() */
if(type != CELL_PRIMITIVE)
{
len = strlen(token);
sPtr->name = (char *)allocMemory(len + 1);
memcpy(sPtr->name, token, len + 1);
cell = copyCell(nilCell);
sPtr->contents = (UINT)cell;
/* make a new context symbol */
if(type == CELL_CONTEXT && context == mainContext)
{
cell->type = CELL_CONTEXT;
cell->contents = (UINT)sPtr;
cell->aux = 0;
sPtr->flags |= (SYMBOL_PROTECTED | SYMBOL_GLOBAL);
}
}
else
{
sPtr->name = token;
sPtr->contents = (UINT)nilCell;
}
sPtr->context = context;
return(sPtr);
}
CELL * assignSymbol(SYMBOL * sPtr, CELL * content)
{
deleteList((CELL*)sPtr->contents);
sPtr->contents = (UINT)content;
return(content);
}
/* ------------------------- dump RB tree info of a symbol -------------------- */
#ifdef SYMBOL_DEBUG
CELL * p_dumpSymbol(CELL * params)
{
char * name;
SYMBOL * sPtr;
getString(params, &name);
sPtr = findInsertSymbol(name, LOOKUP_ONLY);
if(sPtr == NULL)
return(nilCell);
varPrintf(OUT_DEVICE, "name=%s color=%s parent=%s left=%s right=%s\n",
sPtr->name,
(sPtr->color == RED) ? "red" : "black",
(sPtr->parent) ? sPtr->parent->name : "ROOT",
sPtr->left->name,
sPtr->right->name);
return(trueCell);
}
#endif
/* ----------------------------- delete a symbol --------------------------- */
int references(SYMBOL * sPtr, int replaceFlag);
int externalContextReferences(SYMBOL * contextPtr, int replaceFlag);
CELL * p_deleteSymbol(CELL * params)
{
SYMBOL * sPtr = NULL;
CELL * cell;
CELL * ctx;
int checkReferences = TRUE;
cell = evaluateExpression(params);
if(cell->type != CELL_SYMBOL)
return(errorProcExt(ERR_SYMBOL_EXPECTED, params));
sPtr = (SYMBOL*)cell->contents;
if(sPtr == mainContext) return(nilCell);
if(symbolType(sPtr) == CELL_CONTEXT)
{
ctx = (CELL*)sPtr->contents;
if(ctx->contents == (UINT)sPtr)
{
sPtr->flags &= ~SYMBOL_PROTECTED;
cell = ctx;
}
}
if(sPtr->flags & (SYMBOL_PROTECTED | SYMBOL_BUILTIN))
return(nilCell);
/* nil as extra parameter deletes without reference checking.
true as extra parameter deletes only if no references are found,
No extra parameter assumes reference checking is on and if
a reference is found it is replaced with nil.
*/
params = params->next;
/* extra parameter is specified as nil */
if(params != nilCell && !getFlag(params))
checkReferences = FALSE;
/* extra parameter is specified as true */
else if(getFlag(params))
{
if(cell->type == CELL_CONTEXT)
{
if(externalContextReferences(sPtr, FALSE) > 1)
{
sPtr->flags |= SYMBOL_PROTECTED;
return(nilCell);
}
checkReferences = FALSE;
}
else
{
if(references(sPtr, FALSE) > 1)
return(nilCell);
checkReferences = FALSE;
}
}
if(cell->type == CELL_CONTEXT)
{
deleteContextSymbols(cell, checkReferences);
deleteList((CELL *)sPtr->contents);
sPtr->contents = (UINT)copyCell(nilCell);
}
else
{
deleteAndFreeSymbol(sPtr, checkReferences);
}
return(trueCell);
}
void deleteContextSymbols(CELL * cell, int checkReferences)
{
SYMBOL * context;
CELL * symbolList;
CELL * nextSymbol;
context = (SYMBOL *)cell->contents;
if(checkReferences)
externalContextReferences(context, TRUE);
symbolList = getCell(CELL_EXPRESSION);
collectSymbols((SYMBOL *)((CELL *)context->contents)->aux, symbolList);
nextSymbol = (CELL *)symbolList->contents;
while(nextSymbol != nilCell)
{
deleteAndFreeSymbol((SYMBOL*)nextSymbol->contents, FALSE);
nextSymbol = nextSymbol->next;
}
if(checkReferences)
changeContextCells(context);
deleteList(symbolList);
}
void deleteAndFreeSymbol(SYMBOL * sPtr, int checkReferences)
{
SYMBOL * context;
context = sPtr->context;
root = (SYMBOL *)((CELL *)context->contents)->aux;
if(!deleteSymbol(sPtr->name))
return;
((CELL *)context->contents)->aux = (UINT)root; /* root may have changed */
deleteList((CELL *)sPtr->contents);
if(checkReferences) references(sPtr, TRUE);
freeMemory(sPtr->name);
freeMemory(sPtr);
}
void makeContextFromSymbol(SYMBOL * symbol, SYMBOL * treePtr)
{
CELL * contextCell;
UINT * idx = envStackIdx;
/* make sure symbol is not used as local in call hierachy
and symbol is legal */
while(idx > envStack)
{
if(symbol == (SYMBOL *)*(--idx))
{
symbol->contents = (UINT)copyCell(nilCell);
errorProcExt2(ERR_CANNOT_PROTECT_LOCAL, stuffSymbol(symbol));
}
--idx;
}
if(!isLegalSymbol(symbol->name))
{
symbol->contents = (UINT)copyCell(nilCell);
errorProcExt2(ERR_INVALID_PARAMETER, stuffString(symbol->name));
}
contextCell = makeCell(CELL_CONTEXT, (UINT)symbol);
contextCell->aux = (UINT)treePtr;
symbol->contents = (UINT)contextCell;
symbol->context = mainContext;
symbol->flags |= (SYMBOL_PROTECTED | SYMBOL_GLOBAL);
}
/* only used when S in (delete 'S) is not a context */
int references(SYMBOL * sPtr, int replaceFlag)
{
CELL * blockPtr;
int i, count;
blockPtr = cellMemory;
count = 0;
while(blockPtr != NULL)
{
for(i = 0; i < MAX_BLOCK; i++)
{
if( (blockPtr->contents == (UINT)sPtr &&
(*(UINT *)blockPtr == CELL_SYMBOL || *(UINT *)blockPtr == CELL_CONTEXT))
||
( *(UINT *)blockPtr == CELL_DYN_SYMBOL && blockPtr->aux == (UINT)sPtr ) )
{
count++;
if(replaceFlag)
{
blockPtr->type = CELL_SYMBOL;
blockPtr->aux = (UINT)nilCell;
if(*(UINT *)blockPtr == CELL_DYN_SYMBOL)
free((void *)blockPtr->contents);
blockPtr->contents = (UINT)nilSymbol;
}
}
blockPtr++;
}
blockPtr = blockPtr->next;
}
return(count);
}
int externalContextReferences(SYMBOL * contextPtr, int replaceFlag)
{
CELL * blockPtr;
int i, count = 0;
SYMBOL * sPtr;
blockPtr = cellMemory;
while(blockPtr != NULL)
{
for(i = 0; i < MAX_BLOCK; i++)
{
if(blockPtr->type == CELL_SYMBOL)
{
sPtr = (SYMBOL *)blockPtr->contents;
if(sPtr->context == contextPtr)
{
count++;
if(replaceFlag)
blockPtr->contents = (UINT)nilSymbol;
}
}
if(blockPtr->type == CELL_CONTEXT)
{
if((SYMBOL *)blockPtr->contents == contextPtr)
count++;
}
blockPtr++;
}
blockPtr = blockPtr->next;
}
return(count);
}
void changeContextCells(SYMBOL * contextPtr)
{
CELL * blockPtr;
int i;
blockPtr = cellMemory;
while(blockPtr != NULL)
{
for(i = 0; i < MAX_BLOCK; i++)
{
if (blockPtr->type == CELL_CONTEXT)
{
if((SYMBOL *)blockPtr->contents == contextPtr)
{
blockPtr->type = CELL_NIL;
blockPtr->contents = (UINT)nilCell;
blockPtr->aux = (UINT)nilCell;
}
}
blockPtr++;
}
blockPtr = blockPtr->next;
}
}
/* renamed to 'term' in v.10.1.11 */
CELL * p_term(CELL * params)
{
SYMBOL * sPtr;
params = evaluateExpression(params);
if(params->type == CELL_SYMBOL || params->type == CELL_CONTEXT)
sPtr = (SYMBOL *)params->contents;
else
return(errorProcExt(ERR_SYMBOL_OR_CONTEXT_EXPECTED, params));
return(stuffString(sPtr->name));
}
CELL * p_prefix(CELL * params)
{
SYMBOL * sPtr;
getSymbol(params, &sPtr);
return(makeCell(CELL_CONTEXT, (UINT)sPtr->context));
}
/* -------------------------------------------------------------------------
Red-Black Balanced Binary Tree Algorithm adapted from:
Thomas Niemann thomasn@epaperpress.com
See also:
http://epaperpress.com/sortsearch/index.html
and:
Thomas H. Cormen, et al
Introduction to Algorithms
(MIT Electrical Engineering and Computer Science)
(C) 1990 MIT Press
*/
#define compLT(a,b) (a < b)
#define compEQ(a,b) (a == b)
#define BLACK 0
#define RED 1
#define NIL_SYM &sentinel /* all leafs are sentinels */
SYMBOL sentinel = {
0, /* pretty print */
BLACK, /* color */
"NIL", /* name */
0, /* contents */
NULL, /* context */
NULL, /* parent */
NIL_SYM, /* left */
NIL_SYM /* right */
};
void rotateLeft(SYMBOL* x);
void rotateRight(SYMBOL * x);
static void insertFixup(SYMBOL * x);
void deleteFixup(SYMBOL *x);
SYMBOL * createRootContext(char * token)
{
SYMBOL * sPtr;
root = NULL;
mainContext = sPtr = findInsertSymbol(token, TRUE);
sPtr->name = token;
makeContextFromSymbol(sPtr, sPtr);
return(sPtr);
}
/* --------------------------------------------------------------------
lookup the symbol with name key, if it does not exist and the
forceCreation flag is set, create and insert the symbol and
return a pointer to the new symbol. If the context passed is empty
then it's treePtr (root) will be the new symbol.
*/
SYMBOL * findInsertSymbol(char * key, int forceCreation)
{
SYMBOL *current, *parent, *x;
int c;
/* find future parent */
current = (root == NULL) ? NIL_SYM : root;
parent = 0;
while (current != NIL_SYM)
{
if( ((c = str2cmp(key, current->name)) == 0) && ((c = strcmp(key, current->name)) == 0) )
return(current);
parent = current;
current = (c < 0) ? current->left : current->right;
}
/* if forceCreation not specified just return */
if(forceCreation == LOOKUP_ONLY) return(NULL);
/* allocate new symbol */
x = (SYMBOL *)callocMemory(sizeof(SYMBOL));
x->parent = parent;
x->left = NIL_SYM;
x->right = NIL_SYM;
x->color = RED;
/* insert node in tree */
if(parent)
{
if( (c = str2cmp(key, parent->name)) < 0)
parent->left = x;
else if(c > 0)
parent->right = x;
else if(strcmp(key, parent->name) < 0)
parent->left = x;
else
parent->right = x;
}
else
root =x;
insertFixup(x);
/* return new node */
++symbolCount;
return(x);
}
/* --------------------------------------------------------------------
extract symbol in context from tree, return 1 if deleted or 0 if it
couldn't be found.
*/
int deleteSymbol(char * key)
{
SYMBOL *x, *y, *z;
int color, c;
/* find node in tree */
z = (root == NULL) ? NIL_SYM : root;
while(z != NIL_SYM)
{
if( ((c = str2cmp(key, z->name)) == 0) && ((c = strcmp(key, z->name)) == 0) )
break;
else
z = (c < 0) ? z->left : z->right;
}
if (z == NIL_SYM) return(0); /* key to delete not found */
if (z->left == NIL_SYM || z->right == NIL_SYM)
{
/* y has a NIL_SYM node as a child */
y = z;
}
else
{
/* find tree successor with a NIL_SYM node as a child */
y = z->right;
while (y->left != NIL_SYM) y = y->left;
}
/* x is y's only child */
if (y->left != NIL_SYM)
x = y->left;
else
x = y->right;
/* remove y from the parent chain */
x->parent = y->parent;
if (y->parent)
{
if (y == y->parent->left)
y->parent->left = x;
else
y->parent->right = x;
}
else
root = x;
color = y->color;
if (y != z)
{
/* swap y and z */
y->left = z->left;
y->right = z->right;
y->parent = z->parent;
if(z->parent)
{
if(z->parent->left == z)
z->parent->left = y;
else
z->parent->right = y;
}
else root = y;
y->right->parent = y;
y->left->parent = y;
y->color = z->color;
}
if (color == BLACK)
deleteFixup (x);
--symbolCount;
return TRUE;
}
/* -------------------------------------------------------------------- */
void rotateLeft(SYMBOL* x)
{
SYMBOL* y;
y = x->right;
/* establish x->right link */
x->right = y->left;
if (y->left != NIL_SYM)
y->left->parent = x;
/* establish y->parent link */
if(y != NIL_SYM)
y->parent = x->parent;
if (x->parent)
{
if (x == x->parent->left)
x->parent->left = y;
else
x->parent->right = y;
}
else
root = y;
/* link x and y */
y->left = x;
if (x != NIL_SYM)
x->parent = y;
}
void rotateRight(SYMBOL * x)
{
SYMBOL * y;
y = x->left;
/* establish x->left link */
x->left = y->right;
if (y->right != NIL_SYM)
y->right->parent = x;
/* establish y->parent link */
if (y != NIL_SYM)
y->parent = x->parent;
if (x->parent)
{
if (x == x->parent->right)
x->parent->right = y;
else
x->parent->left = y;
}
else
root = y;
/* link x and y */
y->right = x;
if (x != NIL_SYM)
x->parent = y;
}
static void insertFixup(SYMBOL * x)
{
SYMBOL * y;
/* check Red-Black properties */
while (x != root && x->parent->color == RED)
{
/* we have a violation */
if (x->parent == x->parent->parent->left)
{
y = x->parent->parent->right;
if (y->color == RED)
{
/* uncle is RED */
x->parent->color = BLACK;
y->color = BLACK;
x->parent->parent->color = RED;
x = x->parent->parent;
}
else
{
/* uncle is BLACK */
if (x == x->parent->right)
{
/* make x a left child */
x = x->parent;
rotateLeft(x);
}
/* recolor and rotate */
x->parent->color = BLACK;
x->parent->parent->color = RED;
rotateRight(x->parent->parent);
}
}
else
{
/* mirror image of above code */
y = x->parent->parent->left;
if (y->color == RED)
{
/* uncle is RED */
x->parent->color = BLACK;
y->color = BLACK;
x->parent->parent->color = RED;
x = x->parent->parent;
}
else
{
/* uncle is BLACK */
if (x == x->parent->left)
{
x = x->parent;
rotateRight(x);
}
x->parent->color = BLACK;
x->parent->parent->color = RED;
rotateLeft(x->parent->parent);
}
}
}
root->color = BLACK;
}
void deleteFixup(SYMBOL *x)
{
SYMBOL * w;
while (x != root && x->color == BLACK)
{
if (x == x->parent->left)
{
w = x->parent->right;
if (w->color == RED)
{
w->color = BLACK;
x->parent->color = RED;
rotateLeft (x->parent);
w = x->parent->right;
}
if (w->left->color == BLACK && w->right->color == BLACK)
{
w->color = RED;
x = x->parent;
}
else
{
if (w->right->color == BLACK)
{
w->left->color = BLACK;
w->color = RED;
rotateRight (w);
w = x->parent->right;
}
w->color = x->parent->color;
x->parent->color = BLACK;
w->right->color = BLACK;
rotateLeft (x->parent);
x = root;
}
}
else
{
w = x->parent->left;
if (w->color == RED)
{
w->color = BLACK;
x->parent->color = RED;
rotateRight (x->parent);
w = x->parent->left;
}
if (w->right->color == BLACK && w->left->color == BLACK)
{
w->color = RED;
x = x->parent;
}
else
{
if (w->left->color == BLACK)
{
w->right->color = BLACK;
w->color = RED;
rotateLeft (w);
w = x->parent->left;
}
w->color = x->parent->color;
x->parent->color = BLACK;
w->left->color = BLACK;
rotateRight (x->parent);
x = root;
}
}
}
x->color = BLACK;
}
/* eof */
|