5#include "libxr_assert.hpp"
6#include "libxr_def.hpp"
22template <
typename Key>
62 template <
typename Data>
84 template <
typename...
Args>
96 const Data *operator->()
const {
return &
data_; }
121 template <
typename Data, SizeLimitMode LimitMode = SizeLimitMode::MORE>
145 BaseNode *child =
nullptr, *parent =
nullptr;
182 parent->
left = child;
199 RbtreeDeleteFixup(child, parent);
216 (parent->left == &
node ? parent->left : parent->right) = child;
225 RbtreeDeleteFixup(child, parent);
236 template <
typename KeyType>
241 node.right =
nullptr;
242 node.parent =
nullptr;
246 node.key = std::forward<KeyType>(key);
274 template <
typename Data,
typename Func, SizeLimitMode LimitMode = SizeLimitMode::MORE>
290 template <
typename Data>
303 else if (
node->right)
311 else if (
node->parent)
348 RbtreeInsertFixup(&
node);
351 void RbtreeInsertFixup(BaseNode *
node)
353 BaseNode *parent =
nullptr, *
gparent =
nullptr;
371 if (
node == parent->right)
373 BaseNode *
tmp =
nullptr;
374 RbtreeLeftRotate(parent);
396 if (
node == parent->left)
398 BaseNode *
tmp =
nullptr;
399 RbtreeRightRotate(parent);
413 void RbtreeLeftRotate(BaseNode *
x)
420 BaseNode *
y =
x->right;
427 y->parent =
x->parent;
435 if (
x ==
x->parent->left)
449 void RbtreeRightRotate(BaseNode *
y)
471 if (
y ==
y->parent->right)
485 void RbtreeDeleteFixup(BaseNode *
node, BaseNode *parent)
487 BaseNode *
other =
nullptr;
491 if (parent->left ==
node)
498 RbtreeLeftRotate(parent);
499 other = parent->right;
506 parent =
node->parent;
514 RbtreeRightRotate(
other);
515 other = parent->right;
517 other->color = parent->color;
520 RbtreeLeftRotate(parent);
532 RbtreeRightRotate(parent);
533 other = parent->left;
540 parent =
node->parent;
548 RbtreeLeftRotate(
other);
549 other = parent->left;
551 other->color = parent->color;
554 RbtreeRightRotate(parent);
566 template <
typename Data,
typename Func>
571 return ErrorCode::OK;
576 code != ErrorCode::OK)
582 code != ErrorCode::OK)
590 template <
typename Data,
typename Func>
595 return ErrorCode::OK;
600 code != ErrorCode::OK)
606 code != ErrorCode::OK)
625 BaseNode *
Search(BaseNode *
x,
const Key &key)
634 x =
cmp < 0 ?
x->left :
x->right;
639 template <
typename Data, SizeLimitMode LimitMode>
644 Assert::SizeLimitCheck<LimitMode>(
sizeof(
Data),
node->size);
互斥锁类,提供线程同步机制 (Mutex class providing thread synchronization mechanisms).
ErrorCode Lock()
加锁,如果锁已被占用,则阻塞等待 (Lock the mutex, blocking if it is already locked).
void Unlock()
解锁互斥锁 (Unlock the mutex).
红黑树的基本节点结构 (Base node structure of the Red-Black Tree).
BaseNode * right
右子节点 (Right child node).
BaseNode(size_t size)
基本节点构造函数 (Constructor for BaseNode).
Key key
节点键值 (Key associated with the node).
RbtColor color
节点颜色 (Color of the node).
size_t size
节点大小 (Size of the node).
BaseNode * left
左子节点 (Left child node).
BaseNode * parent
父节点 (Parent node).
红黑树的泛型数据节点,继承自 BaseNode (Generic data node for Red-Black Tree, inheriting from BaseNode).
Node()
默认构造函数,初始化数据为空 (Default constructor initializing an empty node).
Data data_
存储的数据 (Stored data).
Node(const Data &data)
使用指定数据构造节点 (Constructor initializing a node with the given data).
Node(Args... args)
通过参数列表构造节点 (Constructor initializing a node using arguments list).
红黑树实现,支持泛型键和值,并提供线程安全操作 (Red-Black Tree implementation supporting generic keys and values with thread...
LibXR::Mutex mutex_
互斥锁,确保线程安全 (Mutex for thread-safety).
int(* compare_fun_)(const Key &, const Key &)
键值比较函数 (Function for key comparison).
Node< Data > * ForeachDisc(Node< Data > *node)
获取红黑树的下一个中序遍历节点 (Get the next node in in-order traversal).
BaseNode * root_
红黑树的根节点 (Root node of the Red-Black Tree).
void Delete(BaseNode &node)
从树中删除指定节点 (Delete a specified node from the tree).
RBTree(int(*compare_fun)(const Key &, const Key &))
构造函数,初始化红黑树 (Constructor initializing the Red-Black Tree).
ErrorCode Foreach(Func func)
遍历红黑树并执行用户提供的操作 (Traverse the Red-Black Tree and apply a user-defined function).
Node< Data > * Search(const Key &key)
搜索红黑树中的节点 (Search for a node in the Red-Black Tree).
void Insert(BaseNode &node, KeyType &&key)
在树中插入新节点 (Insert a new node into the tree).
uint32_t GetNum()
获取树中的节点数量 (Get the number of nodes in the tree).
RbtColor
定义红黑树节点的颜色 (Enumeration for node colors in Red-Black Tree).
@ BLACK
黑色节点 (Black node).
LibXR Color Control Library / LibXR终端颜色控制库
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值