MySQL  8.0.20
Source Code Documentation
ut0rbt.h
Go to the documentation of this file.
1 /*****************************************************************************
2 
3 Copyright (c) 2007, 2018, Oracle and/or its affiliates. All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8 
9 This program is also distributed with certain software (including but not
10 limited to OpenSSL) that is licensed under separate terms, as designated in a
11 particular file or component or in included license documentation. The authors
12 of MySQL hereby grant you an additional permission to link the program and
13 your derivative works with the separately licensed software that they have
14 included with MySQL.
15 
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19 for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 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 #define ibool unsigned int
50 #define TRUE 1
51 #define FALSE 0
52 #endif
53 
54 struct ib_rbt_node_t;
55 typedef void (*ib_rbt_print_node)(const ib_rbt_node_t *node);
56 typedef int (*ib_rbt_compare)(const void *p1, const void *p2);
57 typedef int (*ib_rbt_arg_compare)(const void *, const void *p1, const void *p2);
58 
59 /** Red black tree color types */
61 
62 /** Red black tree node */
63 struct ib_rbt_node_t {
64  ib_rbt_color_t color; /* color of this node */
65 
66  ib_rbt_node_t *left; /* points left child */
67  ib_rbt_node_t *right; /* points right child */
68  ib_rbt_node_t *parent; /* points parent node */
69 
70  char value[1]; /* Data value */
71 };
72 
73 /** Red black tree instance.*/
74 struct ib_rbt_t {
75  ib_rbt_node_t *nil; /* Black colored node that is
76  used as a sentinel. This is
77  pre-allocated too.*/
78 
79  ib_rbt_node_t *root; /* Root of the tree, this is
80  pre-allocated and the first
81  data node is the left child.*/
82 
83  ulint n_nodes; /* Total number of data nodes */
84 
85  ib_rbt_compare compare; /* Fn. to use for comparison */
86  ib_rbt_arg_compare compare_with_arg; /* Fn. to use for comparison
87  with argument */
88  ulint sizeof_value; /* Sizeof the item in bytes */
89  void *cmp_arg; /* Compare func argument */
90 };
91 
92 /** The result of searching for a key in the tree, this is useful for
93 a speedy lookup and insert if key doesn't exist.*/
95  const ib_rbt_node_t *last; /* Last node visited */
96 
97  int result; /* Result of comparing with
98  the last non-nil node that
99  was visited */
100 };
101 
102 /* Size in elements (t is an rb tree instance) */
103 #define rbt_size(t) (t->n_nodes)
104 
105 /* Check whether the rb tree is empty (t is an rb tree instance) */
106 #define rbt_empty(t) (rbt_size(t) == 0)
107 
108 /* Get data value (t is the data type, n is an rb tree node instance) */
109 #define rbt_value(t, n) ((t *)&n->value[0])
110 
111 /* Compare a key with the node value (t is tree, k is key, n is node)*/
112 #define rbt_compare(t, k, n) (t->compare(k, n->value))
113 
114 /** Free an instance of a red black tree */
115 void rbt_free(ib_rbt_t *tree); /*!< in: rb tree to free */
116 /** Create an instance of a red black tree
117  @return rb tree instance */
118 ib_rbt_t *rbt_create(size_t sizeof_value, /*!< in: size in bytes */
119  ib_rbt_compare compare); /*!< in: comparator */
120 /** Create an instance of a red black tree, whose comparison function takes
121  an argument
122  @return rb tree instance */
123 ib_rbt_t *rbt_create_arg_cmp(size_t sizeof_value, /*!< in: size in bytes */
124  ib_rbt_arg_compare compare, /*!< in: comparator */
125  void *cmp_arg); /*!< in: compare fn arg */
126 /** Delete a node from the red black tree, identified by key */
127 ibool rbt_delete(
128  /* in: TRUE on success */
129  ib_rbt_t *tree, /* in: rb tree */
130  const void *key); /* in: key to delete */
131 /** Remove a node from the red black tree, NOTE: This function will not delete
132  the node instance, THAT IS THE CALLERS RESPONSIBILITY.
133  @return the deleted node with the const. */
135  ib_rbt_t *tree, /*!< in: rb tree */
136  const ib_rbt_node_t *node); /*!< in: node to delete, this
137  is a fudge and declared const
138  because the caller has access
139  only to const nodes.*/
140 /** Add data to the red black tree, identified by key (no dups yet!)
141  @return inserted node */
142 const ib_rbt_node_t *rbt_insert(ib_rbt_t *tree, /*!< in: rb tree */
143  const void *key, /*!< in: key for ordering */
144  const void *value); /*!< in: data that will be
145  copied to the node.*/
146 /** Add a new node to the tree, useful for data that is pre-sorted.
147  @return appended node */
148 const ib_rbt_node_t *rbt_add_node(ib_rbt_t *tree, /*!< in: rb tree */
149  ib_rbt_bound_t *parent, /*!< in: parent */
150  const void *value); /*!< in: this value is
151  copied to the node */
152 /** Return the left most data node in the tree
153  @return left most node */
154 const ib_rbt_node_t *rbt_first(const ib_rbt_t *tree); /*!< in: rb tree */
155 /** Return the right most data node in the tree
156  @return right most node */
157 const ib_rbt_node_t *rbt_last(const ib_rbt_t *tree); /*!< in: rb tree */
158 /** Return the next node from current.
159  @return successor node to current that is passed in. */
160 const ib_rbt_node_t *rbt_next(const ib_rbt_t *tree, /*!< in: rb tree */
161  const ib_rbt_node_t * /* in: current node */
162  current);
163 /** Return the prev node from current.
164  @return precedessor node to current that is passed in */
165 const ib_rbt_node_t *rbt_prev(const ib_rbt_t *tree, /*!< in: rb tree */
166  const ib_rbt_node_t * /* in: current node */
167  current);
168 /** Search for the key, a node will be retuned in parent.last, whether it
169  was found or not. If not found then parent.last will contain the
170  parent node for the possibly new key otherwise the matching node.
171  @return result of last comparison */
172 int rbt_search(const ib_rbt_t *tree, /*!< in: rb tree */
173  ib_rbt_bound_t *parent, /*!< in: search bounds */
174  const void *key); /*!< in: key to search */
175 /** Search for the key, a node will be retuned in parent.last, whether it
176  was found or not. If not found then parent.last will contain the
177  parent node for the possibly new key otherwise the matching node.
178  @return result of last comparison */
179 int rbt_search_cmp(const ib_rbt_t *tree, /*!< in: rb tree */
180  ib_rbt_bound_t *parent, /*!< in: search bounds */
181  const void *key, /*!< in: key to search */
182  ib_rbt_compare compare, /*!< in: comparator */
183  ib_rbt_arg_compare arg_compare); /*!< in: fn to compare items
184  with argument */
185 /** Merge the node from dst into src. Return the number of nodes merged.
186  @return no. of recs merged */
187 ulint rbt_merge_uniq(ib_rbt_t *dst, /*!< in: dst rb tree */
188  const ib_rbt_t *src); /*!< in: src rb tree */
189 #if defined UNIV_DEBUG || defined IB_RBT_TESTING
190 /** Verify the integrity of the RB tree. For debugging. 0 failure else height
191  of tree (in count of black nodes).
192  @return true if OK false if tree invalid. */
193 ibool rbt_validate(const ib_rbt_t *tree); /*!< in: tree to validate */
194 #endif /* UNIV_DEBUG || IB_RBT_TESTING */
195 
196 #endif /* INNOBASE_UT0RBT_H */
char value[1]
Definition: ut0rbt.h:70
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:704
ulint n_nodes
Definition: ut0rbt.h:83
ib_rbt_arg_compare compare_with_arg
Definition: ut0rbt.h:86
ulint sizeof_value
Definition: ut0rbt.h:88
const ib_rbt_node_t * last
Definition: ut0rbt.h:95
int(* ib_rbt_compare)(const void *p1, const void *p2)
Definition: ut0rbt.h:56
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:729
ib_rbt_node_t * nil
Definition: ut0rbt.h:75
int result
Definition: ut0rbt.h:97
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 retuned in parent.last, whether it was found or not...
Definition: ut0rbt.cc:865
ib_rbt_compare compare
Definition: ut0rbt.h:85
Red black tree node.
Definition: ut0rbt.h:63
ib_rbt_t * rbt_create(size_t sizeof_value, ib_rbt_compare compare)
Create an instance of a red black tree.
Definition: ut0rbt.cc:674
Definition: ut0rbt.h:60
ib_rbt_node_t * left
Definition: ut0rbt.h:66
int(* ib_rbt_arg_compare)(const void *, const void *p1, const void *p2)
Definition: ut0rbt.h:57
ib_rbt_node_t * parent
Definition: ut0rbt.h:68
Memory primitives.
int rbt_search(const ib_rbt_t *tree, ib_rbt_bound_t *parent, const void *key)
Search for the key, a node will be retuned in parent.last, whether it was found or not...
Definition: ut0rbt.cc:830
ulint rbt_merge_uniq(ib_rbt_t *dst, const ib_rbt_t *src)
Merge the node from dst into src.
Definition: ut0rbt.cc:951
ibool rbt_delete(ib_rbt_t *tree, const void *key)
Delete a node from the red black tree, identified by key.
Definition: ut0rbt.cc:792
ib_rbt_color_t
Red black tree color types.
Definition: ut0rbt.h:60
ib_rbt_color_t color
Definition: ut0rbt.h:64
Definition: ut0rbt.h:60
static const char * key
Definition: suite_stubs.c:14
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:942
const ib_rbt_node_t * rbt_last(const ib_rbt_t *tree)
Return the right most data node in the tree.
Definition: ut0rbt.cc:918
void rbt_free(ib_rbt_t *tree)
Free an instance of a red black tree.
Definition: ut0rbt.cc:646
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:94
void * cmp_arg
Definition: ut0rbt.h:89
void(* ib_rbt_print_node)(const ib_rbt_node_t *node)
Definition: ut0rbt.h:55
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:933
Red black tree instance.
Definition: ut0rbt.h:74
const ib_rbt_node_t * rbt_first(const ib_rbt_t *tree)
Return the left most data node in the tree.
Definition: ut0rbt.cc:901
ib_rbt_node_t * right
Definition: ut0rbt.h:67
ib_rbt_node_t * root
Definition: ut0rbt.h:79
ibool rbt_validate(const ib_rbt_t *tree)
Verify the integrity of the RB tree.
Definition: ut0rbt.cc:976
static int compare(size_t a, size_t b)
Function to compare two size_t integers for their relative order.
Definition: rpl_utility.cc:103
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:656
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:811