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 1368403 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 9430087 ▶ while (n) {
Branch: 1 branch
BranchTrueFalseTotal
while (n) { 9428801 1286 9430087
40 uint32_t bit, i;
41  
42 9428801 ▶ if (n->ncoll) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->ncoll) { 580504 8848297 9428801
43 2847323 ▶ for (i = 0; i < n->ncoll; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n->ncoll; i++) { 2832824 14499 2847323
44 2832824 hamt_entry *e = (hamt_entry *)n->slot[i];
45 2832824 ▶ 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; 566005 2266819 2832824
46 }
47 14499 return NULL;
48 }
49  
50 8848297 bit = HAMT_BIT(hash, depth);
51 8848297 ▶ if (n->datamap & bit) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->datamap & bit) { 758461 8089836 8848297
52 758461 hamt_entry *e = (hamt_entry *)
53 758461 n->slot[hamt_popcnt32(n->datamap & (bit - 1))];
54 758461 ▶ 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; 752468 5993 758461
55 }
56 8089836 ▶ if (!(n->nodemap & bit)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!(n->nodemap & bit)) return NULL; 28152 8061684 8089836
57  
58 8061684 n = (const hamt_node *)n->slot[hamt_node_nslots(n) - 1
59 8061684 - hamt_popcnt32(n->nodemap & (bit - 1))];
60 8061684 depth++;
61 }
62 1286 return NULL;
63 }
64  
65 /* The entry for a key, borrowed, or NULL. Retain what you keep. */
66 1314996 static hamt_entry *hamt_map_find(const hamt_node *root, const char *key,
67 size_t klen, int utf8, uint64_t hash) {
68 1314996 return hamt_node_find(root, 0, key, klen, utf8, hash);
69 }
70  
71 67439 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 67439 ▶ if (n->ncoll) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->ncoll) { 4727 62712 67439
76 15719 ▶ for (i = 0; i < n->ncoll; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n->ncoll; i++) { 13112 2607 15719
77 13112 hamt_entry *old = (hamt_entry *)n->slot[i];
78 13112 ▶ 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; 10992 2120 13112
79 2120 ▶ 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); 1064 1056 2120
if (old == e || old->val == e->val) return hamt_node_retain(n); 908 156 1064
80 156 return hamt_node_with_slot(cx, n, i, e, HAMT_NO_SKIP);
81 }
82 2607 *added = 1;
83 2607 return hamt_node_coll_add(cx, n, e);
84 }
85  
86 62712 bit = HAMT_BIT(e->hash, depth);
87  
88 62712 ▶ if (n->datamap & bit) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->datamap & bit) { 6815 55897 62712
89 6815 uint32_t di = hamt_popcnt32(n->datamap & (bit - 1));
90 6815 hamt_entry *old = (hamt_entry *)n->slot[di];
91 hamt_node *sub, *r;
92  
93 6815 ▶ 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)) { 6039 776 6815
94 /* The same value under the same key is no change, and the same
95 * root comes back: nothing is allocated. */
96 6039 ▶ 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); 2711 3328 6039
if (old == e || old->val == e->val) return hamt_node_retain(n); 2442 269 2711
97 269 return hamt_node_with_slot(cx, n, di, e, HAMT_NO_SKIP);
98 }
99  
100 776 sub = hamt_node_pair(cx, old, e, depth + 1);
101 776 ▶ if (!sub) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!sub) return NULL; 5 771 776
102 771 r = hamt_node_entry_to_child(cx, n, bit, sub);
103 771 ▶ if (!r) { hamt_node_release(cx, sub); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!r) { hamt_node_release(cx, sub); return NULL; } 5 766 771
104 766 *added = 1;
105 766 return r;
106 }
107  
108 55897 ▶ if (n->nodemap & bit) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->nodemap & bit) { 49324 6573 55897
109 49324 uint32_t ci = hamt_node_nslots(n) - 1 - hamt_popcnt32(n->nodemap & (bit - 1));
110 49324 hamt_node *child = (hamt_node *)n->slot[ci];
111 49324 hamt_node *sub = hamt_node_put(cx, child, depth + 1, e, added);
112 hamt_node *r;
113  
114 49324 ▶ if (!sub) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!sub) return NULL; 444 48880 49324
115 48880 ▶ if (sub == child) {
Branch: 1 branch
BranchTrueFalseTotal
if (sub == child) { 20327 28553 48880
116 20327 hamt_node_release(cx, sub);
117 20327 return hamt_node_retain(n);
118 }
119 28553 r = hamt_node_with_slot(cx, n, ci, sub, ci);
120 28553 ▶ 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 28469 28553
121 28469 return r;
122 }
123  
124 {
125 6573 hamt_node *r = hamt_node_add_entry(cx, n, bit, e);
126 6573 ▶ if (r) *added = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (r) *added = 1; 6534 39 6573
127 6573 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 13944 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 13944 ▶ if (n->ncoll) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->ncoll) { 937 13007 13944
166 1767 ▶ for (i = 0; i < n->ncoll; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n->ncoll; i++) { 1766 1 1767
167 hamt_node *r;
168 1766 ▶ 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)) 830 936 1766
169 830 continue;
170 936 r = hamt_node_coll_drop(cx, n, i);
171 936 ▶ *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 */ 936 0 936
172 936 return r;
173 }
174 1 *status = HAMT_DEL_ABSENT;
175 1 return hamt_node_retain(n);
176 }
177  
178 13007 bit = HAMT_BIT(hash, depth);
179  
180 13007 ▶ if (n->datamap & bit) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->datamap & bit) { 3267 9740 13007
181 3267 hamt_entry *old = (hamt_entry *)
182 3267 n->slot[hamt_popcnt32(n->datamap & (bit - 1))];
183 hamt_node *r;
184  
185 3267 ▶ 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 3266 3267
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 3266 ▶ if (hamt_node_nslots(n) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_node_nslots(n) == 1) { 2 3264 3266
192 2 *status = HAMT_DEL_DONE;
193 2 return NULL;
194 }
195 3264 r = hamt_node_drop_entry(cx, n, bit);
196 3264 ▶ *status = r ? HAMT_DEL_DONE : HAMT_DEL_OOM;
Branch: 1 branch
BranchTrueFalseTotal
*status = r ? HAMT_DEL_DONE : HAMT_DEL_OOM; 3242 22 3264
197 3264 return r;
198 }
199  
200 9740 ▶ if (n->nodemap & bit) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->nodemap & bit) { 9738 2 9740
201 9738 uint32_t ci = hamt_node_nslots(n) - 1 - hamt_popcnt32(n->nodemap & (bit - 1));
202 9738 hamt_node *child = (hamt_node *)n->slot[ci];
203 9738 hamt_node *sub = hamt_node_del(cx, child, depth + 1, key, klen, utf8,
204 hash, status);
205 hamt_node *r;
206  
207 9738 ▶ if (*status == HAMT_DEL_OOM) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (*status == HAMT_DEL_OOM) return NULL; 2 9736 9738
208 9736 ▶ if (*status == HAMT_DEL_ABSENT) {
Branch: 1 branch
BranchTrueFalseTotal
if (*status == HAMT_DEL_ABSENT) { 14 9722 9736
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 9722 ▶ 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 9722 9722
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 9722 ▶ if (hamt_node_is_single(sub))
Branch: 1 branch
BranchTrueFalseTotal
if (hamt_node_is_single(sub)) 7426 2296 9722
222 7426 r = hamt_node_child_to_entry(cx, n, bit, (hamt_entry *)sub->slot[0]);
223 else
224 2296 r = hamt_node_with_slot(cx, n, ci, sub, HAMT_NO_SKIP);
225  
226 9722 hamt_node_release(cx, sub);
227 9722 ▶ if (!r) *status = HAMT_DEL_OOM;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) *status = HAMT_DEL_OOM; 2 9720 9722
228 9722 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 »