tree234.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  1. /*
  2. * tree234.c: reasonably generic counted 2-3-4 tree routines.
  3. *
  4. * This file is copyright 1999-2001 Simon Tatham.
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use,
  10. * copy, modify, merge, publish, distribute, sublicense, and/or
  11. * sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  20. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL SIMON TATHAM BE LIABLE FOR
  22. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  23. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. * SOFTWARE.
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <assert.h>
  30. #define TREE234_INTERNALS
  31. #include "tree234.h"
  32. #include "puzzles.h" /* for smalloc/sfree */
  33. #ifdef DEBUG_TREE234
  34. #include <stdarg.h>
  35. static void logprintf(const char *fmt, ...)
  36. {
  37. va_list ap;
  38. va_start(ap, fmt);
  39. vprintf(fmt, ap);
  40. va_end(ap);
  41. }
  42. #define LOG(x) (logprintf x)
  43. #else
  44. #define LOG(x)
  45. #endif
  46. /*
  47. * Create a 2-3-4 tree.
  48. */
  49. tree234 *newtree234(cmpfn234 cmp) {
  50. tree234 *ret = snew(tree234);
  51. LOG(("created tree %p\n", ret));
  52. ret->root = NULL;
  53. ret->cmp = cmp;
  54. return ret;
  55. }
  56. /*
  57. * Free a 2-3-4 tree (not including freeing the elements).
  58. */
  59. static void freenode234(node234 *n) {
  60. if (!n)
  61. return;
  62. freenode234(n->kids[0]);
  63. freenode234(n->kids[1]);
  64. freenode234(n->kids[2]);
  65. freenode234(n->kids[3]);
  66. sfree(n);
  67. }
  68. void freetree234(tree234 *t) {
  69. freenode234(t->root);
  70. sfree(t);
  71. }
  72. /*
  73. * Internal function to count a node.
  74. */
  75. static int countnode234(node234 *n) {
  76. int count = 0;
  77. int i;
  78. if (!n)
  79. return 0;
  80. for (i = 0; i < 4; i++)
  81. count += n->counts[i];
  82. for (i = 0; i < 3; i++)
  83. if (n->elems[i])
  84. count++;
  85. return count;
  86. }
  87. /*
  88. * Count the elements in a tree.
  89. */
  90. int count234(tree234 *t) {
  91. if (t->root)
  92. return countnode234(t->root);
  93. else
  94. return 0;
  95. }
  96. /*
  97. * Propagate a node overflow up a tree until it stops. Returns 0 or
  98. * 1, depending on whether the root had to be split or not.
  99. */
  100. static int add234_insert(node234 *left, void *e, node234 *right,
  101. node234 **root, node234 *n, int ki) {
  102. int lcount, rcount;
  103. /*
  104. * We need to insert the new left/element/right set in n at
  105. * child position ki.
  106. */
  107. lcount = countnode234(left);
  108. rcount = countnode234(right);
  109. while (n) {
  110. LOG((" at %p: %p/%d \"%s\" %p/%d \"%s\" %p/%d \"%s\" %p/%d\n",
  111. n,
  112. n->kids[0], n->counts[0], n->elems[0],
  113. n->kids[1], n->counts[1], n->elems[1],
  114. n->kids[2], n->counts[2], n->elems[2],
  115. n->kids[3], n->counts[3]));
  116. LOG((" need to insert %p/%d \"%s\" %p/%d at position %d\n",
  117. left, lcount, e, right, rcount, ki));
  118. if (n->elems[1] == NULL) {
  119. /*
  120. * Insert in a 2-node; simple.
  121. */
  122. if (ki == 0) {
  123. LOG((" inserting on left of 2-node\n"));
  124. n->kids[2] = n->kids[1]; n->counts[2] = n->counts[1];
  125. n->elems[1] = n->elems[0];
  126. n->kids[1] = right; n->counts[1] = rcount;
  127. n->elems[0] = e;
  128. n->kids[0] = left; n->counts[0] = lcount;
  129. } else { /* ki == 1 */
  130. LOG((" inserting on right of 2-node\n"));
  131. n->kids[2] = right; n->counts[2] = rcount;
  132. n->elems[1] = e;
  133. n->kids[1] = left; n->counts[1] = lcount;
  134. }
  135. if (n->kids[0]) n->kids[0]->parent = n;
  136. if (n->kids[1]) n->kids[1]->parent = n;
  137. if (n->kids[2]) n->kids[2]->parent = n;
  138. LOG((" done\n"));
  139. break;
  140. } else if (n->elems[2] == NULL) {
  141. /*
  142. * Insert in a 3-node; simple.
  143. */
  144. if (ki == 0) {
  145. LOG((" inserting on left of 3-node\n"));
  146. n->kids[3] = n->kids[2]; n->counts[3] = n->counts[2];
  147. n->elems[2] = n->elems[1];
  148. n->kids[2] = n->kids[1]; n->counts[2] = n->counts[1];
  149. n->elems[1] = n->elems[0];
  150. n->kids[1] = right; n->counts[1] = rcount;
  151. n->elems[0] = e;
  152. n->kids[0] = left; n->counts[0] = lcount;
  153. } else if (ki == 1) {
  154. LOG((" inserting in middle of 3-node\n"));
  155. n->kids[3] = n->kids[2]; n->counts[3] = n->counts[2];
  156. n->elems[2] = n->elems[1];
  157. n->kids[2] = right; n->counts[2] = rcount;
  158. n->elems[1] = e;
  159. n->kids[1] = left; n->counts[1] = lcount;
  160. } else { /* ki == 2 */
  161. LOG((" inserting on right of 3-node\n"));
  162. n->kids[3] = right; n->counts[3] = rcount;
  163. n->elems[2] = e;
  164. n->kids[2] = left; n->counts[2] = lcount;
  165. }
  166. if (n->kids[0]) n->kids[0]->parent = n;
  167. if (n->kids[1]) n->kids[1]->parent = n;
  168. if (n->kids[2]) n->kids[2]->parent = n;
  169. if (n->kids[3]) n->kids[3]->parent = n;
  170. LOG((" done\n"));
  171. break;
  172. } else {
  173. node234 *m = snew(node234);
  174. m->parent = n->parent;
  175. LOG((" splitting a 4-node; created new node %p\n", m));
  176. /*
  177. * Insert in a 4-node; split into a 2-node and a
  178. * 3-node, and move focus up a level.
  179. *
  180. * I don't think it matters which way round we put the
  181. * 2 and the 3. For simplicity, we'll put the 3 first
  182. * always.
  183. */
  184. if (ki == 0) {
  185. m->kids[0] = left; m->counts[0] = lcount;
  186. m->elems[0] = e;
  187. m->kids[1] = right; m->counts[1] = rcount;
  188. m->elems[1] = n->elems[0];
  189. m->kids[2] = n->kids[1]; m->counts[2] = n->counts[1];
  190. e = n->elems[1];
  191. n->kids[0] = n->kids[2]; n->counts[0] = n->counts[2];
  192. n->elems[0] = n->elems[2];
  193. n->kids[1] = n->kids[3]; n->counts[1] = n->counts[3];
  194. } else if (ki == 1) {
  195. m->kids[0] = n->kids[0]; m->counts[0] = n->counts[0];
  196. m->elems[0] = n->elems[0];
  197. m->kids[1] = left; m->counts[1] = lcount;
  198. m->elems[1] = e;
  199. m->kids[2] = right; m->counts[2] = rcount;
  200. e = n->elems[1];
  201. n->kids[0] = n->kids[2]; n->counts[0] = n->counts[2];
  202. n->elems[0] = n->elems[2];
  203. n->kids[1] = n->kids[3]; n->counts[1] = n->counts[3];
  204. } else if (ki == 2) {
  205. m->kids[0] = n->kids[0]; m->counts[0] = n->counts[0];
  206. m->elems[0] = n->elems[0];
  207. m->kids[1] = n->kids[1]; m->counts[1] = n->counts[1];
  208. m->elems[1] = n->elems[1];
  209. m->kids[2] = left; m->counts[2] = lcount;
  210. /* e = e; */
  211. n->kids[0] = right; n->counts[0] = rcount;
  212. n->elems[0] = n->elems[2];
  213. n->kids[1] = n->kids[3]; n->counts[1] = n->counts[3];
  214. } else { /* ki == 3 */
  215. m->kids[0] = n->kids[0]; m->counts[0] = n->counts[0];
  216. m->elems[0] = n->elems[0];
  217. m->kids[1] = n->kids[1]; m->counts[1] = n->counts[1];
  218. m->elems[1] = n->elems[1];
  219. m->kids[2] = n->kids[2]; m->counts[2] = n->counts[2];
  220. n->kids[0] = left; n->counts[0] = lcount;
  221. n->elems[0] = e;
  222. n->kids[1] = right; n->counts[1] = rcount;
  223. e = n->elems[2];
  224. }
  225. m->kids[3] = n->kids[3] = n->kids[2] = NULL;
  226. m->counts[3] = n->counts[3] = n->counts[2] = 0;
  227. m->elems[2] = n->elems[2] = n->elems[1] = NULL;
  228. if (m->kids[0]) m->kids[0]->parent = m;
  229. if (m->kids[1]) m->kids[1]->parent = m;
  230. if (m->kids[2]) m->kids[2]->parent = m;
  231. if (n->kids[0]) n->kids[0]->parent = n;
  232. if (n->kids[1]) n->kids[1]->parent = n;
  233. LOG((" left (%p): %p/%d \"%s\" %p/%d \"%s\" %p/%d\n", m,
  234. m->kids[0], m->counts[0], m->elems[0],
  235. m->kids[1], m->counts[1], m->elems[1],
  236. m->kids[2], m->counts[2]));
  237. LOG((" right (%p): %p/%d \"%s\" %p/%d\n", n,
  238. n->kids[0], n->counts[0], n->elems[0],
  239. n->kids[1], n->counts[1]));
  240. left = m; lcount = countnode234(left);
  241. right = n; rcount = countnode234(right);
  242. }
  243. if (n->parent)
  244. ki = (n->parent->kids[0] == n ? 0 :
  245. n->parent->kids[1] == n ? 1 :
  246. n->parent->kids[2] == n ? 2 : 3);
  247. n = n->parent;
  248. }
  249. /*
  250. * If we've come out of here by `break', n will still be
  251. * non-NULL and all we need to do is go back up the tree
  252. * updating counts. If we've come here because n is NULL, we
  253. * need to create a new root for the tree because the old one
  254. * has just split into two. */
  255. if (n) {
  256. while (n->parent) {
  257. int count = countnode234(n);
  258. int childnum;
  259. childnum = (n->parent->kids[0] == n ? 0 :
  260. n->parent->kids[1] == n ? 1 :
  261. n->parent->kids[2] == n ? 2 : 3);
  262. n->parent->counts[childnum] = count;
  263. n = n->parent;
  264. }
  265. return 0; /* root unchanged */
  266. } else {
  267. LOG((" root is overloaded, split into two\n"));
  268. (*root) = snew(node234);
  269. (*root)->kids[0] = left; (*root)->counts[0] = lcount;
  270. (*root)->elems[0] = e;
  271. (*root)->kids[1] = right; (*root)->counts[1] = rcount;
  272. (*root)->elems[1] = NULL;
  273. (*root)->kids[2] = NULL; (*root)->counts[2] = 0;
  274. (*root)->elems[2] = NULL;
  275. (*root)->kids[3] = NULL; (*root)->counts[3] = 0;
  276. (*root)->parent = NULL;
  277. if ((*root)->kids[0]) (*root)->kids[0]->parent = (*root);
  278. if ((*root)->kids[1]) (*root)->kids[1]->parent = (*root);
  279. LOG((" new root is %p/%d \"%s\" %p/%d\n",
  280. (*root)->kids[0], (*root)->counts[0],
  281. (*root)->elems[0],
  282. (*root)->kids[1], (*root)->counts[1]));
  283. return 1; /* root moved */
  284. }
  285. }
  286. /*
  287. * Add an element e to a 2-3-4 tree t. Returns e on success, or if
  288. * an existing element compares equal, returns that.
  289. */
  290. static void *add234_internal(tree234 *t, void *e, int index) {
  291. node234 *n;
  292. int ki;
  293. void *orig_e = e;
  294. int c;
  295. LOG(("adding element \"%s\" to tree %p\n", e, t));
  296. if (t->root == NULL) {
  297. t->root = snew(node234);
  298. t->root->elems[1] = t->root->elems[2] = NULL;
  299. t->root->kids[0] = t->root->kids[1] = NULL;
  300. t->root->kids[2] = t->root->kids[3] = NULL;
  301. t->root->counts[0] = t->root->counts[1] = 0;
  302. t->root->counts[2] = t->root->counts[3] = 0;
  303. t->root->parent = NULL;
  304. t->root->elems[0] = e;
  305. LOG((" created root %p\n", t->root));
  306. return orig_e;
  307. }
  308. n = t->root;
  309. do {
  310. LOG((" node %p: %p/%d \"%s\" %p/%d \"%s\" %p/%d \"%s\" %p/%d\n",
  311. n,
  312. n->kids[0], n->counts[0], n->elems[0],
  313. n->kids[1], n->counts[1], n->elems[1],
  314. n->kids[2], n->counts[2], n->elems[2],
  315. n->kids[3], n->counts[3]));
  316. if (index >= 0) {
  317. if (!n->kids[0]) {
  318. /*
  319. * Leaf node. We want to insert at kid position
  320. * equal to the index:
  321. *
  322. * 0 A 1 B 2 C 3
  323. */
  324. ki = index;
  325. } else {
  326. /*
  327. * Internal node. We always descend through it (add
  328. * always starts at the bottom, never in the
  329. * middle).
  330. */
  331. if (index <= n->counts[0]) {
  332. ki = 0;
  333. } else if (index -= n->counts[0] + 1, index <= n->counts[1]) {
  334. ki = 1;
  335. } else if (index -= n->counts[1] + 1, index <= n->counts[2]) {
  336. ki = 2;
  337. } else if (index -= n->counts[2] + 1, index <= n->counts[3]) {
  338. ki = 3;
  339. } else
  340. return NULL; /* error: index out of range */
  341. }
  342. } else {
  343. if ((c = t->cmp(e, n->elems[0])) < 0)
  344. ki = 0;
  345. else if (c == 0)
  346. return n->elems[0]; /* already exists */
  347. else if (n->elems[1] == NULL || (c = t->cmp(e, n->elems[1])) < 0)
  348. ki = 1;
  349. else if (c == 0)
  350. return n->elems[1]; /* already exists */
  351. else if (n->elems[2] == NULL || (c = t->cmp(e, n->elems[2])) < 0)
  352. ki = 2;
  353. else if (c == 0)
  354. return n->elems[2]; /* already exists */
  355. else
  356. ki = 3;
  357. }
  358. LOG((" moving to child %d (%p)\n", ki, n->kids[ki]));
  359. if (!n->kids[ki])
  360. break;
  361. n = n->kids[ki];
  362. } while (n);
  363. add234_insert(NULL, e, NULL, &t->root, n, ki);
  364. return orig_e;
  365. }
  366. void *add234(tree234 *t, void *e) {
  367. if (!t->cmp) /* tree is unsorted */
  368. return NULL;
  369. return add234_internal(t, e, -1);
  370. }
  371. void *addpos234(tree234 *t, void *e, int index) {
  372. if (index < 0 || /* index out of range */
  373. t->cmp) /* tree is sorted */
  374. return NULL; /* return failure */
  375. return add234_internal(t, e, index); /* this checks the upper bound */
  376. }
  377. /*
  378. * Look up the element at a given numeric index in a 2-3-4 tree.
  379. * Returns NULL if the index is out of range.
  380. */
  381. void *index234(tree234 *t, int index) {
  382. node234 *n;
  383. if (!t->root)
  384. return NULL; /* tree is empty */
  385. if (index < 0 || index >= countnode234(t->root))
  386. return NULL; /* out of range */
  387. n = t->root;
  388. while (n) {
  389. if (index < n->counts[0])
  390. n = n->kids[0];
  391. else if (index -= n->counts[0] + 1, index < 0)
  392. return n->elems[0];
  393. else if (index < n->counts[1])
  394. n = n->kids[1];
  395. else if (index -= n->counts[1] + 1, index < 0)
  396. return n->elems[1];
  397. else if (index < n->counts[2])
  398. n = n->kids[2];
  399. else if (index -= n->counts[2] + 1, index < 0)
  400. return n->elems[2];
  401. else
  402. n = n->kids[3];
  403. }
  404. /* We shouldn't ever get here. I wonder how we did. */
  405. return NULL;
  406. }
  407. /*
  408. * Find an element e in a sorted 2-3-4 tree t. Returns NULL if not
  409. * found. e is always passed as the first argument to cmp, so cmp
  410. * can be an asymmetric function if desired. cmp can also be passed
  411. * as NULL, in which case the compare function from the tree proper
  412. * will be used.
  413. */
  414. void *findrelpos234(tree234 *t, void *e, cmpfn234 cmp,
  415. int relation, int *index) {
  416. node234 *n;
  417. void *ret;
  418. int c;
  419. int idx, ecount, kcount, cmpret;
  420. if (t->root == NULL)
  421. return NULL;
  422. if (cmp == NULL)
  423. cmp = t->cmp;
  424. n = t->root;
  425. /*
  426. * Attempt to find the element itself.
  427. */
  428. idx = 0;
  429. ecount = -1;
  430. /*
  431. * Prepare a fake `cmp' result if e is NULL.
  432. */
  433. cmpret = 0;
  434. if (e == NULL) {
  435. assert(relation == REL234_LT || relation == REL234_GT);
  436. if (relation == REL234_LT)
  437. cmpret = +1; /* e is a max: always greater */
  438. else if (relation == REL234_GT)
  439. cmpret = -1; /* e is a min: always smaller */
  440. }
  441. while (1) {
  442. for (kcount = 0; kcount < 4; kcount++) {
  443. if (kcount >= 3 || n->elems[kcount] == NULL ||
  444. (c = cmpret ? cmpret : cmp(e, n->elems[kcount])) < 0) {
  445. break;
  446. }
  447. if (n->kids[kcount]) idx += n->counts[kcount];
  448. if (c == 0) {
  449. ecount = kcount;
  450. break;
  451. }
  452. idx++;
  453. }
  454. if (ecount >= 0)
  455. break;
  456. if (n->kids[kcount])
  457. n = n->kids[kcount];
  458. else
  459. break;
  460. }
  461. if (ecount >= 0) {
  462. /*
  463. * We have found the element we're looking for. It's
  464. * n->elems[ecount], at tree index idx. If our search
  465. * relation is EQ, LE or GE we can now go home.
  466. */
  467. if (relation != REL234_LT && relation != REL234_GT) {
  468. if (index) *index = idx;
  469. return n->elems[ecount];
  470. }
  471. /*
  472. * Otherwise, we'll do an indexed lookup for the previous
  473. * or next element. (It would be perfectly possible to
  474. * implement these search types in a non-counted tree by
  475. * going back up from where we are, but far more fiddly.)
  476. */
  477. if (relation == REL234_LT)
  478. idx--;
  479. else
  480. idx++;
  481. } else {
  482. /*
  483. * We've found our way to the bottom of the tree and we
  484. * know where we would insert this node if we wanted to:
  485. * we'd put it in in place of the (empty) subtree
  486. * n->kids[kcount], and it would have index idx
  487. *
  488. * But the actual element isn't there. So if our search
  489. * relation is EQ, we're doomed.
  490. */
  491. if (relation == REL234_EQ)
  492. return NULL;
  493. /*
  494. * Otherwise, we must do an index lookup for index idx-1
  495. * (if we're going left - LE or LT) or index idx (if we're
  496. * going right - GE or GT).
  497. */
  498. if (relation == REL234_LT || relation == REL234_LE) {
  499. idx--;
  500. }
  501. }
  502. /*
  503. * We know the index of the element we want; just call index234
  504. * to do the rest. This will return NULL if the index is out of
  505. * bounds, which is exactly what we want.
  506. */
  507. ret = index234(t, idx);
  508. if (ret && index) *index = idx;
  509. return ret;
  510. }
  511. void *find234(tree234 *t, void *e, cmpfn234 cmp) {
  512. return findrelpos234(t, e, cmp, REL234_EQ, NULL);
  513. }
  514. void *findrel234(tree234 *t, void *e, cmpfn234 cmp, int relation) {
  515. return findrelpos234(t, e, cmp, relation, NULL);
  516. }
  517. void *findpos234(tree234 *t, void *e, cmpfn234 cmp, int *index) {
  518. return findrelpos234(t, e, cmp, REL234_EQ, index);
  519. }
  520. /*
  521. * Tree transformation used in delete and split: move a subtree
  522. * right, from child ki of a node to the next child. Update k and
  523. * index so that they still point to the same place in the
  524. * transformed tree. Assumes the destination child is not full, and
  525. * that the source child does have a subtree to spare. Can cope if
  526. * the destination child is undersized.
  527. *
  528. * . C . . B .
  529. * / \ -> / \
  530. * [more] a A b B c d D e [more] a A b c C d D e
  531. *
  532. * . C . . B .
  533. * / \ -> / \
  534. * [more] a A b B c d [more] a A b c C d
  535. */
  536. static void trans234_subtree_right(node234 *n, int ki, int *k, int *index) {
  537. node234 *src, *dest;
  538. int i, srclen, adjust;
  539. src = n->kids[ki];
  540. dest = n->kids[ki+1];
  541. LOG((" trans234_subtree_right(%p, %d):\n", n, ki));
  542. LOG((" parent %p: %p/%d \"%s\" %p/%d \"%s\" %p/%d \"%s\" %p/%d\n",
  543. n,
  544. n->kids[0], n->counts[0], n->elems[0],
  545. n->kids[1], n->counts[1], n->elems[1],
  546. n->kids[2], n->counts[2], n->elems[2],
  547. n->kids[3], n->counts[3]));
  548. LOG((" src %p: %p/%d \"%s\" %p/%d \"%s\" %p/%d \"%s\" %p/%d\n",
  549. src,
  550. src->kids[0], src->counts[0], src->elems[0],
  551. src->kids[1], src->counts[1], src->elems[1],
  552. src->kids[2], src->counts[2], src->elems[2],
  553. src->kids[3], src->counts[3]));
  554. LOG((" dest %p: %p/%d \"%s\" %p/%d \"%s\" %p/%d \"%s\" %p/%d\n",
  555. dest,
  556. dest->kids[0], dest->counts[0], dest->elems[0],
  557. dest->kids[1], dest->counts[1], dest->elems[1],
  558. dest->kids[2], dest->counts[2], dest->elems[2],
  559. dest->kids[3], dest->counts[3]));
  560. /*
  561. * Move over the rest of the destination node to make space.
  562. */
  563. dest->kids[3] = dest->kids[2]; dest->counts[3] = dest->counts[2];
  564. dest->elems[2] = dest->elems[1];
  565. dest->kids[2] = dest->kids[1]; dest->counts[2] = dest->counts[1];
  566. dest->elems[1] = dest->elems[0];
  567. dest->kids[1] = dest->kids[0]; dest->counts[1] = dest->counts[0];
  568. /* which element to move over */
  569. i = (src->elems[2] ? 2 : src->elems[1] ? 1 : 0);
  570. dest->elems[0] = n->elems[ki];
  571. n->elems[ki] = src->elems[i];
  572. src->elems[i] = NULL;
  573. dest->kids[0] = src->kids[i+1]; dest->counts[0] = src->counts[i+1];
  574. src->kids[i+1] = NULL; src->counts[i+1] = 0;
  575. if (dest->kids[0]) dest->kids[0]->parent = dest;
  576. adjust = dest->counts[0] + 1;
  577. n->counts[ki] -= adjust;
  578. n->counts[ki+1] += adjust;
  579. srclen = n->counts[ki];
  580. if (k) {
  581. LOG((" before: k,index = %d,%d\n", (*k), (*index)));
  582. if ((*k) == ki && (*index) > srclen) {
  583. (*index) -= srclen + 1;
  584. (*k)++;
  585. } else if ((*k) == ki+1) {
  586. (*index) += adjust;
  587. }
  588. LOG((" after: k,index = %d,%d\n", (*k), (*index)));
  589. }
  590. LOG((" parent %p: %p/%d \"%s\" %p/%d \"%s\" %p/%d \"%s\" %p/%d\n",
  591. n,
  592. n->kids[0], n->counts[0], n->elems[0],
  593. n->kids[1], n->counts[1], n->elems[1],
  594. n->kids[2], n->counts[2], n->elems[2],
  595. n->kids[3], n->counts[3]));
  596. LOG((" src %p: %p/%d \"%s\" %p/%d \"%s\" %p/%d \"%s\" %p/%d\n",
  597. src,
  598. src->kids[0], src->counts[0], src->elems[0],
  599. src->kids[1], src->counts[1], src->elems[1],
  600. src->kids[2], src->counts[2], src->elems[2],
  601. src->kids[3], src->counts[3]));
  602. LOG((" dest %p: %p/%d \"%s\" %p/%d \"%s\" %p/%d \"%s\" %p/%d\n",
  603. dest,
  604. dest->kids[0], dest->counts[0], dest->elems[0],
  605. dest->kids[1], dest->counts[1], dest->elems[1],
  606. dest->kids[2], dest->counts[2], dest->elems[2],
  607. dest->kids[3], dest->counts[3]));
  608. }
  609. /*
  610. * Tree transformation used in delete and split: move a subtree
  611. * left, from child ki of a node to the previous child. Update k
  612. * and index so that they still point to the same place in the
  613. * transformed tree. Assumes the destination child is not full, and
  614. * that the source child does have a subtree to spare. Can cope if
  615. * the destination child is undersized.
  616. *
  617. * . B . . C .
  618. * / \ -> / \
  619. * a A b c C d D e [more] a A b B c d D e [more]
  620. *
  621. * . A . . B .
  622. * / \ -> / \
  623. * a b B c C d [more] a A b c C d [more]
  624. */
  625. static void trans234_subtree_left(node234 *n, int ki, int *k, int *index) {
  626. node234 *src, *dest;
  627. int i, adjust;
  628. src = n->kids[ki];
  629. dest = n->kids[ki-1];
  630. LOG((" trans234_subtree_left(%p, %d):\n", n, ki));
  631. LOG((" parent %p: %p/%d \"%s\" %p/%d \"%s\" %p/%d \"%s\" %p/%d\n",
  632. n,
  633. n->kids[0], n->counts[0], n->elems[0],
  634. n->kids[1], n->counts[1], n->elems[1],
  635. n->kids[2], n->counts[2], n->elems[2],
  636. n->kids[3], n->counts[3]));
  637. LOG((" dest %p: %p/%d \"%s\" %p/%d \"%s\" %p/%d \"%s\" %p/%d\n",
  638. dest,
  639. dest->kids[0], dest->counts[0], dest->elems[0],
  640. dest->kids[1], dest->counts[1], dest->elems[1],
  641. dest->kids[2], dest->counts[2], dest->elems[2],
  642. dest->kids[3], dest->counts[3]));
  643. LOG((" src %p: %p/%d \"%s\" %p/%d \"%s\" %p/%d \"%s\" %p/%d\n",
  644. src,
  645. src->kids[0], src->counts[0], src->elems[0],
  646. src->kids[1], src->counts[1], src->elems[1],
  647. src->kids[2], src->counts[2], src->elems[2],
  648. src->kids[3], src->counts[3]));
  649. /* where in dest to put it */
  650. i = (dest->elems[1] ? 2 : dest->elems[0] ? 1 : 0);
  651. dest->elems[i] = n->elems[ki-1];
  652. n->elems[ki-1] = src->elems[0];
  653. dest->kids[i+1] = src->kids[0]; dest->counts[i+1] = src->counts[0];
  654. if (dest->kids[i+1]) dest->kids[i+1]->parent = dest;
  655. /*
  656. * Move over the rest of the source node.
  657. */
  658. src->kids[0] = src->kids[1]; src->counts[0] = src->counts[1];
  659. src->elems[0] = src->elems[1];
  660. src->kids[1] = src->kids[2]; src->counts[1] = src->counts[2];
  661. src->elems[1] = src->elems[2];
  662. src->kids[2] = src->kids[3]; src->counts[2] = src->counts[3];
  663. src->elems[2] = NULL;
  664. src->kids[3] = NULL; src->counts[3] = 0;
  665. adjust = dest->counts[i+1] + 1;
  666. n->counts[ki] -= adjust;
  667. n->counts[ki-1] += adjust;
  668. if (k) {
  669. LOG((" before: k,index = %d,%d\n", (*k), (*index)));
  670. if ((*k) == ki) {
  671. (*index) -= adjust;
  672. if ((*index) < 0) {
  673. (*index) += n->counts[ki-1] + 1;
  674. (*k)--;
  675. }
  676. }
  677. LOG((" after: k,index = %d,%d\n", (*k), (*index)));
  678. }
  679. LOG((" parent %p: %p/%d \"%s\" %p/%d \"%s\" %p/%d \"%s\" %p/%d\n",
  680. n,
  681. n->kids[0], n->counts[0], n->elems[0],
  682. n->kids[1], n->counts[1], n->elems[1],
  683. n->kids[2], n->counts[2], n->elems[2],
  684. n->kids[3], n->counts[3]));
  685. LOG((" dest %p: %p/%d \"%s\" %p/%d \"%s\" %p/%d \"%s\" %p/%d\n",
  686. dest,
  687. dest->kids[0], dest->counts[0], dest->elems[0],
  688. dest->kids[1], dest->counts[1], dest->elems[1],
  689. dest->kids[2], dest->counts[2], dest->elems[2],
  690. dest->kids[3], dest->counts[3]));
  691. LOG((" src %p: %p/%d \"%s\" %p/%d \"%s\" %p/%d \"%s\" %p/%d\n",
  692. src,
  693. src->kids[0], src->counts[0], src->elems[0],
  694. src->kids[1], src->counts[1], src->elems[1],
  695. src->kids[2], src->counts[2], src->elems[2],
  696. src->kids[3], src->counts[3]));
  697. }
  698. /*
  699. * Tree transformation used in delete and split: merge child nodes
  700. * ki and ki+1 of a node. Update k and index so that they still
  701. * point to the same place in the transformed tree. Assumes both
  702. * children _are_ sufficiently small.
  703. *
  704. * . B . .
  705. * / \ -> |
  706. * a A b c C d a A b B c C d
  707. *
  708. * This routine can also cope with either child being undersized:
  709. *
  710. * . A . .
  711. * / \ -> |
  712. * a b B c a A b B c
  713. *
  714. * . A . .
  715. * / \ -> |
  716. * a b B c C d a A b B c C d
  717. */
  718. static void trans234_subtree_merge(node234 *n, int ki, int *k, int *index) {
  719. node234 *left, *right;
  720. int i, leftlen, rightlen, lsize, rsize;
  721. left = n->kids[ki]; leftlen = n->counts[ki];
  722. right = n->kids[ki+1]; rightlen = n->counts[ki+1];
  723. LOG((" trans234_subtree_merge(%p, %d):\n", n, ki));
  724. LOG((" parent %p: %p/%d \"%s\" %p/%d \"%s\" %p/%d \"%s\" %p/%d\n",
  725. n,
  726. n->kids[0], n->counts[0], n->elems[0],
  727. n->kids[1], n->counts[1], n->elems[1],
  728. n->kids[2], n->counts[2], n->elems[2],
  729. n->kids[3], n->counts[3]));
  730. LOG((" left %p: %p/%d \"%s\" %p/%d \"%s\" %p/%d \"%s\" %p/%d\n",
  731. left,
  732. left->kids[0], left->counts[0], left->elems[0],
  733. left->kids[1], left->counts[1], left->elems[1],
  734. left->kids[2], left->counts[2], left->elems[2],
  735. left->kids[3], left->counts[3]));
  736. LOG((" right %p: %p/%d \"%s\" %p/%d \"%s\" %p/%d \"%s\" %p/%d\n",
  737. right,
  738. right->kids[0], right->counts[0], right->elems[0],
  739. right->kids[1], right->counts[1], right->elems[1],
  740. right->kids[2], right->counts[2], right->elems[2],
  741. right->kids[3], right->counts[3]));
  742. assert(!left->elems[2] && !right->elems[2]); /* neither is large! */
  743. lsize = (left->elems[1] ? 2 : left->elems[0] ? 1 : 0);
  744. rsize = (right->elems[1] ? 2 : right->elems[0] ? 1 : 0);
  745. left->elems[lsize] = n->elems[ki];
  746. for (i = 0; i < rsize+1; i++) {
  747. left->kids[lsize+1+i] = right->kids[i];
  748. left->counts[lsize+1+i] = right->counts[i];
  749. if (left->kids[lsize+1+i])
  750. left->kids[lsize+1+i]->parent = left;
  751. if (i < rsize)
  752. left->elems[lsize+1+i] = right->elems[i];
  753. }
  754. n->counts[ki] += rightlen + 1;
  755. sfree(right);
  756. /*
  757. * Move the rest of n up by one.
  758. */
  759. for (i = ki+1; i < 3; i++) {
  760. n->kids[i] = n->kids[i+1];
  761. n->counts[i] = n->counts[i+1];
  762. }
  763. for (i = ki; i < 2; i++) {
  764. n->elems[i] = n->elems[i+1];
  765. }
  766. n->kids[3] = NULL;
  767. n->counts[3] = 0;
  768. n->elems[2] = NULL;
  769. if (k) {
  770. LOG((" before: k,index = %d,%d\n", (*k), (*index)));
  771. if ((*k) == ki+1) {
  772. (*k)--;
  773. (*index) += leftlen + 1;
  774. } else if ((*k) > ki+1) {
  775. (*k)--;
  776. }
  777. LOG((" after: k,index = %d,%d\n", (*k), (*index)));
  778. }
  779. LOG((" parent %p: %p/%d \"%s\" %p/%d \"%s\" %p/%d \"%s\" %p/%d\n",
  780. n,
  781. n->kids[0], n->counts[0], n->elems[0],
  782. n->kids[1], n->counts[1], n->elems[1],
  783. n->kids[2], n->counts[2], n->elems[2],
  784. n->kids[3], n->counts[3]));
  785. LOG((" merged %p: %p/%d \"%s\" %p/%d \"%s\" %p/%d \"%s\" %p/%d\n",
  786. left,
  787. left->kids[0], left->counts[0], left->elems[0],
  788. left->kids[1], left->counts[1], left->elems[1],
  789. left->kids[2], left->counts[2], left->elems[2],
  790. left->kids[3], left->counts[3]));
  791. }
  792. /*
  793. * Delete an element e in a 2-3-4 tree. Does not free the element,
  794. * merely removes all links to it from the tree nodes.
  795. */
  796. static void *delpos234_internal(tree234 *t, int index) {
  797. node234 *n;
  798. void *retval;
  799. int ki, i;
  800. retval = NULL;
  801. n = t->root; /* by assumption this is non-NULL */
  802. LOG(("deleting item %d from tree %p\n", index, t));
  803. while (1) {
  804. node234 *sub;
  805. LOG((" node %p: %p/%d \"%s\" %p/%d \"%s\" %p/%d \"%s\" %p/%d index=%d\n",
  806. n,
  807. n->kids[0], n->counts[0], n->elems[0],
  808. n->kids[1], n->counts[1], n->elems[1],
  809. n->kids[2], n->counts[2], n->elems[2],
  810. n->kids[3], n->counts[3],
  811. index));
  812. if (index <= n->counts[0]) {
  813. ki = 0;
  814. } else if (index -= n->counts[0]+1, index <= n->counts[1]) {
  815. ki = 1;
  816. } else if (index -= n->counts[1]+1, index <= n->counts[2]) {
  817. ki = 2;
  818. } else if (index -= n->counts[2]+1, index <= n->counts[3]) {
  819. ki = 3;
  820. } else {
  821. assert(0); /* can't happen */
  822. }
  823. if (!n->kids[0])
  824. break; /* n is a leaf node; we're here! */
  825. /*
  826. * Check to see if we've found our target element. If so,
  827. * we must choose a new target (we'll use the old target's
  828. * successor, which will be in a leaf), move it into the
  829. * place of the old one, continue down to the leaf and
  830. * delete the old copy of the new target.
  831. */
  832. if (index == n->counts[ki]) {
  833. node234 *m;
  834. LOG((" found element in internal node, index %d\n", ki));
  835. assert(n->elems[ki]); /* must be a kid _before_ an element */
  836. ki++; index = 0;
  837. for (m = n->kids[ki]; m->kids[0]; m = m->kids[0])
  838. continue;
  839. LOG((" replacing with element \"%s\" from leaf node %p\n",
  840. m->elems[0], m));
  841. retval = n->elems[ki-1];
  842. n->elems[ki-1] = m->elems[0];
  843. }
  844. /*
  845. * Recurse down to subtree ki. If it has only one element,
  846. * we have to do some transformation to start with.
  847. */
  848. LOG((" moving to subtree %d\n", ki));
  849. sub = n->kids[ki];
  850. if (!sub->elems[1]) {
  851. LOG((" subtree has only one element!\n"));
  852. if (ki > 0 && n->kids[ki-1]->elems[1]) {
  853. /*
  854. * Child ki has only one element, but child
  855. * ki-1 has two or more. So we need to move a
  856. * subtree from ki-1 to ki.
  857. */
  858. trans234_subtree_right(n, ki-1, &ki, &index);
  859. } else if (ki < 3 && n->kids[ki+1] &&
  860. n->kids[ki+1]->elems[1]) {
  861. /*
  862. * Child ki has only one element, but ki+1 has
  863. * two or more. Move a subtree from ki+1 to ki.
  864. */
  865. trans234_subtree_left(n, ki+1, &ki, &index);
  866. } else {
  867. /*
  868. * ki is small with only small neighbours. Pick a
  869. * neighbour and merge with it.
  870. */
  871. trans234_subtree_merge(n, ki>0 ? ki-1 : ki, &ki, &index);
  872. sub = n->kids[ki];
  873. if (!n->elems[0]) {
  874. /*
  875. * The root is empty and needs to be
  876. * removed.
  877. */
  878. LOG((" shifting root!\n"));
  879. t->root = sub;
  880. sub->parent = NULL;
  881. sfree(n);
  882. n = NULL;
  883. }
  884. }
  885. }
  886. if (n)
  887. n->counts[ki]--;
  888. n = sub;
  889. }
  890. /*
  891. * Now n is a leaf node, and ki marks the element number we
  892. * want to delete. We've already arranged for the leaf to be
  893. * bigger than minimum size, so let's just go to it.
  894. */
  895. assert(!n->kids[0]);
  896. if (!retval)
  897. retval = n->elems[ki];
  898. for (i = ki; i < 2 && n->elems[i+1]; i++)
  899. n->elems[i] = n->elems[i+1];
  900. n->elems[i] = NULL;
  901. /*
  902. * It's just possible that we have reduced the leaf to zero
  903. * size. This can only happen if it was the root - so destroy
  904. * it and make the tree empty.
  905. */
  906. if (!n->elems[0]) {
  907. LOG((" removed last element in tree, destroying empty root\n"));
  908. assert(n == t->root);
  909. sfree(n);
  910. t->root = NULL;
  911. }
  912. return retval; /* finished! */
  913. }
  914. void *delpos234(tree234 *t, int index) {
  915. if (index < 0 || index >= countnode234(t->root))
  916. return NULL;
  917. return delpos234_internal(t, index);
  918. }
  919. void *del234(tree234 *t, void *e) {
  920. int index;
  921. if (!findrelpos234(t, e, NULL, REL234_EQ, &index))
  922. return NULL; /* it wasn't in there anyway */
  923. return delpos234_internal(t, index); /* it's there; delete it. */
  924. }
  925. /*
  926. * Join two subtrees together with a separator element between
  927. * them, given their relative height.
  928. *
  929. * (Height<0 means the left tree is shorter, >0 means the right
  930. * tree is shorter, =0 means (duh) they're equal.)
  931. *
  932. * It is assumed that any checks needed on the ordering criterion
  933. * have _already_ been done.
  934. *
  935. * The value returned in `height' is 0 or 1 depending on whether the
  936. * resulting tree is the same height as the original larger one, or
  937. * one higher.
  938. */
  939. static node234 *join234_internal(node234 *left, void *sep,
  940. node234 *right, int *height) {
  941. node234 *root, *node;
  942. int relht = *height;
  943. int ki;
  944. LOG((" join: joining %p \"%s\" %p, relative height is %d\n",
  945. left, sep, right, relht));
  946. if (relht == 0) {
  947. /*
  948. * The trees are the same height. Create a new one-element
  949. * root containing the separator and pointers to the two
  950. * nodes.
  951. */
  952. node234 *newroot;
  953. newroot = snew(node234);
  954. newroot->kids[0] = left; newroot->counts[0] = countnode234(left);
  955. newroot->elems[0] = sep;
  956. newroot->kids[1] = right; newroot->counts[1] = countnode234(right);
  957. newroot->elems[1] = NULL;
  958. newroot->kids[2] = NULL; newroot->counts[2] = 0;
  959. newroot->elems[2] = NULL;
  960. newroot->kids[3] = NULL; newroot->counts[3] = 0;
  961. newroot->parent = NULL;
  962. if (left) left->parent = newroot;
  963. if (right) right->parent = newroot;
  964. *height = 1;
  965. LOG((" join: same height, brand new root\n"));
  966. return newroot;
  967. }
  968. /*
  969. * This now works like the addition algorithm on the larger
  970. * tree. We're replacing a single kid pointer with two kid
  971. * pointers separated by an element; if that causes the node to
  972. * overload, we split it in two, move a separator element up to
  973. * the next node, and repeat.
  974. */
  975. if (relht < 0) {
  976. /*
  977. * Left tree is shorter. Search down the right tree to find
  978. * the pointer we're inserting at.
  979. */
  980. node = root = right;
  981. while (++relht < 0) {
  982. node = node->kids[0];
  983. }
  984. ki = 0;
  985. right = node->kids[ki];
  986. } else {
  987. /*
  988. * Right tree is shorter; search down the left to find the
  989. * pointer we're inserting at.
  990. */
  991. node = root = left;
  992. while (--relht > 0) {
  993. if (node->elems[2])
  994. node = node->kids[3];
  995. else if (node->elems[1])
  996. node = node->kids[2];
  997. else
  998. node = node->kids[1];
  999. }
  1000. if (node->elems[2])
  1001. ki = 3;
  1002. else if (node->elems[1])
  1003. ki = 2;
  1004. else
  1005. ki = 1;
  1006. left = node->kids[ki];
  1007. }
  1008. /*
  1009. * Now proceed as for addition.
  1010. */
  1011. *height = add234_insert(left, sep, right, &root, node, ki);
  1012. return root;
  1013. }
  1014. int height234(tree234 *t) {
  1015. int level = 0;
  1016. node234 *n = t->root;
  1017. while (n) {
  1018. level++;
  1019. n = n->kids[0];
  1020. }
  1021. return level;
  1022. }
  1023. tree234 *join234(tree234 *t1, tree234 *t2) {
  1024. int size2 = countnode234(t2->root);
  1025. if (size2 > 0) {
  1026. void *element;
  1027. int relht;
  1028. if (t1->cmp) {
  1029. element = index234(t2, 0);
  1030. element = findrelpos234(t1, element, NULL, REL234_GE, NULL);
  1031. if (element)
  1032. return NULL;
  1033. }
  1034. element = delpos234(t2, 0);
  1035. relht = height234(t1) - height234(t2);
  1036. t1->root = join234_internal(t1->root, element, t2->root, &relht);
  1037. t2->root = NULL;
  1038. }
  1039. return t1;
  1040. }
  1041. tree234 *join234r(tree234 *t1, tree234 *t2) {
  1042. int size1 = countnode234(t1->root);
  1043. if (size1 > 0) {
  1044. void *element;
  1045. int relht;
  1046. if (t2->cmp) {
  1047. element = index234(t1, size1-1);
  1048. element = findrelpos234(t2, element, NULL, REL234_LE, NULL);
  1049. if (element)
  1050. return NULL;
  1051. }
  1052. element = delpos234(t1, size1-1);
  1053. relht = height234(t1) - height234(t2);
  1054. t2->root = join234_internal(t1->root, element, t2->root, &relht);
  1055. t1->root = NULL;
  1056. }
  1057. return t2;
  1058. }
  1059. /*
  1060. * Split out the first <index> elements in a tree and return a
  1061. * pointer to the root node. Leave the root node of the remainder
  1062. * in t.
  1063. */
  1064. static node234 *split234_internal(tree234 *t, int index) {
  1065. node234 *halves[2] = { NULL, NULL }, *n, *sib, *sub;
  1066. node234 *lparent, *rparent;
  1067. int ki, pki, i, half, lcount, rcount;
  1068. n = t->root;
  1069. LOG(("splitting tree %p at point %d\n", t, index));
  1070. /*
  1071. * Easy special cases. After this we have also dealt completely
  1072. * with the empty-tree case and we can assume the root exists.
  1073. */
  1074. if (index == 0) /* return nothing */
  1075. return NULL;
  1076. if (index == countnode234(t->root)) { /* return the whole tree */
  1077. node234 *ret = t->root;
  1078. t->root = NULL;
  1079. return ret;
  1080. }
  1081. /*
  1082. * Search down the tree to find the split point.
  1083. */
  1084. halves[0] = halves[1] = NULL;
  1085. lparent = rparent = NULL;
  1086. pki = -1;
  1087. while (n) {
  1088. LOG((" node %p: %p/%d \"%s\" %p/%d \"%s\" %p/%d \"%s\" %p/%d index=%d\n",
  1089. n,
  1090. n->kids[0], n->counts[0], n->elems[0],
  1091. n->kids[1], n->counts[1], n->elems[1],
  1092. n->kids[2], n->counts[2], n->elems[2],
  1093. n->kids[3], n->counts[3],
  1094. index));
  1095. lcount = index;
  1096. rcount = countnode234(n) - lcount;
  1097. if (index <= n->counts[0]) {
  1098. ki = 0;
  1099. } else if (index -= n->counts[0]+1, index <= n->counts[1]) {
  1100. ki = 1;
  1101. } else if (index -= n->counts[1]+1, index <= n->counts[2]) {
  1102. ki = 2;
  1103. } else {
  1104. index -= n->counts[2]+1;
  1105. ki = 3;
  1106. }
  1107. LOG((" splitting at subtree %d\n", ki));
  1108. sub = n->kids[ki];
  1109. LOG((" splitting at child index %d\n", ki));
  1110. /*
  1111. * Split the node, put halves[0] on the right of the left
  1112. * one and halves[1] on the left of the right one, put the
  1113. * new node pointers in halves[0] and halves[1], and go up
  1114. * a level.
  1115. */
  1116. sib = snew(node234);
  1117. for (i = 0; i < 3; i++) {
  1118. if (i+ki < 3 && n->elems[i+ki]) {
  1119. sib->elems[i] = n->elems[i+ki];
  1120. sib->kids[i+1] = n->kids[i+ki+1];
  1121. if (sib->kids[i+1]) sib->kids[i+1]->parent = sib;
  1122. sib->counts[i+1] = n->counts[i+ki+1];
  1123. n->elems[i+ki] = NULL;
  1124. n->kids[i+ki+1] = NULL;
  1125. n->counts[i+ki+1] = 0;
  1126. } else {
  1127. sib->elems[i] = NULL;
  1128. sib->kids[i+1] = NULL;
  1129. sib->counts[i+1] = 0;
  1130. }
  1131. }
  1132. if (lparent) {
  1133. lparent->kids[pki] = n;
  1134. lparent->counts[pki] = lcount;
  1135. n->parent = lparent;
  1136. rparent->kids[0] = sib;
  1137. rparent->counts[0] = rcount;
  1138. sib->parent = rparent;
  1139. } else {
  1140. halves[0] = n;
  1141. n->parent = NULL;
  1142. halves[1] = sib;
  1143. sib->parent = NULL;
  1144. }
  1145. lparent = n;
  1146. rparent = sib;
  1147. pki = ki;
  1148. LOG((" left node %p: %p/%d \"%s\" %p/%d \"%s\" %p/%d \"%s\" %p/%d\n",
  1149. n,
  1150. n->kids[0], n->counts[0], n->elems[0],
  1151. n->kids[1], n->counts[1], n->elems[1],
  1152. n->kids[2], n->counts[2], n->elems[2],
  1153. n->kids[3], n->counts[3]));
  1154. LOG((" right node %p: %p/%d \"%s\" %p/%d \"%s\" %p/%d \"%s\" %p/%d\n",
  1155. sib,
  1156. sib->kids[0], sib->counts[0], sib->elems[0],
  1157. sib->kids[1], sib->counts[1], sib->elems[1],
  1158. sib->kids[2], sib->counts[2], sib->elems[2],
  1159. sib->kids[3], sib->counts[3]));
  1160. n = sub;
  1161. }
  1162. /*
  1163. * We've come off the bottom here, so we've successfully split
  1164. * the tree into two equally high subtrees. The only problem is
  1165. * that some of the nodes down the fault line will be smaller
  1166. * than the minimum permitted size. (Since this is a 2-3-4
  1167. * tree, that means they'll be zero-element one-child nodes.)
  1168. */
  1169. LOG((" fell off bottom, lroot is %p, rroot is %p\n",
  1170. halves[0], halves[1]));
  1171. assert(halves[0] != NULL);
  1172. assert(halves[1] != NULL);
  1173. lparent->counts[pki] = rparent->counts[0] = 0;
  1174. lparent->kids[pki] = rparent->kids[0] = NULL;
  1175. /*
  1176. * So now we go back down the tree from each of the two roots,
  1177. * fixing up undersize nodes.
  1178. */
  1179. for (half = 0; half < 2; half++) {
  1180. /*
  1181. * Remove the root if it's undersize (it will contain only
  1182. * one child pointer, so just throw it away and replace it
  1183. * with its child). This might happen several times.
  1184. */
  1185. while (halves[half] && !halves[half]->elems[0]) {
  1186. LOG((" root %p is undersize, throwing away\n", halves[half]));
  1187. halves[half] = halves[half]->kids[0];
  1188. sfree(halves[half]->parent);
  1189. halves[half]->parent = NULL;
  1190. LOG((" new root is %p\n", halves[half]));
  1191. }
  1192. n = halves[half];
  1193. while (n) {
  1194. void (*toward)(node234 *n, int ki, int *k, int *index);
  1195. int ni, merge;
  1196. /*
  1197. * Now we have a potentially undersize node on the
  1198. * right (if half==0) or left (if half==1). Sort it
  1199. * out, by merging with a neighbour or by transferring
  1200. * subtrees over. At this time we must also ensure that
  1201. * nodes are bigger than minimum, in case we need an
  1202. * element to merge two nodes below.
  1203. */
  1204. LOG((" node %p: %p/%d \"%s\" %p/%d \"%s\" %p/%d \"%s\" %p/%d\n",
  1205. n,
  1206. n->kids[0], n->counts[0], n->elems[0],
  1207. n->kids[1], n->counts[1], n->elems[1],
  1208. n->kids[2], n->counts[2], n->elems[2],
  1209. n->kids[3], n->counts[3]));
  1210. if (half == 1) {
  1211. ki = 0; /* the kid we're interested in */
  1212. ni = 1; /* the neighbour */
  1213. merge = 0; /* for merge: leftmost of the two */
  1214. toward = trans234_subtree_left;
  1215. } else {
  1216. ki = (n->kids[3] ? 3 : n->kids[2] ? 2 : 1);
  1217. ni = ki-1;
  1218. merge = ni;
  1219. toward = trans234_subtree_right;
  1220. }
  1221. sub = n->kids[ki];
  1222. if (sub && !sub->elems[1]) {
  1223. /*
  1224. * This node is undersized or minimum-size. If we
  1225. * can merge it with its neighbour, we do so;
  1226. * otherwise we must be able to transfer subtrees
  1227. * over to it until it is greater than minimum
  1228. * size.
  1229. */
  1230. bool undersized = (!sub->elems[0]);
  1231. LOG((" child %d is %ssize\n", ki,
  1232. undersized ? "under" : "minimum-"));
  1233. LOG((" neighbour is %s\n",
  1234. n->kids[ni]->elems[2] ? "large" :
  1235. n->kids[ni]->elems[1] ? "medium" : "small"));
  1236. if (!n->kids[ni]->elems[1] ||
  1237. (undersized && !n->kids[ni]->elems[2])) {
  1238. /*
  1239. * Neighbour is small, or possibly neighbour is
  1240. * medium and we are undersize.
  1241. */
  1242. trans234_subtree_merge(n, merge, NULL, NULL);
  1243. sub = n->kids[merge];
  1244. if (!n->elems[0]) {
  1245. /*
  1246. * n is empty, and hence must have been the
  1247. * root and needs to be removed.
  1248. */
  1249. assert(!n->parent);
  1250. LOG((" shifting root!\n"));
  1251. halves[half] = sub;
  1252. halves[half]->parent = NULL;
  1253. sfree(n);
  1254. }
  1255. } else {
  1256. /* Neighbour is big enough to move trees over. */
  1257. toward(n, ni, NULL, NULL);
  1258. if (undersized)
  1259. toward(n, ni, NULL, NULL);
  1260. }
  1261. }
  1262. n = sub;
  1263. }
  1264. }
  1265. t->root = halves[1];
  1266. return halves[0];
  1267. }
  1268. tree234 *splitpos234(tree234 *t, int index, bool before) {
  1269. tree234 *ret;
  1270. node234 *n;
  1271. int count;
  1272. count = countnode234(t->root);
  1273. if (index < 0 || index > count)
  1274. return NULL; /* error */
  1275. ret = newtree234(t->cmp);
  1276. n = split234_internal(t, index);
  1277. if (before) {
  1278. /* We want to return the ones before the index. */
  1279. ret->root = n;
  1280. } else {
  1281. /*
  1282. * We want to keep the ones before the index and return the
  1283. * ones after.
  1284. */
  1285. ret->root = t->root;
  1286. t->root = n;
  1287. }
  1288. return ret;
  1289. }
  1290. tree234 *split234(tree234 *t, void *e, cmpfn234 cmp, int rel) {
  1291. bool before;
  1292. int index;
  1293. assert(rel != REL234_EQ);
  1294. if (rel == REL234_GT || rel == REL234_GE) {
  1295. before = true;
  1296. rel = (rel == REL234_GT ? REL234_LE : REL234_LT);
  1297. } else {
  1298. before = false;
  1299. }
  1300. if (!findrelpos234(t, e, cmp, rel, &index))
  1301. index = 0;
  1302. return splitpos234(t, index+1, before);
  1303. }
  1304. static node234 *copynode234(node234 *n, copyfn234 copyfn, void *copyfnstate) {
  1305. int i;
  1306. node234 *n2 = snew(node234);
  1307. for (i = 0; i < 3; i++) {
  1308. if (n->elems[i] && copyfn)
  1309. n2->elems[i] = copyfn(copyfnstate, n->elems[i]);
  1310. else
  1311. n2->elems[i] = n->elems[i];
  1312. }
  1313. for (i = 0; i < 4; i++) {
  1314. if (n->kids[i]) {
  1315. n2->kids[i] = copynode234(n->kids[i], copyfn, copyfnstate);
  1316. n2->kids[i]->parent = n2;
  1317. } else {
  1318. n2->kids[i] = NULL;
  1319. }
  1320. n2->counts[i] = n->counts[i];
  1321. }
  1322. return n2;
  1323. }
  1324. tree234 *copytree234(tree234 *t, copyfn234 copyfn, void *copyfnstate) {
  1325. tree234 *t2;
  1326. t2 = newtree234(t->cmp);
  1327. if (t->root) {
  1328. t2->root = copynode234(t->root, copyfn, copyfnstate);
  1329. t2->root->parent = NULL;
  1330. } else
  1331. t2->root = NULL;
  1332. return t2;
  1333. }