libctru  v2.4.1
rbtree.h
Go to the documentation of this file.
1 /**
2  * @file rbtree.h
3  * @brief Red-black trees.
4  */
5 #pragma once
6 
7 #include <stdint.h>
8 #include <stddef.h>
9 
10 /// Retrieves an rbtree item.
11 #define rbtree_item(ptr, type, member) \
12  ((type*)(((char*)ptr) - offsetof(type, member)))
13 
14 typedef struct rbtree rbtree_t; ///< rbtree type.
15 typedef struct rbtree_node rbtree_node_t; ///< rbtree node type.
16 
17 typedef void (*rbtree_node_destructor_t)(rbtree_node_t *Node); ///< rbtree node destructor.
18 typedef int (*rbtree_node_comparator_t)(const rbtree_node_t *lhs,
19  const rbtree_node_t *rhs); ///< rbtree node comparator.
20 
21 /// An rbtree node.
23 {
24  uintptr_t parent_color; ///< Parent color.
25  rbtree_node_t *child[2]; ///< Node children.
26 };
27 
28 /// An rbtree.
29 struct rbtree
30 {
31  rbtree_node_t *root; ///< Root node.
32  rbtree_node_comparator_t comparator; ///< Node comparator.
33  size_t size; ///< Size.
34 };
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 /**
41  * @brief Initializes an rbtree.
42  * @param tree Pointer to the tree.
43  * @param comparator Comparator to use.
44  */
45 void
46 rbtree_init(rbtree_t *tree,
47  rbtree_node_comparator_t comparator);
48 
49 /**
50  * @brief Gets whether an rbtree is empty
51  * @param tree Pointer to the tree.
52  * @return A non-zero value if the tree is not empty.
53  */
54 int
55 rbtree_empty(const rbtree_t *tree);
56 
57 /**
58  * @brief Gets the size of an rbtree.
59  * @param tree Pointer to the tree.
60  */
61 size_t
62 rbtree_size(const rbtree_t *tree);
63 
64 /**
65  * @brief Inserts a node into an rbtree.
66  * @param tree Pointer to the tree.
67  * @param node Pointer to the node.
68  * @return The inserted node.
69  */
70 __attribute__((warn_unused_result))
71 rbtree_node_t*
72 rbtree_insert(rbtree_t *tree,
73  rbtree_node_t *node);
74 
75 /**
76  * @brief Inserts multiple nodes into an rbtree.
77  * @param tree Pointer to the tree.
78  * @param node Pointer to the nodes.
79  */
80 void
81 rbtree_insert_multi(rbtree_t *tree,
82  rbtree_node_t *node);
83 
84 /**
85  * @brief Finds a node within an rbtree.
86  * @param tree Pointer to the tree.
87  * @param node Pointer to the node.
88  * @return The located node.
89  */
90 rbtree_node_t*
91 rbtree_find(const rbtree_t *tree,
92  const rbtree_node_t *node);
93 
94 /**
95  * @brief Gets the minimum node of an rbtree.
96  * @param tree Pointer to the tree.
97  * @return The minimum node.
98  */
99 rbtree_node_t*
100 rbtree_min(const rbtree_t *tree);
101 
102 /**
103  * @brief Gets the maximum node of an rbtree.
104  * @param tree Pointer to the tree.
105  * @return The maximum node.
106  */
107 rbtree_node_t*
108 rbtree_max(const rbtree_t *tree);
109 
110 /**
111  * @brief Gets the next node from an rbtree node.
112  * @param node Pointer to the node.
113  * @return The next node.
114  */
115 rbtree_node_t*
116 rbtree_node_next(const rbtree_node_t *node);
117 
118 /**
119  * @brief Gets the previous node from an rbtree node.
120  * @param node Pointer to the node.
121  * @return The previous node.
122  */
123 rbtree_node_t*
124 rbtree_node_prev(const rbtree_node_t *node);
125 
126 /**
127  * @brief Removes a node from an rbtree.
128  * @param tree Pointer to the tree.
129  * @param node Pointer to the node.
130  * @param destructor Destructor to use when removing the node.
131  * @return The removed node.
132  */
133 rbtree_node_t*
134 rbtree_remove(rbtree_t *tree,
135  rbtree_node_t *node,
136  rbtree_node_destructor_t destructor);
137 
138 /**
139  * @brief Clears an rbtree.
140  * @param tree Pointer to the tree.
141  * @param destructor Destructor to use when clearing the tree's nodes.
142  */
143 void
144 rbtree_clear(rbtree_t *tree,
145  rbtree_node_destructor_t destructor);
146 
147 #ifdef __cplusplus
148 }
149 #endif
void(* rbtree_node_destructor_t)(rbtree_node_t *Node)
rbtree node destructor.
Definition: rbtree.h:17
void rbtree_init(rbtree_t *tree, rbtree_node_comparator_t comparator)
Initializes an rbtree.
rbtree_node_t * rbtree_node_prev(const rbtree_node_t *node)
Gets the previous node from an rbtree node.
void rbtree_insert_multi(rbtree_t *tree, rbtree_node_t *node)
Inserts multiple nodes into an rbtree.
size_t rbtree_size(const rbtree_t *tree)
Gets the size of an rbtree.
__attribute__((warn_unused_result)) rbtree_node_t *rbtree_insert(rbtree_t *tree
Inserts a node into an rbtree.
rbtree_node_t * rbtree_min(const rbtree_t *tree)
Gets the minimum node of an rbtree.
rbtree_node_t * rbtree_max(const rbtree_t *tree)
Gets the maximum node of an rbtree.
rbtree_node_t * rbtree_find(const rbtree_t *tree, const rbtree_node_t *node)
Finds a node within an rbtree.
rbtree_node_t * rbtree_remove(rbtree_t *tree, rbtree_node_t *node, rbtree_node_destructor_t destructor)
Removes a node from an rbtree.
void rbtree_clear(rbtree_t *tree, rbtree_node_destructor_t destructor)
Clears an rbtree.
int rbtree_empty(const rbtree_t *tree)
Gets whether an rbtree is empty.
int(* rbtree_node_comparator_t)(const rbtree_node_t *lhs, const rbtree_node_t *rhs)
rbtree node comparator.
Definition: rbtree.h:18
rbtree_node_t * rbtree_node_next(const rbtree_node_t *node)
Gets the next node from an rbtree node.
An rbtree node.
Definition: rbtree.h:23
uintptr_t parent_color
Parent color.
Definition: rbtree.h:24
rbtree_node_t * child[2]
Node children.
Definition: rbtree.h:25
An rbtree.
Definition: rbtree.h:30
size_t size
Size.
Definition: rbtree.h:33
rbtree_node_comparator_t comparator
Node comparator.
Definition: rbtree.h:32
rbtree_node_t * root
Root node.
Definition: rbtree.h:31