libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::RBTree< Key > Class Template Reference

红黑树实现,支持泛型键和值,并提供线程安全操作 (Red-Black Tree implementation supporting generic keys and values with thread-safe operations). More...

#include <rbt.hpp>

Collaboration diagram for LibXR::RBTree< Key >:

Data Structures

class  BaseNode
 红黑树的基本节点结构 (Base node structure of the Red-Black Tree). More...
 
class  Node
 红黑树的泛型数据节点,继承自 BaseNode (Generic data node for Red-Black Tree, inheriting from BaseNode). More...
 

Public Types

enum class  RbtColor : uint8_t { RED , BLACK }
 定义红黑树节点的颜色 (Enumeration for node colors in Red-Black Tree). More...
 

Public Member Functions

 RBTree (int(*compare_fun)(const Key &, const Key &))
 构造函数,初始化红黑树 (Constructor initializing the Red-Black Tree).
 
template<typename Data , SizeLimitMode LimitMode = SizeLimitMode::MORE>
Node< Data > * Search (const Key &key)
 搜索红黑树中的节点 (Search for a node in the Red-Black Tree).
 
void Delete (BaseNode &node)
 从树中删除指定节点 (Delete a specified node from the tree).
 
template<typename KeyType >
void Insert (BaseNode &node, KeyType &&key)
 在树中插入新节点 (Insert a new node into the tree).
 
uint32_t GetNum ()
 获取树中的节点数量 (Get the number of nodes in the tree).
 
template<typename Data , typename Func , SizeLimitMode LimitMode = SizeLimitMode::MORE>
ErrorCode Foreach (Func func)
 遍历红黑树并执行用户提供的操作 (Traverse the Red-Black Tree and apply a user-defined function).
 
template<typename Data >
Node< Data > * ForeachDisc (Node< Data > *node)
 获取红黑树的下一个中序遍历节点 (Get the next node in in-order traversal).
 

Private Member Functions

void RbtreeInsert (BaseNode &node)
 
void RbtreeInsertFixup (BaseNode *node)
 
void RbtreeLeftRotate (BaseNode *x)
 
void RbtreeRightRotate (BaseNode *y)
 
void RbtreeDeleteFixup (BaseNode *node, BaseNode *parent)
 
template<typename Data , typename Func >
ErrorCode RbtreeForeachStart (BaseNode *node, Func func)
 
template<typename Data , typename Func >
ErrorCode RbtreeForeach (BaseNode *node, Func func)
 
void RbtreeGetNum (BaseNode *node, uint32_t *count)
 
BaseNodeSearch (BaseNode *x, const Key &key)
 

Static Private Member Functions

template<typename Data , SizeLimitMode LimitMode>
static Node< Data > * ToDerivedType (BaseNode *node)
 

Private Attributes

BaseNoderoot_ = nullptr
 红黑树的根节点 (Root node of the Red-Black Tree).
 
LibXR::Mutex mutex_
 互斥锁,确保线程安全 (Mutex for thread-safety).
 
int(* compare_fun_ )(const Key &, const Key &)
 键值比较函数 (Function for key comparison).
 

Detailed Description

template<typename Key>
class LibXR::RBTree< Key >

红黑树实现,支持泛型键和值,并提供线程安全操作 (Red-Black Tree implementation supporting generic keys and values with thread-safe operations).

This class implements a self-balancing binary search tree (Red-Black Tree) to provide efficient insert, delete, and search operations. 该类实现了自平衡二叉查找树(红黑树),以提供高效的插入、删除和查找操作。

Template Parameters
Key用作节点键的类型 (Type used as node key).

Definition at line 23 of file rbt.hpp.

Member Enumeration Documentation

◆ RbtColor

定义红黑树节点的颜色 (Enumeration for node colors in Red-Black Tree).

Enumerator
RED 

红色节点 (Red node).

BLACK 

黑色节点 (Black node).

Definition at line 29 of file rbt.hpp.

30 {
31 RED,
32 BLACK
33 };
@ BLACK
黑色节点 (Black node).
@ RED
红色节点 (Red node).

Constructor & Destructor Documentation

◆ RBTree()

template<typename Key >
LibXR::RBTree< Key >::RBTree ( int(*)(const Key &, const Key &)  compare_fun)
inlineexplicit

构造函数,初始化红黑树 (Constructor initializing the Red-Black Tree).

Parameters
compare_fun比较函数指针,用于键值比较 (Comparison function pointer for key comparison).

Definition at line 107 of file rbt.hpp.

109 {
110 ASSERT(compare_fun_);
111 }
int(* compare_fun_)(const Key &, const Key &)
键值比较函数 (Function for key comparison).
Definition rbt.hpp:333
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

Member Function Documentation

◆ Delete()

template<typename Key >
void LibXR::RBTree< Key >::Delete ( BaseNode node)
inline

从树中删除指定节点 (Delete a specified node from the tree).

Parameters
node要删除的节点 (Node to be deleted).

Definition at line 141 of file rbt.hpp.

142 {
143 mutex_.Lock();
144
145 BaseNode *child = nullptr, *parent = nullptr;
147
148 if (node.left && node.right)
149 {
150 BaseNode *replace = node.right;
151 while (replace->left)
152 {
153 replace = replace->left;
154 }
155
156 if (node.parent)
157 {
158 (node.parent->left == &node ? node.parent->left : node.parent->right) = replace;
159 }
160 else
161 {
162 root_ = replace;
163 }
164
165 child = replace->right;
166 parent = replace->parent;
167 color = replace->color;
168
169 if (parent == &node)
170 {
171 parent = replace;
172 }
173 else
174 {
175 if (child)
176 {
177 child->parent = parent;
178 }
179
180 if (parent)
181 {
182 parent->left = child;
183 }
184
185 if (node.right)
186 {
187 replace->right = node.right;
188 node.right->parent = replace;
189 }
190 }
191
192 replace->parent = node.parent;
193 replace->color = node.color;
194 replace->left = node.left;
195 node.left->parent = replace;
196
197 if (color == RbtColor::BLACK)
198 {
199 RbtreeDeleteFixup(child, parent);
200 }
201 mutex_.Unlock();
202 return;
203 }
204
205 child = node.left ? node.left : node.right;
206 parent = node.parent;
207 color = node.color;
208
209 if (child)
210 {
211 child->parent = parent;
212 }
213
214 if (parent)
215 {
216 (parent->left == &node ? parent->left : parent->right) = child;
217 }
218 else
219 {
220 root_ = child;
221 }
222
223 if (color == RbtColor::BLACK)
224 {
225 RbtreeDeleteFixup(child, parent);
226 }
227 mutex_.Unlock();
228 }
ErrorCode Lock()
加锁,如果锁已被占用,则阻塞等待 (Lock the mutex, blocking if it is already locked).
Definition mutex.cpp:13
void Unlock()
解锁互斥锁 (Unlock the mutex).
Definition mutex.cpp:27
BaseNode * right
右子节点 (Right child node).
Definition rbt.hpp:44
RbtColor color
节点颜色 (Color of the node).
Definition rbt.hpp:42
BaseNode * parent
父节点 (Parent node).
Definition rbt.hpp:45
LibXR::Mutex mutex_
互斥锁,确保线程安全 (Mutex for thread-safety).
Definition rbt.hpp:332
BaseNode * root_
红黑树的根节点 (Root node of the Red-Black Tree).
Definition rbt.hpp:331
RbtColor
定义红黑树节点的颜色 (Enumeration for node colors in Red-Black Tree).
Definition rbt.hpp:30

◆ Foreach()

template<typename Key >
template<typename Data , typename Func , SizeLimitMode LimitMode = SizeLimitMode::MORE>
ErrorCode LibXR::RBTree< Key >::Foreach ( Func  func)
inline

遍历红黑树并执行用户提供的操作 (Traverse the Red-Black Tree and apply a user-defined function).

Template Parameters
Data存储的数据类型 (Type of data stored in the node).
Func用户定义的操作函数 (User-defined function to apply).
LimitMode结构大小检查模式 (Size limit check mode).
Parameters
func作用于每个节点的函数 (Function applied to each node).
Returns
操作结果,成功返回 ErrorCode::OK (Operation result: ErrorCode::OK on success).

Definition at line 275 of file rbt.hpp.

276 {
277 mutex_.Lock();
279 mutex_.Unlock();
280 return result;
281 }

◆ ForeachDisc()

template<typename Key >
template<typename Data >
Node< Data > * LibXR::RBTree< Key >::ForeachDisc ( Node< Data > *  node)
inline

获取红黑树的下一个中序遍历节点 (Get the next node in in-order traversal).

Template Parameters
Data存储的数据类型 (Type of data stored in the node).
Parameters
node当前节点 (Current node).
Returns
指向下一个节点的指针 (Pointer to the next node).

Definition at line 291 of file rbt.hpp.

292 {
293 mutex_.Lock();
294 Node<Data> *result = nullptr;
295 if (!node)
296 {
297 result = static_cast<Node<Data> *>(root_);
298 while (result && result->left)
299 {
300 result = static_cast<Node<Data> *>(result->left);
301 }
302 }
303 else if (node->right)
304 {
305 result = static_cast<Node<Data> *>(node->right);
306 while (result && result->left)
307 {
308 result = static_cast<Node<Data> *>(result->left);
309 }
310 }
311 else if (node->parent)
312 {
313 if (node == node->parent->left)
314 {
315 result = static_cast<Node<Data> *>(node->parent);
316 }
317 else
318 {
319 while (node->parent && node == node->parent->right)
320 {
321 node = static_cast<Node<Data> *>(node->parent);
322 }
323 result = static_cast<Node<Data> *>(node->parent);
324 }
325 }
326 mutex_.Unlock();
327 return result;
328 }

◆ GetNum()

template<typename Key >
uint32_t LibXR::RBTree< Key >::GetNum ( )
inline

获取树中的节点数量 (Get the number of nodes in the tree).

Returns
树中节点的数量 (Number of nodes in the tree).

Definition at line 255 of file rbt.hpp.

256 {
257 mutex_.Lock();
258 uint32_t count = 0;
259 RbtreeGetNum(root_, &count);
260 mutex_.Unlock();
261 return count;
262 }

◆ Insert()

template<typename Key >
template<typename KeyType >
void LibXR::RBTree< Key >::Insert ( BaseNode node,
KeyType &&  key 
)
inline

在树中插入新节点 (Insert a new node into the tree).

Template Parameters
KeyType插入键的类型 (Type of the key to insert).
Parameters
node要插入的节点 (Node to insert).
key节点键 (Key of the node).

Definition at line 237 of file rbt.hpp.

238 {
239 mutex_.Lock();
240 node.left = nullptr;
241 node.right = nullptr;
242 node.parent = nullptr;
243 node.color = RbtColor::RED;
244 node.key = key;
245 node.color = RbtColor::RED;
246 node.key = std::forward<KeyType>(key);
247 RbtreeInsert(node);
248 mutex_.Unlock();
249 }

◆ RbtreeDeleteFixup()

template<typename Key >
void LibXR::RBTree< Key >::RbtreeDeleteFixup ( BaseNode node,
BaseNode parent 
)
inlineprivate

Definition at line 485 of file rbt.hpp.

486 {
487 BaseNode *other = nullptr;
488
489 while ((!node || node->color == RbtColor::BLACK) && node != root_)
490 {
491 if (parent->left == node)
492 {
493 other = parent->right;
494 if (other->color == RbtColor::RED)
495 {
496 other->color = RbtColor::BLACK;
497 parent->color = RbtColor::RED;
498 RbtreeLeftRotate(parent);
499 other = parent->right;
500 }
501 if ((!other->left || other->left->color == RbtColor::BLACK) &&
502 (!other->right || other->right->color == RbtColor::BLACK))
503 {
504 other->color = RbtColor::RED;
505 node = parent;
506 parent = node->parent;
507 }
508 else
509 {
510 if (!other->right || other->right->color == RbtColor::BLACK)
511 {
512 other->left->color = RbtColor::BLACK;
513 other->color = RbtColor::RED;
514 RbtreeRightRotate(other);
515 other = parent->right;
516 }
517 other->color = parent->color;
518 parent->color = RbtColor::BLACK;
519 other->right->color = RbtColor::BLACK;
520 RbtreeLeftRotate(parent);
521 node = root_;
522 break;
523 }
524 }
525 else
526 {
527 other = parent->left;
528 if (other->color == RbtColor::RED)
529 {
531 parent->color = RbtColor::RED;
532 RbtreeRightRotate(parent);
533 other = parent->left;
534 }
535 if ((!other->left || other->left->color == RbtColor::BLACK) &&
536 (!other->right || other->right->color == RbtColor::BLACK))
537 {
538 other->color = RbtColor::RED;
539 node = parent;
540 parent = node->parent;
541 }
542 else
543 {
544 if (!other->left || other->left->color == RbtColor::BLACK)
545 {
546 other->right->color = RbtColor::BLACK;
547 other->color = RbtColor::RED;
548 RbtreeLeftRotate(other);
549 other = parent->left;
550 }
551 other->color = parent->color;
552 parent->color = RbtColor::BLACK;
553 other->left->color = RbtColor::BLACK;
554 RbtreeRightRotate(parent);
555 node = root_;
556 break;
557 }
558 }
559 }
560 if (node)
561 {
563 }
564 }
BaseNode * left
左子节点 (Left child node).
Definition rbt.hpp:43

◆ RbtreeForeach()

template<typename Key >
template<typename Data , typename Func >
ErrorCode LibXR::RBTree< Key >::RbtreeForeach ( BaseNode node,
Func  func 
)
inlineprivate

Definition at line 591 of file rbt.hpp.

592 {
593 if (!node)
594 {
595 return ErrorCode::OK;
596 }
597
598 if (ErrorCode code =
599 RbtreeForeach<Data, Func>(reinterpret_cast<Node<Data> *>(node->left), func);
600 code != ErrorCode::OK)
601 {
602 return code;
603 }
604
605 if (ErrorCode code = func(*reinterpret_cast<Node<Data> *>(node));
606 code != ErrorCode::OK)
607 {
608 return code;
609 }
610
611 return RbtreeForeach<Data, Func>(reinterpret_cast<Node<Data> *>(node->right), func);
612 }

◆ RbtreeForeachStart()

template<typename Key >
template<typename Data , typename Func >
ErrorCode LibXR::RBTree< Key >::RbtreeForeachStart ( BaseNode node,
Func  func 
)
inlineprivate

Definition at line 567 of file rbt.hpp.

568 {
569 if (!node)
570 {
571 return ErrorCode::OK;
572 }
573
574 if (ErrorCode code =
575 RbtreeForeach<Data, Func>(reinterpret_cast<Node<Data> *>(node->left), func);
576 code != ErrorCode::OK)
577 {
578 return code;
579 }
580
581 if (ErrorCode code = func(*reinterpret_cast<Node<Data> *>(node));
582 code != ErrorCode::OK)
583 {
584 return code;
585 }
586
587 return RbtreeForeach<Data, Func>(reinterpret_cast<Node<Data> *>(node->right), func);
588 }

◆ RbtreeGetNum()

template<typename Key >
void LibXR::RBTree< Key >::RbtreeGetNum ( BaseNode node,
uint32_t count 
)
inlineprivate

Definition at line 614 of file rbt.hpp.

615 {
616 if (!node)
617 {
618 return;
619 }
620 ++(*count);
621 RbtreeGetNum(node->left, count);
622 RbtreeGetNum(node->right, count);
623 }

◆ RbtreeInsert()

template<typename Key >
void LibXR::RBTree< Key >::RbtreeInsert ( BaseNode node)
inlineprivate

Definition at line 336 of file rbt.hpp.

337 {
338 BaseNode *parent = nullptr;
339 BaseNode **current = &root_;
340 while (*current)
341 {
342 parent = *current;
343 current =
344 (compare_fun_(node.key, parent->key) < 0) ? &parent->left : &parent->right;
345 }
346 node.parent = parent;
347 *current = &node;
348 RbtreeInsertFixup(&node);
349 }

◆ RbtreeInsertFixup()

template<typename Key >
void LibXR::RBTree< Key >::RbtreeInsertFixup ( BaseNode node)
inlineprivate

Definition at line 351 of file rbt.hpp.

352 {
353 BaseNode *parent = nullptr, *gparent = nullptr;
354
355 while ((parent = node->parent) && parent->color == RbtColor::RED)
356 {
357 gparent = parent->parent;
358
359 if (parent == gparent->left)
360 {
361 BaseNode *uncle = gparent->right;
362 if (uncle && uncle->color == RbtColor::RED)
363 {
364 uncle->color = RbtColor::BLACK;
365 parent->color = RbtColor::BLACK;
366 gparent->color = RbtColor::RED;
367 node = gparent;
368 continue;
369 }
370
371 if (node == parent->right)
372 {
373 BaseNode *tmp = nullptr;
374 RbtreeLeftRotate(parent);
375 tmp = parent;
376 parent = node;
377 node = tmp;
378 }
379
380 parent->color = RbtColor::BLACK;
381 gparent->color = RbtColor::RED;
382 RbtreeRightRotate(gparent);
383 }
384 else
385 {
386 BaseNode *uncle = gparent->left;
387 if (uncle && uncle->color == RbtColor::RED)
388 {
389 uncle->color = RbtColor::BLACK;
390 parent->color = RbtColor::BLACK;
391 gparent->color = RbtColor::RED;
392 node = gparent;
393 continue;
394 }
395
396 if (node == parent->left)
397 {
398 BaseNode *tmp = nullptr;
399 RbtreeRightRotate(parent);
400 tmp = parent;
401 parent = node;
402 node = tmp;
403 }
404
405 parent->color = RbtColor::BLACK;
406 gparent->color = RbtColor::RED;
407 RbtreeLeftRotate(gparent);
408 }
409 }
411 }

◆ RbtreeLeftRotate()

template<typename Key >
void LibXR::RBTree< Key >::RbtreeLeftRotate ( BaseNode x)
inlineprivate

Definition at line 413 of file rbt.hpp.

414 {
415 if (!x || !x->right)
416 {
417 return;
418 }
419
420 BaseNode *y = x->right;
421 x->right = y->left;
422 if (y->left)
423 {
424 y->left->parent = x;
425 }
426
427 y->parent = x->parent;
428
429 if (!x->parent)
430 {
431 root_ = y;
432 }
433 else
434 {
435 if (x == x->parent->left)
436 {
437 x->parent->left = y;
438 }
439 else
440 {
441 x->parent->right = y;
442 }
443 }
444
445 y->left = x;
446 x->parent = y;
447 }

◆ RbtreeRightRotate()

template<typename Key >
void LibXR::RBTree< Key >::RbtreeRightRotate ( BaseNode y)
inlineprivate

Definition at line 449 of file rbt.hpp.

450 {
451 if (!y || !y->left)
452 {
453 return;
454 }
455
456 BaseNode *x = y->left;
457 y->left = x->right;
458 if (x->right)
459 {
460 x->right->parent = y;
461 }
462
463 x->parent = y->parent;
464
465 if (!y->parent)
466 {
467 root_ = x;
468 }
469 else
470 {
471 if (y == y->parent->right)
472 {
473 y->parent->right = x;
474 }
475 else
476 {
477 y->parent->left = x;
478 }
479 }
480
481 x->right = y;
482 y->parent = x;
483 }

◆ Search() [1/2]

template<typename Key >
BaseNode * LibXR::RBTree< Key >::Search ( BaseNode x,
const Key &  key 
)
inlineprivate

Definition at line 625 of file rbt.hpp.

626 {
627 while (x)
628 {
629 int cmp = compare_fun_(key, x->key);
630 if (cmp == 0)
631 {
632 break;
633 }
634 x = cmp < 0 ? x->left : x->right;
635 }
636 return x;
637 }

◆ Search() [2/2]

template<typename Key >
template<typename Data , SizeLimitMode LimitMode = SizeLimitMode::MORE>
Node< Data > * LibXR::RBTree< Key >::Search ( const Key &  key)
inline

搜索红黑树中的节点 (Search for a node in the Red-Black Tree).

Template Parameters
Data存储的数据类型 (Type of data stored in the node).
LimitMode结构大小检查模式 (Size limit check mode).
Parameters
key要搜索的键 (Key to search for).
Returns
指向找到的节点的指针,如果未找到返回 nullptr (Pointer to the found node, or nullptr if not found).

Definition at line 122 of file rbt.hpp.

123 {
124 mutex_.Lock();
125 Node<Data> *result = nullptr;
126 if (root_)
127 {
128 if (BaseNode *found = Search(root_, key))
129 {
131 }
132 }
133 mutex_.Unlock();
134 return result;
135 }
Node< Data > * Search(const Key &key)
搜索红黑树中的节点 (Search for a node in the Red-Black Tree).
Definition rbt.hpp:122

◆ ToDerivedType()

template<typename Key >
template<typename Data , SizeLimitMode LimitMode>
static Node< Data > * LibXR::RBTree< Key >::ToDerivedType ( BaseNode node)
inlinestaticprivate

Definition at line 640 of file rbt.hpp.

641 {
642 if (node)
643 {
644 Assert::SizeLimitCheck<LimitMode>(sizeof(Data), node->size);
645 }
646 return static_cast<Node<Data> *>(node);
647 }

Field Documentation

◆ compare_fun_

template<typename Key >
int(* LibXR::RBTree< Key >::compare_fun_) (const Key &, const Key &)
private

键值比较函数 (Function for key comparison).

Definition at line 333 of file rbt.hpp.

◆ mutex_

template<typename Key >
LibXR::Mutex LibXR::RBTree< Key >::mutex_
private

互斥锁,确保线程安全 (Mutex for thread-safety).

Definition at line 332 of file rbt.hpp.

◆ root_

template<typename Key >
BaseNode* LibXR::RBTree< Key >::root_ = nullptr
private

红黑树的根节点 (Root node of the Red-Black Tree).

Definition at line 331 of file rbt.hpp.


The documentation for this class was generated from the following file: