2 * Copyright (c) 2015 Nuxi, https://nuxi.nl/
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #define _SEARCH_PRIVATE
33 #include "tsearch_path.h"
36 tsearch(const void *key, posix_tnode **rootp,
37 int (*compar)(const void *, const void *))
40 posix_tnode **leaf, *result, *n, *x, *y, *z;
43 /* POSIX requires that tsearch() returns NULL if rootp is NULL. */
48 * Find the leaf where the new key needs to be inserted. Return
49 * if we've found an existing entry. Keep track of the path that
50 * is taken to get to the node, as we will need it to adjust the
55 while (*leaf != NULL) {
56 if ((*leaf)->balance != 0) {
58 * If we reach a node that has a non-zero
59 * balance on the way, we know that we won't
60 * need to perform any rotations above this
61 * point. In this case rotations are always
62 * capable of keeping the subtree in balance.
63 * Make this the root node and reset the path.
68 cmp = compar(key, (*leaf)->key);
70 path_taking_left(&path);
71 leaf = &(*leaf)->llink;
73 path_taking_right(&path);
74 leaf = &(*leaf)->rlink;
80 /* Did not find a matching key in the tree. Insert a new node. */
81 result = *leaf = malloc(sizeof(**leaf));
84 result->key = (void *)key;
90 * Walk along the same path a second time and adjust the
91 * balances. Except for the first node, all of these nodes must
92 * have a balance of zero, meaning that these nodes will not get
95 for (n = *rootp; n != *leaf;) {
96 if (path_took_left(&path)) {
106 * Adjusting the balances may have pushed the balance of the
107 * root node out of range. Perform a rotation to bring the
108 * balance back in range.
111 if (x->balance > 1) {
113 if (y->balance < 0) {
132 x->balance = z->balance > 0 ? -1 : 0;
133 y->balance = z->balance < 0 ? 1 : 0;
152 } else if (x->balance < -1) {
154 if (y->balance > 0) {
166 posix_tnode *z = y->llink;
173 x->balance = z->balance < 0 ? 1 : 0;
174 y->balance = z->balance > 0 ? -1 : 0;
195 /* Return the new entry. */