libxr
Embedded C++ framework for bare-metal, RTOS and Linux
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>

Classes

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.
 

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 }

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
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
定义错误码枚举 / Defines an enumeration for error codes

◆ 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 }

◆ Search()

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

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