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

29 {
30 RED,
31 BLACK
32 };
@ 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 106 of file rbt.hpp.

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

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

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

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

272 {
273 mutex_.Lock();
274 ErrorCode result = RbtreeForeachStart<Data>(root_, func);
275 mutex_.Unlock();
276 return result;
277 }
ErrorCode
定义错误码枚举

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

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

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

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

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

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

◆ RbtreeDeleteFixup()

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

Definition at line 481 of file rbt.hpp.

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

◆ RbtreeForeach()

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

Definition at line 587 of file rbt.hpp.

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

◆ RbtreeForeachStart()

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

Definition at line 563 of file rbt.hpp.

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

◆ RbtreeGetNum()

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

Definition at line 610 of file rbt.hpp.

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

◆ RbtreeInsert()

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

Definition at line 332 of file rbt.hpp.

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

◆ RbtreeInsertFixup()

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

Definition at line 347 of file rbt.hpp.

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

◆ RbtreeLeftRotate()

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

Definition at line 409 of file rbt.hpp.

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

◆ RbtreeRightRotate()

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

Definition at line 445 of file rbt.hpp.

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

◆ Search() [1/2]

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

Definition at line 621 of file rbt.hpp.

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

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

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

◆ ToDerivedType()

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

Definition at line 636 of file rbt.hpp.

637 {
638 if (node)
639 {
640 ASSERT(LibXR::SizeLimitCheck(LimitMode, sizeof(Data), node->size));
641 }
642 return static_cast<Node<Data>*>(node);
643 }
constexpr bool SizeLimitCheck(SizeLimitMode mode, size_t limit, size_t size) noexcept
尺寸约束的纯判断函数

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

◆ mutex_

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

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

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


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