MySQL 9.1.0
Source Code Documentation
ut0rbt.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 2007, 2024, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26*****************************************************************************/
27/** @file include/ut0rbt.h
28 Various utilities
29
30 Created 2007-03-20 Sunny Bains
31 *******************************************************/
32
33#ifndef INNOBASE_UT0RBT_H
34#define INNOBASE_UT0RBT_H
35
36#if !defined(IB_RBT_TESTING)
37#include "univ.i"
38#include "ut0mem.h"
39#else
40#include <assert.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44
45#define ut_malloc malloc
46#define ut_free free
47#define ulint unsigned long
48#define ut_a(c) assert(c)
49#define ut_error assert(0)
50#endif
51
52struct ib_rbt_node_t;
53typedef void (*ib_rbt_print_node)(const ib_rbt_node_t *node);
54typedef int (*ib_rbt_compare)(const void *p1, const void *p2);
55typedef int (*ib_rbt_arg_compare)(const void *, const void *p1, const void *p2);
56
57/** Red black tree color types */
59
60/** Red black tree node */
62 ib_rbt_color_t color; /* color of this node */
63
64 ib_rbt_node_t *left; /* points left child */
65 ib_rbt_node_t *right; /* points right child */
66 ib_rbt_node_t *parent; /* points parent node */
67
68 char value[1]; /* Data value */
69};
70
71/** Red black tree instance.*/
72struct ib_rbt_t {
73 ib_rbt_node_t *nil; /* Black colored node that is
74 used as a sentinel. This is
75 pre-allocated too.*/
76
77 ib_rbt_node_t *root; /* Root of the tree, this is
78 pre-allocated and the first
79 data node is the left child.*/
80
81 ulint n_nodes; /* Total number of data nodes */
82
83 ib_rbt_compare compare; /* Fn. to use for comparison */
84 ib_rbt_arg_compare compare_with_arg; /* Fn. to use for comparison
85 with argument */
86 ulint sizeof_value; /* Sizeof the item in bytes */
87 void *cmp_arg; /* Compare func argument */
88};
89
90/** The result of searching for a key in the tree, this is useful for
91a speedy lookup and insert if key doesn't exist.*/
93 const ib_rbt_node_t *last; /* Last node visited */
94
95 int result; /* Result of comparing with
96 the last non-nil node that
97 was visited */
98};
99
100/* Size in elements (t is an rb tree instance) */
101#define rbt_size(t) (t->n_nodes)
102
103/* Check whether the rb tree is empty (t is an rb tree instance) */
104#define rbt_empty(t) (rbt_size(t) == 0)
105
106/* Get data value (t is the data type, n is an rb tree node instance) */
107#define rbt_value(t, n) ((t *)&n->value[0])
108
109/* Compare a key with the node value (t is tree, k is key, n is node)*/
110#define rbt_compare(t, k, n) (t->compare(k, n->value))
111
112/** Free an instance of a red black tree */
113void rbt_free(ib_rbt_t *tree); /*!< in: rb tree to free */
114/** Create an instance of a red black tree
115 @return rb tree instance */
116ib_rbt_t *rbt_create(size_t sizeof_value, /*!< in: size in bytes */
117 ib_rbt_compare compare); /*!< in: comparator */
118/** Create an instance of a red black tree, whose comparison function takes
119 an argument
120 @return rb tree instance */
121ib_rbt_t *rbt_create_arg_cmp(size_t sizeof_value, /*!< in: size in bytes */
122 ib_rbt_arg_compare compare, /*!< in: comparator */
123 void *cmp_arg); /*!< in: compare fn arg */
124/** Delete a node from the red black tree, identified by key */
125bool rbt_delete(
126 /* in: true on success */
127 ib_rbt_t *tree, /* in: rb tree */
128 const void *key); /* in: key to delete */
129/** Remove a node from the red black tree, NOTE: This function will not delete
130 the node instance, THAT IS THE CALLERS RESPONSIBILITY.
131 @return the deleted node with the const. */
133 ib_rbt_t *tree, /*!< in: rb tree */
134 const ib_rbt_node_t *node); /*!< in: node to delete, this
135 is a fudge and declared const
136 because the caller has access
137 only to const nodes.*/
138/** Add data to the red black tree, identified by key (no dups yet!)
139 @return inserted node */
140const ib_rbt_node_t *rbt_insert(ib_rbt_t *tree, /*!< in: rb tree */
141 const void *key, /*!< in: key for ordering */
142 const void *value); /*!< in: data that will be
143 copied to the node.*/
144/** Add a new node to the tree, useful for data that is pre-sorted.
145 @return appended node */
146const ib_rbt_node_t *rbt_add_node(ib_rbt_t *tree, /*!< in: rb tree */
147 ib_rbt_bound_t *parent, /*!< in: parent */
148 const void *value); /*!< in: this value is
149 copied to the node */
150/** Return the left most data node in the tree
151 @return left most node */
152const ib_rbt_node_t *rbt_first(const ib_rbt_t *tree); /*!< in: rb tree */
153/** Return the right most data node in the tree
154 @return right most node */
155const ib_rbt_node_t *rbt_last(const ib_rbt_t *tree); /*!< in: rb tree */
156/** Return the next node from current.
157 @return successor node to current that is passed in. */
158const ib_rbt_node_t *rbt_next(const ib_rbt_t *tree, /*!< in: rb tree */
159 const ib_rbt_node_t * /* in: current node */
160 current);
161/** Return the prev node from current.
162 @return predecessor node to current that is passed in */
163const ib_rbt_node_t *rbt_prev(const ib_rbt_t *tree, /*!< in: rb tree */
164 const ib_rbt_node_t * /* in: current node */
165 current);
166/** Search for the key, a node will be returned in parent.last, whether it
167 was found or not. If not found then parent.last will contain the
168 parent node for the possibly new key otherwise the matching node.
169 @return result of last comparison */
170int rbt_search(const ib_rbt_t *tree, /*!< in: rb tree */
171 ib_rbt_bound_t *parent, /*!< in: search bounds */
172 const void *key); /*!< in: key to search */
173/** Search for the key, a node will be returned in parent.last, whether it
174 was found or not. If not found then parent.last will contain the
175 parent node for the possibly new key otherwise the matching node.
176 @return result of last comparison */
177int rbt_search_cmp(const ib_rbt_t *tree, /*!< in: rb tree */
178 ib_rbt_bound_t *parent, /*!< in: search bounds */
179 const void *key, /*!< in: key to search */
180 ib_rbt_compare compare, /*!< in: comparator */
181 ib_rbt_arg_compare arg_compare); /*!< in: fn to compare items
182 with argument */
183/** Merge the node from dst into src. Return the number of nodes merged.
184 @return no. of recs merged */
185ulint rbt_merge_uniq(ib_rbt_t *dst, /*!< in: dst rb tree */
186 const ib_rbt_t *src); /*!< in: src rb tree */
187#if defined UNIV_DEBUG || defined IB_RBT_TESTING
188/** Verify the integrity of the RB tree. For debugging. 0 failure else height
189 of tree (in count of black nodes).
190 @return true if OK false if tree invalid. */
191bool rbt_validate(const ib_rbt_t *tree); /*!< in: tree to validate */
192#endif /* UNIV_DEBUG || IB_RBT_TESTING */
193
194#endif /* INNOBASE_UT0RBT_H */
required string key
Definition: replication_asynchronous_connection_failover.proto:60
static int compare(size_t a, size_t b)
Function to compare two size_t integers for their relative order.
Definition: rpl_utility.cc:107
The result of searching for a key in the tree, this is useful for a speedy lookup and insert if key d...
Definition: ut0rbt.h:92
int result
Definition: ut0rbt.h:95
const ib_rbt_node_t * last
Definition: ut0rbt.h:93
Red black tree node.
Definition: ut0rbt.h:61
char value[1]
Definition: ut0rbt.h:68
ib_rbt_node_t * left
Definition: ut0rbt.h:64
ib_rbt_node_t * parent
Definition: ut0rbt.h:66
ib_rbt_node_t * right
Definition: ut0rbt.h:65
ib_rbt_color_t color
Definition: ut0rbt.h:62
Red black tree instance.
Definition: ut0rbt.h:72
ulint sizeof_value
Definition: ut0rbt.h:86
void * cmp_arg
Definition: ut0rbt.h:87
ib_rbt_compare compare
Definition: ut0rbt.h:83
ib_rbt_node_t * root
Definition: ut0rbt.h:77
ib_rbt_arg_compare compare_with_arg
Definition: ut0rbt.h:84
ulint n_nodes
Definition: ut0rbt.h:81
ib_rbt_node_t * nil
Definition: ut0rbt.h:73
Version control for database, common definitions, and include files.
unsigned long int ulint
Definition: univ.i:406
Memory primitives.
void rbt_free(ib_rbt_t *tree)
Free an instance of a red black tree.
Definition: ut0rbt.cc:648
void(* ib_rbt_print_node)(const ib_rbt_node_t *node)
Definition: ut0rbt.h:53
ib_rbt_color_t
Red black tree color types.
Definition: ut0rbt.h:58
@ IB_RBT_BLACK
Definition: ut0rbt.h:58
@ IB_RBT_RED
Definition: ut0rbt.h:58
const ib_rbt_node_t * rbt_next(const ib_rbt_t *tree, const ib_rbt_node_t *current)
Return the next node from current.
Definition: ut0rbt.cc:940
int(* ib_rbt_arg_compare)(const void *, const void *p1, const void *p2)
Definition: ut0rbt.h:55
const ib_rbt_node_t * rbt_first(const ib_rbt_t *tree)
Return the left most data node in the tree.
Definition: ut0rbt.cc:908
ib_rbt_t * rbt_create(size_t sizeof_value, ib_rbt_compare compare)
Create an instance of a red black tree.
Definition: ut0rbt.cc:676
ib_rbt_node_t * rbt_remove_node(ib_rbt_t *tree, const ib_rbt_node_t *node)
Remove a node from the red black tree, NOTE: This function will not delete the node instance,...
Definition: ut0rbt.cc:818
const ib_rbt_node_t * rbt_last(const ib_rbt_t *tree)
Return the right most data node in the tree.
Definition: ut0rbt.cc:925
const ib_rbt_node_t * rbt_add_node(ib_rbt_t *tree, ib_rbt_bound_t *parent, const void *value)
Add a new node to the tree, useful for data that is pre-sorted.
Definition: ut0rbt.cc:735
ib_rbt_t * rbt_create_arg_cmp(size_t sizeof_value, ib_rbt_arg_compare compare, void *cmp_arg)
Create an instance of a red black tree, whose comparison function takes an argument.
Definition: ut0rbt.cc:658
const ib_rbt_node_t * rbt_insert(ib_rbt_t *tree, const void *key, const void *value)
Add data to the red black tree, identified by key (no dups yet!)
Definition: ut0rbt.cc:709
ulint rbt_merge_uniq(ib_rbt_t *dst, const ib_rbt_t *src)
Merge the node from dst into src.
Definition: ut0rbt.cc:958
int rbt_search_cmp(const ib_rbt_t *tree, ib_rbt_bound_t *parent, const void *key, ib_rbt_compare compare, ib_rbt_arg_compare arg_compare)
Search for the key, a node will be returned in parent.last, whether it was found or not.
Definition: ut0rbt.cc:872
int(* ib_rbt_compare)(const void *p1, const void *p2)
Definition: ut0rbt.h:54
int rbt_search(const ib_rbt_t *tree, ib_rbt_bound_t *parent, const void *key)
Search for the key, a node will be returned in parent.last, whether it was found or not.
Definition: ut0rbt.cc:837
bool rbt_delete(ib_rbt_t *tree, const void *key)
Delete a node from the red black tree, identified by key.
Definition: ut0rbt.cc:799
bool rbt_validate(const ib_rbt_t *tree)
Verify the integrity of the RB tree.
Definition: ut0rbt.cc:983
const ib_rbt_node_t * rbt_prev(const ib_rbt_t *tree, const ib_rbt_node_t *current)
Return the prev node from current.
Definition: ut0rbt.cc:949