cb_radixtree.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * SPDX-License-Identifier: Apache-2.0
  3. *
  4. * Change Logs:
  5. * Date Author Notes
  6. * 2023-10-04 tyx first implementation
  7. */
  8. #ifndef CB_RADIXTREE_H_
  9. #define CB_RADIXTREE_H_
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /*
  14. * If runs on a 64-bit system,
  15. * You are advised to change the value to 16 to reduce the number of search layers
  16. */
  17. #define CB_RADIX_TREE_MAP_SIZE 4
  18. typedef union cb_radix_tree_node_slot
  19. {
  20. union cb_radix_tree_node_slot* next_level;
  21. void* value;
  22. const void* const_value;
  23. }cb_rtslots_t;
  24. struct cb_radix_tree_root;
  25. typedef struct cb_radix_tree_root cb_rtroot_t;
  26. struct cb_radix_tree_root
  27. {
  28. unsigned char height;
  29. unsigned char slot_cnt; // Recommended value 4 or 16
  30. unsigned char mask_bits;
  31. unsigned long long max_key;
  32. void *def_value; // The default value. Mark unused space
  33. union cb_radix_tree_node_slot *root_slots;
  34. cb_rtslots_t *(*alloc_node)(cb_rtroot_t *root, unsigned char slots);
  35. void (*free_node)(cb_rtroot_t *root, cb_rtslots_t *ptr, unsigned char slots);
  36. };
  37. cb_rtroot_t *cb_radix_tree_init(cb_rtroot_t *root, unsigned char slots);
  38. void cb_radix_tree_deinit(cb_rtroot_t* root);
  39. void *cb_radix_tree_lookup(cb_rtroot_t *root, const void *key);
  40. int cb_radix_tree_insert(cb_rtroot_t *root, const void *key, const void *value);
  41. void *cb_radix_tree_remove(cb_rtroot_t *root, const void *key);
  42. void cb_radix_tree_shrink(cb_rtroot_t* root);
  43. #ifdef __cplusplus
  44. }
  45. #endif
  46. #endif /* CB_RADIXTREE_H_ */