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.

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

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:14
void Unlock()
解锁互斥锁 (Unlock the mutex).
Definition mutex.cpp:28
LibXR::Mutex mutex_
互斥锁,确保线程安全 (Mutex for thread-safety).
Definition rbt.hpp:330
BaseNode * root_
红黑树的根节点 (Root node of the Red-Black Tree).
Definition rbt.hpp:329
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 273 of file rbt.hpp.

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

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

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

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

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

◆ 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 = std::forward<KeyType>(key);
245 RbtreeInsert(node);
246 mutex_.Unlock();
247 }

◆ RbtreeDeleteFixup()

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

Definition at line 483 of file rbt.hpp.

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

◆ RbtreeForeach()

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

Definition at line 589 of file rbt.hpp.

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

◆ RbtreeForeachStart()

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

Definition at line 565 of file rbt.hpp.

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

◆ RbtreeGetNum()

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

Definition at line 612 of file rbt.hpp.

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

◆ RbtreeInsert()

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

Definition at line 334 of file rbt.hpp.

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

◆ RbtreeInsertFixup()

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

Definition at line 349 of file rbt.hpp.

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

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

◆ RbtreeRightRotate()

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

Definition at line 447 of file rbt.hpp.

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

◆ Search() [1/2]

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

Definition at line 623 of file rbt.hpp.

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

◆ 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 {
130 result = ToDerivedType<Data, LimitMode>(found);
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 638 of file rbt.hpp.

639 {
640 if (node)
641 {
642 Assert::SizeLimitCheck<LimitMode>(sizeof(Data), node->size);
643 }
644 return static_cast<Node<Data> *>(node);
645 }
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 331 of file rbt.hpp.

◆ mutex_

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

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

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


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