Branch data Line data Source code
1 : : // Copyright 2011-2025 David Robillard <d@drobilla.net>
2 : : // SPDX-License-Identifier: ISC
3 : :
4 : : #include <zix/btree.h>
5 : :
6 : : #include <zix/allocator.h>
7 : : #include <zix/attributes.h>
8 : : #include <zix/status.h>
9 : :
10 : : #include <assert.h>
11 : : #include <stdint.h>
12 : : #include <string.h>
13 : :
14 : : // #define ZIX_BTREE_SORTED_CHECK 1
15 : :
16 : : // Define ZixShort as an integer type half the size of a pointer
17 : : #if UINTPTR_MAX > UINT32_MAX // 64-bit
18 : : typedef uint32_t ZixShort;
19 : : #else // 32-bit
20 : : typedef uint16_t ZixShort;
21 : : #endif
22 : :
23 : : #ifndef ZIX_BTREE_PAGE_SIZE
24 : : # define ZIX_BTREE_PAGE_SIZE 4096U
25 : : #endif
26 : :
27 : : #define ZIX_BTREE_NODE_SPACE (ZIX_BTREE_PAGE_SIZE - (2U * sizeof(ZixShort)))
28 : : #define ZIX_BTREE_LEAF_VALS ((ZIX_BTREE_NODE_SPACE / sizeof(void*)) - 1U)
29 : : #define ZIX_BTREE_INODE_VALS (ZIX_BTREE_LEAF_VALS / 2U)
30 : :
31 : : struct ZixBTreeImpl {
32 : : ZixAllocator* allocator;
33 : : ZixBTreeNode* root;
34 : : ZixBTreeCompareFunc cmp;
35 : : const void* cmp_data;
36 : : size_t size;
37 : : };
38 : :
39 : : struct ZixBTreeNodeImpl {
40 : : ZixShort is_leaf;
41 : : ZixShort n_vals;
42 : :
43 : : union {
44 : : struct {
45 : : void* vals[ZIX_BTREE_LEAF_VALS];
46 : : } leaf;
47 : :
48 : : struct {
49 : : void* vals[ZIX_BTREE_INODE_VALS];
50 : : ZixBTreeNode* children[ZIX_BTREE_INODE_VALS + 1U];
51 : : } inode;
52 : : } data;
53 : : };
54 : :
55 : : #if ((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
56 : : (defined(__cplusplus) && __cplusplus >= 201103L))
57 : : static_assert(sizeof(ZixBTree) <= ZIX_BTREE_PAGE_SIZE, "");
58 : : static_assert(sizeof(ZixBTreeNode) <= ZIX_BTREE_PAGE_SIZE, "");
59 : : static_assert(sizeof(ZixBTreeNode) >=
60 : : ZIX_BTREE_PAGE_SIZE - 2U * sizeof(ZixBTreeNode*),
61 : : "");
62 : : #endif
63 : :
64 : : static ZixBTreeNode*
65 : 6976 : zix_btree_node_new(ZixAllocator* const allocator, const bool leaf)
66 : : {
67 : : #if !((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
68 : : (defined(__cplusplus) && __cplusplus >= 201103L))
69 : : assert(sizeof(ZixBTreeNode) <= ZIX_BTREE_PAGE_SIZE);
70 : : assert(sizeof(ZixBTreeNode) >=
71 : : ZIX_BTREE_PAGE_SIZE - (2U * sizeof(ZixBTreeNode*)));
72 : : #endif
73 : :
74 : 6976 : ZixBTreeNode* const node = (ZixBTreeNode*)zix_aligned_alloc(
75 : : allocator, ZIX_BTREE_PAGE_SIZE, ZIX_BTREE_PAGE_SIZE);
76 : :
77 [ + + ]: 6976 : if (node) {
78 : 6906 : node->is_leaf = leaf;
79 : 6906 : node->n_vals = 0U;
80 : : }
81 : :
82 : 6976 : return node;
83 : : }
84 : :
85 : : ZIX_PURE_FUNC static ZixBTreeNode*
86 : 8518638 : zix_btree_child(const ZixBTreeNode* const node, const unsigned i)
87 : : {
88 [ - + ]: 8518638 : assert(!node->is_leaf);
89 [ - + ]: 8518638 : assert(i <= ZIX_BTREE_INODE_VALS);
90 : 8518638 : return node->data.inode.children[i];
91 : : }
92 : :
93 : : ZixBTree*
94 : 96 : zix_btree_new(ZixAllocator* const allocator,
95 : : const ZixBTreeCompareFunc cmp,
96 : : const void* const cmp_data)
97 : : {
98 : : #if !((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
99 : : (defined(__cplusplus) && __cplusplus >= 201103L))
100 : : assert(sizeof(ZixBTree) <= ZIX_BTREE_PAGE_SIZE);
101 : : #endif
102 : :
103 [ - + ]: 96 : assert(cmp);
104 : :
105 : 96 : ZixBTree* const t = (ZixBTree*)zix_malloc(allocator, sizeof(ZixBTree));
106 [ + + ]: 96 : if (t) {
107 : 94 : t->allocator = allocator;
108 : 94 : t->root = NULL;
109 : 94 : t->cmp = cmp;
110 : 94 : t->cmp_data = cmp_data;
111 : 94 : t->size = 0U;
112 : : }
113 : :
114 : 96 : return t;
115 : : }
116 : :
117 : : static void
118 : 1340 : zix_btree_free_children(ZixBTree* const t,
119 : : ZixBTreeNode* const n,
120 : : const ZixBTreeDestroyFunc destroy,
121 : : const void* const destroy_user_data)
122 : : {
123 [ + + ]: 1340 : if (!n->is_leaf) {
124 [ + + ]: 1327 : for (ZixShort i = 0U; i < n->n_vals + 1U; ++i) {
125 : 1250 : zix_btree_free_children(
126 : : t, zix_btree_child(n, i), destroy, destroy_user_data);
127 : 1250 : zix_aligned_free(t->allocator, zix_btree_child(n, i));
128 : : }
129 : : }
130 : :
131 [ + + ]: 1340 : if (destroy) {
132 [ + + ]: 20 : if (n->is_leaf) {
133 [ + + ]: 4100 : for (ZixShort i = 0U; i < n->n_vals; ++i) {
134 : 4084 : destroy(n->data.leaf.vals[i], destroy_user_data);
135 : : }
136 : : } else {
137 [ + + ]: 16 : for (ZixShort i = 0U; i < n->n_vals; ++i) {
138 : 12 : destroy(n->data.inode.vals[i], destroy_user_data);
139 : : }
140 : : }
141 : : }
142 : 1340 : }
143 : :
144 : : void
145 : 96 : zix_btree_free(ZixBTree* const t,
146 : : const ZixBTreeDestroyFunc destroy,
147 : : const void* const destroy_user_data)
148 : : {
149 [ + + ]: 96 : if (t) {
150 : 94 : zix_btree_clear(t, destroy, destroy_user_data);
151 : 94 : zix_aligned_free(t->allocator, t->root);
152 : 94 : zix_free(t->allocator, t);
153 : : }
154 : 96 : }
155 : :
156 : : void
157 : 100 : zix_btree_clear(ZixBTree* const t,
158 : : const ZixBTreeDestroyFunc destroy,
159 : : const void* const destroy_user_data)
160 : : {
161 [ + + ]: 100 : if (t->root) {
162 : 90 : zix_btree_free_children(t, t->root, destroy, destroy_user_data);
163 : 90 : zix_aligned_free(t->allocator, t->root);
164 : 90 : t->root = NULL;
165 : : }
166 : :
167 : 100 : t->size = 0U;
168 : 100 : }
169 : :
170 : : ZIX_REALTIME size_t
171 : 57935 : zix_btree_size(const ZixBTree* const t)
172 : : {
173 [ - + ]: 57935 : assert(t);
174 : 57935 : return t->size;
175 : : }
176 : :
177 : : static ZixShort
178 : 23761892 : zix_btree_max_vals(const ZixBTreeNode* const node)
179 : : {
180 [ + + ]: 23761892 : return node->is_leaf ? ZIX_BTREE_LEAF_VALS : ZIX_BTREE_INODE_VALS;
181 : : }
182 : :
183 : : static ZixShort
184 : 10647284 : zix_btree_min_vals(const ZixBTreeNode* const node)
185 : : {
186 : 10647284 : return (ZixShort)(((zix_btree_max_vals(node) + 1U) / 2U) - 1U);
187 : : }
188 : :
189 : : /// Shift pointers in `array` of length `n` right starting at `i`
190 : : static void
191 : 2584123 : zix_btree_ainsert(void** const array,
192 : : const unsigned n,
193 : : const unsigned i,
194 : : void* const e)
195 : : {
196 : 2584123 : memmove(array + i + 1U, array + i, ((size_t)n - i) * sizeof(e));
197 : 2584123 : array[i] = e;
198 : 2584123 : }
199 : :
200 : : /// Erase element `i` in `array` of length `n` and return erased element
201 : : static void*
202 : 2078999 : zix_btree_aerase(void** const array, const unsigned n, const unsigned i)
203 : : {
204 : 2078999 : void* const ret = array[i];
205 : 2078999 : memmove(array + i, array + i + 1U, ((size_t)n - i) * sizeof(ret));
206 : 2078999 : return ret;
207 : : }
208 : :
209 : : /// Split lhs, the i'th child of `n`, into two nodes
210 : : static ZixBTreeNode*
211 : 6755 : zix_btree_split_child(ZixAllocator* const allocator,
212 : : ZixBTreeNode* const n,
213 : : const unsigned i,
214 : : ZixBTreeNode* const lhs)
215 : : {
216 [ - + ]: 6755 : assert(lhs->n_vals == zix_btree_max_vals(lhs));
217 [ - + ]: 6755 : assert(n->n_vals < ZIX_BTREE_INODE_VALS);
218 [ - + ]: 6755 : assert(i < n->n_vals + 1U);
219 [ - + ]: 6755 : assert(zix_btree_child(n, i) == lhs);
220 : :
221 : 6755 : const unsigned max_n_vals = zix_btree_max_vals(lhs);
222 : 6755 : ZixBTreeNode* rhs = zix_btree_node_new(allocator, lhs->is_leaf);
223 [ + + ]: 6755 : if (!rhs) {
224 : 64 : return NULL;
225 : : }
226 : :
227 : : // LHS and RHS get roughly half, less the middle value which moves up
228 : 6691 : lhs->n_vals /= 2U;
229 : 6691 : rhs->n_vals = (ZixShort)(max_n_vals - lhs->n_vals - 1U);
230 : :
231 [ + + ]: 6691 : if (lhs->is_leaf) {
232 : : // Copy large half from LHS to new RHS node
233 : 6659 : memcpy(rhs->data.leaf.vals,
234 : 6659 : lhs->data.leaf.vals + lhs->n_vals + 1U,
235 : 6659 : rhs->n_vals * sizeof(void*));
236 : :
237 : : // Move middle value up to parent
238 : 6659 : zix_btree_ainsert(
239 : 6659 : n->data.inode.vals, n->n_vals, i, lhs->data.leaf.vals[lhs->n_vals]);
240 : : } else {
241 : : // Copy large half from LHS to new RHS node
242 : 32 : memcpy(rhs->data.inode.vals,
243 : 32 : lhs->data.inode.vals + lhs->n_vals + 1U,
244 : 32 : rhs->n_vals * sizeof(void*));
245 : 32 : memcpy(rhs->data.inode.children,
246 : 32 : lhs->data.inode.children + lhs->n_vals + 1U,
247 : 32 : ((size_t)rhs->n_vals + 1U) * sizeof(ZixBTreeNode*));
248 : :
249 : : // Move middle value up to parent
250 : 32 : zix_btree_ainsert(
251 : 32 : n->data.inode.vals, n->n_vals, i, lhs->data.inode.vals[lhs->n_vals]);
252 : : }
253 : :
254 : : // Insert new RHS node in parent at position i
255 : 6691 : zix_btree_ainsert((void**)n->data.inode.children, ++n->n_vals, i + 1U, rhs);
256 : :
257 : 6691 : return rhs;
258 : : }
259 : :
260 : : #ifdef ZIX_BTREE_SORTED_CHECK
261 : : /// Check that `n` is sorted with respect to search key `e`
262 : : static bool
263 : : zix_btree_node_is_sorted_with_respect_to(const ZixCompareFunc compare,
264 : : const void* const compare_user_data,
265 : : void* const* const values,
266 : : const unsigned n_values,
267 : : const void* const key)
268 : : {
269 : : if (n_values <= 1U) {
270 : : return true;
271 : : }
272 : :
273 : : int cmp = compare(values[0U], key, compare_user_data);
274 : : for (unsigned i = 1U; i < n_values; ++i) {
275 : : const int next_cmp = compare(values[i], key, compare_user_data);
276 : : if ((cmp >= 0 && next_cmp < 0) || (cmp > 0 && next_cmp <= 0)) {
277 : : return false;
278 : : }
279 : :
280 : : cmp = next_cmp;
281 : : }
282 : :
283 : : return true;
284 : : }
285 : : #endif
286 : :
287 : : static unsigned
288 : 13579788 : zix_btree_find_value(const ZixBTreeCompareFunc compare,
289 : : const void* const compare_user_data,
290 : : void* const* const values,
291 : : const unsigned n_values,
292 : : const void* const key,
293 : : bool* const equal)
294 : : {
295 : 13579788 : unsigned first = 0U;
296 : 13579788 : unsigned count = n_values;
297 : :
298 [ + + ]: 95704370 : while (count > 0U) {
299 : 84717270 : const unsigned half = count >> 1U;
300 : 84717270 : const unsigned i = first + half;
301 : 84717270 : void* const value = values[i];
302 : 84717270 : const int cmp = compare(value, key, compare_user_data);
303 : :
304 [ + + ]: 84717270 : if (!cmp) {
305 : 2592688 : *equal = true;
306 : 2592688 : return i;
307 : : }
308 : :
309 [ + + ]: 82124582 : if (cmp < 0) {
310 : 46148759 : first += half + 1U;
311 : 46148759 : count -= half + 1U;
312 : : } else {
313 : 35975823 : count = half;
314 : : }
315 : : }
316 : :
317 [ + + - + ]: 10987100 : assert(first == n_values || compare(values[first], key, compare_user_data));
318 : 10987100 : *equal = false;
319 : 10987100 : return first;
320 : : }
321 : :
322 : : static unsigned
323 : 802858 : zix_btree_find_pattern(const ZixBTreeCompareFunc compare_key,
324 : : const void* const compare_key_user_data,
325 : : void* const* const values,
326 : : const unsigned n_values,
327 : : const void* const key,
328 : : bool* const equal)
329 : : {
330 : : #ifdef ZIX_BTREE_SORTED_CHECK
331 : : assert(zix_btree_node_is_sorted_with_respect_to(
332 : : compare_key, compare_key_user_data, values, n_values, key));
333 : : #endif
334 : :
335 : 802858 : unsigned first = 0U;
336 : 802858 : unsigned count = n_values;
337 : :
338 [ + + ]: 5632668 : while (count > 0U) {
339 : 4829810 : const unsigned half = count >> 1U;
340 : 4829810 : const unsigned i = first + half;
341 : 4829810 : void* const value = values[i];
342 : 4829810 : const int cmp = compare_key(value, key, compare_key_user_data);
343 : :
344 [ + + ]: 4829810 : if (cmp == 0) {
345 : : // Found a match, but keep searching for the leftmost one
346 : 335925 : *equal = true;
347 : 335925 : count = half;
348 : :
349 [ + + ]: 4493885 : } else if (cmp < 0) {
350 : : // Search right half
351 : 2380963 : first += half + 1U;
352 : 2380963 : count -= half + 1U;
353 : :
354 : : } else {
355 : : // Search left half
356 : 2112922 : count = half;
357 : : }
358 : : }
359 : :
360 [ + + + - : 802858 : assert(!*equal ||
+ + - + ]
361 : : (compare_key(values[first], key, compare_key_user_data) == 0 &&
362 : : (first == 0U ||
363 : : (compare_key(values[first - 1U], key, compare_key_user_data) < 0))));
364 : :
365 : 802858 : return first;
366 : : }
367 : :
368 : : /// Convenience wrapper to find a value in an internal node
369 : : static unsigned
370 : 8199198 : zix_btree_inode_find(const ZixBTree* const t,
371 : : const ZixBTreeNode* const n,
372 : : const void* const e,
373 : : bool* const equal)
374 : : {
375 [ - + ]: 8199198 : assert(!n->is_leaf);
376 : :
377 : 16398396 : return zix_btree_find_value(
378 : 8199198 : t->cmp, t->cmp_data, n->data.inode.vals, n->n_vals, e, equal);
379 : : }
380 : :
381 : : /// Convenience wrapper to find a value in a leaf node
382 : : static unsigned
383 : 5380590 : zix_btree_leaf_find(const ZixBTree* const t,
384 : : const ZixBTreeNode* const n,
385 : : const void* const e,
386 : : bool* const equal)
387 : : {
388 [ - + ]: 5380590 : assert(n->is_leaf);
389 : :
390 : 10761180 : return zix_btree_find_value(
391 : 5380590 : t->cmp, t->cmp_data, n->data.leaf.vals, n->n_vals, e, equal);
392 : : }
393 : :
394 : : ZIX_PURE_FUNC static inline bool
395 : 5323642 : zix_btree_can_remove_from(const ZixBTreeNode* const n)
396 : : {
397 [ - + ]: 5323642 : assert(n->n_vals >= zix_btree_min_vals(n));
398 : 5323642 : return n->n_vals > zix_btree_min_vals(n);
399 : : }
400 : :
401 : : ZIX_PURE_FUNC static inline bool
402 : 6547791 : zix_btree_is_full(const ZixBTreeNode* const n)
403 : : {
404 [ - + ]: 6547791 : assert(n->n_vals <= zix_btree_max_vals(n));
405 : 6547791 : return n->n_vals == zix_btree_max_vals(n);
406 : : }
407 : :
408 : : static ZixStatus
409 : 129 : zix_btree_grow_up(ZixBTree* const t)
410 : : {
411 : 129 : ZixBTreeNode* const new_root = zix_btree_node_new(t->allocator, false);
412 [ + + ]: 129 : if (!new_root) {
413 : 4 : return ZIX_STATUS_NO_MEM;
414 : : }
415 : :
416 : : // Set old root as the only child of the new root
417 : 125 : new_root->data.inode.children[0U] = t->root;
418 : :
419 : : // Split the old root to get two balanced siblings
420 : 125 : zix_btree_split_child(t->allocator, new_root, 0U, t->root);
421 : 125 : t->root = new_root;
422 : :
423 : 125 : return ZIX_STATUS_SUCCESS;
424 : : }
425 : :
426 : : ZixStatus
427 : 2615138 : zix_btree_insert(ZixBTree* const t, void* const e)
428 : : {
429 [ - + ]: 2615138 : assert(t);
430 : :
431 [ + + ]: 2615138 : if (!t->root) {
432 : : // Empty tree, create a new leaf root
433 : 92 : t->root = zix_btree_node_new(t->allocator, true);
434 [ + + ]: 92 : if (!t->root) {
435 : 2 : return ZIX_STATUS_NO_MEM;
436 : : }
437 : : }
438 : :
439 [ + + ]: 2615136 : if (zix_btree_is_full(t->root)) {
440 : : // Grow up if necessary to ensure the root is not full
441 : 129 : const ZixStatus st = zix_btree_grow_up(t);
442 [ + + ]: 129 : if (st) {
443 : 4 : return st;
444 : : }
445 : : }
446 : :
447 : : // Walk down from the root until we reach a suitable leaf
448 : 2615132 : ZixBTreeNode* node = t->root;
449 [ + + ]: 6547725 : while (!node->is_leaf) {
450 : : // Search for the value in this node
451 : 3933842 : bool equal = false;
452 : 3933842 : const unsigned i = zix_btree_inode_find(t, node, e, &equal);
453 [ + + ]: 3933842 : if (equal) {
454 : 1249 : return ZIX_STATUS_EXISTS;
455 : : }
456 : :
457 : : // Value not in this node, but may be in the ith child
458 : 3932655 : ZixBTreeNode* child = node->data.inode.children[i];
459 [ + + ]: 3932655 : if (zix_btree_is_full(child)) {
460 : : // The child is full, split it before continuing
461 : : ZixBTreeNode* const rhs =
462 : 6630 : zix_btree_split_child(t->allocator, node, i, child);
463 : :
464 [ + + ]: 6630 : if (!rhs) {
465 : 60 : return ZIX_STATUS_NO_MEM;
466 : : }
467 : :
468 : : // Compare with new split value to determine which side to use
469 : 6570 : const int cmp = t->cmp(node->data.inode.vals[i], e, t->cmp_data);
470 [ + + ]: 6570 : if (cmp < 0) {
471 : 5534 : child = rhs; // Split value is less than the new value, move right
472 [ + + ]: 1036 : } else if (cmp == 0) {
473 : 2 : return ZIX_STATUS_EXISTS; // Split value is exactly the value to insert
474 : : }
475 : : }
476 : :
477 : : // Descend to child node and continue
478 : 3932593 : node = child;
479 : : }
480 : :
481 : : // Search for the value in the leaf
482 : 2613883 : bool equal = false;
483 : 2613883 : const unsigned i = zix_btree_leaf_find(t, node, e, &equal);
484 [ + + ]: 2613883 : if (equal) {
485 : 334697 : return ZIX_STATUS_EXISTS;
486 : : }
487 : :
488 : : // The value is not in the tree, insert into the leaf
489 : 2279186 : zix_btree_ainsert(node->data.leaf.vals, node->n_vals++, i, e);
490 : 2279186 : ++t->size;
491 : 2279186 : return ZIX_STATUS_SUCCESS;
492 : : }
493 : :
494 : : static void
495 : 7274426 : zix_btree_iter_set_frame(ZixBTreeIter* const ti,
496 : : ZixBTreeNode* const n,
497 : : const unsigned i)
498 : : {
499 [ - + ]: 7274426 : assert(i <= UINT16_MAX);
500 : 7274426 : ti->nodes[ti->level] = n;
501 : 7274426 : ti->indexes[ti->level] = (uint16_t)i;
502 : 7274426 : }
503 : :
504 : : static void
505 : 1888 : zix_btree_iter_push(ZixBTreeIter* const ti,
506 : : ZixBTreeNode* const n,
507 : : const ZixShort i)
508 : : {
509 [ - + ]: 1888 : assert(ti->level < ZIX_BTREE_MAX_HEIGHT - 1U);
510 : 1888 : ++ti->level;
511 : 1888 : ti->nodes[ti->level] = n;
512 : 1888 : ti->indexes[ti->level] = (uint16_t)i;
513 : 1888 : }
514 : :
515 : : static void
516 : 70067 : zix_btree_iter_pop(ZixBTreeIter* const ti)
517 : : {
518 [ - + ]: 70067 : assert(ti->level > 0U);
519 : 70067 : ti->nodes[ti->level] = NULL;
520 : 70067 : ti->indexes[ti->level] = 0U;
521 : 70067 : --ti->level;
522 : 70067 : }
523 : :
524 : : /// Enlarge left child by stealing a value from its right sibling
525 : : static ZixBTreeNode*
526 : 149813 : zix_btree_rotate_left(ZixBTreeNode* const parent, const unsigned i)
527 : : {
528 : 149813 : ZixBTreeNode* const lhs = zix_btree_child(parent, i);
529 : 149813 : ZixBTreeNode* const rhs = zix_btree_child(parent, i + 1U);
530 : :
531 [ - + ]: 149813 : assert(lhs->is_leaf == rhs->is_leaf);
532 : :
533 [ + + ]: 149813 : if (lhs->is_leaf) {
534 : : // Move parent value to end of LHS
535 : 149665 : lhs->data.leaf.vals[lhs->n_vals++] = parent->data.inode.vals[i];
536 : :
537 : : // Move first value in RHS to parent
538 : 149665 : parent->data.inode.vals[i] =
539 : 149665 : zix_btree_aerase(rhs->data.leaf.vals, rhs->n_vals, 0U);
540 : : } else {
541 : : // Move parent value to end of LHS
542 : 148 : lhs->data.inode.vals[lhs->n_vals++] = parent->data.inode.vals[i];
543 : :
544 : : // Move first value in RHS to parent
545 : 148 : parent->data.inode.vals[i] =
546 : 148 : zix_btree_aerase(rhs->data.inode.vals, rhs->n_vals, 0U);
547 : :
548 : : // Move first child pointer from RHS to end of LHS
549 : 148 : lhs->data.inode.children[lhs->n_vals] = (ZixBTreeNode*)zix_btree_aerase(
550 : 148 : (void**)rhs->data.inode.children, rhs->n_vals, 0U);
551 : : }
552 : :
553 : 149813 : --rhs->n_vals;
554 : :
555 : 149813 : return lhs;
556 : : }
557 : :
558 : : /// Enlarge a child by stealing a value from its left sibling
559 : : static ZixBTreeNode*
560 : 290853 : zix_btree_rotate_right(ZixBTreeNode* const parent, const unsigned i)
561 : : {
562 : 290853 : ZixBTreeNode* const lhs = zix_btree_child(parent, i - 1U);
563 : 290853 : ZixBTreeNode* const rhs = zix_btree_child(parent, i);
564 : :
565 [ - + ]: 290853 : assert(lhs->is_leaf == rhs->is_leaf);
566 : :
567 [ + + ]: 290853 : if (lhs->is_leaf) {
568 : : // Prepend parent value to RHS
569 : 290151 : zix_btree_ainsert(
570 : 290151 : rhs->data.leaf.vals, rhs->n_vals++, 0U, parent->data.inode.vals[i - 1U]);
571 : :
572 : : // Move last value from LHS to parent
573 : 290151 : parent->data.inode.vals[i - 1U] = lhs->data.leaf.vals[--lhs->n_vals];
574 : : } else {
575 : : // Prepend parent value to RHS
576 : 702 : zix_btree_ainsert(
577 : 702 : rhs->data.inode.vals, rhs->n_vals++, 0U, parent->data.inode.vals[i - 1U]);
578 : :
579 : : // Move last child pointer from LHS and prepend to RHS
580 : 702 : zix_btree_ainsert((void**)rhs->data.inode.children,
581 : : rhs->n_vals,
582 : : 0U,
583 : 702 : lhs->data.inode.children[lhs->n_vals]);
584 : :
585 : : // Move last value from LHS to parent
586 : 702 : parent->data.inode.vals[i - 1U] = lhs->data.inode.vals[--lhs->n_vals];
587 : : }
588 : :
589 : 290853 : return rhs;
590 : : }
591 : :
592 : : /// Move n[i] down, merge the left and right child, return the merged node
593 : : static ZixBTreeNode*
594 : 5516 : zix_btree_merge(ZixBTree* const t, ZixBTreeNode* const n, const unsigned i)
595 : : {
596 : 5516 : ZixBTreeNode* const lhs = zix_btree_child(n, i);
597 : 5516 : ZixBTreeNode* const rhs = zix_btree_child(n, i + 1U);
598 : :
599 [ - + ]: 5516 : assert(lhs->is_leaf == rhs->is_leaf);
600 [ - + ]: 5516 : assert(lhs->n_vals + rhs->n_vals < zix_btree_max_vals(lhs));
601 : :
602 : : // Move parent value to end of LHS
603 [ + + ]: 5516 : if (lhs->is_leaf) {
604 : 5486 : lhs->data.leaf.vals[lhs->n_vals++] =
605 : 5486 : zix_btree_aerase(n->data.inode.vals, n->n_vals, i);
606 : : } else {
607 : 30 : lhs->data.inode.vals[lhs->n_vals++] =
608 : 30 : zix_btree_aerase(n->data.inode.vals, n->n_vals, i);
609 : : }
610 : :
611 : : // Erase corresponding child pointer (to RHS) in parent
612 : 5516 : zix_btree_aerase((void**)n->data.inode.children, n->n_vals, i + 1U);
613 : :
614 : : // Add everything from RHS to end of LHS
615 [ + + ]: 5516 : if (lhs->is_leaf) {
616 : 5486 : memcpy(lhs->data.leaf.vals + lhs->n_vals,
617 : 5486 : rhs->data.leaf.vals,
618 : 5486 : rhs->n_vals * sizeof(void*));
619 : : } else {
620 : 30 : memcpy(lhs->data.inode.vals + lhs->n_vals,
621 : 30 : rhs->data.inode.vals,
622 : 30 : rhs->n_vals * sizeof(void*));
623 : 30 : memcpy(lhs->data.inode.children + lhs->n_vals,
624 : 30 : rhs->data.inode.children,
625 : 30 : ((size_t)rhs->n_vals + 1U) * sizeof(void*));
626 : : }
627 : :
628 : 5516 : lhs->n_vals += rhs->n_vals;
629 [ + + ]: 5516 : if (--n->n_vals == 0U) {
630 : : // Root is now empty, replace it with its only child
631 [ - + ]: 50 : assert(n == t->root);
632 : 50 : t->root = lhs;
633 : 50 : zix_aligned_free(t->allocator, n);
634 : : }
635 : :
636 : 5516 : zix_aligned_free(t->allocator, rhs);
637 : 5516 : return lhs;
638 : : }
639 : :
640 : : /// Remove and return the min value from the subtree rooted at `n`
641 : : static void*
642 : 46573 : zix_btree_remove_min(ZixBTree* const t, ZixBTreeNode* n)
643 : : {
644 [ - + ]: 46573 : assert(zix_btree_can_remove_from(n));
645 : :
646 [ + + ]: 46805 : while (!n->is_leaf) {
647 : 232 : ZixBTreeNode* const* const children = n->data.inode.children;
648 : :
649 : 232 : n = zix_btree_can_remove_from(children[0U]) ? children[0U]
650 [ + + ]: 244 : : zix_btree_can_remove_from(children[1U]) ? zix_btree_rotate_left(n, 0U)
651 [ + + ]: 12 : : zix_btree_merge(t, n, 0U);
652 : : }
653 : :
654 : 46573 : return zix_btree_aerase(n->data.leaf.vals, --n->n_vals, 0U);
655 : : }
656 : :
657 : : /// Remove and return the max value from the subtree rooted at `n`
658 : : static void*
659 : 2390 : zix_btree_remove_max(ZixBTree* const t, ZixBTreeNode* n)
660 : : {
661 [ - + ]: 2390 : assert(zix_btree_can_remove_from(n));
662 : :
663 [ + + ]: 2414 : while (!n->is_leaf) {
664 : 24 : ZixBTreeNode* const* const children = n->data.inode.children;
665 : :
666 : 24 : const unsigned y = n->n_vals - 1U;
667 : 24 : const unsigned z = n->n_vals;
668 : :
669 : 24 : n = zix_btree_can_remove_from(children[z]) ? children[z]
670 [ + + ]: 34 : : zix_btree_can_remove_from(children[y]) ? zix_btree_rotate_right(n, z)
671 [ + + ]: 10 : : zix_btree_merge(t, n, y);
672 : : }
673 : :
674 : 2390 : return n->data.leaf.vals[--n->n_vals];
675 : : }
676 : :
677 : : static ZixBTreeNode*
678 : 445626 : zix_btree_fatten_child(ZixBTree* const t, ZixBTreeIter* const iter)
679 : : {
680 : 445626 : ZixBTreeNode* const n = iter->nodes[iter->level];
681 : 445626 : const ZixShort i = iter->indexes[iter->level];
682 : :
683 [ - + ]: 445626 : assert(n);
684 [ - + ]: 445626 : assert(!n->is_leaf);
685 [ - + ]: 445626 : assert(n->n_vals);
686 : 445626 : ZixBTreeNode* const* const children = n->data.inode.children;
687 : :
688 [ + + + + ]: 445626 : if (i > 0U && zix_btree_can_remove_from(children[i - 1U])) {
689 : 290845 : return zix_btree_rotate_right(n, i); // Steal a key from left sibling
690 : : }
691 : :
692 [ + + + + ]: 154781 : if (i < n->n_vals && zix_btree_can_remove_from(children[i + 1U])) {
693 : 149803 : return zix_btree_rotate_left(n, i); // Steal a key from right sibling
694 : : }
695 : :
696 : : // Both child's siblings are minimal, merge them
697 : :
698 [ + + ]: 4978 : if (i == n->n_vals) {
699 : 339 : --iter->indexes[iter->level];
700 : 339 : return zix_btree_merge(t, n, i - 1U); // Merge last two children
701 : : }
702 : :
703 : 4639 : return zix_btree_merge(t, n, i); // Merge left and right siblings
704 : : }
705 : :
706 : : /// Replace the ith value in `n` with one from a child if possible
707 : : static ZixStatus
708 : 49447 : zix_btree_replace_value(ZixBTree* const t,
709 : : ZixBTreeNode* const n,
710 : : const unsigned i,
711 : : void** const out)
712 : : {
713 : 49447 : ZixBTreeNode* const lhs = zix_btree_child(n, i);
714 : 49447 : ZixBTreeNode* const rhs = zix_btree_child(n, i + 1U);
715 [ + + + + ]: 49447 : if (!zix_btree_can_remove_from(lhs) && !zix_btree_can_remove_from(rhs)) {
716 : 484 : return ZIX_STATUS_NOT_FOUND;
717 : : }
718 : :
719 : : // Stash the value for the caller before it is replaced
720 : 48963 : *out = n->data.inode.vals[i];
721 : :
722 : 48963 : n->data.inode.vals[i] =
723 : : // Left child has more values, steal its largest
724 : 2010 : (lhs->n_vals > rhs->n_vals) ? zix_btree_remove_max(t, lhs)
725 : :
726 : : // Right child has more values, steal its smallest
727 [ + + ]: 95916 : : (rhs->n_vals > lhs->n_vals) ? zix_btree_remove_min(t, rhs)
728 : :
729 : : // Children are balanced, use index parity as a low-bias tie breaker
730 [ + + ]: 47583 : : (i & 1U) ? zix_btree_remove_max(t, lhs)
731 [ + + ]: 630 : : zix_btree_remove_min(t, rhs);
732 : :
733 : 48963 : return ZIX_STATUS_SUCCESS;
734 : : }
735 : :
736 : : ZixStatus
737 : 2145051 : zix_btree_remove(ZixBTree* const t,
738 : : const void* const e,
739 : : void** const out,
740 : : ZixBTreeIter* const next)
741 : : {
742 [ - + ]: 2145051 : assert(t);
743 [ - + ]: 2145051 : assert(out);
744 : :
745 [ + + ]: 2145051 : if (!t->root) {
746 : 2 : return ZIX_STATUS_NOT_FOUND;
747 : : }
748 : :
749 : 2145049 : ZixBTreeNode* n = t->root;
750 : 2145049 : ZixBTreeIter* ti = next;
751 : 2145049 : ZixStatus st = ZIX_STATUS_SUCCESS;
752 : :
753 : 2145049 : *ti = zix_btree_end_iter;
754 : :
755 : : /* To remove in a single walk down, the tree is adjusted along the way so
756 : : that the current node always has at least one more value than the
757 : : minimum. This ensures that there is always room to remove, without
758 : : having to merge nodes again on a traversal back up. */
759 : :
760 [ + + + + ]: 2145049 : if (!n->is_leaf && n->n_vals == 1U &&
761 [ + + ]: 148955 : !zix_btree_can_remove_from(n->data.inode.children[0U]) &&
762 [ + + ]: 1148 : !zix_btree_can_remove_from(n->data.inode.children[1U])) {
763 : : // Root has only two children, both minimal, merge them into a new root
764 : 50 : n = zix_btree_merge(t, n, 0U);
765 : : }
766 : :
767 [ + + ]: 5427517 : while (!n->is_leaf) {
768 [ + + - + ]: 3331431 : assert(n == t->root || zix_btree_can_remove_from(n));
769 : :
770 : : // Search for the value in the current node and update the iterator
771 : 3331431 : bool equal = false;
772 : 3331431 : const unsigned i = zix_btree_inode_find(t, n, e, &equal);
773 : :
774 : 3331431 : zix_btree_iter_set_frame(ti, n, i);
775 : :
776 [ + + ]: 3331431 : if (equal) {
777 : : // Found in internal node
778 [ + + ]: 49447 : if (!(st = zix_btree_replace_value(t, n, i, out))) {
779 : : // Replaced hole with a value from a direct child
780 : 48963 : --t->size;
781 : 48963 : return st;
782 : : }
783 : :
784 : : // Both preceding and succeeding child are minimal, merge and continue
785 : 484 : n = zix_btree_merge(t, n, i);
786 : :
787 : : } else {
788 : : // Not found in internal node, is in the ith child if anywhere
789 : 3281984 : n = zix_btree_can_remove_from(zix_btree_child(n, i))
790 : 2836358 : ? zix_btree_child(n, i)
791 [ + + ]: 3281984 : : zix_btree_fatten_child(t, ti);
792 : : }
793 : :
794 : 3282468 : ++ti->level;
795 : : }
796 : :
797 : : // We're at the leaf the value may be in, search for the value in it
798 : 2096086 : bool equal = false;
799 : 2096086 : const unsigned i = zix_btree_leaf_find(t, n, e, &equal);
800 : :
801 [ + + ]: 2096086 : if (!equal) { // Not found in tree
802 : 224653 : *ti = zix_btree_end_iter;
803 : 224653 : return ZIX_STATUS_NOT_FOUND;
804 : : }
805 : :
806 : : // Erase from leaf node
807 : 1871433 : *out = zix_btree_aerase(n->data.leaf.vals, --n->n_vals, i);
808 : :
809 : : // Update next iterator
810 [ + + ]: 1871433 : if (n->n_vals == 0U) {
811 : : // Removed the last element in the tree
812 [ - + ]: 50 : assert(n == t->root);
813 [ - + ]: 50 : assert(t->size == 1U);
814 : 50 : *ti = zix_btree_end_iter;
815 [ + + ]: 1871383 : } else if (i == n->n_vals) {
816 : : // Removed the largest element in this leaf, increment to the next
817 : 68696 : zix_btree_iter_set_frame(ti, n, i - 1U);
818 : 68696 : zix_btree_iter_increment(ti);
819 : : } else {
820 : 1802687 : zix_btree_iter_set_frame(ti, n, i);
821 : : }
822 : :
823 : 1871433 : --t->size;
824 : 1871433 : return ZIX_STATUS_SUCCESS;
825 : : }
826 : :
827 : : ZixStatus
828 : 671810 : zix_btree_find(const ZixBTree* const t,
829 : : const void* const e,
830 : : ZixBTreeIter* const ti)
831 : : {
832 [ - + ]: 671810 : assert(t);
833 [ - + ]: 671810 : assert(ti);
834 : :
835 : 671810 : ZixBTreeNode* n = t->root;
836 : :
837 : 671810 : *ti = zix_btree_end_iter;
838 [ + + ]: 671810 : if (!n) {
839 : 2 : return ZIX_STATUS_NOT_FOUND;
840 : : }
841 : :
842 [ + + ]: 1604546 : while (!n->is_leaf) {
843 : 933925 : bool equal = false;
844 : 933925 : const unsigned i = zix_btree_inode_find(t, n, e, &equal);
845 : :
846 : 933925 : zix_btree_iter_set_frame(ti, n, i);
847 : :
848 [ + + ]: 933925 : if (equal) {
849 : 1187 : return ZIX_STATUS_SUCCESS;
850 : : }
851 : :
852 : 932738 : ++ti->level;
853 : 932738 : n = zix_btree_child(n, i);
854 : : }
855 : :
856 : 670621 : bool equal = false;
857 : 670621 : const unsigned i = zix_btree_leaf_find(t, n, e, &equal);
858 [ + + ]: 670621 : if (equal) {
859 : 334737 : zix_btree_iter_set_frame(ti, n, i);
860 : 334737 : return ZIX_STATUS_SUCCESS;
861 : : }
862 : :
863 : 335884 : *ti = zix_btree_end_iter;
864 : 335884 : return ZIX_STATUS_NOT_FOUND;
865 : : }
866 : :
867 : : ZixStatus
868 : 335902 : zix_btree_lower_bound(const ZixBTree* const t,
869 : : const ZixBTreeCompareFunc compare_key,
870 : : const void* const compare_key_user_data,
871 : : const void* const key,
872 : : ZixBTreeIter* const ti)
873 : : {
874 [ - + ]: 335902 : assert(t);
875 [ - + ]: 335902 : assert(ti);
876 : :
877 : 335902 : *ti = zix_btree_end_iter;
878 : :
879 : 335902 : ZixBTreeNode* n = t->root; // Current node
880 : 335902 : uint16_t found_level = 0U; // Lowest level a match was found at
881 : 335902 : bool found = false; // True if a match was ever found
882 [ + + ]: 335902 : if (!n) {
883 : 2 : return ZIX_STATUS_SUCCESS;
884 : : }
885 : :
886 : : // Search down until we reach a leaf
887 [ + + ]: 802858 : while (!n->is_leaf) {
888 : 466958 : bool equal = false;
889 : :
890 : 466958 : const unsigned i = zix_btree_find_pattern(compare_key,
891 : : compare_key_user_data,
892 : 466958 : n->data.inode.vals,
893 : : n->n_vals,
894 : : key,
895 : : &equal);
896 : :
897 : 466958 : zix_btree_iter_set_frame(ti, n, i);
898 [ + + ]: 466958 : if (equal) {
899 : 1193 : found_level = ti->level;
900 : 1193 : found = true;
901 : : }
902 : :
903 : 466958 : ++ti->level;
904 : 466958 : n = zix_btree_child(n, i);
905 : : }
906 : :
907 : 335900 : bool equal = false;
908 : :
909 : 335900 : const unsigned i = zix_btree_find_pattern(compare_key,
910 : : compare_key_user_data,
911 : 335900 : n->data.leaf.vals,
912 : : n->n_vals,
913 : : key,
914 : : &equal);
915 : :
916 : 335900 : zix_btree_iter_set_frame(ti, n, i);
917 [ + + ]: 335900 : if (equal) {
918 : 334705 : return ZIX_STATUS_SUCCESS;
919 : : }
920 : :
921 [ + - ]: 1195 : if (ti->indexes[ti->level] == ti->nodes[ti->level]->n_vals) {
922 [ + + ]: 1195 : if (found) {
923 : : // Found on a previous level but went too far
924 : 1187 : ti->level = found_level;
925 : : } else {
926 : : // Reached end (key is greater than everything in tree)
927 : 8 : *ti = zix_btree_end_iter;
928 : : }
929 : : }
930 : :
931 : 1195 : return ZIX_STATUS_SUCCESS;
932 : : }
933 : :
934 : : ZIX_REALTIME void*
935 : 1800677 : zix_btree_get(const ZixBTreeIter ti)
936 : : {
937 : 1800677 : const ZixBTreeNode* const node = ti.nodes[ti.level];
938 : 1800677 : const unsigned index = ti.indexes[ti.level];
939 : :
940 [ - + ]: 1800677 : assert(node);
941 [ - + ]: 1800677 : assert(index < node->n_vals);
942 : :
943 : 1800677 : return node->is_leaf ? node->data.leaf.vals[index]
944 [ + + ]: 1800677 : : node->data.inode.vals[index];
945 : : }
946 : :
947 : : ZIX_NONBLOCKING ZixBTreeIter
948 : 166 : zix_btree_begin(const ZixBTree* const t)
949 : : {
950 [ - + ]: 166 : assert(t);
951 : :
952 : 166 : ZixBTreeIter iter = zix_btree_end_iter;
953 : :
954 [ + + ]: 166 : if (t->size > 0U) {
955 : 92 : ZixBTreeNode* n = t->root;
956 : 92 : zix_btree_iter_set_frame(&iter, n, 0U);
957 : :
958 [ + + ]: 179 : while (!n->is_leaf) {
959 : 87 : n = zix_btree_child(n, 0U);
960 : 87 : zix_btree_iter_push(&iter, n, 0U);
961 : : }
962 : : }
963 : :
964 : 166 : return iter;
965 : : }
966 : :
967 : : ZIX_REALTIME ZixBTreeIter
968 : 122 : zix_btree_end(const ZixBTree* const t)
969 : : {
970 : : (void)t;
971 : :
972 : 122 : return zix_btree_end_iter;
973 : : }
974 : :
975 : : ZIX_REALTIME bool
976 : 24690 : zix_btree_iter_equals(const ZixBTreeIter lhs, const ZixBTreeIter rhs)
977 : : {
978 : 24690 : const size_t indexes_size = ((size_t)lhs.level + 1U) * sizeof(uint16_t);
979 : :
980 [ + + + + ]: 32928 : return (lhs.level == rhs.level) && (lhs.nodes[0U] == rhs.nodes[0U]) &&
981 [ + + + + ]: 8238 : (!lhs.nodes[0U] || !memcmp(lhs.indexes, rhs.indexes, indexes_size));
982 : : }
983 : :
984 : : ZIX_NONBLOCKING ZixStatus
985 : 582065 : zix_btree_iter_increment(ZixBTreeIter* const i)
986 : : {
987 [ - + ]: 582065 : assert(i);
988 [ - + ]: 582065 : assert(!zix_btree_iter_is_end(*i));
989 : :
990 : : // Move to the next value in the current node
991 : 582065 : const uint16_t index = ++i->indexes[i->level];
992 : :
993 [ + + ]: 582065 : if (i->nodes[i->level]->is_leaf) {
994 : : // Leaf, move up if necessary until we're not at the end of the node
995 [ + + ]: 650334 : while (i->indexes[i->level] >= i->nodes[i->level]->n_vals) {
996 [ + + ]: 135709 : if (i->level == 0U) {
997 : : // End of root, end of tree
998 : 65642 : i->nodes[0U] = NULL;
999 : 65642 : return ZIX_STATUS_REACHED_END;
1000 : : }
1001 : :
1002 : : // At end of internal node, move up
1003 : 70067 : zix_btree_iter_pop(i);
1004 : : }
1005 : :
1006 : : } else {
1007 : : // Internal node, move down to next child
1008 : 1798 : const ZixBTreeNode* const node = i->nodes[i->level];
1009 : 1798 : ZixBTreeNode* const child = node->data.inode.children[index];
1010 : :
1011 : 1798 : zix_btree_iter_push(i, child, 0U);
1012 : :
1013 : : // Move down and left until we hit a leaf
1014 [ + + ]: 1801 : while (!i->nodes[i->level]->is_leaf) {
1015 : 3 : zix_btree_iter_push(i, i->nodes[i->level]->data.inode.children[0U], 0U);
1016 : : }
1017 : : }
1018 : :
1019 : 516423 : return ZIX_STATUS_SUCCESS;
1020 : : }
1021 : :
1022 : : ZIX_NONBLOCKING ZixBTreeIter
1023 : 8188 : zix_btree_iter_next(const ZixBTreeIter iter)
1024 : : {
1025 : 8188 : ZixBTreeIter next = iter;
1026 : :
1027 : 8188 : zix_btree_iter_increment(&next);
1028 : :
1029 : 8188 : return next;
1030 : : }
|