Line | Count | Source |
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 "git-compat-util.h" |
8 | | #include "cbtree.h" |
9 | | |
10 | | static inline uint8_t *cb_node_key(struct cb_tree *t, struct cb_node *node) |
11 | 0 | { |
12 | 0 | return (uint8_t *) node + t->key_offset; |
13 | 0 | } |
14 | | |
15 | | static struct cb_node *cb_node_of(const void *p) |
16 | 0 | { |
17 | 0 | return (struct cb_node *)((uintptr_t)p - 1); |
18 | 0 | } |
19 | | |
20 | | /* locate the best match, does not do a final comparison */ |
21 | | static struct cb_node *cb_internal_best_match(struct cb_node *p, |
22 | | const uint8_t *k, size_t klen) |
23 | 0 | { |
24 | 0 | while (1 & (uintptr_t)p) { |
25 | 0 | struct cb_node *q = cb_node_of(p); |
26 | 0 | uint8_t c = q->byte < klen ? k[q->byte] : 0; |
27 | 0 | size_t direction = (1 + (q->otherbits | c)) >> 8; |
28 | |
|
29 | 0 | p = q->child[direction]; |
30 | 0 | } |
31 | 0 | return p; |
32 | 0 | } |
33 | | |
34 | | /* returns NULL if successful, existing cb_node if duplicate */ |
35 | | struct cb_node *cb_insert(struct cb_tree *t, struct cb_node *node, size_t klen) |
36 | 0 | { |
37 | 0 | size_t newbyte, newotherbits; |
38 | 0 | uint8_t c; |
39 | 0 | int newdirection; |
40 | 0 | struct cb_node **wherep, *p; |
41 | 0 | uint8_t *node_key, *p_key; |
42 | |
|
43 | 0 | assert(!((uintptr_t)node & 1)); /* allocations must be aligned */ |
44 | |
|
45 | 0 | if (!t->root) { /* insert into empty tree */ |
46 | 0 | t->root = node; |
47 | 0 | return NULL; /* success */ |
48 | 0 | } |
49 | | |
50 | 0 | node_key = cb_node_key(t, node); |
51 | | |
52 | | /* see if a node already exists */ |
53 | 0 | p = cb_internal_best_match(t->root, node_key, klen); |
54 | 0 | p_key = cb_node_key(t, p); |
55 | | |
56 | | /* find first differing byte */ |
57 | 0 | for (newbyte = 0; newbyte < klen; newbyte++) { |
58 | 0 | if (p_key[newbyte] != node_key[newbyte]) |
59 | 0 | goto different_byte_found; |
60 | 0 | } |
61 | 0 | return p; /* element exists, let user deal with it */ |
62 | | |
63 | 0 | different_byte_found: |
64 | 0 | newotherbits = p_key[newbyte] ^ node_key[newbyte]; |
65 | 0 | newotherbits |= newotherbits >> 1; |
66 | 0 | newotherbits |= newotherbits >> 2; |
67 | 0 | newotherbits |= newotherbits >> 4; |
68 | 0 | newotherbits = (newotherbits & ~(newotherbits >> 1)) ^ 255; |
69 | 0 | c = p_key[newbyte]; |
70 | 0 | newdirection = (1 + (newotherbits | c)) >> 8; |
71 | |
|
72 | 0 | node->byte = newbyte; |
73 | 0 | node->otherbits = newotherbits; |
74 | 0 | node->child[1 - newdirection] = node; |
75 | | |
76 | | /* find a place to insert it */ |
77 | 0 | wherep = &t->root; |
78 | 0 | for (;;) { |
79 | 0 | struct cb_node *q; |
80 | 0 | size_t direction; |
81 | |
|
82 | 0 | p = *wherep; |
83 | 0 | if (!(1 & (uintptr_t)p)) |
84 | 0 | break; |
85 | 0 | q = cb_node_of(p); |
86 | 0 | if (q->byte > newbyte) |
87 | 0 | break; |
88 | 0 | if (q->byte == newbyte && q->otherbits > newotherbits) |
89 | 0 | break; |
90 | 0 | c = q->byte < klen ? node_key[q->byte] : 0; |
91 | 0 | direction = (1 + (q->otherbits | c)) >> 8; |
92 | 0 | wherep = q->child + direction; |
93 | 0 | } |
94 | |
|
95 | 0 | node->child[newdirection] = *wherep; |
96 | 0 | *wherep = (struct cb_node *)(1 + (uintptr_t)node); |
97 | |
|
98 | 0 | return NULL; /* success */ |
99 | 0 | } |
100 | | |
101 | | struct cb_node *cb_lookup(struct cb_tree *t, const uint8_t *k, size_t klen) |
102 | 0 | { |
103 | 0 | struct cb_node *p = cb_internal_best_match(t->root, k, klen); |
104 | |
|
105 | 0 | return p && !memcmp(cb_node_key(t, p), k, klen) ? p : NULL; |
106 | 0 | } |
107 | | |
108 | | static int cb_descend(struct cb_node *p, cb_iter fn, void *arg) |
109 | 0 | { |
110 | 0 | if (1 & (uintptr_t)p) { |
111 | 0 | struct cb_node *q = cb_node_of(p); |
112 | 0 | int ret = cb_descend(q->child[0], fn, arg); |
113 | 0 | if (ret) |
114 | 0 | return ret; |
115 | 0 | return cb_descend(q->child[1], fn, arg); |
116 | 0 | } else { |
117 | 0 | return fn(p, arg); |
118 | 0 | } |
119 | 0 | } |
120 | | |
121 | | int cb_each(struct cb_tree *t, const uint8_t *kpfx, size_t klen, |
122 | | cb_iter fn, void *arg) |
123 | 0 | { |
124 | 0 | struct cb_node *p = t->root; |
125 | 0 | struct cb_node *top = p; |
126 | 0 | size_t i = 0; |
127 | 0 | uint8_t *p_key; |
128 | |
|
129 | 0 | if (!p) |
130 | 0 | return 0; /* empty tree */ |
131 | | |
132 | | /* Walk tree, maintaining top pointer */ |
133 | 0 | while (1 & (uintptr_t)p) { |
134 | 0 | struct cb_node *q = cb_node_of(p); |
135 | 0 | uint8_t c = q->byte < klen ? kpfx[q->byte] : 0; |
136 | 0 | size_t direction = (1 + (q->otherbits | c)) >> 8; |
137 | |
|
138 | 0 | p = q->child[direction]; |
139 | 0 | if (q->byte < klen) |
140 | 0 | top = p; |
141 | 0 | } |
142 | |
|
143 | 0 | p_key = cb_node_key(t, p); |
144 | 0 | for (i = 0; i < klen; i++) { |
145 | 0 | if (p_key[i] != kpfx[i]) |
146 | 0 | return 0; /* "best" match failed */ |
147 | 0 | } |
148 | | |
149 | 0 | return cb_descend(top, fn, arg); |
150 | 0 | } |