Sturk 1.0.2
Publish-subscribe C implementation.
Loading...
Searching...
No Matches
rbtree.h
Go to the documentation of this file.
1/*
2BSD 3-Clause License
3
4Copyright (c) 2025, Szymon Turno
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
8
91. Redistributions of source code must retain the above copyright notice, this
10 list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30*/
31
38#ifndef ST_RBTREE_H
39#define ST_RBTREE_H
40
41#include "st/logger/except.h"
42#include "vx/graph.h"
43#include <stdint.h>
44
55
66
67/* clang-format off */
75VX_GRAPH(struct StRbNode, ST_BST_N_CHILDREN, union {intptr_t parcol; int32_t align;});
77/* clang-format on */
78
88static inline struct StRbNode* st_rb_left(struct StRbNode* node)
89{
91 return vx_graph_4vx(vx_graph_2vx(node)->nbor[ST_BST_LEFT], node);
92}
93
103static inline struct StRbNode* st_rb_right(struct StRbNode* node)
104{
105 ST_ENSURE_MEM(node, ST_ERROR);
106 return vx_graph_4vx(vx_graph_2vx(node)->nbor[ST_BST_RIGHT], node);
107}
108
124struct StRbNode* st_rb_link(struct StRbNode* node, struct StRbNode* parent);
125
139struct StRbNode* st_rb_insrebal(struct StRbNode* root, struct StRbNode* node);
140
150struct StRbNode* st_rb_parent(struct StRbNode* node);
151
162struct StRbNode* st_rb_first(struct StRbNode* node, enum StBstOrder order);
163
174struct StRbNode* st_rb_next(struct StRbNode* node, enum StBstOrder order);
175
176#endif /* ST_RBTREE_H */
Exceptions.
#define ST_ENSURE_MEM(ptr, lvl)
Raise an exception and return NULL if the pointer is NULL.
Definition except.h:110
@ ST_ERROR
error trace level.
Definition trace.h:80
struct StRbNode * st_rb_insrebal(struct StRbNode *root, struct StRbNode *node)
Insert a node in a red-black tree and rebalance the tree.
static struct StRbNode * st_rb_left(struct StRbNode *node)
Get the left child of the given node.
Definition rbtree.h:88
struct StRbNode * st_rb_next(struct StRbNode *node, enum StBstOrder order)
Get the next node for the current red-black tree node and the BST order.
struct StRbNode * st_rb_link(struct StRbNode *node, struct StRbNode *parent)
Prepare a node for insertion in a red-black tree.
struct StRbNode * st_rb_first(struct StRbNode *node, enum StBstOrder order)
Get the first node of the red-black tree for the given BST order.
struct StRbNode * st_rb_parent(struct StRbNode *node)
Get the parent of the red-black tree node.
StBstOrder
Nodes order for a binary search tree.
Definition rbtree.h:50
@ ST_BST_PREORDER
pre-order.
Definition rbtree.h:52
@ ST_BST_INORDER
in-order.
Definition rbtree.h:51
@ ST_BST_POSTORDER
post-order.
Definition rbtree.h:53
StBstChild
Indices for children in a binary search tree.
Definition rbtree.h:61
@ ST_BST_LEFT
left child.
Definition rbtree.h:62
@ ST_BST_RIGHT
right child.
Definition rbtree.h:63
@ ST_BST_N_CHILDREN
number of children.
Definition rbtree.h:64
static struct StRbNode * st_rb_right(struct StRbNode *node)
Get the right child of the given node.
Definition rbtree.h:103
Node of a red-black tree.
Graph.
#define vx_graph_2vx(graph)
Cast to generic vertex from a graph node.
Definition graph.h:88
#define VX_GRAPH(name, deg, type)
Define the graph.
Definition graph.h:72
#define vx_graph_4vx(v, graph)
Cast from a generic vertex to a graph node.
Definition graph.h:101