Coverage Report

Created: 2026-11-12 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/open62541_15/deps/ziptree.c
Line
Count
Source
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 
4
 *
5
 *    Copyright 2021-2022 (c) Julius Pfrommer
6
 */
7
8
#include "ziptree.h"
9
10
/* Dummy types */
11
struct zip_elem;
12
typedef struct zip_elem zip_elem;
13
typedef ZIP_ENTRY(zip_elem) zip_entry;
14
typedef ZIP_HEAD(, zip_elem) zip_head;
15
16
/* Access macros */
17
3.23G
#define ZIP_ENTRY_PTR(x) ((zip_entry*)((char*)x + fieldoffset))
18
1.78G
#define ZIP_KEY_PTR(x) (const void*)((const char*)x + keyoffset)
19
20
/* Hash pointers to keep the tie-breeaking of equal keys (mostly) uncorrelated
21
 * from the rank (pointer order). Hashing code taken from sdbm-hash
22
 * (http://www.cse.yorku.ca/~oz/hash.html). */
23
static unsigned int
24
1.69G
__ZIP_PTR_HASH(const void *p) {
25
1.69G
    unsigned int h = 0;
26
1.69G
    const unsigned char *data = (const unsigned char*)&p;
27
15.2G
    for(size_t i = 0; i < (sizeof(void*) / sizeof(char)); i++)
28
13.5G
        h = data[i] + (h << 6) + (h << 16) - h;
29
1.69G
    return h;
30
1.69G
}
31
32
static ZIP_INLINE enum ZIP_CMP
33
849M
__ZIP_RANK_CMP(const void *p1, const void *p2) {
34
    /* assert(p1 != p2); */
35
849M
    unsigned int h1 = __ZIP_PTR_HASH(p1);
36
849M
    unsigned int h2 = __ZIP_PTR_HASH(p2);
37
849M
    if(h1 == h2)
38
0
        return (p1 < p2) ? ZIP_CMP_LESS : ZIP_CMP_MORE;
39
849M
    return (h1 < h2) ? ZIP_CMP_LESS : ZIP_CMP_MORE;
40
849M
}
41
42
static ZIP_INLINE enum ZIP_CMP
43
1.29G
__ZIP_UNIQUE_CMP(zip_cmp_cb cmp, const void *p1, const void *p2) {
44
1.29G
    if(p1 == p2)
45
191M
        return ZIP_CMP_EQ;
46
1.10G
    enum ZIP_CMP order = cmp(p1, p2);
47
1.10G
    if(order == ZIP_CMP_EQ)
48
28.0M
        return (p1 < p2) ? ZIP_CMP_LESS : ZIP_CMP_MORE;
49
1.07G
    return order;
50
1.10G
}
51
52
#if 0
53
#include <assert.h>
54
ZIP_UNUSED static ZIP_INLINE void
55
__ZIP_VALIDATE(zip_cmp_cb cmp, unsigned short fieldoffset,
56
               unsigned short keyoffset, void *elm,
57
               void *min_elm, void *max_elm) {
58
    if(!elm)
59
        return;
60
    enum ZIP_CMP c1 = __ZIP_UNIQUE_CMP(cmp, ZIP_KEY_PTR(min_elm), ZIP_KEY_PTR(elm));
61
    assert((elm == min_elm && c1 == ZIP_CMP_EQ) || c1 == ZIP_CMP_LESS);
62
63
    enum ZIP_CMP c2 = __ZIP_UNIQUE_CMP(cmp, ZIP_KEY_PTR(max_elm), ZIP_KEY_PTR(elm));
64
    assert((elm == max_elm && c2 == ZIP_CMP_EQ) || c2 == ZIP_CMP_MORE);
65
66
    assert(!ZIP_ENTRY_PTR(elm)->right ||
67
           __ZIP_RANK_CMP(elm, ZIP_ENTRY_PTR(elm)->right) == ZIP_CMP_MORE);
68
    assert(!ZIP_ENTRY_PTR(elm)->left ||
69
           __ZIP_RANK_CMP(elm, ZIP_ENTRY_PTR(elm)->left) == ZIP_CMP_MORE);
70
71
    __ZIP_VALIDATE(cmp, fieldoffset, keyoffset, ZIP_ENTRY_PTR(elm)->right, elm, max_elm);
72
    __ZIP_VALIDATE(cmp, fieldoffset, keyoffset, ZIP_ENTRY_PTR(elm)->left, min_elm, elm);
73
}
74
#endif
75
76
/* Walk down the right-side spine of cur. Elements that are larger than x_key
77
 * are moved under x->right. */
78
static void
79
__ZIP_INSERT_MOVE_RIGHT(zip_cmp_cb cmp, unsigned short fieldoffset,
80
                        unsigned short keyoffset, const void *x_key,
81
47.6M
                        zip_elem **fix_edge, zip_elem *cur) {
82
114M
    while(ZIP_ENTRY_PTR(cur)->right) {
83
66.9M
        zip_elem *move_candidate = ZIP_ENTRY_PTR(cur)->right;
84
66.9M
        if(__ZIP_UNIQUE_CMP(cmp, x_key, ZIP_KEY_PTR(move_candidate)) == ZIP_CMP_MORE) {
85
28.8M
            cur = ZIP_ENTRY_PTR(cur)->right;
86
28.8M
            continue;
87
28.8M
        }
88
38.0M
        ZIP_ENTRY_PTR(cur)->right = ZIP_ENTRY_PTR(move_candidate)->left;
89
38.0M
        ZIP_ENTRY_PTR(move_candidate)->left = NULL;
90
38.0M
        *fix_edge = move_candidate;
91
38.0M
        fix_edge = &ZIP_ENTRY_PTR(move_candidate)->left;
92
38.0M
    }
93
47.6M
}
94
95
static void
96
__ZIP_INSERT_MOVE_LEFT(zip_cmp_cb cmp, unsigned short fieldoffset,
97
                       unsigned short keyoffset, const void *x_key,
98
120M
                       zip_elem **fix_edge, zip_elem *cur) {
99
188M
    while(ZIP_ENTRY_PTR(cur)->left) {
100
68.4M
        zip_elem *move_candidate = ZIP_ENTRY_PTR(cur)->left;
101
68.4M
        if(__ZIP_UNIQUE_CMP(cmp, x_key, ZIP_KEY_PTR(move_candidate)) == ZIP_CMP_LESS) {
102
37.8M
            cur = ZIP_ENTRY_PTR(cur)->left;
103
37.8M
            continue;
104
37.8M
        }
105
30.6M
        ZIP_ENTRY_PTR(cur)->left = ZIP_ENTRY_PTR(move_candidate)->right;
106
30.6M
        ZIP_ENTRY_PTR(move_candidate)->right = NULL;
107
30.6M
        *fix_edge = move_candidate;
108
30.6M
        fix_edge = &ZIP_ENTRY_PTR(move_candidate)->right;
109
30.6M
    }
110
120M
}
111
112
void
113
__ZIP_INSERT(void *h, zip_cmp_cb cmp, unsigned short fieldoffset,
114
300M
             unsigned short keyoffset, void *elm) {
115
300M
    zip_elem *x = (zip_elem*)elm;
116
300M
    ZIP_ENTRY_PTR(x)->left = NULL;
117
300M
    ZIP_ENTRY_PTR(x)->right = NULL;
118
119
300M
    const void *x_key = ZIP_KEY_PTR(x);
120
300M
    zip_head *head = (zip_head*)h;
121
300M
    if(!head->root) {
122
54.1M
        head->root = x;
123
54.1M
        return;
124
54.1M
    }
125
126
    /* Go down the tree to find the top element "cur" that has a rank smaller
127
     * than "x" */
128
246M
    zip_elem *prev = NULL;
129
246M
    zip_elem *cur = head->root;
130
246M
    enum ZIP_CMP cur_order, prev_order = ZIP_CMP_EQ;
131
822M
    do {
132
822M
        cur_order = __ZIP_UNIQUE_CMP(cmp, x_key, ZIP_KEY_PTR(cur));
133
822M
        if(cur_order == ZIP_CMP_EQ)
134
0
            return; /* x is already inserted */
135
822M
        if(__ZIP_RANK_CMP(cur, x) == ZIP_CMP_LESS)
136
168M
            break;
137
654M
        prev = cur;
138
654M
        prev_order = cur_order;
139
654M
        cur = (cur_order == ZIP_CMP_MORE) ?
140
362M
            ZIP_ENTRY_PTR(cur)->right : ZIP_ENTRY_PTR(cur)->left;
141
654M
    } while(cur);
142
143
    /* Insert "x" instead of "cur" under its parent "prev" */
144
246M
    if(cur == head->root) {
145
80.6M
        head->root = x;
146
165M
    } else {
147
165M
        if(prev_order == ZIP_CMP_MORE)
148
53.2M
            ZIP_ENTRY_PTR(prev)->right = x;
149
112M
        else
150
112M
            ZIP_ENTRY_PTR(prev)->left = x;
151
165M
    }
152
153
246M
    if(!cur)
154
78.4M
        return;
155
156
    /* Re-insert "cur" under "x". Repair by moving elements that ended up on the
157
     * wrong side of "x". */
158
168M
    if(cur_order == ZIP_CMP_MORE) {
159
47.6M
        ZIP_ENTRY_PTR(x)->left = cur;
160
47.6M
        __ZIP_INSERT_MOVE_RIGHT(cmp, fieldoffset, keyoffset,
161
47.6M
                                x_key, &ZIP_ENTRY_PTR(x)->right, cur);
162
120M
    } else {
163
120M
        ZIP_ENTRY_PTR(x)->right = cur;
164
120M
        __ZIP_INSERT_MOVE_LEFT(cmp, fieldoffset, keyoffset,
165
120M
                               x_key, &ZIP_ENTRY_PTR(x)->left, cur);
166
120M
    }
167
168M
}
168
169
void *
170
__ZIP_REMOVE(void *h, zip_cmp_cb cmp, unsigned short fieldoffset,
171
191M
             unsigned short keyoffset, void *elm) {
172
191M
    zip_head *head = (zip_head*)h;
173
191M
    zip_elem *x = (zip_elem*)elm;
174
191M
    zip_elem *cur = head->root;
175
191M
    if(!cur)
176
0
        return NULL;
177
178
191M
    const void *x_key = ZIP_KEY_PTR(x);
179
191M
    zip_elem **prev_edge = &head->root;
180
191M
    enum ZIP_CMP cur_order = __ZIP_UNIQUE_CMP(cmp, x_key, ZIP_KEY_PTR(cur));
181
333M
    while(cur_order != ZIP_CMP_EQ) {
182
142M
        prev_edge = (cur_order == ZIP_CMP_LESS) ?
183
105M
            &ZIP_ENTRY_PTR(cur)->left : &ZIP_ENTRY_PTR(cur)->right;
184
142M
        cur = *prev_edge;
185
142M
        if(!cur)
186
0
            return NULL;
187
142M
        cur_order = __ZIP_UNIQUE_CMP(cmp, x_key, ZIP_KEY_PTR(cur));
188
142M
    }
189
191M
    *prev_edge = (zip_elem*)__ZIP_ZIP(fieldoffset,
190
191M
                                      ZIP_ENTRY_PTR(cur)->left,
191
191M
                                      ZIP_ENTRY_PTR(cur)->right);
192
191M
    return cur;
193
191M
}
194
195
void *
196
__ZIP_ITER(unsigned short fieldoffset, zip_iter_cb cb,
197
114M
           void *context, void *elm) {
198
114M
    if(!elm)
199
55.7M
        return NULL;
200
59.1M
    zip_elem *left = ZIP_ENTRY_PTR(elm)->left;
201
59.1M
    zip_elem *right = ZIP_ENTRY_PTR(elm)->right;
202
59.1M
    void *res = __ZIP_ITER(fieldoffset, cb, context, left);
203
59.1M
    if(res)
204
3.73M
        return res;
205
55.4M
    res = cb(context, elm);
206
55.4M
    if(res)
207
1.53M
        return res;
208
53.8M
    return __ZIP_ITER(fieldoffset, cb, context, right);
209
55.4M
}
210
211
void *
212
__ZIP_ITER_KEY(zip_cmp_cb cmp, unsigned short fieldoffset,
213
               unsigned short keyoffset, const void *key,
214
1.39k
               zip_iter_cb cb, void *context, void *elm) {
215
1.39k
    if(!elm)
216
709
        return NULL;
217
218
681
    void *res;
219
681
    enum ZIP_CMP eq = cmp(key, ZIP_KEY_PTR(elm));
220
681
    if(eq != ZIP_CMP_MORE) {
221
573
        res = __ZIP_ITER_KEY(cmp, fieldoffset, keyoffset, key,
222
573
                             cb, context, ZIP_ENTRY_PTR(elm)->left);
223
573
        if(res)
224
124
            return res;
225
573
    }
226
227
557
    if(eq == ZIP_CMP_EQ) {
228
309
        res = cb(context, elm);
229
309
        if(res)
230
309
            return res;
231
309
    }
232
233
248
    if(eq != ZIP_CMP_LESS) {
234
108
        res = __ZIP_ITER_KEY(cmp, fieldoffset, keyoffset, key,
235
108
                             cb, context, ZIP_ENTRY_PTR(elm)->right);
236
108
        if(res)
237
0
            return res;
238
108
    }
239
240
248
    return NULL;
241
248
}
242
243
void *
244
191M
__ZIP_ZIP(unsigned short fieldoffset, void *left, void *right) {
245
191M
    if(!left)
246
163M
        return right;
247
27.6M
    if(!right)
248
8.78M
        return left;
249
18.8M
    zip_elem *l = (zip_elem*)left;
250
18.8M
    zip_elem *r = (zip_elem*)right;
251
18.8M
    zip_elem *root = NULL;
252
18.8M
    zip_elem **prev_edge = &root;
253
46.0M
    while(l && r) {
254
27.2M
        if(__ZIP_RANK_CMP(l, r) == ZIP_CMP_LESS) {
255
12.4M
            *prev_edge = r;
256
12.4M
            prev_edge = &ZIP_ENTRY_PTR(r)->left;
257
12.4M
            r = ZIP_ENTRY_PTR(r)->left;
258
14.7M
        } else {
259
14.7M
            *prev_edge = l;
260
14.7M
            prev_edge = &ZIP_ENTRY_PTR(l)->right;
261
14.7M
            l = ZIP_ENTRY_PTR(l)->right;
262
14.7M
        }
263
27.2M
    }
264
18.8M
    *prev_edge = (l) ? l : r;
265
18.8M
    return root;
266
27.6M
}
267
268
/* Walk down from cur and move all elements <= split-key to the left side. All
269
 * elements that are moved over have to be below left_rightmost. Returns the
270
 * hierarchy of elements that remain on the right side. */
271
static void
272
__ZIP_UNZIP_MOVE_LEFT(zip_cmp_cb cmp, unsigned short fieldoffset,
273
                      unsigned short keyoffset, const void *key,
274
0
                      zip_elem **fix_edge, zip_elem *cur) {
275
0
    while(ZIP_ENTRY_PTR(cur)->left) {
276
0
        zip_elem *next = ZIP_ENTRY_PTR(cur)->left;
277
0
        if(cmp(key, ZIP_KEY_PTR(next)) == ZIP_CMP_LESS) {
278
0
            cur = next;
279
0
            continue;
280
0
        }
281
0
        *fix_edge = next;
282
0
        ZIP_ENTRY_PTR(cur)->left = ZIP_ENTRY_PTR(next)->right;
283
0
        ZIP_ENTRY_PTR(next)->right = NULL;
284
0
        fix_edge = &ZIP_ENTRY_PTR(next)->right;
285
0
    }
286
0
}
287
288
static void
289
__ZIP_UNZIP_MOVE_RIGHT(zip_cmp_cb cmp, unsigned short fieldoffset,
290
                       unsigned short keyoffset, const void *key,
291
0
                       zip_elem **fix_edge, zip_elem *cur) {
292
0
    while(ZIP_ENTRY_PTR(cur)->right) {
293
0
        zip_elem *next = ZIP_ENTRY_PTR(cur)->right;
294
0
        if(cmp(key, ZIP_KEY_PTR(next)) != ZIP_CMP_LESS) {
295
0
            cur = next;
296
0
            continue;
297
0
        }
298
0
        *fix_edge = next;
299
0
        ZIP_ENTRY_PTR(cur)->right = ZIP_ENTRY_PTR(next)->left;
300
0
        ZIP_ENTRY_PTR(next)->left = NULL;
301
0
        fix_edge = &ZIP_ENTRY_PTR(next)->left;
302
0
    }
303
0
}
304
305
/* Split the tree into a left side with keys <= split-key and a right side with
306
 * key > split-key. */
307
void
308
__ZIP_UNZIP(zip_cmp_cb cmp, unsigned short fieldoffset,
309
            unsigned short keyoffset, const void *key,
310
24.4k
            void *h, void *l, void *r) {
311
24.4k
    zip_elem *prev;
312
24.4k
    zip_head *head = (zip_head*)h;
313
24.4k
    zip_head *left = (zip_head*)l;
314
24.4k
    zip_head *right = (zip_head*)r;
315
24.4k
    if(!head->root) {
316
23.3k
        left->root = NULL;
317
23.3k
        right->root = NULL;
318
23.3k
        return;
319
23.3k
    }
320
1.09k
    zip_elem *cur = head->root;
321
1.09k
    if(cmp(key, ZIP_KEY_PTR(cur)) != ZIP_CMP_LESS) {
322
0
        left->root = cur;
323
0
        do {
324
0
            prev = cur;
325
0
            cur = ZIP_ENTRY_PTR(cur)->right;
326
0
            if(!cur) {
327
0
                right->root = NULL;
328
0
                return;
329
0
            }
330
0
        } while(cmp(key, ZIP_KEY_PTR(cur)) != ZIP_CMP_LESS);
331
0
        ZIP_ENTRY_PTR(prev)->right = NULL;
332
0
        right->root = cur;
333
0
        __ZIP_UNZIP_MOVE_LEFT(cmp, fieldoffset, keyoffset, key,
334
0
                              &ZIP_ENTRY_PTR(prev)->right, cur);
335
1.09k
    } else {
336
1.09k
        right->root = cur;
337
2.32k
        do {
338
2.32k
            prev = cur;
339
2.32k
            cur = ZIP_ENTRY_PTR(cur)->left;
340
2.32k
            if(!cur) {
341
1.09k
                left->root = NULL;
342
1.09k
                return;
343
1.09k
            }
344
2.32k
        } while(cmp(key, ZIP_KEY_PTR(cur)) == ZIP_CMP_LESS);
345
0
        ZIP_ENTRY_PTR(prev)->left = NULL;
346
0
        left->root = cur;
347
0
        __ZIP_UNZIP_MOVE_RIGHT(cmp, fieldoffset, keyoffset, key,
348
0
                               &ZIP_ENTRY_PTR(prev)->left, cur);
349
0
    }
350
1.09k
}