-
Notifications
You must be signed in to change notification settings - Fork 969
Expand file tree
/
Copy path_ceb_int.c
More file actions
466 lines (405 loc) · 17.6 KB
/
Copy path_ceb_int.c
File metadata and controls
466 lines (405 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
* Compact Elastic Binary Trees - exported functions operating on integer keys
*
* Copyright (C) 2014-2025 Willy Tarreau - w@1wt.eu
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/* NOTE: this file is only meant to be included from other C files. It will
* use the following private macros that must be defined by the caller:
* - CEB_KEY_TYPE: uint32_t, uint64_t, unsigned long
* - CEB_KEY_MEMBER: member of the struct ceb_node holding the key
* - CEB_MKEY_PFX: function name prefix for multi-key
* - CEB_UKEY_PFX: function name prefix for unique keys
*
* The dump functions will only be build if CEB_ENABLE_DUMP is defined.
*/
#include "cebtree-prv.h"
/*
* Below are the functions that support duplicate keys (_ceb_*)
*/
/*****************************************************************************\
* The declarations below always cause two functions to be declared, one *
* starting with "cebs_*" and one with "cebs_ofs_*" which takes a key offset *
* just after the root. The one without kofs just has this argument omitted *
* from its declaration and replaced with sizeof(struct ceb_node) in the *
* call to the underlying functions. *
\*****************************************************************************/
/* Inserts node <node> into tree <tree> based on its key that immediately
* follows the node. Returns the inserted node or the one that already contains
* the same key.
*/
CEB_FDECL3(struct ceb_node *, CEB_MKEY_PFX, _insert, struct ceb_root **, root, ptrdiff_t, kofs, struct ceb_node *, node)
{
CEB_KEY_TYPE key = NODEK(node, kofs)->CEB_KEY_MEMBER;
int is_dup;
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_insert(root, node, kofs, CEB_KT_U32, key, 0, NULL, &is_dup);
else
return _ceb_insert(root, node, kofs, CEB_KT_U64, 0, key, NULL, &is_dup);
}
/* return the first node or NULL if not found. */
CEB_FDECL2(struct ceb_node *, CEB_MKEY_PFX, _first, struct ceb_root *const *, root, ptrdiff_t, kofs)
{
int is_dup;
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_first(root, kofs, CEB_KT_U32, 0, &is_dup);
else
return _ceb_first(root, kofs, CEB_KT_U64, 0, &is_dup);
}
/* return the last node or NULL if not found. */
CEB_FDECL2(struct ceb_node *, CEB_MKEY_PFX, _last, struct ceb_root *const *, root, ptrdiff_t, kofs)
{
int is_dup;
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_last(root, kofs, CEB_KT_U32, 0, &is_dup);
else
return _ceb_last(root, kofs, CEB_KT_U64, 0, &is_dup);
}
/* look up the specified key, and returns either the node containing it, or
* NULL if not found.
*/
CEB_FDECL3(struct ceb_node *, CEB_MKEY_PFX, _lookup, struct ceb_root *const *, root, ptrdiff_t, kofs, CEB_KEY_TYPE, key)
{
int is_dup;
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_lookup(root, kofs, CEB_KT_U32, key, 0, NULL, &is_dup);
else
return _ceb_lookup(root, kofs, CEB_KT_U64, 0, key, NULL, &is_dup);
}
/* look up the specified key or the highest below it, and returns either the
* node containing it, or NULL if not found.
*/
CEB_FDECL3(struct ceb_node *, CEB_MKEY_PFX, _lookup_le, struct ceb_root *const *, root, ptrdiff_t, kofs, CEB_KEY_TYPE, key)
{
int is_dup;
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_lookup_le(root, kofs, CEB_KT_U32, key, 0, NULL, &is_dup);
else
return _ceb_lookup_le(root, kofs, CEB_KT_U64, 0, key, NULL, &is_dup);
}
/* look up highest key below the specified one, and returns either the
* node containing it, or NULL if not found.
*/
CEB_FDECL3(struct ceb_node *, CEB_MKEY_PFX, _lookup_lt, struct ceb_root *const *, root, ptrdiff_t, kofs, CEB_KEY_TYPE, key)
{
int is_dup;
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_lookup_lt(root, kofs, CEB_KT_U32, key, 0, NULL, &is_dup);
else
return _ceb_lookup_lt(root, kofs, CEB_KT_U64, 0, key, NULL, &is_dup);
}
/* look up the specified key or the smallest above it, and returns either the
* node containing it, or NULL if not found.
*/
CEB_FDECL3(struct ceb_node *, CEB_MKEY_PFX, _lookup_ge, struct ceb_root *const *, root, ptrdiff_t, kofs, CEB_KEY_TYPE, key)
{
int is_dup;
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_lookup_ge(root, kofs, CEB_KT_U32, key, 0, NULL, &is_dup);
else
return _ceb_lookup_ge(root, kofs, CEB_KT_U64, 0, key, NULL, &is_dup);
}
/* look up the smallest key above the specified one, and returns either the
* node containing it, or NULL if not found.
*/
CEB_FDECL3(struct ceb_node *, CEB_MKEY_PFX, _lookup_gt, struct ceb_root *const *, root, ptrdiff_t, kofs, CEB_KEY_TYPE, key)
{
int is_dup;
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_lookup_gt(root, kofs, CEB_KT_U32, key, 0, NULL, &is_dup);
else
return _ceb_lookup_gt(root, kofs, CEB_KT_U64, 0, key, NULL, &is_dup);
}
/* search for the next node after the specified one, and return it, or NULL if
* not found. The approach consists in looking up that node, recalling the last
* time a left turn was made, and returning the first node along the right
* branch at that fork.
*/
CEB_FDECL3(struct ceb_node *, CEB_MKEY_PFX, _next_unique, struct ceb_root *const *, root, ptrdiff_t, kofs, struct ceb_node *, node)
{
CEB_KEY_TYPE key = NODEK(node, kofs)->CEB_KEY_MEMBER;
int is_dup;
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_next_unique(root, kofs, CEB_KT_U32, key, 0, NULL, &is_dup);
else
return _ceb_next_unique(root, kofs, CEB_KT_U64, 0, key, NULL, &is_dup);
}
/* search for the prev node before the specified one, and return it, or NULL if
* not found. The approach consists in looking up that node, recalling the last
* time a right turn was made, and returning the last node along the left
* branch at that fork.
*/
CEB_FDECL3(struct ceb_node *, CEB_MKEY_PFX, _prev_unique, struct ceb_root *const *, root, ptrdiff_t, kofs, struct ceb_node *, node)
{
CEB_KEY_TYPE key = NODEK(node, kofs)->CEB_KEY_MEMBER;
int is_dup;
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_prev_unique(root, kofs, CEB_KT_U32, key, 0, NULL, &is_dup);
else
return _ceb_prev_unique(root, kofs, CEB_KT_U64, 0, key, NULL, &is_dup);
}
/* search for the next node after the specified one containing the same value,
* and return it, or NULL if not found.
*/
CEB_FDECL3(struct ceb_node *, CEB_MKEY_PFX, _next_dup, struct ceb_root *const *, root, ptrdiff_t, kofs, struct ceb_node *, node)
{
CEB_KEY_TYPE key = NODEK(node, kofs)->CEB_KEY_MEMBER;
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_next_dup(root, kofs, CEB_KT_U32, key, 0, NULL, node);
else
return _ceb_next_dup(root, kofs, CEB_KT_U64, 0, key, NULL, node);
}
/* search for the prev node before the specified one containing the same value,
* and return it, or NULL if not found.
*/
CEB_FDECL3(struct ceb_node *, CEB_MKEY_PFX, _prev_dup, struct ceb_root *const *, root, ptrdiff_t, kofs, struct ceb_node *, node)
{
CEB_KEY_TYPE key = NODEK(node, kofs)->CEB_KEY_MEMBER;
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_prev_dup(root, kofs, CEB_KT_U32, key, 0, NULL, node);
else
return _ceb_prev_dup(root, kofs, CEB_KT_U64, 0, key, NULL, node);
}
/* search for the next node after the specified one, and return it, or NULL if
* not found. The approach consists in looking up that node, recalling the last
* time a left turn was made, and returning the first node along the right
* branch at that fork.
*/
CEB_FDECL3(struct ceb_node *, CEB_MKEY_PFX, _next, struct ceb_root *const *, root, ptrdiff_t, kofs, struct ceb_node *, node)
{
CEB_KEY_TYPE key = NODEK(node, kofs)->CEB_KEY_MEMBER;
int is_dup;
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_next(root, kofs, CEB_KT_U32, key, 0, NULL, node, &is_dup);
else
return _ceb_next(root, kofs, CEB_KT_U64, 0, key, NULL, node, &is_dup);
}
/* search for the prev node before the specified one, and return it, or NULL if
* not found. The approach consists in looking up that node, recalling the last
* time a right turn was made, and returning the last node along the left
* branch at that fork.
*/
CEB_FDECL3(struct ceb_node *, CEB_MKEY_PFX, _prev, struct ceb_root *const *, root, ptrdiff_t, kofs, struct ceb_node *, node)
{
CEB_KEY_TYPE key = NODEK(node, kofs)->CEB_KEY_MEMBER;
int is_dup;
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_prev(root, kofs, CEB_KT_U32, key, 0, NULL, node, &is_dup);
else
return _ceb_prev(root, kofs, CEB_KT_U64, 0, key, NULL, node, &is_dup);
}
/* look up the specified node with its key and deletes it if found, and in any
* case, returns the node.
*/
CEB_FDECL3(struct ceb_node *, CEB_MKEY_PFX, _delete, struct ceb_root **, root, ptrdiff_t, kofs, struct ceb_node *, node)
{
CEB_KEY_TYPE key = NODEK(node, kofs)->CEB_KEY_MEMBER;
int is_dup;
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_delete(root, node, kofs, CEB_KT_U32, key, 0, NULL, &is_dup);
else
return _ceb_delete(root, node, kofs, CEB_KT_U64, 0, key, NULL, &is_dup);
}
/* look up the specified key, and detaches it and returns it if found, or NULL
* if not found.
*/
CEB_FDECL3(struct ceb_node *, CEB_MKEY_PFX, _pick, struct ceb_root **, root, ptrdiff_t, kofs, CEB_KEY_TYPE, key)
{
int is_dup;
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_delete(root, NULL, kofs, CEB_KT_U32, key, 0, NULL, &is_dup);
else
return _ceb_delete(root, NULL, kofs, CEB_KT_U64, 0, key, NULL, &is_dup);
}
/*
* Below are the functions that only support unique keys (_cebu_*)
*/
/*****************************************************************************\
* The declarations below always cause two functions to be declared, one *
* starting with "cebu32_*" and one with "cebu32_ofs_*" which takes a key *
* offset just after the root. The one without kofs just has this argument *
* omitted from its declaration and replaced with sizeof(struct ceb_node) in *
* the call to the underlying functions. *
\*****************************************************************************/
/* Inserts node <node> into unique tree <tree> based on its key that
* immediately follows the node. Returns the inserted node or the one
* that already contains the same key.
*/
CEB_FDECL3(struct ceb_node *, CEB_UKEY_PFX, _insert, struct ceb_root **, root, ptrdiff_t, kofs, struct ceb_node *, node)
{
CEB_KEY_TYPE key = NODEK(node, kofs)->CEB_KEY_MEMBER;
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_insert(root, node, kofs, CEB_KT_U32, key, 0, NULL, NULL);
else
return _ceb_insert(root, node, kofs, CEB_KT_U64, 0, key, NULL, NULL);
}
/* return the first node or NULL if not found. */
CEB_FDECL2(struct ceb_node *, CEB_UKEY_PFX, _first, struct ceb_root *const *, root, ptrdiff_t, kofs)
{
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_first(root, kofs, CEB_KT_U32, 0, NULL);
else
return _ceb_first(root, kofs, CEB_KT_U64, 0, NULL);
}
/* return the last node or NULL if not found. */
CEB_FDECL2(struct ceb_node *, CEB_UKEY_PFX, _last, struct ceb_root *const *, root, ptrdiff_t, kofs)
{
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_last(root, kofs, CEB_KT_U32, 0, NULL);
else
return _ceb_last(root, kofs, CEB_KT_U64, 0, NULL);
}
/* look up the specified key, and returns either the node containing it, or
* NULL if not found.
*/
CEB_FDECL3(struct ceb_node *, CEB_UKEY_PFX, _lookup, struct ceb_root *const *, root, ptrdiff_t, kofs, CEB_KEY_TYPE, key)
{
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_lookup(root, kofs, CEB_KT_U32, key, 0, NULL, NULL);
else
return _ceb_lookup(root, kofs, CEB_KT_U64, 0, key, NULL, NULL);
}
/* look up the specified key or the highest below it, and returns either the
* node containing it, or NULL if not found.
*/
CEB_FDECL3(struct ceb_node *, CEB_UKEY_PFX, _lookup_le, struct ceb_root *const *, root, ptrdiff_t, kofs, CEB_KEY_TYPE, key)
{
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_lookup_le(root, kofs, CEB_KT_U32, key, 0, NULL, NULL);
else
return _ceb_lookup_le(root, kofs, CEB_KT_U64, 0, key, NULL, NULL);
}
/* look up highest key below the specified one, and returns either the
* node containing it, or NULL if not found.
*/
CEB_FDECL3(struct ceb_node *, CEB_UKEY_PFX, _lookup_lt, struct ceb_root *const *, root, ptrdiff_t, kofs, CEB_KEY_TYPE, key)
{
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_lookup_lt(root, kofs, CEB_KT_U32, key, 0, NULL, NULL);
else
return _ceb_lookup_lt(root, kofs, CEB_KT_U64, 0, key, NULL, NULL);
}
/* look up the specified key or the smallest above it, and returns either the
* node containing it, or NULL if not found.
*/
CEB_FDECL3(struct ceb_node *, CEB_UKEY_PFX, _lookup_ge, struct ceb_root *const *, root, ptrdiff_t, kofs, CEB_KEY_TYPE, key)
{
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_lookup_ge(root, kofs, CEB_KT_U32, key, 0, NULL, NULL);
else
return _ceb_lookup_ge(root, kofs, CEB_KT_U64, 0, key, NULL, NULL);
}
/* look up the smallest key above the specified one, and returns either the
* node containing it, or NULL if not found.
*/
CEB_FDECL3(struct ceb_node *, CEB_UKEY_PFX, _lookup_gt, struct ceb_root *const *, root, ptrdiff_t, kofs, CEB_KEY_TYPE, key)
{
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_lookup_gt(root, kofs, CEB_KT_U32, key, 0, NULL, NULL);
else
return _ceb_lookup_gt(root, kofs, CEB_KT_U64, 0, key, NULL, NULL);
}
/* search for the next node after the specified one, and return it, or NULL if
* not found. The approach consists in looking up that node, recalling the last
* time a left turn was made, and returning the first node along the right
* branch at that fork.
*/
CEB_FDECL3(struct ceb_node *, CEB_UKEY_PFX, _next, struct ceb_root *const *, root, ptrdiff_t, kofs, struct ceb_node *, node)
{
CEB_KEY_TYPE key = NODEK(node, kofs)->CEB_KEY_MEMBER;
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_next_unique(root, kofs, CEB_KT_U32, key, 0, NULL, NULL);
else
return _ceb_next_unique(root, kofs, CEB_KT_U64, 0, key, NULL, NULL);
}
/* search for the prev node before the specified one, and return it, or NULL if
* not found. The approach consists in looking up that node, recalling the last
* time a right turn was made, and returning the last node along the left
* branch at that fork.
*/
CEB_FDECL3(struct ceb_node *, CEB_UKEY_PFX, _prev, struct ceb_root *const *, root, ptrdiff_t, kofs, struct ceb_node *, node)
{
CEB_KEY_TYPE key = NODEK(node, kofs)->CEB_KEY_MEMBER;
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_prev_unique(root, kofs, CEB_KT_U32, key, 0, NULL, NULL);
else
return _ceb_prev_unique(root, kofs, CEB_KT_U64, 0, key, NULL, NULL);
}
/* look up the specified node with its key and deletes it if found, and in any
* case, returns the node.
*/
CEB_FDECL3(struct ceb_node *, CEB_UKEY_PFX, _delete, struct ceb_root **, root, ptrdiff_t, kofs, struct ceb_node *, node)
{
CEB_KEY_TYPE key = NODEK(node, kofs)->CEB_KEY_MEMBER;
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_delete(root, node, kofs, CEB_KT_U32, key, 0, NULL, NULL);
else
return _ceb_delete(root, node, kofs, CEB_KT_U64, 0, key, NULL, NULL);
}
/* look up the specified key, and detaches it and returns it if found, or NULL
* if not found.
*/
CEB_FDECL3(struct ceb_node *, CEB_UKEY_PFX, _pick, struct ceb_root **, root, ptrdiff_t, kofs, CEB_KEY_TYPE, key)
{
if (sizeof(CEB_KEY_TYPE) <= 4)
return _ceb_delete(root, NULL, kofs, CEB_KT_U32, key, 0, NULL, NULL);
else
return _ceb_delete(root, NULL, kofs, CEB_KT_U64, 0, key, NULL, NULL);
}
/*
* Functions used to dump trees in Dot format. These are only enabled if
* CEB_ENABLE_DUMP is defined.
*/
#if defined(CEB_ENABLE_DUMP)
#include <stdio.h>
#define TO_STR(x) _TO_STR(x)
#define _TO_STR(x) #x
/* dumps a ceb_node tree using the default functions above. If a node matches
* <ctx>, this one will be highlighted in red. If the <sub> value is non-null,
* only a subgraph will be printed. If it's null, and root is non-null, then
* the tree is dumped at once, otherwise if root is NULL, then a prologue is
* dumped when label is not NULL, or the epilogue when label is NULL. As a
* summary:
* sub root label
* 0 NULL NULL epilogue only (closing brace and LF)
* 0 NULL text prologue with <text> as label
* 0 tree * prologue+tree+epilogue at once
* N>0 tree * only the tree, after a prologue and before an epilogue
*/
CEB_FDECL5(void, CEB_MKEY_PFX, _default_dump, struct ceb_root *const *, root, ptrdiff_t, kofs, const char *, label, const void *, ctx, int, sub)
{
if (!sub && label) {
printf("\ndigraph " TO_STR(CEB_MKEY_PFX) "_tree {\n"
" fontname=\"fixed\";\n"
" fontsize=8\n"
" label=\"%s\"\n"
"", label);
printf(" node [fontname=\"fixed\" fontsize=8 shape=\"box\" style=\"filled\" color=\"black\" fillcolor=\"white\"];\n"
" edge [fontname=\"fixed\" fontsize=8 style=\"solid\" color=\"magenta\" dir=\"forward\"];\n");
} else
printf("\n### sub %d ###\n\n", sub);
if (root)
ceb_imm_default_dump_tree(kofs, sizeof(CEB_KEY_TYPE) <= 4 ? CEB_KT_U32 : CEB_KT_U64, root, 0, NULL, 0, ctx, sub, NULL, NULL, NULL, NULL);
if (!sub && (root || !label))
printf("}\n");
}
#endif /* CEB_ENABLE_DUMP */