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, void **rootp,
37 int (*compar)(const void *, const void *))
40 node_t *root, **base, **leaf, *result, *n, *x, *y, *z;
43 /* POSIX requires that tsearch() returns NULL if rootp is NULL. */
49 * Find the leaf where the new key needs to be inserted. Return
50 * if we've found an existing entry. Keep track of the path that
51 * is taken to get to the node, as we will need it to adjust the
57 while (*leaf != NULL) {
58 if ((*leaf)->balance != 0) {
60 * If we reach a node that has a non-zero
61 * balance on the way, we know that we won't
62 * need to perform any rotations above this
63 * point. In this case rotations are always
64 * capable of keeping the subtree in balance.
65 * Make this the base node and reset the path.
70 cmp = compar(key, (*leaf)->key);
72 path_taking_left(&path);
73 leaf = &(*leaf)->llink;
75 path_taking_right(&path);
76 leaf = &(*leaf)->rlink;
78 return (&(*leaf)->key);
82 /* Did not find a matching key in the tree. Insert a new node. */
83 result = *leaf = malloc(sizeof(**leaf));
86 result->key = (void *)key;
92 * Walk along the same path a second time and adjust the
93 * balances. Except for the first node, all of these nodes must
94 * have a balance of zero, meaning that these nodes will not get
97 for (n = *base; n != *leaf;) {
98 if (path_took_left(&path)) {
108 * Adjusting the balances may have pushed the balance of the
109 * base node out of range. Perform a rotation to bring the
110 * balance back in range.
113 if (x->balance > 1) {
115 if (y->balance < 0) {
134 x->balance = z->balance > 0 ? -1 : 0;
135 y->balance = z->balance < 0 ? 1 : 0;
154 } else if (x->balance < -1) {
156 if (y->balance > 0) {
168 node_t *z = y->llink;
175 x->balance = z->balance < 0 ? 1 : 0;
176 y->balance = z->balance > 0 ? -1 : 0;
197 /* Return the new entry. */
199 return (&result->key);