--- /dev/null
+/*\r
+ * tree234.c: reasonably generic counted 2-3-4 tree routines.\r
+ * \r
+ * This file is copyright 1999-2001 Simon Tatham.\r
+ * \r
+ * Permission is hereby granted, free of charge, to any person\r
+ * obtaining a copy of this software and associated documentation\r
+ * files (the "Software"), to deal in the Software without\r
+ * restriction, including without limitation the rights to use,\r
+ * copy, modify, merge, publish, distribute, sublicense, and/or\r
+ * sell copies of the Software, and to permit persons to whom the\r
+ * Software is furnished to do so, subject to the following\r
+ * conditions:\r
+ * \r
+ * The above copyright notice and this permission notice shall be\r
+ * included in all copies or substantial portions of the Software.\r
+ * \r
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES\r
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+ * NONINFRINGEMENT. IN NO EVENT SHALL SIMON TATHAM BE LIABLE FOR\r
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF\r
+ * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
+ * SOFTWARE.\r
+ */\r
+\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <assert.h>\r
+\r
+#include "puttymem.h"\r
+#include "tree234.h"\r
+\r
+#ifdef TEST\r
+#define LOG(x) (printf x)\r
+#else\r
+#define LOG(x)\r
+#endif\r
+\r
+typedef struct node234_Tag node234;\r
+\r
+struct tree234_Tag {\r
+ node234 *root;\r
+ cmpfn234 cmp;\r
+};\r
+\r
+struct node234_Tag {\r
+ node234 *parent;\r
+ node234 *kids[4];\r
+ int counts[4];\r
+ void *elems[3];\r
+};\r
+\r
+/*\r
+ * Create a 2-3-4 tree.\r
+ */\r
+tree234 *newtree234(cmpfn234 cmp)\r
+{\r
+ tree234 *ret = snew(tree234);\r
+ LOG(("created tree %p\n", ret));\r
+ ret->root = NULL;\r
+ ret->cmp = cmp;\r
+ return ret;\r
+}\r
+\r
+/*\r
+ * Free a 2-3-4 tree (not including freeing the elements).\r
+ */\r
+static void freenode234(node234 * n)\r
+{\r
+ if (!n)\r
+ return;\r
+ freenode234(n->kids[0]);\r
+ freenode234(n->kids[1]);\r
+ freenode234(n->kids[2]);\r
+ freenode234(n->kids[3]);\r
+ sfree(n);\r
+}\r
+\r
+void freetree234(tree234 * t)\r
+{\r
+ freenode234(t->root);\r
+ sfree(t);\r
+}\r
+\r
+/*\r
+ * Internal function to count a node.\r
+ */\r
+static int countnode234(node234 * n)\r
+{\r
+ int count = 0;\r
+ int i;\r
+ if (!n)\r
+ return 0;\r
+ for (i = 0; i < 4; i++)\r
+ count += n->counts[i];\r
+ for (i = 0; i < 3; i++)\r
+ if (n->elems[i])\r
+ count++;\r
+ return count;\r
+}\r
+\r
+/*\r
+ * Count the elements in a tree.\r
+ */\r
+int count234(tree234 * t)\r
+{\r
+ if (t->root)\r
+ return countnode234(t->root);\r
+ else\r
+ return 0;\r
+}\r
+\r
+/*\r
+ * Add an element e to a 2-3-4 tree t. Returns e on success, or if\r
+ * an existing element compares equal, returns that.\r
+ */\r
+static void *add234_internal(tree234 * t, void *e, int index)\r
+{\r
+ node234 *n, **np, *left, *right;\r
+ void *orig_e = e;\r
+ int c, lcount, rcount;\r
+\r
+ LOG(("adding node %p to tree %p\n", e, t));\r
+ if (t->root == NULL) {\r
+ t->root = snew(node234);\r
+ t->root->elems[1] = t->root->elems[2] = NULL;\r
+ t->root->kids[0] = t->root->kids[1] = NULL;\r
+ t->root->kids[2] = t->root->kids[3] = NULL;\r
+ t->root->counts[0] = t->root->counts[1] = 0;\r
+ t->root->counts[2] = t->root->counts[3] = 0;\r
+ t->root->parent = NULL;\r
+ t->root->elems[0] = e;\r
+ LOG((" created root %p\n", t->root));\r
+ return orig_e;\r
+ }\r
+\r
+ n = NULL; /* placate gcc; will always be set below since t->root != NULL */\r
+ np = &t->root;\r
+ while (*np) {\r
+ int childnum;\r
+ n = *np;\r
+ LOG((" node %p: %p/%d [%p] %p/%d [%p] %p/%d [%p] %p/%d\n",\r
+ n,\r
+ n->kids[0], n->counts[0], n->elems[0],\r
+ n->kids[1], n->counts[1], n->elems[1],\r
+ n->kids[2], n->counts[2], n->elems[2],\r
+ n->kids[3], n->counts[3]));\r
+ if (index >= 0) {\r
+ if (!n->kids[0]) {\r
+ /*\r
+ * Leaf node. We want to insert at kid position\r
+ * equal to the index:\r
+ * \r
+ * 0 A 1 B 2 C 3\r
+ */\r
+ childnum = index;\r
+ } else {\r
+ /*\r
+ * Internal node. We always descend through it (add\r
+ * always starts at the bottom, never in the\r
+ * middle).\r
+ */\r
+ do { /* this is a do ... while (0) to allow `break' */\r
+ if (index <= n->counts[0]) {\r
+ childnum = 0;\r
+ break;\r
+ }\r
+ index -= n->counts[0] + 1;\r
+ if (index <= n->counts[1]) {\r
+ childnum = 1;\r
+ break;\r
+ }\r
+ index -= n->counts[1] + 1;\r
+ if (index <= n->counts[2]) {\r
+ childnum = 2;\r
+ break;\r
+ }\r
+ index -= n->counts[2] + 1;\r
+ if (index <= n->counts[3]) {\r
+ childnum = 3;\r
+ break;\r
+ }\r
+ return NULL; /* error: index out of range */\r
+ } while (0);\r
+ }\r
+ } else {\r
+ if ((c = t->cmp(e, n->elems[0])) < 0)\r
+ childnum = 0;\r
+ else if (c == 0)\r
+ return n->elems[0]; /* already exists */\r
+ else if (n->elems[1] == NULL\r
+ || (c = t->cmp(e, n->elems[1])) < 0) childnum = 1;\r
+ else if (c == 0)\r
+ return n->elems[1]; /* already exists */\r
+ else if (n->elems[2] == NULL\r
+ || (c = t->cmp(e, n->elems[2])) < 0) childnum = 2;\r
+ else if (c == 0)\r
+ return n->elems[2]; /* already exists */\r
+ else\r
+ childnum = 3;\r
+ }\r
+ np = &n->kids[childnum];\r
+ LOG((" moving to child %d (%p)\n", childnum, *np));\r
+ }\r
+\r
+ /*\r
+ * We need to insert the new element in n at position np.\r
+ */\r
+ left = NULL;\r
+ lcount = 0;\r
+ right = NULL;\r
+ rcount = 0;\r
+ while (n) {\r
+ LOG((" at %p: %p/%d [%p] %p/%d [%p] %p/%d [%p] %p/%d\n",\r
+ n,\r
+ n->kids[0], n->counts[0], n->elems[0],\r
+ n->kids[1], n->counts[1], n->elems[1],\r
+ n->kids[2], n->counts[2], n->elems[2],\r
+ n->kids[3], n->counts[3]));\r
+ LOG((" need to insert %p/%d [%p] %p/%d at position %d\n",\r
+ left, lcount, e, right, rcount, np - n->kids));\r
+ if (n->elems[1] == NULL) {\r
+ /*\r
+ * Insert in a 2-node; simple.\r
+ */\r
+ if (np == &n->kids[0]) {\r
+ LOG((" inserting on left of 2-node\n"));\r
+ n->kids[2] = n->kids[1];\r
+ n->counts[2] = n->counts[1];\r
+ n->elems[1] = n->elems[0];\r
+ n->kids[1] = right;\r
+ n->counts[1] = rcount;\r
+ n->elems[0] = e;\r
+ n->kids[0] = left;\r
+ n->counts[0] = lcount;\r
+ } else { /* np == &n->kids[1] */\r
+ LOG((" inserting on right of 2-node\n"));\r
+ n->kids[2] = right;\r
+ n->counts[2] = rcount;\r
+ n->elems[1] = e;\r
+ n->kids[1] = left;\r
+ n->counts[1] = lcount;\r
+ }\r
+ if (n->kids[0])\r
+ n->kids[0]->parent = n;\r
+ if (n->kids[1])\r
+ n->kids[1]->parent = n;\r
+ if (n->kids[2])\r
+ n->kids[2]->parent = n;\r
+ LOG((" done\n"));\r
+ break;\r
+ } else if (n->elems[2] == NULL) {\r
+ /*\r
+ * Insert in a 3-node; simple.\r
+ */\r
+ if (np == &n->kids[0]) {\r
+ LOG((" inserting on left of 3-node\n"));\r
+ n->kids[3] = n->kids[2];\r
+ n->counts[3] = n->counts[2];\r
+ n->elems[2] = n->elems[1];\r
+ n->kids[2] = n->kids[1];\r
+ n->counts[2] = n->counts[1];\r
+ n->elems[1] = n->elems[0];\r
+ n->kids[1] = right;\r
+ n->counts[1] = rcount;\r
+ n->elems[0] = e;\r
+ n->kids[0] = left;\r
+ n->counts[0] = lcount;\r
+ } else if (np == &n->kids[1]) {\r
+ LOG((" inserting in middle of 3-node\n"));\r
+ n->kids[3] = n->kids[2];\r
+ n->counts[3] = n->counts[2];\r
+ n->elems[2] = n->elems[1];\r
+ n->kids[2] = right;\r
+ n->counts[2] = rcount;\r
+ n->elems[1] = e;\r
+ n->kids[1] = left;\r
+ n->counts[1] = lcount;\r
+ } else { /* np == &n->kids[2] */\r
+ LOG((" inserting on right of 3-node\n"));\r
+ n->kids[3] = right;\r
+ n->counts[3] = rcount;\r
+ n->elems[2] = e;\r
+ n->kids[2] = left;\r
+ n->counts[2] = lcount;\r
+ }\r
+ if (n->kids[0])\r
+ n->kids[0]->parent = n;\r
+ if (n->kids[1])\r
+ n->kids[1]->parent = n;\r
+ if (n->kids[2])\r
+ n->kids[2]->parent = n;\r
+ if (n->kids[3])\r
+ n->kids[3]->parent = n;\r
+ LOG((" done\n"));\r
+ break;\r
+ } else {\r
+ node234 *m = snew(node234);\r
+ m->parent = n->parent;\r
+ LOG((" splitting a 4-node; created new node %p\n", m));\r
+ /*\r
+ * Insert in a 4-node; split into a 2-node and a\r
+ * 3-node, and move focus up a level.\r
+ * \r
+ * I don't think it matters which way round we put the\r
+ * 2 and the 3. For simplicity, we'll put the 3 first\r
+ * always.\r
+ */\r
+ if (np == &n->kids[0]) {\r
+ m->kids[0] = left;\r
+ m->counts[0] = lcount;\r
+ m->elems[0] = e;\r
+ m->kids[1] = right;\r
+ m->counts[1] = rcount;\r
+ m->elems[1] = n->elems[0];\r
+ m->kids[2] = n->kids[1];\r
+ m->counts[2] = n->counts[1];\r
+ e = n->elems[1];\r
+ n->kids[0] = n->kids[2];\r
+ n->counts[0] = n->counts[2];\r
+ n->elems[0] = n->elems[2];\r
+ n->kids[1] = n->kids[3];\r
+ n->counts[1] = n->counts[3];\r
+ } else if (np == &n->kids[1]) {\r
+ m->kids[0] = n->kids[0];\r
+ m->counts[0] = n->counts[0];\r
+ m->elems[0] = n->elems[0];\r
+ m->kids[1] = left;\r
+ m->counts[1] = lcount;\r
+ m->elems[1] = e;\r
+ m->kids[2] = right;\r
+ m->counts[2] = rcount;\r
+ e = n->elems[1];\r
+ n->kids[0] = n->kids[2];\r
+ n->counts[0] = n->counts[2];\r
+ n->elems[0] = n->elems[2];\r
+ n->kids[1] = n->kids[3];\r
+ n->counts[1] = n->counts[3];\r
+ } else if (np == &n->kids[2]) {\r
+ m->kids[0] = n->kids[0];\r
+ m->counts[0] = n->counts[0];\r
+ m->elems[0] = n->elems[0];\r
+ m->kids[1] = n->kids[1];\r
+ m->counts[1] = n->counts[1];\r
+ m->elems[1] = n->elems[1];\r
+ m->kids[2] = left;\r
+ m->counts[2] = lcount;\r
+ /* e = e; */\r
+ n->kids[0] = right;\r
+ n->counts[0] = rcount;\r
+ n->elems[0] = n->elems[2];\r
+ n->kids[1] = n->kids[3];\r
+ n->counts[1] = n->counts[3];\r
+ } else { /* np == &n->kids[3] */\r
+ m->kids[0] = n->kids[0];\r
+ m->counts[0] = n->counts[0];\r
+ m->elems[0] = n->elems[0];\r
+ m->kids[1] = n->kids[1];\r
+ m->counts[1] = n->counts[1];\r
+ m->elems[1] = n->elems[1];\r
+ m->kids[2] = n->kids[2];\r
+ m->counts[2] = n->counts[2];\r
+ n->kids[0] = left;\r
+ n->counts[0] = lcount;\r
+ n->elems[0] = e;\r
+ n->kids[1] = right;\r
+ n->counts[1] = rcount;\r
+ e = n->elems[2];\r
+ }\r
+ m->kids[3] = n->kids[3] = n->kids[2] = NULL;\r
+ m->counts[3] = n->counts[3] = n->counts[2] = 0;\r
+ m->elems[2] = n->elems[2] = n->elems[1] = NULL;\r
+ if (m->kids[0])\r
+ m->kids[0]->parent = m;\r
+ if (m->kids[1])\r
+ m->kids[1]->parent = m;\r
+ if (m->kids[2])\r
+ m->kids[2]->parent = m;\r
+ if (n->kids[0])\r
+ n->kids[0]->parent = n;\r
+ if (n->kids[1])\r
+ n->kids[1]->parent = n;\r
+ LOG((" left (%p): %p/%d [%p] %p/%d [%p] %p/%d\n", m,\r
+ m->kids[0], m->counts[0], m->elems[0],\r
+ m->kids[1], m->counts[1], m->elems[1],\r
+ m->kids[2], m->counts[2]));\r
+ LOG((" right (%p): %p/%d [%p] %p/%d\n", n,\r
+ n->kids[0], n->counts[0], n->elems[0],\r
+ n->kids[1], n->counts[1]));\r
+ left = m;\r
+ lcount = countnode234(left);\r
+ right = n;\r
+ rcount = countnode234(right);\r
+ }\r
+ if (n->parent)\r
+ np = (n->parent->kids[0] == n ? &n->parent->kids[0] :\r
+ n->parent->kids[1] == n ? &n->parent->kids[1] :\r
+ n->parent->kids[2] == n ? &n->parent->kids[2] :\r
+ &n->parent->kids[3]);\r
+ n = n->parent;\r
+ }\r
+\r
+ /*\r
+ * If we've come out of here by `break', n will still be\r
+ * non-NULL and all we need to do is go back up the tree\r
+ * updating counts. If we've come here because n is NULL, we\r
+ * need to create a new root for the tree because the old one\r
+ * has just split into two. */\r
+ if (n) {\r
+ while (n->parent) {\r
+ int count = countnode234(n);\r
+ int childnum;\r
+ childnum = (n->parent->kids[0] == n ? 0 :\r
+ n->parent->kids[1] == n ? 1 :\r
+ n->parent->kids[2] == n ? 2 : 3);\r
+ n->parent->counts[childnum] = count;\r
+ n = n->parent;\r
+ }\r
+ } else {\r
+ LOG((" root is overloaded, split into two\n"));\r
+ t->root = snew(node234);\r
+ t->root->kids[0] = left;\r
+ t->root->counts[0] = lcount;\r
+ t->root->elems[0] = e;\r
+ t->root->kids[1] = right;\r
+ t->root->counts[1] = rcount;\r
+ t->root->elems[1] = NULL;\r
+ t->root->kids[2] = NULL;\r
+ t->root->counts[2] = 0;\r
+ t->root->elems[2] = NULL;\r
+ t->root->kids[3] = NULL;\r
+ t->root->counts[3] = 0;\r
+ t->root->parent = NULL;\r
+ if (t->root->kids[0])\r
+ t->root->kids[0]->parent = t->root;\r
+ if (t->root->kids[1])\r
+ t->root->kids[1]->parent = t->root;\r
+ LOG((" new root is %p/%d [%p] %p/%d\n",\r
+ t->root->kids[0], t->root->counts[0],\r
+ t->root->elems[0], t->root->kids[1], t->root->counts[1]));\r
+ }\r
+\r
+ return orig_e;\r
+}\r
+\r
+void *add234(tree234 * t, void *e)\r
+{\r
+ if (!t->cmp) /* tree is unsorted */\r
+ return NULL;\r
+\r
+ return add234_internal(t, e, -1);\r
+}\r
+void *addpos234(tree234 * t, void *e, int index)\r
+{\r
+ if (index < 0 || /* index out of range */\r
+ t->cmp) /* tree is sorted */\r
+ return NULL; /* return failure */\r
+\r
+ return add234_internal(t, e, index); /* this checks the upper bound */\r
+}\r
+\r
+/*\r
+ * Look up the element at a given numeric index in a 2-3-4 tree.\r
+ * Returns NULL if the index is out of range.\r
+ */\r
+void *index234(tree234 * t, int index)\r
+{\r
+ node234 *n;\r
+\r
+ if (!t->root)\r
+ return NULL; /* tree is empty */\r
+\r
+ if (index < 0 || index >= countnode234(t->root))\r
+ return NULL; /* out of range */\r
+\r
+ n = t->root;\r
+\r
+ while (n) {\r
+ if (index < n->counts[0])\r
+ n = n->kids[0];\r
+ else if (index -= n->counts[0] + 1, index < 0)\r
+ return n->elems[0];\r
+ else if (index < n->counts[1])\r
+ n = n->kids[1];\r
+ else if (index -= n->counts[1] + 1, index < 0)\r
+ return n->elems[1];\r
+ else if (index < n->counts[2])\r
+ n = n->kids[2];\r
+ else if (index -= n->counts[2] + 1, index < 0)\r
+ return n->elems[2];\r
+ else\r
+ n = n->kids[3];\r
+ }\r
+\r
+ /* We shouldn't ever get here. I wonder how we did. */\r
+ return NULL;\r
+}\r
+\r
+/*\r
+ * Find an element e in a sorted 2-3-4 tree t. Returns NULL if not\r
+ * found. e is always passed as the first argument to cmp, so cmp\r
+ * can be an asymmetric function if desired. cmp can also be passed\r
+ * as NULL, in which case the compare function from the tree proper\r
+ * will be used.\r
+ */\r
+void *findrelpos234(tree234 * t, void *e, cmpfn234 cmp,\r
+ int relation, int *index)\r
+{\r
+ node234 *n;\r
+ void *ret;\r
+ int c;\r
+ int idx, ecount, kcount, cmpret;\r
+\r
+ if (t->root == NULL)\r
+ return NULL;\r
+\r
+ if (cmp == NULL)\r
+ cmp = t->cmp;\r
+\r
+ n = t->root;\r
+ /*\r
+ * Attempt to find the element itself.\r
+ */\r
+ idx = 0;\r
+ ecount = -1;\r
+ /*\r
+ * Prepare a fake `cmp' result if e is NULL.\r
+ */\r
+ cmpret = 0;\r
+ if (e == NULL) {\r
+ assert(relation == REL234_LT || relation == REL234_GT);\r
+ if (relation == REL234_LT)\r
+ cmpret = +1; /* e is a max: always greater */\r
+ else if (relation == REL234_GT)\r
+ cmpret = -1; /* e is a min: always smaller */\r
+ }\r
+ while (1) {\r
+ for (kcount = 0; kcount < 4; kcount++) {\r
+ if (kcount >= 3 || n->elems[kcount] == NULL ||\r
+ (c = cmpret ? cmpret : cmp(e, n->elems[kcount])) < 0) {\r
+ break;\r
+ }\r
+ if (n->kids[kcount])\r
+ idx += n->counts[kcount];\r
+ if (c == 0) {\r
+ ecount = kcount;\r
+ break;\r
+ }\r
+ idx++;\r
+ }\r
+ if (ecount >= 0)\r
+ break;\r
+ if (n->kids[kcount])\r
+ n = n->kids[kcount];\r
+ else\r
+ break;\r
+ }\r
+\r
+ if (ecount >= 0) {\r
+ /*\r
+ * We have found the element we're looking for. It's\r
+ * n->elems[ecount], at tree index idx. If our search\r
+ * relation is EQ, LE or GE we can now go home.\r
+ */\r
+ if (relation != REL234_LT && relation != REL234_GT) {\r
+ if (index)\r
+ *index = idx;\r
+ return n->elems[ecount];\r
+ }\r
+\r
+ /*\r
+ * Otherwise, we'll do an indexed lookup for the previous\r
+ * or next element. (It would be perfectly possible to\r
+ * implement these search types in a non-counted tree by\r
+ * going back up from where we are, but far more fiddly.)\r
+ */\r
+ if (relation == REL234_LT)\r
+ idx--;\r
+ else\r
+ idx++;\r
+ } else {\r
+ /*\r
+ * We've found our way to the bottom of the tree and we\r
+ * know where we would insert this node if we wanted to:\r
+ * we'd put it in in place of the (empty) subtree\r
+ * n->kids[kcount], and it would have index idx\r
+ * \r
+ * But the actual element isn't there. So if our search\r
+ * relation is EQ, we're doomed.\r
+ */\r
+ if (relation == REL234_EQ)\r
+ return NULL;\r
+\r
+ /*\r
+ * Otherwise, we must do an index lookup for index idx-1\r
+ * (if we're going left - LE or LT) or index idx (if we're\r
+ * going right - GE or GT).\r
+ */\r
+ if (relation == REL234_LT || relation == REL234_LE) {\r
+ idx--;\r
+ }\r
+ }\r
+\r
+ /*\r
+ * We know the index of the element we want; just call index234\r
+ * to do the rest. This will return NULL if the index is out of\r
+ * bounds, which is exactly what we want.\r
+ */\r
+ ret = index234(t, idx);\r
+ if (ret && index)\r
+ *index = idx;\r
+ return ret;\r
+}\r
+void *find234(tree234 * t, void *e, cmpfn234 cmp)\r
+{\r
+ return findrelpos234(t, e, cmp, REL234_EQ, NULL);\r
+}\r
+void *findrel234(tree234 * t, void *e, cmpfn234 cmp, int relation)\r
+{\r
+ return findrelpos234(t, e, cmp, relation, NULL);\r
+}\r
+void *findpos234(tree234 * t, void *e, cmpfn234 cmp, int *index)\r
+{\r
+ return findrelpos234(t, e, cmp, REL234_EQ, index);\r
+}\r
+\r
+/*\r
+ * Delete an element e in a 2-3-4 tree. Does not free the element,\r
+ * merely removes all links to it from the tree nodes.\r
+ */\r
+static void *delpos234_internal(tree234 * t, int index)\r
+{\r
+ node234 *n;\r
+ void *retval;\r
+ int ei = -1;\r
+\r
+ retval = 0;\r
+\r
+ n = t->root;\r
+ LOG(("deleting item %d from tree %p\n", index, t));\r
+ while (1) {\r
+ while (n) {\r
+ int ki;\r
+ node234 *sub;\r
+\r
+ LOG(\r
+ (" node %p: %p/%d [%p] %p/%d [%p] %p/%d [%p] %p/%d index=%d\n",\r
+ n, n->kids[0], n->counts[0], n->elems[0], n->kids[1],\r
+ n->counts[1], n->elems[1], n->kids[2], n->counts[2],\r
+ n->elems[2], n->kids[3], n->counts[3], index));\r
+ if (index < n->counts[0]) {\r
+ ki = 0;\r
+ } else if (index -= n->counts[0] + 1, index < 0) {\r
+ ei = 0;\r
+ break;\r
+ } else if (index < n->counts[1]) {\r
+ ki = 1;\r
+ } else if (index -= n->counts[1] + 1, index < 0) {\r
+ ei = 1;\r
+ break;\r
+ } else if (index < n->counts[2]) {\r
+ ki = 2;\r
+ } else if (index -= n->counts[2] + 1, index < 0) {\r
+ ei = 2;\r
+ break;\r
+ } else {\r
+ ki = 3;\r
+ }\r
+ /*\r
+ * Recurse down to subtree ki. If it has only one element,\r
+ * we have to do some transformation to start with.\r
+ */\r
+ LOG((" moving to subtree %d\n", ki));\r
+ sub = n->kids[ki];\r
+ if (!sub->elems[1]) {\r
+ LOG((" subtree has only one element!\n", ki));\r
+ if (ki > 0 && n->kids[ki - 1]->elems[1]) {\r
+ /*\r
+ * Case 3a, left-handed variant. Child ki has\r
+ * only one element, but child ki-1 has two or\r
+ * more. So we need to move a subtree from ki-1\r
+ * to ki.\r
+ * \r
+ * . C . . B .\r
+ * / \ -> / \\r
+ * [more] a A b B c d D e [more] a A b c C d D e\r
+ */\r
+ node234 *sib = n->kids[ki - 1];\r
+ int lastelem = (sib->elems[2] ? 2 :\r
+ sib->elems[1] ? 1 : 0);\r
+ sub->kids[2] = sub->kids[1];\r
+ sub->counts[2] = sub->counts[1];\r
+ sub->elems[1] = sub->elems[0];\r
+ sub->kids[1] = sub->kids[0];\r
+ sub->counts[1] = sub->counts[0];\r
+ sub->elems[0] = n->elems[ki - 1];\r
+ sub->kids[0] = sib->kids[lastelem + 1];\r
+ sub->counts[0] = sib->counts[lastelem + 1];\r
+ if (sub->kids[0])\r
+ sub->kids[0]->parent = sub;\r
+ n->elems[ki - 1] = sib->elems[lastelem];\r
+ sib->kids[lastelem + 1] = NULL;\r
+ sib->counts[lastelem + 1] = 0;\r
+ sib->elems[lastelem] = NULL;\r
+ n->counts[ki] = countnode234(sub);\r
+ LOG((" case 3a left\n"));\r
+ LOG(\r
+ (" index and left subtree count before adjustment: %d, %d\n",\r
+ index, n->counts[ki - 1]));\r
+ index += n->counts[ki - 1];\r
+ n->counts[ki - 1] = countnode234(sib);\r
+ index -= n->counts[ki - 1];\r
+ LOG(\r
+ (" index and left subtree count after adjustment: %d, %d\n",\r
+ index, n->counts[ki - 1]));\r
+ } else if (ki < 3 && n->kids[ki + 1]\r
+ && n->kids[ki + 1]->elems[1]) {\r
+ /*\r
+ * Case 3a, right-handed variant. ki has only\r
+ * one element but ki+1 has two or more. Move a\r
+ * subtree from ki+1 to ki.\r
+ * \r
+ * . B . . C .\r
+ * / \ -> / \\r
+ * a A b c C d D e [more] a A b B c d D e [more]\r
+ */\r
+ node234 *sib = n->kids[ki + 1];\r
+ int j;\r
+ sub->elems[1] = n->elems[ki];\r
+ sub->kids[2] = sib->kids[0];\r
+ sub->counts[2] = sib->counts[0];\r
+ if (sub->kids[2])\r
+ sub->kids[2]->parent = sub;\r
+ n->elems[ki] = sib->elems[0];\r
+ sib->kids[0] = sib->kids[1];\r
+ sib->counts[0] = sib->counts[1];\r
+ for (j = 0; j < 2 && sib->elems[j + 1]; j++) {\r
+ sib->kids[j + 1] = sib->kids[j + 2];\r
+ sib->counts[j + 1] = sib->counts[j + 2];\r
+ sib->elems[j] = sib->elems[j + 1];\r
+ }\r
+ sib->kids[j + 1] = NULL;\r
+ sib->counts[j + 1] = 0;\r
+ sib->elems[j] = NULL;\r
+ n->counts[ki] = countnode234(sub);\r
+ n->counts[ki + 1] = countnode234(sib);\r
+ LOG((" case 3a right\n"));\r
+ } else {\r
+ /*\r
+ * Case 3b. ki has only one element, and has no\r
+ * neighbour with more than one. So pick a\r
+ * neighbour and merge it with ki, taking an\r
+ * element down from n to go in the middle.\r
+ *\r
+ * . B . .\r
+ * / \ -> |\r
+ * a A b c C d a A b B c C d\r
+ * \r
+ * (Since at all points we have avoided\r
+ * descending to a node with only one element,\r
+ * we can be sure that n is not reduced to\r
+ * nothingness by this move, _unless_ it was\r
+ * the very first node, ie the root of the\r
+ * tree. In that case we remove the now-empty\r
+ * root and replace it with its single large\r
+ * child as shown.)\r
+ */\r
+ node234 *sib;\r
+ int j;\r
+\r
+ if (ki > 0) {\r
+ ki--;\r
+ index += n->counts[ki] + 1;\r
+ }\r
+ sib = n->kids[ki];\r
+ sub = n->kids[ki + 1];\r
+\r
+ sub->kids[3] = sub->kids[1];\r
+ sub->counts[3] = sub->counts[1];\r
+ sub->elems[2] = sub->elems[0];\r
+ sub->kids[2] = sub->kids[0];\r
+ sub->counts[2] = sub->counts[0];\r
+ sub->elems[1] = n->elems[ki];\r
+ sub->kids[1] = sib->kids[1];\r
+ sub->counts[1] = sib->counts[1];\r
+ if (sub->kids[1])\r
+ sub->kids[1]->parent = sub;\r
+ sub->elems[0] = sib->elems[0];\r
+ sub->kids[0] = sib->kids[0];\r
+ sub->counts[0] = sib->counts[0];\r
+ if (sub->kids[0])\r
+ sub->kids[0]->parent = sub;\r
+\r
+ n->counts[ki + 1] = countnode234(sub);\r
+\r
+ sfree(sib);\r
+\r
+ /*\r
+ * That's built the big node in sub. Now we\r
+ * need to remove the reference to sib in n.\r
+ */\r
+ for (j = ki; j < 3 && n->kids[j + 1]; j++) {\r
+ n->kids[j] = n->kids[j + 1];\r
+ n->counts[j] = n->counts[j + 1];\r
+ n->elems[j] = j < 2 ? n->elems[j + 1] : NULL;\r
+ }\r
+ n->kids[j] = NULL;\r
+ n->counts[j] = 0;\r
+ if (j < 3)\r
+ n->elems[j] = NULL;\r
+ LOG((" case 3b ki=%d\n", ki));\r
+\r
+ if (!n->elems[0]) {\r
+ /*\r
+ * The root is empty and needs to be\r
+ * removed.\r
+ */\r
+ LOG((" shifting root!\n"));\r
+ t->root = sub;\r
+ sub->parent = NULL;\r
+ sfree(n);\r
+ }\r
+ }\r
+ }\r
+ n = sub;\r
+ }\r
+ if (!retval)\r
+ retval = n->elems[ei];\r
+\r
+ if (ei == -1)\r
+ return NULL; /* although this shouldn't happen */\r
+\r
+ /*\r
+ * Treat special case: this is the one remaining item in\r
+ * the tree. n is the tree root (no parent), has one\r
+ * element (no elems[1]), and has no kids (no kids[0]).\r
+ */\r
+ if (!n->parent && !n->elems[1] && !n->kids[0]) {\r
+ LOG((" removed last element in tree\n"));\r
+ sfree(n);\r
+ t->root = NULL;\r
+ return retval;\r
+ }\r
+\r
+ /*\r
+ * Now we have the element we want, as n->elems[ei], and we\r
+ * have also arranged for that element not to be the only\r
+ * one in its node. So...\r
+ */\r
+\r
+ if (!n->kids[0] && n->elems[1]) {\r
+ /*\r
+ * Case 1. n is a leaf node with more than one element,\r
+ * so it's _really easy_. Just delete the thing and\r
+ * we're done.\r
+ */\r
+ int i;\r
+ LOG((" case 1\n"));\r
+ for (i = ei; i < 2 && n->elems[i + 1]; i++)\r
+ n->elems[i] = n->elems[i + 1];\r
+ n->elems[i] = NULL;\r
+ /*\r
+ * Having done that to the leaf node, we now go back up\r
+ * the tree fixing the counts.\r
+ */\r
+ while (n->parent) {\r
+ int childnum;\r
+ childnum = (n->parent->kids[0] == n ? 0 :\r
+ n->parent->kids[1] == n ? 1 :\r
+ n->parent->kids[2] == n ? 2 : 3);\r
+ n->parent->counts[childnum]--;\r
+ n = n->parent;\r
+ }\r
+ return retval; /* finished! */\r
+ } else if (n->kids[ei]->elems[1]) {\r
+ /*\r
+ * Case 2a. n is an internal node, and the root of the\r
+ * subtree to the left of e has more than one element.\r
+ * So find the predecessor p to e (ie the largest node\r
+ * in that subtree), place it where e currently is, and\r
+ * then start the deletion process over again on the\r
+ * subtree with p as target.\r
+ */\r
+ node234 *m = n->kids[ei];\r
+ void *target;\r
+ LOG((" case 2a\n"));\r
+ while (m->kids[0]) {\r
+ m = (m->kids[3] ? m->kids[3] :\r
+ m->kids[2] ? m->kids[2] :\r
+ m->kids[1] ? m->kids[1] : m->kids[0]);\r
+ }\r
+ target = (m->elems[2] ? m->elems[2] :\r
+ m->elems[1] ? m->elems[1] : m->elems[0]);\r
+ n->elems[ei] = target;\r
+ index = n->counts[ei] - 1;\r
+ n = n->kids[ei];\r
+ } else if (n->kids[ei + 1]->elems[1]) {\r
+ /*\r
+ * Case 2b, symmetric to 2a but s/left/right/ and\r
+ * s/predecessor/successor/. (And s/largest/smallest/).\r
+ */\r
+ node234 *m = n->kids[ei + 1];\r
+ void *target;\r
+ LOG((" case 2b\n"));\r
+ while (m->kids[0]) {\r
+ m = m->kids[0];\r
+ }\r
+ target = m->elems[0];\r
+ n->elems[ei] = target;\r
+ n = n->kids[ei + 1];\r
+ index = 0;\r
+ } else {\r
+ /*\r
+ * Case 2c. n is an internal node, and the subtrees to\r
+ * the left and right of e both have only one element.\r
+ * So combine the two subnodes into a single big node\r
+ * with their own elements on the left and right and e\r
+ * in the middle, then restart the deletion process on\r
+ * that subtree, with e still as target.\r
+ */\r
+ node234 *a = n->kids[ei], *b = n->kids[ei + 1];\r
+ int j;\r
+\r
+ LOG((" case 2c\n"));\r
+ a->elems[1] = n->elems[ei];\r
+ a->kids[2] = b->kids[0];\r
+ a->counts[2] = b->counts[0];\r
+ if (a->kids[2])\r
+ a->kids[2]->parent = a;\r
+ a->elems[2] = b->elems[0];\r
+ a->kids[3] = b->kids[1];\r
+ a->counts[3] = b->counts[1];\r
+ if (a->kids[3])\r
+ a->kids[3]->parent = a;\r
+ sfree(b);\r
+ n->counts[ei] = countnode234(a);\r
+ /*\r
+ * That's built the big node in a, and destroyed b. Now\r
+ * remove the reference to b (and e) in n.\r
+ */\r
+ for (j = ei; j < 2 && n->elems[j + 1]; j++) {\r
+ n->elems[j] = n->elems[j + 1];\r
+ n->kids[j + 1] = n->kids[j + 2];\r
+ n->counts[j + 1] = n->counts[j + 2];\r
+ }\r
+ n->elems[j] = NULL;\r
+ n->kids[j + 1] = NULL;\r
+ n->counts[j + 1] = 0;\r
+ /*\r
+ * It's possible, in this case, that we've just removed\r
+ * the only element in the root of the tree. If so,\r
+ * shift the root.\r
+ */\r
+ if (n->elems[0] == NULL) {\r
+ LOG((" shifting root!\n"));\r
+ t->root = a;\r
+ a->parent = NULL;\r
+ sfree(n);\r
+ }\r
+ /*\r
+ * Now go round the deletion process again, with n\r
+ * pointing at the new big node and e still the same.\r
+ */\r
+ n = a;\r
+ index = a->counts[0] + a->counts[1] + 1;\r
+ }\r
+ }\r
+}\r
+void *delpos234(tree234 * t, int index)\r
+{\r
+ if (index < 0 || index >= countnode234(t->root))\r
+ return NULL;\r
+ return delpos234_internal(t, index);\r
+}\r
+void *del234(tree234 * t, void *e)\r
+{\r
+ int index;\r
+ if (!findrelpos234(t, e, NULL, REL234_EQ, &index))\r
+ return NULL; /* it wasn't in there anyway */\r
+ return delpos234_internal(t, index); /* it's there; delete it. */\r
+}\r
+\r
+#ifdef TEST\r
+\r
+/*\r
+ * Test code for the 2-3-4 tree. This code maintains an alternative\r
+ * representation of the data in the tree, in an array (using the\r
+ * obvious and slow insert and delete functions). After each tree\r
+ * operation, the verify() function is called, which ensures all\r
+ * the tree properties are preserved:\r
+ * - node->child->parent always equals node\r
+ * - tree->root->parent always equals NULL\r
+ * - number of kids == 0 or number of elements + 1;\r
+ * - tree has the same depth everywhere\r
+ * - every node has at least one element\r
+ * - subtree element counts are accurate\r
+ * - any NULL kid pointer is accompanied by a zero count\r
+ * - in a sorted tree: ordering property between elements of a\r
+ * node and elements of its children is preserved\r
+ * and also ensures the list represented by the tree is the same\r
+ * list it should be. (This last check also doubly verifies the\r
+ * ordering properties, because the `same list it should be' is by\r
+ * definition correctly ordered. It also ensures all nodes are\r
+ * distinct, because the enum functions would get caught in a loop\r
+ * if not.)\r
+ */\r
+\r
+#include <stdarg.h>\r
+\r
+/*\r
+ * Error reporting function.\r
+ */\r
+void error(char *fmt, ...)\r
+{\r
+ va_list ap;\r
+ printf("ERROR: ");\r
+ va_start(ap, fmt);\r
+ vfprintf(stdout, fmt, ap);\r
+ va_end(ap);\r
+ printf("\n");\r
+}\r
+\r
+/* The array representation of the data. */\r
+void **array;\r
+int arraylen, arraysize;\r
+cmpfn234 cmp;\r
+\r
+/* The tree representation of the same data. */\r
+tree234 *tree;\r
+\r
+typedef struct {\r
+ int treedepth;\r
+ int elemcount;\r
+} chkctx;\r
+\r
+int chknode(chkctx * ctx, int level, node234 * node,\r
+ void *lowbound, void *highbound)\r
+{\r
+ int nkids, nelems;\r
+ int i;\r
+ int count;\r
+\r
+ /* Count the non-NULL kids. */\r
+ for (nkids = 0; nkids < 4 && node->kids[nkids]; nkids++);\r
+ /* Ensure no kids beyond the first NULL are non-NULL. */\r
+ for (i = nkids; i < 4; i++)\r
+ if (node->kids[i]) {\r
+ error("node %p: nkids=%d but kids[%d] non-NULL",\r
+ node, nkids, i);\r
+ } else if (node->counts[i]) {\r
+ error("node %p: kids[%d] NULL but count[%d]=%d nonzero",\r
+ node, i, i, node->counts[i]);\r
+ }\r
+\r
+ /* Count the non-NULL elements. */\r
+ for (nelems = 0; nelems < 3 && node->elems[nelems]; nelems++);\r
+ /* Ensure no elements beyond the first NULL are non-NULL. */\r
+ for (i = nelems; i < 3; i++)\r
+ if (node->elems[i]) {\r
+ error("node %p: nelems=%d but elems[%d] non-NULL",\r
+ node, nelems, i);\r
+ }\r
+\r
+ if (nkids == 0) {\r
+ /*\r
+ * If nkids==0, this is a leaf node; verify that the tree\r
+ * depth is the same everywhere.\r
+ */\r
+ if (ctx->treedepth < 0)\r
+ ctx->treedepth = level; /* we didn't know the depth yet */\r
+ else if (ctx->treedepth != level)\r
+ error("node %p: leaf at depth %d, previously seen depth %d",\r
+ node, level, ctx->treedepth);\r
+ } else {\r
+ /*\r
+ * If nkids != 0, then it should be nelems+1, unless nelems\r
+ * is 0 in which case nkids should also be 0 (and so we\r
+ * shouldn't be in this condition at all).\r
+ */\r
+ int shouldkids = (nelems ? nelems + 1 : 0);\r
+ if (nkids != shouldkids) {\r
+ error("node %p: %d elems should mean %d kids but has %d",\r
+ node, nelems, shouldkids, nkids);\r
+ }\r
+ }\r
+\r
+ /*\r
+ * nelems should be at least 1.\r
+ */\r
+ if (nelems == 0) {\r
+ error("node %p: no elems", node, nkids);\r
+ }\r
+\r
+ /*\r
+ * Add nelems to the running element count of the whole tree.\r
+ */\r
+ ctx->elemcount += nelems;\r
+\r
+ /*\r
+ * Check ordering property: all elements should be strictly >\r
+ * lowbound, strictly < highbound, and strictly < each other in\r
+ * sequence. (lowbound and highbound are NULL at edges of tree\r
+ * - both NULL at root node - and NULL is considered to be <\r
+ * everything and > everything. IYSWIM.)\r
+ */\r
+ if (cmp) {\r
+ for (i = -1; i < nelems; i++) {\r
+ void *lower = (i == -1 ? lowbound : node->elems[i]);\r
+ void *higher =\r
+ (i + 1 == nelems ? highbound : node->elems[i + 1]);\r
+ if (lower && higher && cmp(lower, higher) >= 0) {\r
+ error("node %p: kid comparison [%d=%s,%d=%s] failed",\r
+ node, i, lower, i + 1, higher);\r
+ }\r
+ }\r
+ }\r
+\r
+ /*\r
+ * Check parent pointers: all non-NULL kids should have a\r
+ * parent pointer coming back to this node.\r
+ */\r
+ for (i = 0; i < nkids; i++)\r
+ if (node->kids[i]->parent != node) {\r
+ error("node %p kid %d: parent ptr is %p not %p",\r
+ node, i, node->kids[i]->parent, node);\r
+ }\r
+\r
+\r
+ /*\r
+ * Now (finally!) recurse into subtrees.\r
+ */\r
+ count = nelems;\r
+\r
+ for (i = 0; i < nkids; i++) {\r
+ void *lower = (i == 0 ? lowbound : node->elems[i - 1]);\r
+ void *higher = (i >= nelems ? highbound : node->elems[i]);\r
+ int subcount =\r
+ chknode(ctx, level + 1, node->kids[i], lower, higher);\r
+ if (node->counts[i] != subcount) {\r
+ error("node %p kid %d: count says %d, subtree really has %d",\r
+ node, i, node->counts[i], subcount);\r
+ }\r
+ count += subcount;\r
+ }\r
+\r
+ return count;\r
+}\r
+\r
+void verify(void)\r
+{\r
+ chkctx ctx;\r
+ int i;\r
+ void *p;\r
+\r
+ ctx.treedepth = -1; /* depth unknown yet */\r
+ ctx.elemcount = 0; /* no elements seen yet */\r
+ /*\r
+ * Verify validity of tree properties.\r
+ */\r
+ if (tree->root) {\r
+ if (tree->root->parent != NULL)\r
+ error("root->parent is %p should be null", tree->root->parent);\r
+ chknode(&ctx, 0, tree->root, NULL, NULL);\r
+ }\r
+ printf("tree depth: %d\n", ctx.treedepth);\r
+ /*\r
+ * Enumerate the tree and ensure it matches up to the array.\r
+ */\r
+ for (i = 0; NULL != (p = index234(tree, i)); i++) {\r
+ if (i >= arraylen)\r
+ error("tree contains more than %d elements", arraylen);\r
+ if (array[i] != p)\r
+ error("enum at position %d: array says %s, tree says %s",\r
+ i, array[i], p);\r
+ }\r
+ if (ctx.elemcount != i) {\r
+ error("tree really contains %d elements, enum gave %d",\r
+ ctx.elemcount, i);\r
+ }\r
+ if (i < arraylen) {\r
+ error("enum gave only %d elements, array has %d", i, arraylen);\r
+ }\r
+ i = count234(tree);\r
+ if (ctx.elemcount != i) {\r
+ error("tree really contains %d elements, count234 gave %d",\r
+ ctx.elemcount, i);\r
+ }\r
+}\r
+\r
+void internal_addtest(void *elem, int index, void *realret)\r
+{\r
+ int i, j;\r
+ void *retval;\r
+\r
+ if (arraysize < arraylen + 1) {\r
+ arraysize = arraylen + 1 + 256;\r
+ array = sresize(array, arraysize, void *);\r
+ }\r
+\r
+ i = index;\r
+ /* now i points to the first element >= elem */\r
+ retval = elem; /* expect elem returned (success) */\r
+ for (j = arraylen; j > i; j--)\r
+ array[j] = array[j - 1];\r
+ array[i] = elem; /* add elem to array */\r
+ arraylen++;\r
+\r
+ if (realret != retval) {\r
+ error("add: retval was %p expected %p", realret, retval);\r
+ }\r
+\r
+ verify();\r
+}\r
+\r
+void addtest(void *elem)\r
+{\r
+ int i;\r
+ void *realret;\r
+\r
+ realret = add234(tree, elem);\r
+\r
+ i = 0;\r
+ while (i < arraylen && cmp(elem, array[i]) > 0)\r
+ i++;\r
+ if (i < arraylen && !cmp(elem, array[i])) {\r
+ void *retval = array[i]; /* expect that returned not elem */\r
+ if (realret != retval) {\r
+ error("add: retval was %p expected %p", realret, retval);\r
+ }\r
+ } else\r
+ internal_addtest(elem, i, realret);\r
+}\r
+\r
+void addpostest(void *elem, int i)\r
+{\r
+ void *realret;\r
+\r
+ realret = addpos234(tree, elem, i);\r
+\r
+ internal_addtest(elem, i, realret);\r
+}\r
+\r
+void delpostest(int i)\r
+{\r
+ int index = i;\r
+ void *elem = array[i], *ret;\r
+\r
+ /* i points to the right element */\r
+ while (i < arraylen - 1) {\r
+ array[i] = array[i + 1];\r
+ i++;\r
+ }\r
+ arraylen--; /* delete elem from array */\r
+\r
+ if (tree->cmp)\r
+ ret = del234(tree, elem);\r
+ else\r
+ ret = delpos234(tree, index);\r
+\r
+ if (ret != elem) {\r
+ error("del returned %p, expected %p", ret, elem);\r
+ }\r
+\r
+ verify();\r
+}\r
+\r
+void deltest(void *elem)\r
+{\r
+ int i;\r
+\r
+ i = 0;\r
+ while (i < arraylen && cmp(elem, array[i]) > 0)\r
+ i++;\r
+ if (i >= arraylen || cmp(elem, array[i]) != 0)\r
+ return; /* don't do it! */\r
+ delpostest(i);\r
+}\r
+\r
+/* A sample data set and test utility. Designed for pseudo-randomness,\r
+ * and yet repeatability. */\r
+\r
+/*\r
+ * This random number generator uses the `portable implementation'\r
+ * given in ANSI C99 draft N869. It assumes `unsigned' is 32 bits;\r
+ * change it if not.\r
+ */\r
+int randomnumber(unsigned *seed)\r
+{\r
+ *seed *= 1103515245;\r
+ *seed += 12345;\r
+ return ((*seed) / 65536) % 32768;\r
+}\r
+\r
+int mycmp(void *av, void *bv)\r
+{\r
+ char const *a = (char const *) av;\r
+ char const *b = (char const *) bv;\r
+ return strcmp(a, b);\r
+}\r
+\r
+#define lenof(x) ( sizeof((x)) / sizeof(*(x)) )\r
+\r
+char *strings[] = {\r
+ "a", "ab", "absque", "coram", "de",\r
+ "palam", "clam", "cum", "ex", "e",\r
+ "sine", "tenus", "pro", "prae",\r
+ "banana", "carrot", "cabbage", "broccoli", "onion", "zebra",\r
+ "penguin", "blancmange", "pangolin", "whale", "hedgehog",\r
+ "giraffe", "peanut", "bungee", "foo", "bar", "baz", "quux",\r
+ "murfl", "spoo", "breen", "flarn", "octothorpe",\r
+ "snail", "tiger", "elephant", "octopus", "warthog", "armadillo",\r
+ "aardvark", "wyvern", "dragon", "elf", "dwarf", "orc", "goblin",\r
+ "pixie", "basilisk", "warg", "ape", "lizard", "newt", "shopkeeper",\r
+ "wand", "ring", "amulet"\r
+};\r
+\r
+#define NSTR lenof(strings)\r
+\r
+int findtest(void)\r
+{\r
+ const static int rels[] = {\r
+ REL234_EQ, REL234_GE, REL234_LE, REL234_LT, REL234_GT\r
+ };\r
+ const static char *const relnames[] = {\r
+ "EQ", "GE", "LE", "LT", "GT"\r
+ };\r
+ int i, j, rel, index;\r
+ char *p, *ret, *realret, *realret2;\r
+ int lo, hi, mid, c;\r
+\r
+ for (i = 0; i < NSTR; i++) {\r
+ p = strings[i];\r
+ for (j = 0; j < sizeof(rels) / sizeof(*rels); j++) {\r
+ rel = rels[j];\r
+\r
+ lo = 0;\r
+ hi = arraylen - 1;\r
+ while (lo <= hi) {\r
+ mid = (lo + hi) / 2;\r
+ c = strcmp(p, array[mid]);\r
+ if (c < 0)\r
+ hi = mid - 1;\r
+ else if (c > 0)\r
+ lo = mid + 1;\r
+ else\r
+ break;\r
+ }\r
+\r
+ if (c == 0) {\r
+ if (rel == REL234_LT)\r
+ ret = (mid > 0 ? array[--mid] : NULL);\r
+ else if (rel == REL234_GT)\r
+ ret = (mid < arraylen - 1 ? array[++mid] : NULL);\r
+ else\r
+ ret = array[mid];\r
+ } else {\r
+ assert(lo == hi + 1);\r
+ if (rel == REL234_LT || rel == REL234_LE) {\r
+ mid = hi;\r
+ ret = (hi >= 0 ? array[hi] : NULL);\r
+ } else if (rel == REL234_GT || rel == REL234_GE) {\r
+ mid = lo;\r
+ ret = (lo < arraylen ? array[lo] : NULL);\r
+ } else\r
+ ret = NULL;\r
+ }\r
+\r
+ realret = findrelpos234(tree, p, NULL, rel, &index);\r
+ if (realret != ret) {\r
+ error("find(\"%s\",%s) gave %s should be %s",\r
+ p, relnames[j], realret, ret);\r
+ }\r
+ if (realret && index != mid) {\r
+ error("find(\"%s\",%s) gave %d should be %d",\r
+ p, relnames[j], index, mid);\r
+ }\r
+ if (realret && rel == REL234_EQ) {\r
+ realret2 = index234(tree, index);\r
+ if (realret2 != realret) {\r
+ error("find(\"%s\",%s) gave %s(%d) but %d -> %s",\r
+ p, relnames[j], realret, index, index, realret2);\r
+ }\r
+ }\r
+#if 0\r
+ printf("find(\"%s\",%s) gave %s(%d)\n", p, relnames[j],\r
+ realret, index);\r
+#endif\r
+ }\r
+ }\r
+\r
+ realret = findrelpos234(tree, NULL, NULL, REL234_GT, &index);\r
+ if (arraylen && (realret != array[0] || index != 0)) {\r
+ error("find(NULL,GT) gave %s(%d) should be %s(0)",\r
+ realret, index, array[0]);\r
+ } else if (!arraylen && (realret != NULL)) {\r
+ error("find(NULL,GT) gave %s(%d) should be NULL", realret, index);\r
+ }\r
+\r
+ realret = findrelpos234(tree, NULL, NULL, REL234_LT, &index);\r
+ if (arraylen\r
+ && (realret != array[arraylen - 1] || index != arraylen - 1)) {\r
+ error("find(NULL,LT) gave %s(%d) should be %s(0)", realret, index,\r
+ array[arraylen - 1]);\r
+ } else if (!arraylen && (realret != NULL)) {\r
+ error("find(NULL,LT) gave %s(%d) should be NULL", realret, index);\r
+ }\r
+}\r
+\r
+int main(void)\r
+{\r
+ int in[NSTR];\r
+ int i, j, k;\r
+ unsigned seed = 0;\r
+\r
+ for (i = 0; i < NSTR; i++)\r
+ in[i] = 0;\r
+ array = NULL;\r
+ arraylen = arraysize = 0;\r
+ tree = newtree234(mycmp);\r
+ cmp = mycmp;\r
+\r
+ verify();\r
+ for (i = 0; i < 10000; i++) {\r
+ j = randomnumber(&seed);\r
+ j %= NSTR;\r
+ printf("trial: %d\n", i);\r
+ if (in[j]) {\r
+ printf("deleting %s (%d)\n", strings[j], j);\r
+ deltest(strings[j]);\r
+ in[j] = 0;\r
+ } else {\r
+ printf("adding %s (%d)\n", strings[j], j);\r
+ addtest(strings[j]);\r
+ in[j] = 1;\r
+ }\r
+ findtest();\r
+ }\r
+\r
+ while (arraylen > 0) {\r
+ j = randomnumber(&seed);\r
+ j %= arraylen;\r
+ deltest(array[j]);\r
+ }\r
+\r
+ freetree234(tree);\r
+\r
+ /*\r
+ * Now try an unsorted tree. We don't really need to test\r
+ * delpos234 because we know del234 is based on it, so it's\r
+ * already been tested in the above sorted-tree code; but for\r
+ * completeness we'll use it to tear down our unsorted tree\r
+ * once we've built it.\r
+ */\r
+ tree = newtree234(NULL);\r
+ cmp = NULL;\r
+ verify();\r
+ for (i = 0; i < 1000; i++) {\r
+ printf("trial: %d\n", i);\r
+ j = randomnumber(&seed);\r
+ j %= NSTR;\r
+ k = randomnumber(&seed);\r
+ k %= count234(tree) + 1;\r
+ printf("adding string %s at index %d\n", strings[j], k);\r
+ addpostest(strings[j], k);\r
+ }\r
+ while (count234(tree) > 0) {\r
+ printf("cleanup: tree size %d\n", count234(tree));\r
+ j = randomnumber(&seed);\r
+ j %= count234(tree);\r
+ printf("deleting string %s from index %d\n", array[j], j);\r
+ delpostest(j);\r
+ }\r
+\r
+ return 0;\r
+}\r
+\r
+#endif\r