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
|
/*
* Compact Elastic Binary Trees - exported functions operating on indirect strings
*
* 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.
*/
#ifndef _CEBIS_TREE_H
#define _CEBIS_TREE_H
#include "cebtree.h"
/* simpler version */
struct ceb_node *cebis_imm_insert(struct ceb_root **root, struct ceb_node *node);
struct ceb_node *cebis_imm_first(struct ceb_root *const *root);
struct ceb_node *cebis_imm_last(struct ceb_root *const *root);
struct ceb_node *cebis_imm_lookup(struct ceb_root *const *root, const void *key);
struct ceb_node *cebis_imm_lookup_le(struct ceb_root *const *root, const void *key);
struct ceb_node *cebis_imm_lookup_lt(struct ceb_root *const *root, const void *key);
struct ceb_node *cebis_imm_lookup_ge(struct ceb_root *const *root, const void *key);
struct ceb_node *cebis_imm_lookup_gt(struct ceb_root *const *root, const void *key);
struct ceb_node *cebis_imm_next_unique(struct ceb_root *const *root, struct ceb_node *node);
struct ceb_node *cebis_imm_prev_unique(struct ceb_root *const *root, struct ceb_node *node);
struct ceb_node *cebis_imm_next_dup(struct ceb_root *const *root, struct ceb_node *node);
struct ceb_node *cebis_imm_prev_dup(struct ceb_root *const *root, struct ceb_node *node);
struct ceb_node *cebis_imm_next(struct ceb_root *const *root, struct ceb_node *node);
struct ceb_node *cebis_imm_prev(struct ceb_root *const *root, struct ceb_node *node);
struct ceb_node *cebis_imm_delete(struct ceb_root **root, struct ceb_node *node);
struct ceb_node *cebis_imm_pick(struct ceb_root **root, const void *key);
struct ceb_node *cebuis_imm_insert(struct ceb_root **root, struct ceb_node *node);
struct ceb_node *cebuis_imm_first(struct ceb_root *const *root);
struct ceb_node *cebuis_imm_last(struct ceb_root *const *root);
struct ceb_node *cebuis_imm_lookup(struct ceb_root *const *root, const void *key);
struct ceb_node *cebuis_imm_lookup_le(struct ceb_root *const *root, const void *key);
struct ceb_node *cebuis_imm_lookup_lt(struct ceb_root *const *root, const void *key);
struct ceb_node *cebuis_imm_lookup_ge(struct ceb_root *const *root, const void *key);
struct ceb_node *cebuis_imm_lookup_gt(struct ceb_root *const *root, const void *key);
struct ceb_node *cebuis_imm_next(struct ceb_root *const *root, struct ceb_node *node);
struct ceb_node *cebuis_imm_prev(struct ceb_root *const *root, struct ceb_node *node);
struct ceb_node *cebuis_imm_delete(struct ceb_root **root, struct ceb_node *node);
struct ceb_node *cebuis_imm_pick(struct ceb_root **root, const void *key);
/* generic dump function */
void cebis_imm_default_dump(struct ceb_root *const *root, const char *label, const void *ctx, int sub);
/* returns the pointer to the indirect char* key, not the pointer itself,
* or NULL if node is NULL (note that the indirect pointer cannot be NULL
* if node is non-NULL).
*/
static inline char *cebis_imm_key(const struct ceb_node *node)
{
return node ? *(char **)_ceb_key_ptr(node, sizeof(struct ceb_node)) : NULL;
}
/* version taking a key offset */
struct ceb_node *cebis_ofs_insert(struct ceb_root **root, ptrdiff_t kofs, struct ceb_node *node);
struct ceb_node *cebis_ofs_first(struct ceb_root *const *root, ptrdiff_t kofs);
struct ceb_node *cebis_ofs_last(struct ceb_root *const *root, ptrdiff_t kofs);
struct ceb_node *cebis_ofs_lookup(struct ceb_root *const *root, ptrdiff_t kofs, const void *key);
struct ceb_node *cebis_ofs_lookup_le(struct ceb_root *const *root, ptrdiff_t kofs, const void *key);
struct ceb_node *cebis_ofs_lookup_lt(struct ceb_root *const *root, ptrdiff_t kofs, const void *key);
struct ceb_node *cebis_ofs_lookup_ge(struct ceb_root *const *root, ptrdiff_t kofs, const void *key);
struct ceb_node *cebis_ofs_lookup_gt(struct ceb_root *const *root, ptrdiff_t kofs, const void *key);
struct ceb_node *cebis_ofs_next_unique(struct ceb_root *const *root, ptrdiff_t kofs, struct ceb_node *node);
struct ceb_node *cebis_ofs_prev_unique(struct ceb_root *const *root, ptrdiff_t kofs, struct ceb_node *node);
struct ceb_node *cebis_ofs_next_dup(struct ceb_root *const *root, ptrdiff_t kofs, struct ceb_node *node);
struct ceb_node *cebis_ofs_prev_dup(struct ceb_root *const *root, ptrdiff_t kofs, struct ceb_node *node);
struct ceb_node *cebis_ofs_next(struct ceb_root *const *root, ptrdiff_t kofs, struct ceb_node *node);
struct ceb_node *cebis_ofs_prev(struct ceb_root *const *root, ptrdiff_t kofs, struct ceb_node *node);
struct ceb_node *cebis_ofs_delete(struct ceb_root **root, ptrdiff_t kofs, struct ceb_node *node);
struct ceb_node *cebis_ofs_pick(struct ceb_root **root, ptrdiff_t kofs, const void *key);
struct ceb_node *cebuis_ofs_insert(struct ceb_root **root, ptrdiff_t kofs, struct ceb_node *node);
struct ceb_node *cebuis_ofs_first(struct ceb_root *const *root, ptrdiff_t kofs);
struct ceb_node *cebuis_ofs_last(struct ceb_root *const *root, ptrdiff_t kofs);
struct ceb_node *cebuis_ofs_lookup(struct ceb_root *const *root, ptrdiff_t kofs, const void *key);
struct ceb_node *cebuis_ofs_lookup_le(struct ceb_root *const *root, ptrdiff_t kofs, const void *key);
struct ceb_node *cebuis_ofs_lookup_lt(struct ceb_root *const *root, ptrdiff_t kofs, const void *key);
struct ceb_node *cebuis_ofs_lookup_ge(struct ceb_root *const *root, ptrdiff_t kofs, const void *key);
struct ceb_node *cebuis_ofs_lookup_gt(struct ceb_root *const *root, ptrdiff_t kofs, const void *key);
struct ceb_node *cebuis_ofs_next(struct ceb_root *const *root, ptrdiff_t kofs, struct ceb_node *node);
struct ceb_node *cebuis_ofs_prev(struct ceb_root *const *root, ptrdiff_t kofs, struct ceb_node *node);
struct ceb_node *cebuis_ofs_delete(struct ceb_root **root, ptrdiff_t kofs, struct ceb_node *node);
struct ceb_node *cebuis_ofs_pick(struct ceb_root **root, ptrdiff_t kofs, const void *key);
/* generic dump function taking a key offset */
void cebis_ofs_default_dump(struct ceb_root *const *root, ptrdiff_t kofs, const char *label, const void *ctx, int sub);
/* returns the pointer to the indirect char* key, not the pointer itself,
* or NULL if node is NULL (note that the indirect pointer cannot be NULL
* if node is non-NULL).
*/
static inline char *cebis_ofs_key(const struct ceb_node *node, ptrdiff_t kofs)
{
return node ? *(char **)_ceb_key_ptr(node, kofs) : NULL;
}
/* insert at root <root>, the item <item_ptr> using node member <nname> and key
* member <kname>, and returns either the inserted item, or the one that was
* found there.
*/
#define cebis_item_insert(root, nname, kname, item_ptr) ({ \
ptrdiff_t _nofs = offsetof(typeof(*(item_ptr)), nname); \
ptrdiff_t _kofs = offsetof(typeof(*(item_ptr)), kname) - _nofs; \
struct ceb_node *_node = cebis_ofs_insert(root, _kofs, &(item_ptr)->nname); \
(typeof(item_ptr))((char *)(_node) - _nofs); \
})
/* descend root <root>, and return the first item of type <type>, using node
* member <nname> and key member <kname>, or NULL if the tree is empty.
*/
#define cebis_item_first(root, nname, kname, type) ({ \
ptrdiff_t _nofs = offsetof(type, nname); \
ptrdiff_t _kofs = offsetof(type, kname) - _nofs; \
struct ceb_node *_node = cebis_ofs_first(root, _kofs); \
_node ? (type *)((char *)(_node) - _nofs) : NULL; \
})
/* descend root <root>, and return the last item of type <type>, using node
* member <nname> and key member <kname>, or NULL if the tree is empty.
*/
#define cebis_item_last(root, nname, kname, type) ({ \
ptrdiff_t _nofs = offsetof(type, nname); \
ptrdiff_t _kofs = offsetof(type, kname) - _nofs; \
struct ceb_node *_node = cebis_ofs_last(root, _kofs); \
_node ? (type *)((char *)(_node) - _nofs) : NULL; \
})
/* lookup from root <root>, the first item of type <type> which contains the
* key <key>, using node member <nname> and key member <kname>. Returns the
* pointer to the item, or NULL if not found.
*/
#define cebis_item_lookup(root, nname, kname, key, type) ({ \
ptrdiff_t _nofs = offsetof(type, nname); \
ptrdiff_t _kofs = offsetof(type, kname) - _nofs; \
struct ceb_node *_node = cebis_ofs_lookup(root, _kofs, key); \
_node ? (type *)((char *)(_node) - _nofs) : NULL; \
})
/* lookup from root <root>, the item of type <type> which contains the last of
* the greatest keys that are lower than or equal to <key>, using node member
* <nname> and key member <kname>. Returns the pointer to the item, or NULL if
* not found.
*/
#define cebis_item_lookup_le(root, nname, kname, key, type) ({ \
ptrdiff_t _nofs = offsetof(type, nname); \
ptrdiff_t _kofs = offsetof(type, kname) - _nofs; \
struct ceb_node *_node = cebis_ofs_lookup_le(root, _kofs, key); \
_node ? (type *)((char *)(_node) - _nofs) : NULL; \
})
/* lookup from root <root>, the item of type <type> which contains the last of
* the greatest keys that are strictly lower than <key>, using node member
* <nname> and key member <kname>. Returns the pointer to the item, or NULL if
* not found.
*/
#define cebis_item_lookup_lt(root, nname, kname, key, type) ({ \
ptrdiff_t _nofs = offsetof(type, nname); \
ptrdiff_t _kofs = offsetof(type, kname) - _nofs; \
struct ceb_node *_node = cebis_ofs_lookup_lt(root, _kofs, key); \
_node ? (type *)((char *)(_node) - _nofs) : NULL; \
})
/* lookup from root <root>, the item of type <type> which contains the first of
* the lowest keys key that are greater than or equal to <key>, using node
* member <nname> and key member <kname>. Returns the pointer to the item, or
* NULL if not found.
*/
#define cebis_item_lookup_ge(root, nname, kname, key, type) ({ \
ptrdiff_t _nofs = offsetof(type, nname); \
ptrdiff_t _kofs = offsetof(type, kname) - _nofs; \
struct ceb_node *_node = cebis_ofs_lookup_ge(root, _kofs, key); \
_node ? (type *)((char *)(_node) - _nofs) : NULL; \
})
/* lookup from root <root>, the item of type <type> which contains the first of
* the lowest keys that are strictly greater than <key>, using node member
* <nname> and key member <kname>. Returns the pointer to the item, or NULL if
* not found.
*/
#define cebis_item_lookup_gt(root, nname, kname, key, type) ({ \
ptrdiff_t _nofs = offsetof(type, nname); \
ptrdiff_t _kofs = offsetof(type, kname) - _nofs; \
struct ceb_node *_node = cebis_ofs_lookup_gt(root, _kofs, key); \
_node ? (type *)((char *)(_node) - _nofs) : NULL; \
})
/* lookup from root <root>, the first item after item <item_ptr> that has a
* strictly greater key, using node member <nname> and key member <kname>, and
* returns either the found item, or NULL if none exists.
*/
#define cebis_item_next_unique(root, nname, kname, item_ptr) ({ \
ptrdiff_t _nofs = offsetof(typeof(*(item_ptr)), nname); \
ptrdiff_t _kofs = offsetof(typeof(*(item_ptr)), kname) - _nofs; \
struct ceb_node *_node = cebis_ofs_next_unique(root, _kofs, &(item_ptr)->nname);\
_node ? (typeof(item_ptr))((char *)(_node) - _nofs) : NULL; \
})
/* lookup from root <root>, the last item before item <item_ptr> that has a
* strictly lower key, using node member <nname> and key member <kname>, and
* returns either the found item, or NULL if none exists.
*/
#define cebis_item_prev_unique(root, nname, kname, item_ptr) ({ \
ptrdiff_t _nofs = offsetof(typeof(*(item_ptr)), nname); \
ptrdiff_t _kofs = offsetof(typeof(*(item_ptr)), kname) - _nofs; \
struct ceb_node *_node = cebis_ofs_prev_unique(root, _kofs, &(item_ptr)->nname);\
_node ? (typeof(item_ptr))((char *)(_node) - _nofs) : NULL; \
})
/* lookup from root <root>, the first item after item <item_ptr> that has
* exactly the same key, using node member <nname> and key member <kname>, and
* returns either the found item, or NULL if none exists.
*/
#define cebis_item_next_dup(root, nname, kname, item_ptr) ({ \
ptrdiff_t _nofs = offsetof(typeof(*(item_ptr)), nname); \
ptrdiff_t _kofs = offsetof(typeof(*(item_ptr)), kname) - _nofs; \
struct ceb_node *_node = cebis_ofs_next_dup(root, _kofs, &(item_ptr)->nname); \
_node ? (typeof(item_ptr))((char *)(_node) - _nofs) : NULL; \
})
/* lookup from root <root>, the last item before item <item_ptr> that has
* exactly the same key, using node member <nname> and key member <kname>, and
* returns either the found item, or NULL if none exists.
*/
#define cebis_item_prev_dup(root, nname, kname, item_ptr) ({ \
ptrdiff_t _nofs = offsetof(typeof(*(item_ptr)), nname); \
ptrdiff_t _kofs = offsetof(typeof(*(item_ptr)), kname) - _nofs; \
struct ceb_node *_node = cebis_ofs_prev_dup(root, _kofs, &(item_ptr)->nname); \
_node ? (typeof(item_ptr))((char *)(_node) - _nofs) : NULL; \
})
/* lookup from root <root>, the first item immediately after item <item_ptr>,
* using node member <nname> and key member <kname>, and returns either the
* found item, or NULL if none exists.
*/
#define cebis_item_next(root, nname, kname, item_ptr) ({ \
ptrdiff_t _nofs = offsetof(typeof(*(item_ptr)), nname); \
ptrdiff_t _kofs = offsetof(typeof(*(item_ptr)), kname) - _nofs; \
struct ceb_node *_node = cebis_ofs_next(root, _kofs, &(item_ptr)->nname); \
_node ? (typeof(item_ptr))((char *)(_node) - _nofs) : NULL; \
})
/* lookup from root <root>, the last item immediately before item <item_ptr>,
* using node member <nname> and key member <kname>, and returns either the
* found item, or NULL if none exists.
*/
#define cebis_item_prev(root, nname, kname, item_ptr) ({ \
ptrdiff_t _nofs = offsetof(typeof(*(item_ptr)), nname); \
ptrdiff_t _kofs = offsetof(typeof(*(item_ptr)), kname) - _nofs; \
struct ceb_node *_node = cebis_ofs_prev(root, _kofs, &(item_ptr)->nname); \
_node ? (typeof(item_ptr))((char *)(_node) - _nofs) : NULL; \
})
/* lookup from root <root>, the item <item_ptr> using node member <nname> and
* key member <kname>, deletes it and returns it. If the item is NULL or absent
* from the tree, NULL is returned.
*/
#define cebis_item_delete(root, nname, kname, item_ptr) ({ \
ptrdiff_t _nofs = offsetof(typeof(*(item_ptr)), nname); \
ptrdiff_t _kofs = offsetof(typeof(*(item_ptr)), kname) - _nofs; \
typeof(item_ptr) _item = (item_ptr); \
struct ceb_node *_node = cebis_ofs_delete(root, _kofs, _item ? &_item->nname : NULL); \
_node ? (typeof(item_ptr))((char *)(_node) - _nofs) : NULL; \
})
/* lookup from root <root>, the first item of type <type> which contains the
* key <key>, using node member <nname> and key member <kname>, deletes it and
* returns it. If the key is not found in the tree, NULL is returned.
*/
#define cebis_item_pick(root, nname, kname, key, type) ({ \
ptrdiff_t _nofs = offsetof(type, nname); \
ptrdiff_t _kofs = offsetof(type, kname) - _nofs; \
struct ceb_node *_node = cebis_ofs_pick(root, _kofs, key); \
_node ? (type *)((char *)(_node) - _nofs) : NULL; \
})
/*** versions using unique keys below ***/
/* insert at root <root>, the item <item_ptr> using node member <nname> and key
* member <kname>, and returns either the inserted item, or the one that was
* found there.
*/
#define cebuis_item_insert(root, nname, kname, item_ptr) ({ \
ptrdiff_t _nofs = offsetof(typeof(*(item_ptr)), nname); \
ptrdiff_t _kofs = offsetof(typeof(*(item_ptr)), kname) - _nofs; \
struct ceb_node *_node = cebuis_ofs_insert(root, _kofs, &(item_ptr)->nname); \
(typeof(item_ptr))((char *)(_node) - _nofs); \
})
/* descend root <root>, and return the first item of type <type>, using node
* member <nname> and key member <kname>, or NULL if the tree is empty.
*/
#define cebuis_item_first(root, nname, kname, type) ({ \
ptrdiff_t _nofs = offsetof(type, nname); \
ptrdiff_t _kofs = offsetof(type, kname) - _nofs; \
struct ceb_node *_node = cebuis_ofs_first(root, _kofs); \
_node ? (type *)((char *)(_node) - _nofs) : NULL; \
})
/* descend root <root>, and return the last item of type <type>, using node
* member <nname> and key member <kname>, or NULL if the tree is empty.
*/
#define cebuis_item_last(root, nname, kname, type) ({ \
ptrdiff_t _nofs = offsetof(type, nname); \
ptrdiff_t _kofs = offsetof(type, kname) - _nofs; \
struct ceb_node *_node = cebuis_ofs_last(root, _kofs); \
_node ? (type *)((char *)(_node) - _nofs) : NULL; \
})
/* lookup from root <root>, the item of type <type> which contains the key
* <key>, using node member <nname> and key member <kname>. Returns the pointer
* to the item, or NULL if not found.
*/
#define cebuis_item_lookup(root, nname, kname, key, type) ({ \
ptrdiff_t _nofs = offsetof(type, nname); \
ptrdiff_t _kofs = offsetof(type, kname) - _nofs; \
struct ceb_node *_node = cebuis_ofs_lookup(root, _kofs, key); \
_node ? (type *)((char *)(_node) - _nofs) : NULL; \
})
/* lookup from root <root>, the item of type <type> which contains the greatest
* key that is lower than or equal to <key>, using node member <nname> and key
* member <kname>. Returns the pointer to the item, or NULL if not found.
*/
#define cebuis_item_lookup_le(root, nname, kname, key, type) ({ \
ptrdiff_t _nofs = offsetof(type, nname); \
ptrdiff_t _kofs = offsetof(type, kname) - _nofs; \
struct ceb_node *_node = cebuis_ofs_lookup_le(root, _kofs, key); \
_node ? (type *)((char *)(_node) - _nofs) : NULL; \
})
/* lookup from root <root>, the item of type <type> which contains the greatest
* key that is strictly lower than <key>, using node member <nname> and key
* member <kname>. Returns the pointer to the item, or NULL if not found.
*/
#define cebuis_item_lookup_lt(root, nname, kname, key, type) ({ \
ptrdiff_t _nofs = offsetof(type, nname); \
ptrdiff_t _kofs = offsetof(type, kname) - _nofs; \
struct ceb_node *_node = cebuis_ofs_lookup_lt(root, _kofs, key); \
_node ? (type *)((char *)(_node) - _nofs) : NULL; \
})
/* lookup from root <root>, the item of type <type> which contains the lowest
* key key that is greater than or equal to <key>, using node member <nname>
* and key member <kname>. Returns the pointer to the item, or NULL if not
* found.
*/
#define cebuis_item_lookup_ge(root, nname, kname, key, type) ({ \
ptrdiff_t _nofs = offsetof(type, nname); \
ptrdiff_t _kofs = offsetof(type, kname) - _nofs; \
struct ceb_node *_node = cebuis_ofs_lookup_ge(root, _kofs, key); \
_node ? (type *)((char *)(_node) - _nofs) : NULL; \
})
/* lookup from root <root>, the item of type <type> which contains the lowest
* key that is strictly greater than <key>, using node member <nname> and key
* member <kname>. Returns the pointer to the item, or NULL if not found.
*/
#define cebuis_item_lookup_gt(root, nname, kname, key, type) ({ \
ptrdiff_t _nofs = offsetof(type, nname); \
ptrdiff_t _kofs = offsetof(type, kname) - _nofs; \
struct ceb_node *_node = cebuis_ofs_lookup_gt(root, _kofs, key); \
_node ? (type *)((char *)(_node) - _nofs) : NULL; \
})
/* lookup from root <root>, the first item immediately after item <item_ptr>,
* using node member <nname> and key member <kname>, and returns either the
* found item, or NULL if none exists.
*/
#define cebuis_item_next(root, nname, kname, item_ptr) ({ \
ptrdiff_t _nofs = offsetof(typeof(*(item_ptr)), nname); \
ptrdiff_t _kofs = offsetof(typeof(*(item_ptr)), kname) - _nofs; \
struct ceb_node *_node = cebuis_ofs_next(root, _kofs, &(item_ptr)->nname); \
_node ? (typeof(item_ptr))((char *)(_node) - _nofs) : NULL; \
})
/* lookup from root <root>, the last item immediately before item <item_ptr>,
* using node member <nname> and key member <kname>, and returns either the
* found item, or NULL if none exists.
*/
#define cebuis_item_prev(root, nname, kname, item_ptr) ({ \
ptrdiff_t _nofs = offsetof(typeof(*(item_ptr)), nname); \
ptrdiff_t _kofs = offsetof(typeof(*(item_ptr)), kname) - _nofs; \
struct ceb_node *_node = cebuis_ofs_prev(root, _kofs, &(item_ptr)->nname); \
_node ? (typeof(item_ptr))((char *)(_node) - _nofs) : NULL; \
})
/* lookup from root <root>, the item <item_ptr> using node member <nname> and
* key member <kname>, deletes it and returns it. If the item is NULL or absent
* from the tree, NULL is returned.
*/
#define cebuis_item_delete(root, nname, kname, item_ptr) ({ \
ptrdiff_t _nofs = offsetof(typeof(*(item_ptr)), nname); \
ptrdiff_t _kofs = offsetof(typeof(*(item_ptr)), kname) - _nofs; \
typeof(item_ptr) _item = (item_ptr); \
struct ceb_node *_node = cebuis_ofs_delete(root, _kofs, _item ? &_item->nname : NULL); \
_node ? (typeof(item_ptr))((char *)(_node) - _nofs) : NULL; \
})
/* lookup from root <root>, the first item of type <type> which contains the
* key <key>, using node member <nname> and key member <kname>, deletes it and
* returns it. If the key is not found in the tree, NULL is returned.
*/
#define cebuis_item_pick(root, nname, kname, key, type) ({ \
ptrdiff_t _nofs = offsetof(type, nname); \
ptrdiff_t _kofs = offsetof(type, kname) - _nofs; \
struct ceb_node *_node = cebuis_ofs_pick(root, _kofs, key); \
_node ? (type *)((char *)(_node) - _nofs) : NULL; \
})
#endif /* _CEBIS_TREE_H */
|