Merge branch 'en/sparse-checkout-set'
[git/debian.git] / cbtree.c
blob336e46dbba5a06e7d9b5fdb5dd6f3c7bdd971186
1 /*
2 * crit-bit tree implementation, does no allocations internally
3 * For more information on crit-bit trees: https://cr.yp.to/critbit.html
4 * Based on Adam Langley's adaptation of Dan Bernstein's public domain code
5 * git clone https://github.com/agl/critbit.git
6 */
7 #include "cbtree.h"
9 static struct cb_node *cb_node_of(const void *p)
11 return (struct cb_node *)((uintptr_t)p - 1);
14 /* locate the best match, does not do a final comparision */
15 static struct cb_node *cb_internal_best_match(struct cb_node *p,
16 const uint8_t *k, size_t klen)
18 while (1 & (uintptr_t)p) {
19 struct cb_node *q = cb_node_of(p);
20 uint8_t c = q->byte < klen ? k[q->byte] : 0;
21 size_t direction = (1 + (q->otherbits | c)) >> 8;
23 p = q->child[direction];
25 return p;
28 /* returns NULL if successful, existing cb_node if duplicate */
29 struct cb_node *cb_insert(struct cb_tree *t, struct cb_node *node, size_t klen)
31 size_t newbyte, newotherbits;
32 uint8_t c;
33 int newdirection;
34 struct cb_node **wherep, *p;
36 assert(!((uintptr_t)node & 1)); /* allocations must be aligned */
38 if (!t->root) { /* insert into empty tree */
39 t->root = node;
40 return NULL; /* success */
43 /* see if a node already exists */
44 p = cb_internal_best_match(t->root, node->k, klen);
46 /* find first differing byte */
47 for (newbyte = 0; newbyte < klen; newbyte++) {
48 if (p->k[newbyte] != node->k[newbyte])
49 goto different_byte_found;
51 return p; /* element exists, let user deal with it */
53 different_byte_found:
54 newotherbits = p->k[newbyte] ^ node->k[newbyte];
55 newotherbits |= newotherbits >> 1;
56 newotherbits |= newotherbits >> 2;
57 newotherbits |= newotherbits >> 4;
58 newotherbits = (newotherbits & ~(newotherbits >> 1)) ^ 255;
59 c = p->k[newbyte];
60 newdirection = (1 + (newotherbits | c)) >> 8;
62 node->byte = newbyte;
63 node->otherbits = newotherbits;
64 node->child[1 - newdirection] = node;
66 /* find a place to insert it */
67 wherep = &t->root;
68 for (;;) {
69 struct cb_node *q;
70 size_t direction;
72 p = *wherep;
73 if (!(1 & (uintptr_t)p))
74 break;
75 q = cb_node_of(p);
76 if (q->byte > newbyte)
77 break;
78 if (q->byte == newbyte && q->otherbits > newotherbits)
79 break;
80 c = q->byte < klen ? node->k[q->byte] : 0;
81 direction = (1 + (q->otherbits | c)) >> 8;
82 wherep = q->child + direction;
85 node->child[newdirection] = *wherep;
86 *wherep = (struct cb_node *)(1 + (uintptr_t)node);
88 return NULL; /* success */
91 struct cb_node *cb_lookup(struct cb_tree *t, const uint8_t *k, size_t klen)
93 struct cb_node *p = cb_internal_best_match(t->root, k, klen);
95 return p && !memcmp(p->k, k, klen) ? p : NULL;
98 static enum cb_next cb_descend(struct cb_node *p, cb_iter fn, void *arg)
100 if (1 & (uintptr_t)p) {
101 struct cb_node *q = cb_node_of(p);
102 enum cb_next n = cb_descend(q->child[0], fn, arg);
104 return n == CB_BREAK ? n : cb_descend(q->child[1], fn, arg);
105 } else {
106 return fn(p, arg);
110 void cb_each(struct cb_tree *t, const uint8_t *kpfx, size_t klen,
111 cb_iter fn, void *arg)
113 struct cb_node *p = t->root;
114 struct cb_node *top = p;
115 size_t i = 0;
117 if (!p) return; /* empty tree */
119 /* Walk tree, maintaining top pointer */
120 while (1 & (uintptr_t)p) {
121 struct cb_node *q = cb_node_of(p);
122 uint8_t c = q->byte < klen ? kpfx[q->byte] : 0;
123 size_t direction = (1 + (q->otherbits | c)) >> 8;
125 p = q->child[direction];
126 if (q->byte < klen)
127 top = p;
130 for (i = 0; i < klen; i++) {
131 if (p->k[i] != kpfx[i])
132 return; /* "best" match failed */
134 cb_descend(top, fn, arg);