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