169#ifdef ONLY_FOR_DOXYGEN
177#ifndef RBT_NODE_H_PREFIX_
178#error RBT_NODE_H_PREFIX_ is not defined.
182#error RBT_VALUE_T is not defined.
185#ifndef RBT_VALUE_NULL
186#error RBT_VALUE_NULL is not defined.
189#ifndef RBT_VALUE_IS_EQUAL
190#error RBT_VALUE_IS_EQUAL is not defined.
193#ifndef RBT_VALUE_COPY
194#error RBT_VALUE_COPY is not defined.
197#ifndef RBT_VALUE_FREE
198#error RBT_VALUE_FREE is not defined.
201#ifndef RBT_VALUE_FPRINT
202#error RBT_VALUE_FPRINT is not defined.
218#define RBT_NODE_COPY RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_copy)
221#define RBT_NODE_COUNT RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_count)
223#undef RBT_NODE_CREATE
224#define RBT_NODE_CREATE RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_create)
226#undef RBT_NODE_FILTER
227#define RBT_NODE_FILTER RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_filter)
229#undef RBT_NODE_FPRINT
230#define RBT_NODE_FPRINT RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_fprint)
232#undef RBT_NODE_FPRINT_INTERNAL
233#define RBT_NODE_FPRINT_INTERNAL RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_fprint_internal)
236#define RBT_NODE_FREE RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_free)
239#define RBT_KEY_DATA_T RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, key_data_t)
241#undef RBT_NODE_INSERT_CHILD
242#define RBT_NODE_INSERT_CHILD RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_insert_child)
244#undef RBT_NODE_INSERT_PARENT
245#define RBT_NODE_INSERT_PARENT RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_insert_parent)
247#undef RBT_NODE_INSERT_SIBLING
248#define RBT_NODE_INSERT_SIBLING RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_insert_sibling)
250#undef RBT_NODE_IS_COPY
251#define RBT_NODE_IS_COPY RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_is_copy)
253#undef RBT_NODE_MERGE_CHILD
254#define RBT_NODE_MERGE_CHILD RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_merge_child)
257#define RBT_NODE_NEW RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_new)
259#undef RBT_NODE_REMOVE
260#define RBT_NODE_REMOVE RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_remove)
262#undef RBT_NODE_RETRIEVE
263#define RBT_NODE_RETRIEVE RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_retrieve)
266#define RBT_NODE_QUERY RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_query)
268#undef RBT_NODE_TRAVERSE
269#define RBT_NODE_TRAVERSE RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_traverse)
271#undef RBT_NODE_STACK_T
272#define RBT_NODE_STACK_T RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_stack_t)
275#define RBT_NODE_T RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_t)
278#define RBT_VALUE_T RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, value_t)
280#undef RBT_NODE_WITH_PREFIX_SUBTREE_DO
281#define RBT_NODE_WITH_PREFIX_SUBTREE_DO RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, with_prefix_subtree_do)
283#undef RBT_NODE_FILTER_FUNCTION_T
284#define RBT_NODE_FILTER_FUNCTION_T RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_filter_function_t)
286#undef RBT_NODE_FILTER_WITH_KEY_FUNCTION_T
287#define RBT_NODE_FILTER_WITH_KEY_FUNCTION_T RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_filter_with_key_function_t)
289#undef RBT_NODE_TRAVERSE_FUNCTION_T
290#define RBT_NODE_TRAVERSE_FUNCTION_T RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_traverse_function_t)
292#undef RBT_NODE_TRAVERSE_WITH_KEY_FUNCTION_T
293#define RBT_NODE_TRAVERSE_WITH_KEY_FUNCTION_T RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_traverse_with_key_function_t)
296#undef _RBT_NODE_PRINT_STACK_T
297#define _RBT_NODE_PRINT_STACK_T _RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_print_stack_t)
299#undef _RBT_NODE_COPY_STACK_T
300#define _RBT_NODE_COPY_STACK_T _RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_copy_stack_t)
302#undef _RBT_NODE_IS_COPY_STACK_T
303#define _RBT_NODE_IS_COPY_STACK_T _RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_is_copy_stack_t)
305#undef _RBT_NODE_TRAVERSE_STACK_T
306#define _RBT_NODE_TRAVERSE_STACK_T _RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_traverse_stack_t)
308#undef _RBT_NODE_TRAVERSE_WITH_KEY_STACK_T
309#define _RBT_NODE_TRAVERSE_WITH_KEY_STACK_T _RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_traverse_with_key_stack_t)
311#undef _RBT_NODE_FILTER_STACK_T
312#define _RBT_NODE_FILTER_STACK_T _RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_filter_stack_t)
314#undef _RBT_NODE_FILTER_WITH_KEY_STACK_T
315#define _RBT_NODE_FILTER_WITH_KEY_STACK_T _RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_filter_with_key_stack_t)
324#undef _RBT_NODE_STACK_ALLOCATE
325#define _RBT_NODE_STACK_ALLOCATE(stack, stack_tmp, stack_unused, stack_t) \
328 if (stack_unused != NULL) \
330 stack_tmp = stack_unused->next; \
331 stack_unused->next = stack; \
332 stack = stack_unused; \
333 stack_unused = stack_tmp; \
337 stack_tmp = malloc(sizeof(stack_t)); \
338 if (stack_tmp == NULL) \
343 stack_tmp->next = stack; \
350#undef _RBT_NODE_STACK_POP
351#define _RBT_NODE_STACK_POP(stack, stack_tmp, stack_unused) \
354 stack_tmp = stack->next; \
355 stack->next = stack_unused; \
356 stack_unused = stack; \
361#undef _RBT_NODE_STACK_FREE
362#define _RBT_NODE_STACK_FREE(stack, stack_tmp, stack_unused) \
365 while (stack != NULL) \
367 stack_tmp = stack->next; \
371 while (stack_unused != NULL) \
373 stack_tmp = stack_unused->next; \
374 free(stack_unused); \
375 stack_unused = stack_tmp; \
394#undef RBT_VALUE_IS_NULL
395#define RBT_VALUE_IS_NULL(value) RBT_VALUE_IS_EQUAL(value, RBT_VALUE_NULL)
516#ifdef RBT_NODE_CACHE_SIZE
518#define RBT_NODE_CACHE RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_cache)
519#undef RBT_NODE_CACHE_T
520#define RBT_NODE_CACHE_T RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_cache_t)
521#undef RBT_NODE_CACHE_FREE
522#define RBT_NODE_CACHE_FREE RBT_TOKEN_2_W(RBT_NODE_H_PREFIX_, node_cache_free)
529struct RBT_NODE_CACHE_T
558RBT_NODE_CACHE_T RBT_NODE_CACHE = {.node=NULL, .n=0};
565#ifdef RBT_CONCURRENCY_PTHREAD
566# include "concurrency/pthread.h"
571#ifdef RBT_NODE_CACHE_SIZE
572# ifndef RBT_NODE_CACHE_LOCK
573# define RBT_NODE_CACHE_LOCK
576# ifndef RBT_NODE_CACHE_UNLOCK
577# define RBT_NODE_CACHE_UNLOCK
591 while (RBT_NODE_CACHE.node != NULL)
593 node_tmp = (RBT_NODE_CACHE.node)->left;
594 free(RBT_NODE_CACHE.node);
595 RBT_NODE_CACHE.node = node_tmp;
597 RBT_NODE_CACHE.n = 0;
598 RBT_NODE_CACHE_UNLOCK
768struct _RBT_NODE_PRINT_STACK_T
780 RBT_KEY_SIZE_T indent;
809 struct _RBT_NODE_PRINT_STACK_T * next;
811_RBT_NODE_PRINT_STACK_T;
864 RBT_KEY_SIZE_T indent,
871 _RBT_NODE_PRINT_STACK_T * stack, * stack_tmp, * stack_unused;
873 unsigned long shifted_vert;
891 fprintf(fd,
"\033[32m");
893 fprintf(fd,
"%*p ", print_pointer, node);
897 fprintf(fd,
"\033[34m");
902 for (i = indent - 1; i; i--)
904 shifted_vert = (vert >> i);
905 if (shifted_vert & 1)
928 fprintf(fd,
"\033[35m");
936 i = node->
bits - skip;
949 fprintf(fd,
"\033[36m");
952 RBT_VALUE_FPRINT(fd, node->
value);
957 if (node->
left != NULL)
959 is_last_child = (node->
right == NULL);
966 _RBT_NODE_STACK_ALLOCATE(stack, stack_tmp, stack_unused, _RBT_NODE_PRINT_STACK_T);
967 stack->node = node->
right;
968 stack->indent = indent;
969 stack->vert = vert - 1;
970 stack->is_last_child = 1;
975 else if (node->
right != NULL)
981 else if (stack != NULL)
984 indent = stack->indent;
986 is_last_child = stack->is_last_child;
988 _RBT_NODE_STACK_POP(stack, stack_tmp, stack_unused);
997 fprintf(fd,
"\033[0m");
1000 _RBT_NODE_STACK_FREE(stack, stack_tmp, stack_unused);
1084 RBT_KEY_SIZE_T bits,
1091 RBT_KEY_SIZE_T bytes;
1093#ifdef RBT_NODE_CACHE_SIZE
1095 node = RBT_NODE_CACHE.node;
1098 RBT_NODE_CACHE.node = node->
left;
1099 RBT_NODE_CACHE.n --;
1100 RBT_NODE_CACHE_UNLOCK
1104 RBT_NODE_CACHE_UNLOCK
1111 debug_print(
"failed to allocate memory for node\n");
1115#ifdef RBT_NODE_CACHE_SIZE
1120 node->
key = malloc(bytes);
1121 if (node->
key == NULL)
1123 debug_printf(
"failed to allocate %" RBT_KEY_SIZE_T_FORMAT
" bytes for key\n", bits);
1127 memcpy(node->
key, key, bytes);
1129 node->
value = RBT_VALUE_NULL;
1130 RBT_VALUE_COPY(node->
value, value, free(node->
key);free(node);
return NULL);
1132 node->
right = right;
1144#ifdef RBT_NODE_CACHE_SIZE
1148 #undef RBT_NODE_CACHE_OR_FREE
1149 #define RBT_NODE_CACHE_OR_FREE(node) \
1152 RBT_NODE_CACHE_LOCK \
1154 ! RBT_NODE_CACHE_SIZE || \
1155 RBT_NODE_CACHE.n <= RBT_NODE_CACHE_SIZE \
1158 node->left = RBT_NODE_CACHE.node; \
1159 RBT_NODE_CACHE.node = node; \
1160 RBT_NODE_CACHE.n ++; \
1161 RBT_NODE_CACHE_UNLOCK \
1165 RBT_NODE_CACHE_UNLOCK \
1174 #undef RBT_NODE_CACHE_OR_FREE
1175 #define RBT_NODE_CACHE_OR_FREE(node) free(node)
1224 RBT_VALUE_FREE(node->
value);
1225 if (node->
right == NULL)
1227 if (node->
left == NULL)
1240 else if (node->
left == NULL)
1248 orphan = node->
right;
1252 if (descendent == NULL)
1255 while (descendent->
left !=NULL)
1257 descendent = descendent->
left;
1260 descendent->
left = orphan;
1261 descendent = orphan;
1262 while (descendent->
left !=NULL)
1264 descendent = descendent->
left;
1297 debug_printf(
"merging child node %p (parent: %p)\n", child_node, parent_node);
1298 RBT_PIN_T * tmp_key;
1299 RBT_KEY_SIZE_T parent_bytes, child_bytes;
1310 tmp_key = realloc(parent_node->
key, parent_bytes + child_bytes);
1311 if (tmp_key == NULL)
1313 debug_printf(
"failed to realloc key (%s)\n", strerror(errno));
1317 parent_node->
key = tmp_key;
1318 memcpy(((
BYTE_T *) parent_node->
key) + parent_bytes, child_node->
key, child_bytes);
1325 tmp_value = parent_node->
value;
1327 child_node->
value = tmp_value;
1333 if (parent_node->
left == child_node)
1335 tmp_node = parent_node->
right;
1339 tmp_node = parent_node->
left;
1341 parent_node->
left = child_node->
left;
1344 child_node->
left = tmp_node;
1345 child_node->
right = NULL;
1384 debug_printf(
"removing node %p (parent: %p)\n", node, parent_node);
1386 if (node->
left == NULL)
1388 if (node->
right == NULL)
1395 if (parent_node == NULL)
1398 RBT_VALUE_COPY(node->
value, RBT_VALUE_NULL, );
1412 if (parent_node->
left == node)
1423 if (parent_node->
left == node)
1425 parent_node->
left = NULL;
1429 parent_node->
right = NULL;
1448 else if (node->
right == NULL)
1460 RBT_VALUE_COPY(node->
value, RBT_VALUE_NULL, );
1493 RBT_KEY_SIZE_T bits,
1498 RBT_KEY_SIZE_T pins, staggered_bits, parent_pins;
1500 RBT_PIN_T * tmp_key;
1511 node->
bits - bits + staggered_bits,
1518 debug_printf(
"failed to create child node (%s)\n", strerror(errno));
1523 if (staggered_bits > 0)
1528 if (tmp_key == NULL && parent_pins)
1530 debug_printf(
"failed to realloc key (%s)\n", strerror(errno));
1533 node->
key = tmp_key;
1538 value = child->
value;
1540 node->
value = value;
1544 node->
right = child;
1550 node->
right = child;
1585 RBT_KEY_SIZE_T bits,
1592 * child_node_ptr = node;
1653 RBT_KEY_SIZE_T bits,
1654 RBT_KEY_SIZE_T common_bits,
1655 RBT_KEY_SIZE_T common_pins,
1656 RBT_KEY_SIZE_T common_staggered_bits,
1660 RBT_KEY_SIZE_T parent_pins;
1662 RBT_PIN_T * tmp_key;
1670 "inserting sibling (%"
1671 RBT_KEY_SIZE_T_FORMAT
", %"
1672 RBT_KEY_SIZE_T_FORMAT
", %"
1673 RBT_KEY_SIZE_T_FORMAT
", %"
1674 RBT_KEY_SIZE_T_FORMAT
")\n",
1675 bits, common_bits, common_pins, common_staggered_bits
1680 bits - common_bits + common_staggered_bits,
1688 debug_printf(
"failed to create baby node (%s)\n", strerror(errno));
1698 node->
key + common_pins,
1699 node->
bits - common_bits + common_staggered_bits,
1705 if (sibling == NULL)
1707 debug_printf(
"failed to create sibling node (%s)\n", strerror(errno));
1713 parent_pins = common_pins;
1714 if (common_staggered_bits > 0)
1720 if (tmp_key == NULL && parent_pins)
1722 debug_printf(
"failed to realloc key (%s)\n", strerror(errno));
1728 node->
key = tmp_key;
1729 node->
bits = common_bits;
1731 value = node->
value;
1733 sibling->
value = value;
1738 node->
left = sibling;
1743 node->
right = sibling;
1786struct _RBT_NODE_COPY_STACK_T
1806 struct _RBT_NODE_COPY_STACK_T * next;
1808_RBT_NODE_COPY_STACK_T;
1842 _RBT_NODE_COPY_STACK_T * stack, * stack_tmp, * stack_unused;
1853 child_ptr = &new_root;
1855 stack_unused = NULL;
1858 while (node != NULL)
1861 * child_ptr = new_node;
1863 if (node->
left != NULL)
1865 if (node->
right != NULL)
1867 _RBT_NODE_STACK_ALLOCATE(stack, stack_tmp, stack_unused, _RBT_NODE_COPY_STACK_T);
1868 stack->node = node->
right;
1869 stack->ptr = &(new_node->
right);
1871 child_ptr = &(new_node->
left);
1874 else if (node->
right != NULL)
1876 child_ptr = &(new_node->
right);
1879 else if (stack != NULL)
1882 child_ptr = stack->ptr;
1883 _RBT_NODE_STACK_POP(stack, stack_tmp, stack_unused);
1894 _RBT_NODE_STACK_FREE(stack, stack_tmp, stack_unused);
1918struct _RBT_NODE_FILTER_STACK_T
1936 struct _RBT_NODE_FILTER_STACK_T * next;
1938_RBT_NODE_FILTER_STACK_T;
2008 int rc, unwanted, is_left, placeholder;
2010 RBT_NODE_STACK_T * parent_stack, * parent_stack_tmp, * parent_stack_unused;
2011 _RBT_NODE_FILTER_STACK_T * stack, * stack_tmp, * stack_unused;
2022 stack_unused = NULL;
2023 parent_stack = NULL;
2024 parent_stack_unused = NULL;
2038 if (parent_stack != NULL)
2040 debug_printf(
"filtering node: %p (parent: %p)\n", node, parent_stack->
node);
2044 debug_printf(
"filtering node: %p (no parent)\n", node);
2048 va_start(func_args, include_empty);
2053 unwanted = func_v(&(node->
value), func_args);
2062 if (parent_stack != NULL)
2064 is_left = parent_stack->
node->
left == node;
2070 placeholder = (node->
left != NULL) && (node->
right != NULL);
2073 sibling_node = parent_stack->
node->
right;
2077 sibling_node = parent_stack->
node->
left;
2089 if (tmp_node != node)
2096 if (parent_stack->
node->
right == sibling_node)
2098 node = sibling_node;
2105 node = parent_stack->
node;
2106 _RBT_NODE_STACK_POP(parent_stack, parent_stack_tmp, parent_stack_unused);
2107 if (stack != NULL && stack->parent_stack->node == node)
2109 _RBT_NODE_STACK_POP(stack, stack_tmp, stack_unused);
2122 while (parent_stack != NULL && parent_stack != stack->parent_stack)
2124 _RBT_NODE_STACK_POP(parent_stack, parent_stack_tmp, parent_stack_unused);
2126 _RBT_NODE_STACK_POP(stack, stack_tmp, stack_unused);
2156 if (node->
left != NULL)
2158 _RBT_NODE_STACK_ALLOCATE(parent_stack, parent_stack_tmp, parent_stack_unused,
RBT_NODE_STACK_T);
2159 parent_stack->
node = node;
2160 if (node->
right != NULL)
2162 _RBT_NODE_STACK_ALLOCATE(stack, stack_tmp, stack_unused, _RBT_NODE_FILTER_STACK_T);
2163 stack->node = node->
right;
2164 stack->parent_stack = parent_stack;
2169 else if (node->
right != NULL)
2171 _RBT_NODE_STACK_ALLOCATE(parent_stack, parent_stack_tmp, parent_stack_unused,
RBT_NODE_STACK_T);
2172 parent_stack->
node = node;
2180 while (parent_stack != NULL && parent_stack != stack->parent_stack)
2182 _RBT_NODE_STACK_POP(parent_stack, parent_stack_tmp, parent_stack_unused);
2184 _RBT_NODE_STACK_POP(stack, stack_tmp, stack_unused);
2191 _RBT_NODE_STACK_FREE(parent_stack, parent_stack_tmp, parent_stack_unused);
2192 _RBT_NODE_STACK_FREE(stack, stack_tmp, stack_unused);
2213struct _RBT_NODE_IS_COPY_STACK_T
2231 struct _RBT_NODE_IS_COPY_STACK_T * next;
2233_RBT_NODE_IS_COPY_STACK_T;
2265 _RBT_NODE_IS_COPY_STACK_T * stack, * stack_tmp, * stack_unused;
2287 stack_unused = NULL;
2294 (a->
left == NULL) != (b->
left == NULL) ||
2303 if (a->
left != NULL)
2305 if (a->
right != NULL)
2307 _RBT_NODE_STACK_ALLOCATE(stack, stack_tmp, stack_unused, _RBT_NODE_IS_COPY_STACK_T);
2308 stack->a = a->
right;
2309 stack->b = b->
right;
2314 else if (a->
right != NULL)
2319 else if (stack != NULL)
2323 _RBT_NODE_STACK_POP(stack, stack_tmp, stack_unused);
2330 _RBT_NODE_STACK_FREE(stack, stack_tmp, stack_unused);
2348#ifndef _RBT_RETRIEVE_RETURN_WITH_PARENT
2349 #define _RBT_RETRIEVE_RETURN_WITH_PARENT(node, parent_node_ptr) \
2352 if (parent_node_ptr != NULL) \
2354 * parent_node_ptr = parent_node; \
2444 RBT_KEY_SIZE_T bits,
2451 debug_printf(
"bits: %" RBT_KEY_SIZE_T_FORMAT
"\n", bits);
2459 RBT_KEY_SIZE_T common_bits, common_staggered_bits, common_pins;
2462 RBT_PIN_T * tmp_key;
2466 child_node_ptr = NULL;
2472 if (node->
bits == 0)
2485 RBT_VALUE_COPY(node->
value, value,
return NULL);
2488 _RBT_RETRIEVE_RETURN_WITH_PARENT(node, parent_node_ptr);
2498 else if (node->
left == NULL && node->
right == NULL)
2515 tmp_key = malloc(common_pins);
2516 if (tmp_key == NULL)
2519 "failed to malloc %" RBT_KEY_SIZE_T_FORMAT
" bytes for key (error: %s)\n",
2526 node->
key = tmp_key;
2527 memcpy(node->
key, key, common_pins);
2529 RBT_VALUE_COPY(node->
value, value,
return NULL);
2538 debug_printf(
"failed to create node (%s)\n", strerror(errno));
2543 parent_node->
right = node;
2547 parent_node->
left = node;
2551 _RBT_RETRIEVE_RETURN_WITH_PARENT(node, parent_node_ptr);
2565 debug_print(
"root node has at least one child\n");
2570 child_node_ptr = &node->
right;
2575 child_node_ptr = &node->
left;
2581 if (* child_node_ptr == NULL)
2583 debug_print(
"target child of root node is missing\n");
2591 debug_printf(
"failed to create node (%s)\n", strerror(errno));
2594 * child_node_ptr = node;
2596 _RBT_RETRIEVE_RETURN_WITH_PARENT(node, parent_node_ptr);
2606 node = * child_node_ptr;
2625 debug_printf(
"common bits: %" RBT_KEY_SIZE_T_FORMAT
"\n", common_bits);
2630 if (common_bits == bits)
2635 if (common_bits == node->
bits)
2641 if (parent_node_ptr != NULL)
2643 (* parent_node_ptr)->bits = 0;
2649 RBT_VALUE_COPY(node->
value, value,
return NULL);
2652 _RBT_RETRIEVE_RETURN_WITH_PARENT(node, parent_node_ptr);
2667 if (parent_node_ptr != NULL)
2669 (* parent_node_ptr)->bits = node->
bits - common_bits;
2681 _RBT_RETRIEVE_RETURN_WITH_PARENT(node, parent_node_ptr);
2697 if (common_bits == node->
bits)
2700 bits += common_staggered_bits - common_bits;
2703 if (
N_BIT_IS_1(key[0], common_staggered_bits))
2706 child_node_ptr = &(node->
right);
2711 child_node_ptr = &(node->
left);
2716 if (* child_node_ptr == NULL)
2729 node = * child_node_ptr;
2730 _RBT_RETRIEVE_RETURN_WITH_PARENT(node, parent_node_ptr);
2741 node = * child_node_ptr;
2762 common_staggered_bits,
2765 _RBT_RETRIEVE_RETURN_WITH_PARENT(node, parent_node_ptr);
2779 errno = ENOTRECOVERABLE;
2838 RBT_KEY_SIZE_T bits,
2845 int effective_deletion;
2857 if (effective_deletion)
2886 return RBT_VALUE_NULL;
2892 return RBT_VALUE_NULL;
2906 if (errno || target_node == NULL)
2908 return RBT_VALUE_NULL;
2912 return target_node->
value;
2929 return RBT_VALUE_NULL;
2947 return RBT_VALUE_NULL;
2953 target_value = target_node->
value;
2954 target_node->
value = RBT_VALUE_NULL;
2955 if (effective_deletion)
2960 return RBT_VALUE_NULL;
2968 RBT_VALUE_COPY(target_node->
value, value, target_node->
value = target_value;
return value);
2970 return target_value;
2986 return RBT_VALUE_NULL;
2988 target_value = target_node->
value;
2989 if (effective_deletion)
2994 target_node->
value = RBT_VALUE_NULL;
2998 return RBT_VALUE_NULL;
3003 target_node->
value = value;
3005 return target_value;
3012 return RBT_VALUE_NULL;
3036typedef struct _RBT_NODE_TRAVERSE_STACK_T
3049 RBT_KEY_SIZE_T height;
3055 struct _RBT_NODE_TRAVERSE_STACK_T * next;
3057_RBT_NODE_TRAVERSE_STACK_T;
3091 RBT_KEY_SIZE_T height,
3122 _RBT_NODE_TRAVERSE_STACK_T * stack, * stack_tmp, * stack_unused;
3123 RBT_KEY_SIZE_T height;
3135 stack_unused = NULL;
3140 va_start(func_args, func_v);
3141 if (func_v(node, height, func_args) || errno)
3148 if (node->
right == NULL)
3150 if (node->
left == NULL)
3159 height = stack->height;
3160 _RBT_NODE_STACK_POP(stack, stack_tmp, stack_unused);
3170 else if (node->
left == NULL)
3177 _RBT_NODE_STACK_ALLOCATE(stack, stack_tmp, stack_unused, _RBT_NODE_TRAVERSE_STACK_T);
3179 stack->node = node->
right;
3180 stack->height = height;
3184 _RBT_NODE_STACK_FREE(stack, stack_tmp, stack_unused);
3212typedef struct _RBT_NODE_FILTER_WITH_KEY_STACK_T
3224 RBT_KEY_SIZE_T bytes_in_key_prefix;
3236 struct _RBT_NODE_FILTER_WITH_KEY_STACK_T * next;
3238_RBT_NODE_FILTER_WITH_KEY_STACK_T;
3367 stack_unused = NULL;
3388 _RBT_NODE_STACK_POP(stack, stack_tmp, stack_unused);
3408 _RBT_NODE_STACK_FREE(stack, stack_tmp, stack_unused);
3448 RBT_KEY_SIZE_T bits,
3449 void (* func_v)(
RBT_NODE_T * subtree, va_list args),
3453 RBT_NODE_T subroot_node, * tmp_node, * node_ptr;
3454 RBT_KEY_SIZE_T bytes, prefix_bytes, total_bits, extra_bits;
3457 node_ptr = & subroot_node;
3466 if (tmp_node == NULL)
3471 extra_bits = subroot_node.
bits;
3472 total_bits = bits + extra_bits;
3474 subroot_node.
key = malloc(bytes);
3475 if (subroot_node.
key == NULL)
3477 debug_printf(
"failed to allocate memory for key (%s)\n", strerror(errno));
3481 memcpy(subroot_node.
key, key, prefix_bytes);
3482 memcpy(subroot_node.
key + prefix_bytes, tmp_node->
key, bytes - prefix_bytes);
3483 subroot_node.
bits = total_bits;
3485 subroot_node.
left = tmp_node->
left;
3489 va_start(func_args, func_v);
3490 func_v(&subroot_node, func_args);
3493 free(subroot_node.
key);
#define N_BIT_IS_1(x, n)
Determine if the nth bit is 1.
Definition: common.h:304
#define BITS_PER_BYTE
The number of bits in a byte.
Definition: common.h:190
const char * rbt_retrieve_action_string(rbt_retrieve_action_t action)
Return a string representing a retrieval action.
Definition: common.h:377
#define MIN(a, b)
Return the minimum of a or b.
Definition: common.h:203
#define RBT_TOKEN_2_W(a, b)
A wrapper for RBT_TOKEN_2() to ensure macro expansion.
Definition: common.h:240
#define FIRST_BIT_IS_1(x)
Determine if the most significant bit is 1.
Definition: common.h:292
#define BYTE_T
A byte type.
Definition: common.h:184
rbt_query_action_t
Query action for RBT_NODE_QUERY().
Definition: common.h:408
@ RBT_QUERY_ACTION_RETRIEVE_AND_INSERT
Definition: common.h:427
@ RBT_QUERY_ACTION_SWAP
Definition: common.h:435
@ RBT_QUERY_ACTION_RETRIEVE
Definition: common.h:422
@ RBT_QUERY_ACTION_DELETE
Definition: common.h:412
@ RBT_QUERY_ACTION_INSERT
Definition: common.h:417
rbt_retrieve_action_t
Retrieval action for RBT_NODE_RETRIEVE().
Definition: common.h:330
@ RBT_RETRIEVE_ACTION_NOTHING
Definition: common.h:334
@ RBT_RETRIEVE_ACTION_PREFIX_SUBTREE
Definition: common.h:364
@ RBT_RETRIEVE_ACTION_INSERT_OR_REPLACE
Definition: common.h:346
@ RBT_RETRIEVE_ACTION_INSERT
Definition: common.h:340
#define debug_print(msg)
Definition: debug.h:160
#define error_print(msg)
Definition: debug.h:234
#define debug_printf(fmt,...)
Definition: debug.h:138
#define debug_print_func(func, print_newline,...)
Definition: debug.h:190
#define BITS_TO_PINS_TO_BYTES(x)
Definition: key.h:162
#define RBT_DIVMOD(a, b, c, d)
Definition: key.h:224
#define RBT_PIN_SIZE
Definition: key.h:128
RBT_KEY_SIZE_T RBT_COMMON_BIT_PREFIX_LEN(RBT_PIN_T *a, RBT_PIN_T *b, RBT_KEY_SIZE_T max)
Definition: key.h:250
#define RBT_PIN_SIZE_BITS
Definition: key.h:133
#define PINS_TO_BYTES(x)
Definition: key.h:151
void RBT_FPRINT_BITS(FILE *fd, RBT_PIN_T *key, RBT_KEY_SIZE_T n, RBT_KEY_SIZE_T skip)
Definition: key.h:189
void RBT_NODE_MERGE_CHILD(RBT_NODE_T *parent_node, RBT_NODE_T *child_node)
Definition: node.h:1292
RBT_VALUE_T RBT_VALUE_T
The node's value type.
Definition: node.h:409
#define RBT_VALUE_IS_NULL(value)
Definition: node.h:395
void RBT_NODE_FILTER(RBT_NODE_T *node, RBT_NODE_FILTER_FUNCTION_T func_v, int include_empty,...)
Filter tree nodes.
Definition: node.h:2001
struct _RBT_NODE_TRAVERSE_WITH_KEY_STACK_T _RBT_NODE_TRAVERSE_WITH_KEY_STACK_T
Linked list for implementing RBT_NODE_TRAVERSE_WITH_KEY's dynamic stack.
RBT_NODE_T * RBT_NODE_CREATE(RBT_PIN_T *key, RBT_KEY_SIZE_T bits, RBT_VALUE_T value, RBT_NODE_T *left, RBT_NODE_T *right)
Create a node.
Definition: node.h:1082
int(* RBT_NODE_TRAVERSE_WITH_KEY_FUNCTION_T)(RBT_KEY_DATA_T *key_data, RBT_KEY_SIZE_T height, va_list args)
A tree traversal function.
Definition: node.h:3335
void RBT_NODE_INSERT_PARENT(RBT_NODE_T *node, RBT_KEY_SIZE_T bits, RBT_VALUE_T value)
Insert a parent node.
Definition: node.h:1491
int(* RBT_NODE_FILTER_WITH_KEY_FUNCTION_T)(RBT_KEY_DATA_T *key_data, va_list args)
The function type accepted by RBT_NODE_FILTER_WITH_KEY().
Definition: node.h:3302
int(* RBT_NODE_FILTER_FUNCTION_T)(RBT_VALUE_T *value, va_list args)
The function type accepted by RBT_NODE_FILTER().
Definition: node.h:1962
RBT_NODE_T * RBT_NODE_REMOVE(RBT_NODE_T *node, RBT_NODE_T *parent_node)
Remove a node from a tree.
Definition: node.h:1379
RBT_NODE_T * RBT_NODE_NEW()
Create a new tree.
Definition: node.h:1759
RBT_NODE_T * RBT_NODE_COPY(RBT_NODE_T *node)
Copy a tree.
Definition: node.h:1836
struct RBT_NODE_T RBT_NODE_T
Rabbit Tree node type.
int RBT_NODE_IS_COPY(RBT_NODE_T *a, RBT_NODE_T *b)
Check if a tree is a copy.
Definition: node.h:2260
RBT_KEY_SIZE_T RBT_NODE_COUNT(RBT_NODE_T *node)
Definition: node.h:3350
RBT_NODE_T * RBT_NODE_INSERT_SIBLING(RBT_NODE_T *node, RBT_PIN_T *key, RBT_KEY_SIZE_T bits, RBT_KEY_SIZE_T common_bits, RBT_KEY_SIZE_T common_pins, RBT_KEY_SIZE_T common_staggered_bits, RBT_VALUE_T value)
Insert a sibling node.
Definition: node.h:1650
#define RBT_NODE_CACHE_OR_FREE(node)
Definition: node.h:1175
void RBT_NODE_FPRINT_INTERNAL(FILE *fd, RBT_NODE_T *node, int print_bits, int print_pointer, RBT_KEY_SIZE_T indent, RBT_KEY_SIZE_T vert, int is_last_child, int skip)
Definition: node.h:859
RBT_NODE_T * RBT_NODE_RETRIEVE(RBT_NODE_T *node, RBT_PIN_T *key, RBT_KEY_SIZE_T bits, rbt_retrieve_action_t action, RBT_VALUE_T value, RBT_NODE_T **parent_node_ptr)
Retrieve a node matching the given key.
Definition: node.h:2441
RBT_VALUE_T RBT_NODE_QUERY(RBT_NODE_T *root_node, RBT_PIN_T *key, RBT_KEY_SIZE_T bits, rbt_query_action_t action, RBT_VALUE_T value)
Query a tree.
Definition: node.h:2835
int RBT_NODE_WITH_PREFIX_SUBTREE_DO(RBT_NODE_T *node, RBT_PIN_T *key, RBT_KEY_SIZE_T bits, void(*func_v)(RBT_NODE_T *subtree, va_list args),...)
Perform some action on the subtree defined by a common prefix.
Definition: node.h:3445
struct RBT_NODE_STACK_T RBT_NODE_STACK_T
Linked list of nodes.
void RBT_NODE_FPRINT(FILE *fd, RBT_NODE_T *node, int print_bits, int print_pointer)
Print trees using box-drawing characters.
Definition: node.h:1036
void RBT_NODE_INSERT_CHILD(RBT_NODE_T **child_node_ptr, RBT_PIN_T *key, RBT_KEY_SIZE_T bits, RBT_VALUE_T value)
Insert a child node.
Definition: node.h:1582
void RBT_NODE_FREE(RBT_NODE_T *node)
Definition: node.h:1190
void RBT_NODE_TRAVERSE(RBT_NODE_T *node, RBT_NODE_TRAVERSE_FUNCTION_T func_v,...)
Definition: node.h:3115
struct RBT_KEY_DATA_T RBT_KEY_DATA_T
Rabbit Tree key data type with associated node.
int(* RBT_NODE_TRAVERSE_FUNCTION_T)(RBT_NODE_T *node, RBT_KEY_SIZE_T height, va_list args)
A tree traversal function.
Definition: node.h:3089
Rabbit Tree key data type with associated node.
Definition: node.h:478
RBT_KEY_SIZE_T bits
Number of significant bits in the key.
Definition: node.h:492
RBT_PIN_T * key
The key.
Definition: node.h:486
RBT_KEY_SIZE_T bytes
Number of significant bits in the key.
Definition: node.h:502
RBT_NODE_T * node
The associated node.
Definition: node.h:508
Linked list of nodes.
Definition: node.h:617
struct RBT_NODE_STACK_T * next
Definition: node.h:626
RBT_NODE_T * node
Definition: node.h:621
Rabbit Tree node type.
Definition: node.h:420
struct RBT_NODE_T * right
Right child node.
Definition: node.h:462
RBT_KEY_SIZE_T bits
Number of significant bits in the key.
Definition: node.h:438
struct RBT_NODE_T * left
Left child node.
Definition: node.h:454
RBT_VALUE_T value
Value associated with the node.
Definition: node.h:446
RBT_PIN_T * key
Key segment associated with this node.
Definition: node.h:428
Linked list for implementing RBT_NODE_TRAVERSE_WITH_KEY's dynamic stack.
Definition: node.h:3255
RBT_KEY_SIZE_T bytes_in_key_prefix
The number of bytes in the key prefix for this node.
Definition: node.h:3266
RBT_KEY_SIZE_T height
The height of the node above the root node (or the depth, depending on how you look at it).
Definition: node.h:3273
RBT_NODE_T * node
The node on the stack.
Definition: node.h:3260
struct _RBT_NODE_TRAVERSE_WITH_KEY_STACK_T * next
The next item in the stack.
Definition: node.h:3279