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 >:
[legend]

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

template<typename Key >
enum class LibXR::RBTree::RbtColor : uint8_t
strong

定义红黑树节点的颜色 (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(* compare_fun )(const Key &, const Key &))
inlineexplicit

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

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

Definition at line 107 of file rbt.hpp.

107 : compare_fun_(compare_fun)
108 {
109 ASSERT(compare_fun_);
110 }
int(* compare_fun_)(const Key &, const Key &)
键值比较函数 (Function for key comparison).
Definition rbt.hpp:330

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 140 of file rbt.hpp.

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

273 {
274 mutex_.Lock();
275 ErrorCode result = RbtreeForeachStart<Data>(root_, func);
276 mutex_.Unlock();
277 return result;
278 }

◆ 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 288 of file rbt.hpp.

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

◆ 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 252 of file rbt.hpp.

253 {
254 mutex_.Lock();
255 uint32_t count = 0;
256 RbtreeGetNum(root_, &count);
257 mutex_.Unlock();
258 return count;
259 }

◆ 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 236 of file rbt.hpp.

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

◆ RbtreeDeleteFixup()

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

Definition at line 482 of file rbt.hpp.

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

◆ RbtreeForeach()

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

Definition at line 588 of file rbt.hpp.

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

◆ RbtreeForeachStart()

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

Definition at line 564 of file rbt.hpp.

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

◆ RbtreeGetNum()

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

Definition at line 611 of file rbt.hpp.

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

◆ RbtreeInsert()

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

Definition at line 333 of file rbt.hpp.

334 {
335 BaseNode* parent = nullptr;
336 BaseNode** current = &root_;
337 while (*current)
338 {
339 parent = *current;
340 current =
341 (compare_fun_(node.key, parent->key) < 0) ? &parent->left : &parent->right;
342 }
343 node.parent = parent;
344 *current = &node;
345 RbtreeInsertFixup(&node);
346 }
BaseNode * parent
父节点 (Parent node).
Definition rbt.hpp:45

◆ RbtreeInsertFixup()

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

Definition at line 348 of file rbt.hpp.

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

◆ RbtreeLeftRotate()

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

Definition at line 410 of file rbt.hpp.

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

◆ RbtreeRightRotate()

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

Definition at line 446 of file rbt.hpp.

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

◆ Search() [1/2]

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

Definition at line 622 of file rbt.hpp.

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

◆ 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 121 of file rbt.hpp.

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

◆ ToDerivedType()

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

Definition at line 637 of file rbt.hpp.

638 {
639 if (node)
640 {
641 Assert::SizeLimitCheck<LimitMode>(sizeof(Data), node->size);
642 }
643 return static_cast<Node<Data>*>(node);
644 }
static void SizeLimitCheck(size_t limit, size_t size)
在非调试模式下的占位大小检查函数(无实际作用)。 Dummy size limit check for non-debug builds.

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 330 of file rbt.hpp.

◆ mutex_

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

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

Definition at line 329 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 328 of file rbt.hpp.


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