include/hamt/hamt_map.h

filter: Statementstmt 100% 113 / 113 Branchbran 98.7% 80 / 82 (1 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 99.4% 193 / 195 (1 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/hamt/hamt_algebra.h ↑ Summary blib/lib/HAMT.pm »
1 #ifndef HAMT_MAP_H
2 #define HAMT_MAP_H
3  
4 /* Lookup, insert and delete on a trie of hamt_node.
5 *
6 * Everything here works on a ROOT, and an empty map is a NULL root. An
7 * update copies the nodes on one path from the root to where the key lives
8 * and shares every other node with the trie it started from, which it never
9 * touches: the caller still holds the old root, and it is exactly as it was.
10 *
11 * THE CANONICAL SHAPE. Below the root, a node exists only where two or more
12 * entries share the path to it. Insert cannot break that. Delete can - it
13 * can leave a node holding one entry and nothing else - so on the way back
14 * up, a child that has come down to a single entry is folded into its
15 * parent as that entry. The fold can repeat all the way to the root.
16 *
17 * It follows that two maps holding the same keys have the same shape,
18 * whatever order they were built in and whatever was deleted on the way. */
19  
20 #ifndef HAMT_MAP_FWD
21 #define HAMT_MAP_FWD
22 typedef struct hamt_map hamt_map;
23 #endif
24 struct hamt_map {
25 hamt_node *root;
26 size_t count;
27 };
28  
29 #define HAMT_DEL_OOM (-1)
30 #define HAMT_DEL_ABSENT 0
31 #define HAMT_DEL_DONE 1
32  
33 /* The entry for a key in the subtree under `n`, which hangs at `depth`:
34 * borrowed, or NULL. Allocates nothing and changes no reference count. */
35 1371081 static hamt_entry *hamt_node_find(const hamt_node *n, unsigned depth,
36 const char *key, size_t klen, int utf8,
37 uint64_t hash) {
38  
39 9387305 ▶ while (n) {
Branch: 1 branch
BranchTrueFalseTotal
while (n) { 9386014 1291 9387305
40 uint32_t bit, i;
41  
42 9386014 ▶ if (n->ncoll) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->ncoll) { 576523 8809491 9386014
43 2889113 ▶ for (i = 0; i < n->ncoll; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n->ncoll; i++) { 2873826 15287 2889113
44 2873826 hamt_entry *e = (hamt_entry *)n->slot[i];
45 2873826 ▶ if (hamt_entry_key_eq(e, key, klen, utf8, hash)) return e;
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_entry_key_eq(e, key, klen, utf8, hash)) return e; 561236 2312590 2873826
46 }
47 15287 return NULL;
48 }
49  
50 8809491 bit = HAMT_BIT(hash, depth);
51 8809491 ▶ if (n->datamap & bit) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->datamap & bit) { 765098 8044393 8809491
52 765098 hamt_entry *e = (hamt_entry *)
53 765098 n->slot[hamt_popcnt32(n->datamap & (bit - 1))];
54 765098 ▶ return hamt_entry_key_eq(e, key, klen, utf8, hash) ? e : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return hamt_entry_key_eq(e, key, klen, utf8, hash) ? e : NULL; 758951 6147 765098
55 }
56 8044393 ▶ if (!(n->nodemap & bit)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!(n->nodemap & bit)) return NULL; 28169 8016224 8044393
57  
58 8016224 n = (const hamt_node *)n->slot[hamt_node_nslots(n) - 1
59 8016224 - hamt_popcnt32(n->nodemap & (bit - 1))];
60 8016224 depth++;
61 }
62 1291 return NULL;
63 }
64  
65 /* The entry for a key, borrowed, or NULL. Retain what you keep. */
66 1315007 static hamt_entry *hamt_map_find(const hamt_node *root, const char *key,
67 size_t klen, int utf8, uint64_t hash) {
68 1315007 return hamt_node_find(root, 0, key, klen, utf8, hash);
69 }
70  
71 71061 static hamt_node *hamt_node_put(void *cx, hamt_node *n, unsigned depth,
72 hamt_entry *e, int *added) {
73 uint32_t bit, i;
74  
75 71061 ▶ if (n->ncoll) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->ncoll) { 5018 66043 71061
76 16513 ▶ for (i = 0; i < n->ncoll; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n->ncoll; i++) { 13718 2795 16513
77 13718 hamt_entry *old = (hamt_entry *)n->slot[i];
78 13718 ▶ if (!hamt_entry_key_eq(old, e->key, e->klen, e->utf8, e->hash)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!hamt_entry_key_eq(old, e->key, e->klen, e->utf8, e->hash)) continue; 11495 2223 13718
79 2223 ▶ if (old == e || old->val == e->val) return hamt_node_retain(n);
Branch: 2 branches
BranchTrueFalseTotal
if (old == e || old->val == e->val) return hamt_node_retain(n); 1109 1114 2223
if (old == e || old->val == e->val) return hamt_node_retain(n); 939 170 1109
80 170 return hamt_node_with_slot(cx, n, i, e, HAMT_NO_SKIP);
81 }
82 2795 *added = 1;
83 2795 return hamt_node_coll_add(cx, n, e);
84 }
85  
86 66043 bit = HAMT_BIT(e->hash, depth);
87  
88 66043 ▶ if (n->datamap & bit) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->datamap & bit) { 7304 58739 66043
89 7304 uint32_t di = hamt_popcnt32(n->datamap & (bit - 1));
90 7304 hamt_entry *old = (hamt_entry *)n->slot[di];
91 hamt_node *sub, *r;
92  
93 7304 ▶ if (hamt_entry_key_eq(old, e->key, e->klen, e->utf8, e->hash)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_entry_key_eq(old, e->key, e->klen, e->utf8, e->hash)) { 6486 818 7304
94 /* The same value under the same key is no change, and the same
95 * root comes back: nothing is allocated. */
96 6486 ▶ if (old == e || old->val == e->val) return hamt_node_retain(n);
Branch: 2 branches
BranchTrueFalseTotal
if (old == e || old->val == e->val) return hamt_node_retain(n); 2922 3564 6486
if (old == e || old->val == e->val) return hamt_node_retain(n); 2640 282 2922
97 282 return hamt_node_with_slot(cx, n, di, e, HAMT_NO_SKIP);
98 }
99  
100 818 sub = hamt_node_pair(cx, old, e, depth + 1);
101 818 ▶ if (!sub) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!sub) return NULL; 5 813 818
102 813 r = hamt_node_entry_to_child(cx, n, bit, sub);
103 813 ▶ if (!r) { hamt_node_release(cx, sub); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!r) { hamt_node_release(cx, sub); return NULL; } 5 808 813
104 808 *added = 1;
105 808 return r;
106 }
107  
108 58739 ▶ if (n->nodemap & bit) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->nodemap & bit) { 52240 6499 58739
109 52240 uint32_t ci = hamt_node_nslots(n) - 1 - hamt_popcnt32(n->nodemap & (bit - 1));
110 52240 hamt_node *child = (hamt_node *)n->slot[ci];
111 52240 hamt_node *sub = hamt_node_put(cx, child, depth + 1, e, added);
112 hamt_node *r;
113  
114 52240 ▶ if (!sub) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!sub) return NULL; 444 51796 52240
115 51796 ▶ if (sub == child) {
Branch: 1 branch
BranchTrueFalseTotal
if (sub == child) { 21182 30614 51796
116 21182 hamt_node_release(cx, sub);
117 21182 return hamt_node_retain(n);
118 }
119 30614 r = hamt_node_with_slot(cx, n, ci, sub, ci);
120 30614 ▶ if (!r) { hamt_node_release(cx, sub); *added = 0; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!r) { hamt_node_release(cx, sub); *added = 0; return NULL; } 84 30530 30614
121 30530 return r;
122 }
123  
124 {
125 6499 hamt_node *r = hamt_node_add_entry(cx, n, bit, e);
126 6499 ▶ if (r) *added = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (r) *added = 1; 6460 39 6499
127 6499 return r;
128 }
129 }
130  
131 /* The root of a trie that holds everything `root` holds and `e` as well, or
132 * in place of the entry with e's key. One reference, owned by the caller.
133 * The entry is shared: the caller keeps its own reference to it.
134 *
135 * `*added` is 1 when the key was not there before and 0 when it was. When
136 * the key was there with the very same value, the root that comes back IS
137 * `root`, retained.
138 *
139 * NULL means no memory, and only that: `root` is untouched, and nothing was
140 * retained. */
141 12 static hamt_node *hamt_map_put(void *cx, hamt_node *root, hamt_entry *e,
142 int *added) {
143 hamt_node *r;
144  
145 12 *added = 0;
146 12 ▶ if (!root) {
Branch: 1 branch
BranchTrueFalseTotal
if (!root) { 3 9 12
147 3 r = hamt_node_new(cx, 1);
148 3 ▶ if (!r) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return NULL; 1 2 3
149 2 r->datamap = HAMT_BIT(e->hash, 0);
150 2 r->slot[0] = e;
151 2 hamt_node_adopt(r, HAMT_NO_SKIP);
152 2 *added = 1;
153 2 return r;
154 }
155 9 r = hamt_node_put(cx, root, 0, e, added);
156 9 ▶ if (!r) *added = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) *added = 0; 4 5 9
157 9 return r;
158 }
159  
160 14833 static hamt_node *hamt_node_del(void *cx, hamt_node *n, unsigned depth,
161 const char *key, size_t klen, int utf8,
162 uint64_t hash, int *status) {
163 uint32_t bit, i;
164  
165 14833 ▶ if (n->ncoll) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->ncoll) { 1001 13832 14833
166 1803 ▶ for (i = 0; i < n->ncoll; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n->ncoll; i++) { 1802 1 1803
167 hamt_node *r;
168 1802 ▶ if (!hamt_entry_key_eq((hamt_entry *)n->slot[i], key, klen, utf8, hash))
Branch: 1 branch
BranchTrueFalseTotal
if (!hamt_entry_key_eq((hamt_entry *)n->slot[i], key, klen, utf8, hash)) 802 1000 1802
169 802 continue;
170 1000 r = hamt_node_coll_drop(cx, n, i);
171 1000 ▶ *status = r ? HAMT_DEL_DONE : HAMT_DEL_OOM; /* # uncoverable condition right count:1 */
Branch: 1 branch
BranchTrueFalseTotal
*status = r ? HAMT_DEL_DONE : HAMT_DEL_OOM; /* # uncoverable condition right count:1 */ 1000 0 1000
172 1000 return r;
173 }
174 1 *status = HAMT_DEL_ABSENT;
175 1 return hamt_node_retain(n);
176 }
177  
178 13832 bit = HAMT_BIT(hash, depth);
179  
180 13832 ▶ if (n->datamap & bit) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->datamap & bit) { 3479 10353 13832
181 3479 hamt_entry *old = (hamt_entry *)
182 3479 n->slot[hamt_popcnt32(n->datamap & (bit - 1))];
183 hamt_node *r;
184  
185 3479 ▶ if (!hamt_entry_key_eq(old, key, klen, utf8, hash)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hamt_entry_key_eq(old, key, klen, utf8, hash)) { 1 3478 3479
186 1 *status = HAMT_DEL_ABSENT;
187 1 return hamt_node_retain(n);
188 }
189 /* The last entry of the last node: the map is empty. Only a root
190 * gets here, since nothing below a root is ever down to one. */
191 3478 ▶ if (hamt_node_nslots(n) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_node_nslots(n) == 1) { 2 3476 3478
192 2 *status = HAMT_DEL_DONE;
193 2 return NULL;
194 }
195 3476 r = hamt_node_drop_entry(cx, n, bit);
196 3476 ▶ *status = r ? HAMT_DEL_DONE : HAMT_DEL_OOM;
Branch: 1 branch
BranchTrueFalseTotal
*status = r ? HAMT_DEL_DONE : HAMT_DEL_OOM; 3454 22 3476
197 3476 return r;
198 }
199  
200 10353 ▶ if (n->nodemap & bit) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->nodemap & bit) { 10351 2 10353
201 10351 uint32_t ci = hamt_node_nslots(n) - 1 - hamt_popcnt32(n->nodemap & (bit - 1));
202 10351 hamt_node *child = (hamt_node *)n->slot[ci];
203 10351 hamt_node *sub = hamt_node_del(cx, child, depth + 1, key, klen, utf8,
204 hash, status);
205 hamt_node *r;
206  
207 10351 ▶ if (*status == HAMT_DEL_OOM) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (*status == HAMT_DEL_OOM) return NULL; 2 10349 10351
208 10349 ▶ if (*status == HAMT_DEL_ABSENT) {
Branch: 1 branch
BranchTrueFalseTotal
if (*status == HAMT_DEL_ABSENT) { 14 10335 10349
209 14 hamt_node_release(cx, sub);
210 14 return hamt_node_retain(n);
211 }
212 /*
213 # uncoverable branch true
214 # uncoverable condition left count:1 */
215 10335 ▶ if (!sub) HAMT_FATAL("a node below the root came back empty");
Branch: 1 branch
BranchTrueFalseTotal
if (!sub) HAMT_FATAL("a node below the root came back empty"); 0 10335 10335
216  
217 /* The fold. The child is down to one entry, so it stops existing and
218 * the entry moves up into the position the child had. If that leaves
219 * THIS node with one entry and nothing else, the caller folds it in
220 * turn. */
221 10335 ▶ if (hamt_node_is_single(sub))
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_node_is_single(sub)) 8321 2014 10335
222 8321 r = hamt_node_child_to_entry(cx, n, bit, (hamt_entry *)sub->slot[0]);
223 else
224 2014 r = hamt_node_with_slot(cx, n, ci, sub, HAMT_NO_SKIP);
225  
226 10335 hamt_node_release(cx, sub);
227 10335 ▶ if (!r) *status = HAMT_DEL_OOM;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) *status = HAMT_DEL_OOM; 2 10333 10335
228 10335 return r;
229 }
230  
231 2 *status = HAMT_DEL_ABSENT;
232 2 return hamt_node_retain(n);
233 }
234  
235 /* The root of a trie that holds everything `root` holds but the entry for
236 * this key. One reference, owned by the caller, when it is not NULL.
237 *
238 * HAMT_DEL_DONE the key was there. The root may be NULL: an empty map.
239 * HAMT_DEL_ABSENT it was not. The root that comes back IS `root`, retained
240 * (or NULL for NULL).
241 * HAMT_DEL_OOM no memory. NULL comes back and `root` is untouched. */
242 16 static hamt_node *hamt_map_del(void *cx, hamt_node *root, const char *key,
243 size_t klen, int utf8, uint64_t hash,
244 int *status) {
245 16 ▶ if (!root) {
Branch: 1 branch
BranchTrueFalseTotal
if (!root) { 1 15 16
246 1 *status = HAMT_DEL_ABSENT;
247 1 return NULL;
248 }
249 15 return hamt_node_del(cx, root, 0, key, klen, utf8, hash, status);
250 }
251  
252 #endif
« include/hamt/hamt_algebra.h ↑ Summary blib/lib/HAMT.pm »