Files
cAI/cKit/Search/c_RedBlackBST.h
T
2026-08-10 01:21:15 +08:00

67 lines
1.9 KiB
C

#ifndef INCLUDED_C_REDBLACKBST_H
#define INCLUDED_C_REDBLACKBST_H
#ifndef INCLUDED_C_BASE_H
#include <c_Base.h>
#endif /*INCLUDED_C_BASE_H*/
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
// Link Color Definitions
typedef enum {
C_RB_BLACK = 0,
C_RB_RED = 1
} c_RBColor_t;
// Node Structure Layout
typedef struct c_RBNode {
struct c_RBNode* left;
struct c_RBNode* right;
c_RBColor_t color;
// Payload layout: key block followed immediately by the value block in memory
} c_RBNode_t;
// Red-Black BST Context Structure
typedef struct {
c_RBNode_t* root;
c_size_t key_size;
c_size_t val_size;
c_size_t size;
int (*compar)(const void*, const void*);
} c_RedBlackBST_t;
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
// --- Internal Helper Accessors ---
C_STATIC_FORCE_INLINE
void* c_RBBST_NodeKey(c_RBNode_t* node) {
return (void*)((char*)node + sizeof(c_RBNode_t));
}
C_STATIC_FORCE_INLINE
void* c_RBBST_NodeVal(c_RBNode_t* node, c_size_t key_size) {
return (void*)((char*)node + sizeof(c_RBNode_t) + key_size);
}
C_STATIC_FORCE_INLINE
c_bool_t c_RBBST_IsRed(c_RBNode_t* node) {
if (node == NULL) return C_FALSE;
return node->color == C_RB_RED;
}
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
c_err_t c_RedBlackBST_Init(c_RedBlackBST_t* tree, c_size_t key_size, c_size_t val_size,
int (*compar)(const void*, const void*));
void c_RedBlackBST_Destroy(c_RedBlackBST_t* tree);
c_bool_t c_RedBlackBST_Contains(const c_RedBlackBST_t* tree, const void* key);
c_err_t c_RedBlackBST_Put(c_RedBlackBST_t* tree, const void* key, const void* val);
void* c_RedBlackBST_Get(const c_RedBlackBST_t* tree, const void* key);
#endif /*INCLUDED_C_REDBLACKBST_H*/