| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2007, 2026, Oracle and/or its affiliates. |
| 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 designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 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 |
|
| 52 |
struct ib_rbt_node_t; |
| 53 |
typedef void (*ib_rbt_print_node)(const ib_rbt_node_t *node); |
| 54 |
typedef int (*ib_rbt_compare)(const void *p1, const void *p2); |
| 55 |
typedef int (*ib_rbt_arg_compare)(const void *, const void *p1, const void *p2); |
| 56 |
|
| 57 |
/** Red black tree color types */ |
| 58 |
enum ib_rbt_color_t { IB_RBT_RED, IB_RBT_BLACK }; |
| 59 |
|
| 60 |
/** Red black tree node */ |
| 61 |
struct ib_rbt_node_t { |
| 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.*/ |
| 72 |
struct 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 |
| 91 |
a speedy lookup and insert if key doesn't exist.*/ |
| 92 |
struct ib_rbt_bound_t { |
| 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 */ |
| 113 |
void 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 */ |
| 116 |
ib_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 */ |
| 121 |
ib_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 */ |
| 125 |
bool 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. */ |
| 132 |
ib_rbt_node_t *rbt_remove_node( |
| 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 */ |
| 140 |
const 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 */ |
| 146 |
const 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 */ |
| 152 |
const 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 */ |
| 155 |
const 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. */ |
| 158 |
const 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 */ |
| 163 |
const 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 */ |
| 170 |
int 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 */ |
| 177 |
int 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 */ |
| 185 |
ulint 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. */ |
| 191 |
bool rbt_validate(const ib_rbt_t *tree); /*!< in: tree to validate */ |
| 192 |
#endif /* UNIV_DEBUG || IB_RBT_TESTING */ |
| 193 |
|
| 194 |
#endif /* INNOBASE_UT0RBT_H */ |