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 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
|
/*
* tree234.c: reasonably generic counted 2-3-4 tree routines.
*
* This file is copyright 1999-2001 Simon Tatham.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL SIMON TATHAM BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "defs.h"
#include "tree234.h"
#ifdef TEST
#define LOG(x) (printf x)
#define snew(type) ((type *)malloc(sizeof(type)))
#define snewn(n, type) ((type *)malloc((n) * sizeof(type)))
#define sresize(ptr, n, type) \
((type *)realloc(sizeof((type *)0 == (ptr)) ? (ptr) : (ptr), \
(n) * sizeof(type)))
#define sfree(ptr) free(ptr)
#else
#include "puttymem.h"
#define LOG(x)
#endif
typedef struct node234_Tag node234;
struct tree234_Tag {
node234 *root;
cmpfn234 cmp;
};
struct node234_Tag {
node234 *parent;
node234 *kids[4];
int counts[4];
void *elems[3];
};
/*
* Create a 2-3-4 tree.
*/
tree234 *newtree234(cmpfn234 cmp)
{
tree234 *ret = snew(tree234);
LOG(("created tree %p\n", ret));
ret->root = NULL;
ret->cmp = cmp;
return ret;
}
/*
* Free a 2-3-4 tree (not including freeing the elements).
*/
static void freenode234(node234 * n)
{
if (!n)
return;
freenode234(n->kids[0]);
freenode234(n->kids[1]);
freenode234(n->kids[2]);
freenode234(n->kids[3]);
sfree(n);
}
void freetree234(tree234 * t)
{
freenode234(t->root);
sfree(t);
}
/*
* Internal function to count a node.
*/
static int countnode234(node234 * n)
{
int count = 0;
int i;
if (!n)
return 0;
for (i = 0; i < 4; i++)
count += n->counts[i];
for (i = 0; i < 3; i++)
if (n->elems[i])
count++;
return count;
}
/*
* Internal function to return the number of elements in a node.
*/
static int elements234(node234 *n)
{
int i;
for (i = 0; i < 3; i++)
if (!n->elems[i])
break;
return i;
}
/*
* Count the elements in a tree.
*/
int count234(tree234 * t)
{
if (t->root)
return countnode234(t->root);
else
return 0;
}
/*
* Add an element e to a 2-3-4 tree t. Returns e on success, or if
* an existing element compares equal, returns that.
*/
static void *add234_internal(tree234 * t, void *e, int index)
{
node234 *n, **np, *left, *right;
void *orig_e = e;
int c, lcount, rcount;
LOG(("adding node %p to tree %p\n", e, t));
if (t->root == NULL) {
t->root = snew(node234);
t->root->elems[1] = t->root->elems[2] = NULL;
t->root->kids[0] = t->root->kids[1] = NULL;
t->root->kids[2] = t->root->kids[3] = NULL;
t->root->counts[0] = t->root->counts[1] = 0;
t->root->counts[2] = t->root->counts[3] = 0;
t->root->parent = NULL;
t->root->elems[0] = e;
LOG((" created root %p\n", t->root));
return orig_e;
}
n = NULL; /* placate gcc; will always be set below since t->root != NULL */
np = &t->root;
while (*np) {
int childnum;
n = *np;
LOG((" node %p: %p/%d [%p] %p/%d [%p] %p/%d [%p] %p/%d\n",
n,
n->kids[0], n->counts[0], n->elems[0],
n->kids[1], n->counts[1], n->elems[1],
n->kids[2], n->counts[2], n->elems[2],
n->kids[3], n->counts[3]));
if (index >= 0) {
if (!n->kids[0]) {
/*
* Leaf node. We want to insert at kid position
* equal to the index:
*
* 0 A 1 B 2 C 3
*/
childnum = index;
} else {
/*
* Internal node. We always descend through it (add
* always starts at the bottom, never in the
* middle).
*/
do { /* this is a do ... while (0) to allow `break' */
if (index <= n->counts[0]) {
childnum = 0;
break;
}
index -= n->counts[0] + 1;
if (index <= n->counts[1]) {
childnum = 1;
break;
}
index -= n->counts[1] + 1;
if (index <= n->counts[2]) {
childnum = 2;
break;
}
index -= n->counts[2] + 1;
if (index <= n->counts[3]) {
childnum = 3;
break;
}
return NULL; /* error: index out of range */
} while (0);
}
} else {
if ((c = t->cmp(e, n->elems[0])) < 0)
childnum = 0;
else if (c == 0)
return n->elems[0]; /* already exists */
else if (n->elems[1] == NULL
|| (c = t->cmp(e, n->elems[1])) < 0) childnum = 1;
else if (c == 0)
return n->elems[1]; /* already exists */
else if (n->elems[2] == NULL
|| (c = t->cmp(e, n->elems[2])) < 0) childnum = 2;
else if (c == 0)
return n->elems[2]; /* already exists */
else
childnum = 3;
}
np = &n->kids[childnum];
LOG((" moving to child %d (%p)\n", childnum, *np));
}
/*
* We need to insert the new element in n at position np.
*/
left = NULL;
lcount = 0;
right = NULL;
rcount = 0;
while (n) {
LOG((" at %p: %p/%d [%p] %p/%d [%p] %p/%d [%p] %p/%d\n",
n,
n->kids[0], n->counts[0], n->elems[0],
n->kids[1], n->counts[1], n->elems[1],
n->kids[2], n->counts[2], n->elems[2],
n->kids[3], n->counts[3]));
LOG((" need to insert %p/%d [%p] %p/%d at position %d\n",
left, lcount, e, right, rcount, (int)(np - n->kids)));
if (n->elems[1] == NULL) {
/*
* Insert in a 2-node; simple.
*/
if (np == &n->kids[0]) {
LOG((" inserting on left of 2-node\n"));
n->kids[2] = n->kids[1];
n->counts[2] = n->counts[1];
n->elems[1] = n->elems[0];
n->kids[1] = right;
n->counts[1] = rcount;
n->elems[0] = e;
n->kids[0] = left;
n->counts[0] = lcount;
} else { /* np == &n->kids[1] */
LOG((" inserting on right of 2-node\n"));
n->kids[2] = right;
n->counts[2] = rcount;
n->elems[1] = e;
n->kids[1] = left;
n->counts[1] = lcount;
}
if (n->kids[0])
n->kids[0]->parent = n;
if (n->kids[1])
n->kids[1]->parent = n;
if (n->kids[2])
n->kids[2]->parent = n;
LOG((" done\n"));
break;
} else if (n->elems[2] == NULL) {
/*
* Insert in a 3-node; simple.
*/
if (np == &n->kids[0]) {
LOG((" inserting on left of 3-node\n"));
n->kids[3] = n->kids[2];
n->counts[3] = n->counts[2];
n->elems[2] = n->elems[1];
n->kids[2] = n->kids[1];
n->counts[2] = n->counts[1];
n->elems[1] = n->elems[0];
n->kids[1] = right;
n->counts[1] = rcount;
n->elems[0] = e;
n->kids[0] = left;
n->counts[0] = lcount;
} else if (np == &n->kids[1]) {
LOG((" inserting in middle of 3-node\n"));
n->kids[3] = n->kids[2];
n->counts[3] = n->counts[2];
n->elems[2] = n->elems[1];
n->kids[2] = right;
n->counts[2] = rcount;
n->elems[1] = e;
n->kids[1] = left;
n->counts[1] = lcount;
} else { /* np == &n->kids[2] */
LOG((" inserting on right of 3-node\n"));
n->kids[3] = right;
n->counts[3] = rcount;
n->elems[2] = e;
n->kids[2] = left;
n->counts[2] = lcount;
}
if (n->kids[0])
n->kids[0]->parent = n;
if (n->kids[1])
n->kids[1]->parent = n;
if (n->kids[2])
n->kids[2]->parent = n;
if (n->kids[3])
n->kids[3]->parent = n;
LOG((" done\n"));
break;
} else {
node234 *m = snew(node234);
m->parent = n->parent;
LOG((" splitting a 4-node; created new node %p\n", m));
/*
* Insert in a 4-node; split into a 2-node and a
* 3-node, and move focus up a level.
*
* I don't think it matters which way round we put the
* 2 and the 3. For simplicity, we'll put the 3 first
* always.
*/
if (np == &n->kids[0]) {
m->kids[0] = left;
m->counts[0] = lcount;
m->elems[0] = e;
m->kids[1] = right;
m->counts[1] = rcount;
m->elems[1] = n->elems[0];
m->kids[2] = n->kids[1];
m->counts[2] = n->counts[1];
e = n->elems[1];
n->kids[0] = n->kids[2];
n->counts[0] = n->counts[2];
n->elems[0] = n->elems[2];
n->kids[1] = n->kids[3];
n->counts[1] = n->counts[3];
} else if (np == &n->kids[1]) {
m->kids[0] = n->kids[0];
m->counts[0] = n->counts[0];
m->elems[0] = n->elems[0];
m->kids[1] = left;
m->counts[1] = lcount;
m->elems[1] = e;
m->kids[2] = right;
m->counts[2] = rcount;
e = n->elems[1];
n->kids[0] = n->kids[2];
n->counts[0] = n->counts[2];
n->elems[0] = n->elems[2];
n->kids[1] = n->kids[3];
n->counts[1] = n->counts[3];
} else if (np == &n->kids[2]) {
m->kids[0] = n->kids[0];
m->counts[0] = n->counts[0];
m->elems[0] = n->elems[0];
m->kids[1] = n->kids[1];
m->counts[1] = n->counts[1];
m->elems[1] = n->elems[1];
m->kids[2] = left;
m->counts[2] = lcount;
/* e = e; */
n->kids[0] = right;
n->counts[0] = rcount;
n->elems[0] = n->elems[2];
n->kids[1] = n->kids[3];
n->counts[1] = n->counts[3];
} else { /* np == &n->kids[3] */
m->kids[0] = n->kids[0];
m->counts[0] = n->counts[0];
m->elems[0] = n->elems[0];
m->kids[1] = n->kids[1];
m->counts[1] = n->counts[1];
m->elems[1] = n->elems[1];
m->kids[2] = n->kids[2];
m->counts[2] = n->counts[2];
n->kids[0] = left;
n->counts[0] = lcount;
n->elems[0] = e;
n->kids[1] = right;
n->counts[1] = rcount;
e = n->elems[2];
}
m->kids[3] = n->kids[3] = n->kids[2] = NULL;
m->counts[3] = n->counts[3] = n->counts[2] = 0;
m->elems[2] = n->elems[2] = n->elems[1] = NULL;
if (m->kids[0])
m->kids[0]->parent = m;
if (m->kids[1])
m->kids[1]->parent = m;
if (m->kids[2])
m->kids[2]->parent = m;
if (n->kids[0])
n->kids[0]->parent = n;
if (n->kids[1])
n->kids[1]->parent = n;
LOG((" left (%p): %p/%d [%p] %p/%d [%p] %p/%d\n", m,
m->kids[0], m->counts[0], m->elems[0],
m->kids[1], m->counts[1], m->elems[1],
m->kids[2], m->counts[2]));
LOG((" right (%p): %p/%d [%p] %p/%d\n", n,
n->kids[0], n->counts[0], n->elems[0],
n->kids[1], n->counts[1]));
left = m;
lcount = countnode234(left);
right = n;
rcount = countnode234(right);
}
if (n->parent)
np = (n->parent->kids[0] == n ? &n->parent->kids[0] :
n->parent->kids[1] == n ? &n->parent->kids[1] :
n->parent->kids[2] == n ? &n->parent->kids[2] :
&n->parent->kids[3]);
n = n->parent;
}
/*
* If we've come out of here by `break', n will still be
* non-NULL and all we need to do is go back up the tree
* updating counts. If we've come here because n is NULL, we
* need to create a new root for the tree because the old one
* has just split into two. */
if (n) {
while (n->parent) {
int count = countnode234(n);
int childnum;
childnum = (n->parent->kids[0] == n ? 0 :
n->parent->kids[1] == n ? 1 :
n->parent->kids[2] == n ? 2 : 3);
n->parent->counts[childnum] = count;
n = n->parent;
}
} else {
LOG((" root is overloaded, split into two\n"));
t->root = snew(node234);
t->root->kids[0] = left;
t->root->counts[0] = lcount;
t->root->elems[0] = e;
t->root->kids[1] = right;
t->root->counts[1] = rcount;
t->root->elems[1] = NULL;
t->root->kids[2] = NULL;
t->root->counts[2] = 0;
t->root->elems[2] = NULL;
t->root->kids[3] = NULL;
t->root->counts[3] = 0;
t->root->parent = NULL;
if (t->root->kids[0])
t->root->kids[0]->parent = t->root;
if (t->root->kids[1])
t->root->kids[1]->parent = t->root;
LOG((" new root is %p/%d [%p] %p/%d\n",
t->root->kids[0], t->root->counts[0],
t->root->elems[0], t->root->kids[1], t->root->counts[1]));
}
return orig_e;
}
void *add234(tree234 * t, void *e)
{
if (!t->cmp) /* tree is unsorted */
return NULL;
return add234_internal(t, e, -1);
}
void *addpos234(tree234 * t, void *e, int index)
{
if (index < 0 || /* index out of range */
t->cmp) /* tree is sorted */
return NULL; /* return failure */
return add234_internal(t, e, index); /* this checks the upper bound */
}
/*
* Look up the element at a given numeric index in a 2-3-4 tree.
* Returns NULL if the index is out of range.
*/
void *index234(tree234 * t, int index)
{
node234 *n;
if (!t->root)
return NULL; /* tree is empty */
if (index < 0 || index >= countnode234(t->root))
return NULL; /* out of range */
n = t->root;
while (n) {
if (index < n->counts[0])
n = n->kids[0];
else if (index -= n->counts[0] + 1, index < 0)
return n->elems[0];
else if (index < n->counts[1])
n = n->kids[1];
else if (index -= n->counts[1] + 1, index < 0)
return n->elems[1];
else if (index < n->counts[2])
n = n->kids[2];
else if (index -= n->counts[2] + 1, index < 0)
return n->elems[2];
else
n = n->kids[3];
}
/* We shouldn't ever get here. I wonder how we did. */
return NULL;
}
/*
* Find an element e in a sorted 2-3-4 tree t. Returns NULL if not
* found. e is always passed as the first argument to cmp, so cmp
* can be an asymmetric function if desired. cmp can also be passed
* as NULL, in which case the compare function from the tree proper
* will be used.
*/
void *findrelpos234(tree234 * t, void *e, cmpfn234 cmp,
int relation, int *index)
{
search234_state ss;
int reldir = (relation == REL234_LT || relation == REL234_LE ? -1 :
relation == REL234_GT || relation == REL234_GE ? +1 : 0);
bool equal_permitted = (relation != REL234_LT && relation != REL234_GT);
void *toret;
/* Only LT / GT relations are permitted with a null query element. */
assert(!(equal_permitted && !e));
if (cmp == NULL)
cmp = t->cmp;
search234_start(&ss, t);
while (ss.element) {
int cmpret;
if (e) {
cmpret = cmp(e, ss.element);
} else {
cmpret = -reldir; /* invent a fixed compare result */
}
if (cmpret == 0) {
/*
* We've found an element that compares exactly equal to
* the query element.
*/
if (equal_permitted) {
/* If our search relation permits equality, we've
* finished already. */
if (index)
*index = ss.index;
return ss.element;
} else {
/* Otherwise, pretend this element was slightly too
* big/small, according to the direction of search. */
cmpret = reldir;
}
}
search234_step(&ss, cmpret);
}
/*
* No element compares equal to the one we were after, but
* ss.index indicates the index that element would have if it were
* inserted.
*
* So if our search relation is EQ, we must simply return failure.
*/
if (relation == REL234_EQ)
return NULL;
/*
* Otherwise, we must do an index lookup for the previous index
* (if we're going left - LE or LT) or this index (if we're going
* right - GE or GT).
*/
if (relation == REL234_LT || relation == REL234_LE) {
ss.index--;
}
/*
* We know the index of the element we want; just call index234
* to do the rest. This will return NULL if the index is out of
* bounds, which is exactly what we want.
*/
toret = index234(t, ss.index);
if (toret && index)
*index = ss.index;
return toret;
}
void *find234(tree234 * t, void *e, cmpfn234 cmp)
{
return findrelpos234(t, e, cmp, REL234_EQ, NULL);
}
void *findrel234(tree234 * t, void *e, cmpfn234 cmp, int relation)
{
return findrelpos234(t, e, cmp, relation, NULL);
}
void *findpos234(tree234 * t, void *e, cmpfn234 cmp, int *index)
{
return findrelpos234(t, e, cmp, REL234_EQ, index);
}
void search234_start(search234_state *state, tree234 *t)
{
state->_node = t->root;
state->_base = 0; /* index of first element in this node's subtree */
state->_last = -1; /* indicate that this node is not previously visted */
search234_step(state, 0);
}
void search234_step(search234_state *state, int direction)
{
node234 *node = state->_node;
int i;
if (!node) {
state->element = NULL;
state->index = 0;
return;
}
if (state->_last != -1) {
/*
* We're already pointing at some element of a node, so we
* should restrict to the elements left or right of it,
* depending on the requested search direction.
*/
assert(direction);
assert(node);
if (direction > 0)
state->_lo = state->_last + 1;
else
state->_hi = state->_last - 1;
if (state->_lo > state->_hi) {
/*
* We've run out of elements in this node, i.e. we've
* narrowed to nothing but a child pointer. Descend to
* that child, and update _base to the leftmost index of
* its subtree.
*/
for (i = 0; i < state->_lo; i++)
state->_base += 1 + node->counts[i];
state->_node = node = node->kids[state->_lo];
state->_last = -1;
}
}
if (state->_last == -1) {
/*
* We've just entered a new node - either because of the above
* code, or because we were called from search234_start - and
* anything in that node is a viable answer.
*/
state->_lo = 0;
state->_hi = node ? elements234(node)-1 : 0;
}
/*
* Now we've got something we can return.
*/
if (!node) {
state->element = NULL;
state->index = state->_base;
} else {
state->_last = (state->_lo + state->_hi) / 2;
state->element = node->elems[state->_last];
state->index = state->_base + state->_last;
for (i = 0; i <= state->_last; i++)
state->index += node->counts[i];
}
}
/*
* Delete an element e in a 2-3-4 tree. Does not free the element,
* merely removes all links to it from the tree nodes.
*/
static void *delpos234_internal(tree234 * t, int index)
{
node234 *n;
void *retval;
int ei = -1;
retval = 0;
n = t->root;
LOG(("deleting item %d from tree %p\n", index, t));
while (1) {
while (n) {
int ki;
node234 *sub;
LOG(
(" node %p: %p/%d [%p] %p/%d [%p] %p/%d [%p] %p/%d index=%d\n",
n, n->kids[0], n->counts[0], n->elems[0], n->kids[1],
n->counts[1], n->elems[1], n->kids[2], n->counts[2],
n->elems[2], n->kids[3], n->counts[3], index));
if (index < n->counts[0]) {
ki = 0;
} else if (index -= n->counts[0] + 1, index < 0) {
ei = 0;
break;
} else if (index < n->counts[1]) {
ki = 1;
} else if (index -= n->counts[1] + 1, index < 0) {
ei = 1;
break;
} else if (index < n->counts[2]) {
ki = 2;
} else if (index -= n->counts[2] + 1, index < 0) {
ei = 2;
break;
} else {
ki = 3;
}
/*
* Recurse down to subtree ki. If it has only one element,
* we have to do some transformation to start with.
*/
LOG((" moving to subtree %d\n", ki));
sub = n->kids[ki];
if (!sub->elems[1]) {
LOG((" subtree has only one element!\n"));
if (ki > 0 && n->kids[ki - 1]->elems[1]) {
/*
* Case 3a, left-handed variant. Child ki has
* only one element, but child ki-1 has two or
* more. So we need to move a subtree from ki-1
* to ki.
*
* . C . . B .
* / \ -> / \
* [more] a A b B c d D e [more] a A b c C d D e
*/
node234 *sib = n->kids[ki - 1];
int lastelem = (sib->elems[2] ? 2 :
sib->elems[1] ? 1 : 0);
sub->kids[2] = sub->kids[1];
sub->counts[2] = sub->counts[1];
sub->elems[1] = sub->elems[0];
sub->kids[1] = sub->kids[0];
sub->counts[1] = sub->counts[0];
sub->elems[0] = n->elems[ki - 1];
sub->kids[0] = sib->kids[lastelem + 1];
sub->counts[0] = sib->counts[lastelem + 1];
if (sub->kids[0])
sub->kids[0]->parent = sub;
n->elems[ki - 1] = sib->elems[lastelem];
sib->kids[lastelem + 1] = NULL;
sib->counts[lastelem + 1] = 0;
sib->elems[lastelem] = NULL;
n->counts[ki] = countnode234(sub);
LOG((" case 3a left\n"));
LOG(
(" index and left subtree count before adjustment: %d, %d\n",
index, n->counts[ki - 1]));
index += n->counts[ki - 1];
n->counts[ki - 1] = countnode234(sib);
index -= n->counts[ki - 1];
LOG(
(" index and left subtree count after adjustment: %d, %d\n",
index, n->counts[ki - 1]));
} else if (ki < 3 && n->kids[ki + 1]
&& n->kids[ki + 1]->elems[1]) {
/*
* Case 3a, right-handed variant. ki has only
* one element but ki+1 has two or more. Move a
* subtree from ki+1 to ki.
*
* . B . . C .
* / \ -> / \
* a A b c C d D e [more] a A b B c d D e [more]
*/
node234 *sib = n->kids[ki + 1];
int j;
sub->elems[1] = n->elems[ki];
sub->kids[2] = sib->kids[0];
sub->counts[2] = sib->counts[0];
if (sub->kids[2])
sub->kids[2]->parent = sub;
n->elems[ki] = sib->elems[0];
sib->kids[0] = sib->kids[1];
sib->counts[0] = sib->counts[1];
for (j = 0; j < 2 && sib->elems[j + 1]; j++) {
sib->kids[j + 1] = sib->kids[j + 2];
sib->counts[j + 1] = sib->counts[j + 2];
sib->elems[j] = sib->elems[j + 1];
}
sib->kids[j + 1] = NULL;
sib->counts[j + 1] = 0;
sib->elems[j] = NULL;
n->counts[ki] = countnode234(sub);
n->counts[ki + 1] = countnode234(sib);
LOG((" case 3a right\n"));
} else {
/*
* Case 3b. ki has only one element, and has no
* neighbour with more than one. So pick a
* neighbour and merge it with ki, taking an
* element down from n to go in the middle.
*
* . B . .
* / \ -> |
* a A b c C d a A b B c C d
*
* (Since at all points we have avoided
* descending to a node with only one element,
* we can be sure that n is not reduced to
* nothingness by this move, _unless_ it was
* the very first node, ie the root of the
* tree. In that case we remove the now-empty
* root and replace it with its single large
* child as shown.)
*/
node234 *sib;
int j;
if (ki > 0) {
ki--;
index += n->counts[ki] + 1;
}
sib = n->kids[ki];
sub = n->kids[ki + 1];
sub->kids[3] = sub->kids[1];
sub->counts[3] = sub->counts[1];
sub->elems[2] = sub->elems[0];
sub->kids[2] = sub->kids[0];
sub->counts[2] = sub->counts[0];
sub->elems[1] = n->elems[ki];
sub->kids[1] = sib->kids[1];
sub->counts[1] = sib->counts[1];
if (sub->kids[1])
sub->kids[1]->parent = sub;
sub->elems[0] = sib->elems[0];
sub->kids[0] = sib->kids[0];
sub->counts[0] = sib->counts[0];
if (sub->kids[0])
sub->kids[0]->parent = sub;
n->counts[ki + 1] = countnode234(sub);
sfree(sib);
/*
* That's built the big node in sub. Now we
* need to remove the reference to sib in n.
*/
for (j = ki; j < 3 && n->kids[j + 1]; j++) {
n->kids[j] = n->kids[j + 1];
n->counts[j] = n->counts[j + 1];
n->elems[j] = j < 2 ? n->elems[j + 1] : NULL;
}
n->kids[j] = NULL;
n->counts[j] = 0;
if (j < 3)
n->elems[j] = NULL;
LOG((" case 3b ki=%d\n", ki));
if (!n->elems[0]) {
/*
* The root is empty and needs to be
* removed.
*/
LOG((" shifting root!\n"));
t->root = sub;
sub->parent = NULL;
sfree(n);
}
}
}
n = sub;
}
if (!retval)
retval = n->elems[ei];
if (ei == -1)
return NULL; /* although this shouldn't happen */
/*
* Treat special case: this is the one remaining item in
* the tree. n is the tree root (no parent), has one
* element (no elems[1]), and has no kids (no kids[0]).
*/
if (!n->parent && !n->elems[1] && !n->kids[0]) {
LOG((" removed last element in tree\n"));
sfree(n);
t->root = NULL;
return retval;
}
/*
* Now we have the element we want, as n->elems[ei], and we
* have also arranged for that element not to be the only
* one in its node. So...
*/
if (!n->kids[0] && n->elems[1]) {
/*
* Case 1. n is a leaf node with more than one element,
* so it's _really easy_. Just delete the thing and
* we're done.
*/
int i;
LOG((" case 1\n"));
for (i = ei; i < 2 && n->elems[i + 1]; i++)
n->elems[i] = n->elems[i + 1];
n->elems[i] = NULL;
/*
* Having done that to the leaf node, we now go back up
* the tree fixing the counts.
*/
while (n->parent) {
int childnum;
childnum = (n->parent->kids[0] == n ? 0 :
n->parent->kids[1] == n ? 1 :
n->parent->kids[2] == n ? 2 : 3);
n->parent->counts[childnum]--;
n = n->parent;
}
return retval; /* finished! */
} else if (n->kids[ei]->elems[1]) {
/*
* Case 2a. n is an internal node, and the root of the
* subtree to the left of e has more than one element.
* So find the predecessor p to e (ie the largest node
* in that subtree), place it where e currently is, and
* then start the deletion process over again on the
* subtree with p as target.
*/
node234 *m = n->kids[ei];
void *target;
LOG((" case 2a\n"));
while (m->kids[0]) {
m = (m->kids[3] ? m->kids[3] :
m->kids[2] ? m->kids[2] :
m->kids[1] ? m->kids[1] : m->kids[0]);
}
target = (m->elems[2] ? m->elems[2] :
m->elems[1] ? m->elems[1] : m->elems[0]);
n->elems[ei] = target;
index = n->counts[ei] - 1;
n = n->kids[ei];
} else if (n->kids[ei + 1]->elems[1]) {
/*
* Case 2b, symmetric to 2a but s/left/right/ and
* s/predecessor/successor/. (And s/largest/smallest/).
*/
node234 *m = n->kids[ei + 1];
void *target;
LOG((" case 2b\n"));
while (m->kids[0]) {
m = m->kids[0];
}
target = m->elems[0];
n->elems[ei] = target;
n = n->kids[ei + 1];
index = 0;
} else {
/*
* Case 2c. n is an internal node, and the subtrees to
* the left and right of e both have only one element.
* So combine the two subnodes into a single big node
* with their own elements on the left and right and e
* in the middle, then restart the deletion process on
* that subtree, with e still as target.
*/
node234 *a = n->kids[ei], *b = n->kids[ei + 1];
int j;
LOG((" case 2c\n"));
a->elems[1] = n->elems[ei];
a->kids[2] = b->kids[0];
a->counts[2] = b->counts[0];
if (a->kids[2])
a->kids[2]->parent = a;
a->elems[2] = b->elems[0];
a->kids[3] = b->kids[1];
a->counts[3] = b->counts[1];
if (a->kids[3])
a->kids[3]->parent = a;
sfree(b);
n->counts[ei] = countnode234(a);
/*
* That's built the big node in a, and destroyed b. Now
* remove the reference to b (and e) in n.
*/
for (j = ei; j < 2 && n->elems[j + 1]; j++) {
n->elems[j] = n->elems[j + 1];
n->kids[j + 1] = n->kids[j + 2];
n->counts[j + 1] = n->counts[j + 2];
}
n->elems[j] = NULL;
n->kids[j + 1] = NULL;
n->counts[j + 1] = 0;
/*
* It's possible, in this case, that we've just removed
* the only element in the root of the tree. If so,
* shift the root.
*/
if (n->elems[0] == NULL) {
LOG((" shifting root!\n"));
t->root = a;
a->parent = NULL;
sfree(n);
}
/*
* Now go round the deletion process again, with n
* pointing at the new big node and e still the same.
*/
n = a;
index = a->counts[0] + a->counts[1] + 1;
}
}
}
void *delpos234(tree234 * t, int index)
{
if (index < 0 || index >= countnode234(t->root))
return NULL;
return delpos234_internal(t, index);
}
void *del234(tree234 * t, void *e)
{
int index;
if (!findrelpos234(t, e, NULL, REL234_EQ, &index))
return NULL; /* it wasn't in there anyway */
return delpos234_internal(t, index); /* it's there; delete it. */
}
#ifdef TEST
/*
* Test code for the 2-3-4 tree. This code maintains an alternative
* representation of the data in the tree, in an array (using the
* obvious and slow insert and delete functions). After each tree
* operation, the verify() function is called, which ensures all
* the tree properties are preserved:
* - node->child->parent always equals node
* - tree->root->parent always equals NULL
* - number of kids == 0 or number of elements + 1;
* - tree has the same depth everywhere
* - every node has at least one element
* - subtree element counts are accurate
* - any NULL kid pointer is accompanied by a zero count
* - in a sorted tree: ordering property between elements of a
* node and elements of its children is preserved
* and also ensures the list represented by the tree is the same
* list it should be. (This last check also doubly verifies the
* ordering properties, because the `same list it should be' is by
* definition correctly ordered. It also ensures all nodes are
* distinct, because the enum functions would get caught in a loop
* if not.)
*/
#include <stdarg.h>
#include <string.h>
int n_errors = 0;
/*
* Error reporting function.
*/
PRINTF_LIKE(1, 2) void error(char *fmt, ...)
{
va_list ap;
printf("ERROR: ");
va_start(ap, fmt);
vfprintf(stdout, fmt, ap);
va_end(ap);
printf("\n");
n_errors++;
}
/* The array representation of the data. */
void **array;
int arraylen, arraysize;
cmpfn234 cmp;
/* The tree representation of the same data. */
tree234 *tree;
typedef struct {
int treedepth;
int elemcount;
} chkctx;
int chknode(chkctx * ctx, int level, node234 * node,
void *lowbound, void *highbound)
{
int nkids, nelems;
int i;
int count;
/* Count the non-NULL kids. */
for (nkids = 0; nkids < 4 && node->kids[nkids]; nkids++);
/* Ensure no kids beyond the first NULL are non-NULL. */
for (i = nkids; i < 4; i++)
if (node->kids[i]) {
error("node %p: nkids=%d but kids[%d] non-NULL",
node, nkids, i);
} else if (node->counts[i]) {
error("node %p: kids[%d] NULL but count[%d]=%d nonzero",
node, i, i, node->counts[i]);
}
/* Count the non-NULL elements. */
for (nelems = 0; nelems < 3 && node->elems[nelems]; nelems++);
/* Ensure no elements beyond the first NULL are non-NULL. */
for (i = nelems; i < 3; i++)
if (node->elems[i]) {
error("node %p: nelems=%d but elems[%d] non-NULL",
node, nelems, i);
}
if (nkids == 0) {
/*
* If nkids==0, this is a leaf node; verify that the tree
* depth is the same everywhere.
*/
if (ctx->treedepth < 0)
ctx->treedepth = level; /* we didn't know the depth yet */
else if (ctx->treedepth != level)
error("node %p: leaf at depth %d, previously seen depth %d",
node, level, ctx->treedepth);
} else {
/*
* If nkids != 0, then it should be nelems+1, unless nelems
* is 0 in which case nkids should also be 0 (and so we
* shouldn't be in this condition at all).
*/
int shouldkids = (nelems ? nelems + 1 : 0);
if (nkids != shouldkids) {
error("node %p: %d elems should mean %d kids but has %d",
node, nelems, shouldkids, nkids);
}
}
/*
* nelems should be at least 1.
*/
if (nelems == 0) {
error("node %p: no elems", node, nkids);
}
/*
* Add nelems to the running element count of the whole tree.
*/
ctx->elemcount += nelems;
/*
* Check ordering property: all elements should be strictly >
* lowbound, strictly < highbound, and strictly < each other in
* sequence. (lowbound and highbound are NULL at edges of tree
* - both NULL at root node - and NULL is considered to be <
* everything and > everything. IYSWIM.)
*/
if (cmp) {
for (i = -1; i < nelems; i++) {
void *lower = (i == -1 ? lowbound : node->elems[i]);
void *higher =
(i + 1 == nelems ? highbound : node->elems[i + 1]);
if (lower && higher && cmp(lower, higher) >= 0) {
error("node %p: kid comparison [%d=%s,%d=%s] failed",
node, i, lower, i + 1, higher);
}
}
}
/*
* Check parent pointers: all non-NULL kids should have a
* parent pointer coming back to this node.
*/
for (i = 0; i < nkids; i++)
if (node->kids[i]->parent != node) {
error("node %p kid %d: parent ptr is %p not %p",
node, i, node->kids[i]->parent, node);
}
/*
* Now (finally!) recurse into subtrees.
*/
count = nelems;
for (i = 0; i < nkids; i++) {
void *lower = (i == 0 ? lowbound : node->elems[i - 1]);
void *higher = (i >= nelems ? highbound : node->elems[i]);
int subcount =
chknode(ctx, level + 1, node->kids[i], lower, higher);
if (node->counts[i] != subcount) {
error("node %p kid %d: count says %d, subtree really has %d",
node, i, node->counts[i], subcount);
}
count += subcount;
}
return count;
}
void verify(void)
{
chkctx ctx;
int i;
void *p;
ctx.treedepth = -1; /* depth unknown yet */
ctx.elemcount = 0; /* no elements seen yet */
/*
* Verify validity of tree properties.
*/
if (tree->root) {
if (tree->root->parent != NULL)
error("root->parent is %p should be null", tree->root->parent);
chknode(&ctx, 0, tree->root, NULL, NULL);
}
printf("tree depth: %d\n", ctx.treedepth);
/*
* Enumerate the tree and ensure it matches up to the array.
*/
for (i = 0; NULL != (p = index234(tree, i)); i++) {
if (i >= arraylen)
error("tree contains more than %d elements", arraylen);
if (array[i] != p)
error("enum at position %d: array says %s, tree says %s",
i, array[i], p);
}
if (ctx.elemcount != i) {
error("tree really contains %d elements, enum gave %d",
ctx.elemcount, i);
}
if (i < arraylen) {
error("enum gave only %d elements, array has %d", i, arraylen);
}
i = count234(tree);
if (ctx.elemcount != i) {
error("tree really contains %d elements, count234 gave %d",
ctx.elemcount, i);
}
}
void internal_addtest(void *elem, int index, void *realret)
{
int i, j;
void *retval;
if (arraysize < arraylen + 1) {
arraysize = arraylen + 1 + 256;
array = sresize(array, arraysize, void *);
}
i = index;
/* now i points to the first element >= elem */
retval = elem; /* expect elem returned (success) */
for (j = arraylen; j > i; j--)
array[j] = array[j - 1];
array[i] = elem; /* add elem to array */
arraylen++;
if (realret != retval) {
error("add: retval was %p expected %p", realret, retval);
}
verify();
}
void addtest(void *elem)
{
int i;
void *realret;
realret = add234(tree, elem);
i = 0;
while (i < arraylen && cmp(elem, array[i]) > 0)
i++;
if (i < arraylen && !cmp(elem, array[i])) {
void *retval = array[i]; /* expect that returned not elem */
if (realret != retval) {
error("add: retval was %p expected %p", realret, retval);
}
} else
internal_addtest(elem, i, realret);
}
void addpostest(void *elem, int i)
{
void *realret;
realret = addpos234(tree, elem, i);
internal_addtest(elem, i, realret);
}
void delpostest(int i)
{
int index = i;
void *elem = array[i], *ret;
/* i points to the right element */
while (i < arraylen - 1) {
array[i] = array[i + 1];
i++;
}
arraylen--; /* delete elem from array */
if (tree->cmp)
ret = del234(tree, elem);
else
ret = delpos234(tree, index);
if (ret != elem) {
error("del returned %p, expected %p", ret, elem);
}
verify();
}
void deltest(void *elem)
{
int i;
i = 0;
while (i < arraylen && cmp(elem, array[i]) > 0)
i++;
if (i >= arraylen || cmp(elem, array[i]) != 0)
return; /* don't do it! */
delpostest(i);
}
/* A sample data set and test utility. Designed for pseudo-randomness,
* and yet repeatability. */
/*
* This random number generator uses the `portable implementation'
* given in ANSI C99 draft N869. It assumes `unsigned' is 32 bits;
* change it if not.
*/
int randomnumber(unsigned *seed)
{
*seed *= 1103515245;
*seed += 12345;
return ((*seed) / 65536) % 32768;
}
int mycmp(void *av, void *bv)
{
char const *a = (char const *) av;
char const *b = (char const *) bv;
return strcmp(a, b);
}
#define lenof(x) ( sizeof((x)) / sizeof(*(x)) )
char *strings[] = {
"a", "ab", "absque", "coram", "de",
"palam", "clam", "cum", "ex", "e",
"sine", "tenus", "pro", "prae",
"banana", "carrot", "cabbage", "broccoli", "onion", "zebra",
"penguin", "blancmange", "pangolin", "whale", "hedgehog",
"giraffe", "peanut", "bungee", "foo", "bar", "baz", "quux",
"murfl", "spoo", "breen", "flarn", "octothorpe",
"snail", "tiger", "elephant", "octopus", "warthog", "armadillo",
"aardvark", "wyvern", "dragon", "elf", "dwarf", "orc", "goblin",
"pixie", "basilisk", "warg", "ape", "lizard", "newt", "shopkeeper",
"wand", "ring", "amulet"
};
#define NSTR lenof(strings)
int findtest(void)
{
const static int rels[] = {
REL234_EQ, REL234_GE, REL234_LE, REL234_LT, REL234_GT
};
const static char *const relnames[] = {
"EQ", "GE", "LE", "LT", "GT"
};
int i, j, rel, index;
char *p, *ret, *realret, *realret2;
int lo, hi, mid, c;
for (i = 0; i < NSTR; i++) {
p = strings[i];
for (j = 0; j < sizeof(rels) / sizeof(*rels); j++) {
rel = rels[j];
lo = 0;
hi = arraylen - 1;
while (lo <= hi) {
mid = (lo + hi) / 2;
c = strcmp(p, array[mid]);
if (c < 0)
hi = mid - 1;
else if (c > 0)
lo = mid + 1;
else
break;
}
if (c == 0) {
if (rel == REL234_LT)
ret = (mid > 0 ? array[--mid] : NULL);
else if (rel == REL234_GT)
ret = (mid < arraylen - 1 ? array[++mid] : NULL);
else
ret = array[mid];
} else {
assert(lo == hi + 1);
if (rel == REL234_LT || rel == REL234_LE) {
mid = hi;
ret = (hi >= 0 ? array[hi] : NULL);
} else if (rel == REL234_GT || rel == REL234_GE) {
mid = lo;
ret = (lo < arraylen ? array[lo] : NULL);
} else
ret = NULL;
}
realret = findrelpos234(tree, p, NULL, rel, &index);
if (realret != ret) {
error("find(\"%s\",%s) gave %s should be %s",
p, relnames[j], realret, ret);
}
if (realret && index != mid) {
error("find(\"%s\",%s) gave %d should be %d",
p, relnames[j], index, mid);
}
if (realret && rel == REL234_EQ) {
realret2 = index234(tree, index);
if (realret2 != realret) {
error("find(\"%s\",%s) gave %s(%d) but %d -> %s",
p, relnames[j], realret, index, index, realret2);
}
}
#if 0
printf("find(\"%s\",%s) gave %s(%d)\n", p, relnames[j],
realret, index);
#endif
}
}
realret = findrelpos234(tree, NULL, NULL, REL234_GT, &index);
if (arraylen && (realret != array[0] || index != 0)) {
error("find(NULL,GT) gave %s(%d) should be %s(0)",
realret, index, array[0]);
} else if (!arraylen && (realret != NULL)) {
error("find(NULL,GT) gave %s(%d) should be NULL", realret, index);
}
realret = findrelpos234(tree, NULL, NULL, REL234_LT, &index);
if (arraylen
&& (realret != array[arraylen - 1] || index != arraylen - 1)) {
error("find(NULL,LT) gave %s(%d) should be %s(0)", realret, index,
array[arraylen - 1]);
} else if (!arraylen && (realret != NULL)) {
error("find(NULL,LT) gave %s(%d) should be NULL", realret, index);
}
}
void searchtest_recurse(search234_state ss, int lo, int hi,
char **expected, char *directionbuf,
char *directionptr)
{
*directionptr = '\0';
if (!ss.element) {
if (lo != hi) {
error("search234(%s) gave NULL for non-empty interval [%d,%d)",
directionbuf, lo, hi);
} else if (ss.index != lo) {
error("search234(%s) gave index %d should be %d",
directionbuf, ss.index, lo);
} else {
printf("%*ssearch234(%s) gave NULL,%d\n",
(int)(directionptr-directionbuf) * 2, "", directionbuf,
ss.index);
}
} else if (lo == hi) {
error("search234(%s) gave %s for empty interval [%d,%d)",
directionbuf, (char *)ss.element, lo, hi);
} else if (ss.element != expected[ss.index]) {
error("search234(%s) gave element %s should be %s",
directionbuf, (char *)ss.element, expected[ss.index]);
} else if (ss.index < lo || ss.index >= hi) {
error("search234(%s) gave index %d should be in [%d,%d)",
directionbuf, ss.index, lo, hi);
return;
} else {
search234_state next;
printf("%*ssearch234(%s) gave %s,%d\n",
(int)(directionptr-directionbuf) * 2, "", directionbuf,
(char *)ss.element, ss.index);
next = ss;
search234_step(&next, -1);
*directionptr = '-';
searchtest_recurse(next, lo, ss.index,
expected, directionbuf, directionptr+1);
next = ss;
search234_step(&next, +1);
*directionptr = '+';
searchtest_recurse(next, ss.index+1, hi,
expected, directionbuf, directionptr+1);
}
}
void searchtest(void)
{
char *expected[NSTR], *p;
char directionbuf[NSTR * 10];
int n;
search234_state ss;
printf("beginning searchtest:");
for (n = 0; (p = index234(tree, n)) != NULL; n++) {
expected[n] = p;
printf(" %d=%s", n, p);
}
printf(" count=%d\n", n);
search234_start(&ss, tree);
searchtest_recurse(ss, 0, n, expected, directionbuf, directionbuf);
}
int main(void)
{
int in[NSTR];
int i, j, k;
unsigned seed = 0;
for (i = 0; i < NSTR; i++)
in[i] = 0;
array = NULL;
arraylen = arraysize = 0;
tree = newtree234(mycmp);
cmp = mycmp;
verify();
searchtest();
for (i = 0; i < 10000; i++) {
j = randomnumber(&seed);
j %= NSTR;
printf("trial: %d\n", i);
if (in[j]) {
printf("deleting %s (%d)\n", strings[j], j);
deltest(strings[j]);
in[j] = 0;
} else {
printf("adding %s (%d)\n", strings[j], j);
addtest(strings[j]);
in[j] = 1;
}
findtest();
searchtest();
}
while (arraylen > 0) {
j = randomnumber(&seed);
j %= arraylen;
deltest(array[j]);
}
freetree234(tree);
/*
* Now try an unsorted tree. We don't really need to test
* delpos234 because we know del234 is based on it, so it's
* already been tested in the above sorted-tree code; but for
* completeness we'll use it to tear down our unsorted tree
* once we've built it.
*/
tree = newtree234(NULL);
cmp = NULL;
verify();
for (i = 0; i < 1000; i++) {
printf("trial: %d\n", i);
j = randomnumber(&seed);
j %= NSTR;
k = randomnumber(&seed);
k %= count234(tree) + 1;
printf("adding string %s at index %d\n", strings[j], k);
addpostest(strings[j], k);
}
while (count234(tree) > 0) {
printf("cleanup: tree size %d\n", count234(tree));
j = randomnumber(&seed);
j %= count234(tree);
printf("deleting string %s from index %d\n",
(const char *)array[j], j);
delpostest(j);
}
printf("%d errors found\n", n_errors);
return (n_errors != 0);
}
#endif
|