1459d04a5SEd Schouten /*-
2459d04a5SEd Schouten * Copyright (c) 2015 Nuxi, https://nuxi.nl/
364566a3eSAlfred Perlstein *
4459d04a5SEd Schouten * Redistribution and use in source and binary forms, with or without
5459d04a5SEd Schouten * modification, are permitted provided that the following conditions
6459d04a5SEd Schouten * are met:
7459d04a5SEd Schouten * 1. Redistributions of source code must retain the above copyright
8459d04a5SEd Schouten * notice, this list of conditions and the following disclaimer.
9459d04a5SEd Schouten * 2. Redistributions in binary form must reproduce the above copyright
10459d04a5SEd Schouten * notice, this list of conditions and the following disclaimer in the
11459d04a5SEd Schouten * documentation and/or other materials provided with the distribution.
1264566a3eSAlfred Perlstein *
13459d04a5SEd Schouten * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14459d04a5SEd Schouten * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15459d04a5SEd Schouten * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16459d04a5SEd Schouten * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17459d04a5SEd Schouten * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18459d04a5SEd Schouten * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19459d04a5SEd Schouten * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20459d04a5SEd Schouten * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21459d04a5SEd Schouten * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22459d04a5SEd Schouten * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23459d04a5SEd Schouten * SUCH DAMAGE.
2464566a3eSAlfred Perlstein */
2564566a3eSAlfred Perlstein
2664566a3eSAlfred Perlstein #define _SEARCH_PRIVATE
2764566a3eSAlfred Perlstein #include <search.h>
2864566a3eSAlfred Perlstein #include <stdlib.h>
2964566a3eSAlfred Perlstein
30459d04a5SEd Schouten #include "tsearch_path.h"
31459d04a5SEd Schouten
32*4ef9bd22SEd Schouten posix_tnode *
tsearch(const void * key,posix_tnode ** rootp,int (* compar)(const void *,const void *))33*4ef9bd22SEd Schouten tsearch(const void *key, posix_tnode **rootp,
348a29851fSPedro F. Giffuni int (*compar)(const void *, const void *))
3564566a3eSAlfred Perlstein {
36459d04a5SEd Schouten struct path path;
37*4ef9bd22SEd Schouten posix_tnode **leaf, *result, *n, *x, *y, *z;
38459d04a5SEd Schouten int cmp;
3964566a3eSAlfred Perlstein
40459d04a5SEd Schouten /* POSIX requires that tsearch() returns NULL if rootp is NULL. */
4164566a3eSAlfred Perlstein if (rootp == NULL)
42459d04a5SEd Schouten return (NULL);
4364566a3eSAlfred Perlstein
44459d04a5SEd Schouten /*
45459d04a5SEd Schouten * Find the leaf where the new key needs to be inserted. Return
46459d04a5SEd Schouten * if we've found an existing entry. Keep track of the path that
47459d04a5SEd Schouten * is taken to get to the node, as we will need it to adjust the
48459d04a5SEd Schouten * balances.
49459d04a5SEd Schouten */
50459d04a5SEd Schouten path_init(&path);
51*4ef9bd22SEd Schouten leaf = rootp;
52459d04a5SEd Schouten while (*leaf != NULL) {
53459d04a5SEd Schouten if ((*leaf)->balance != 0) {
54459d04a5SEd Schouten /*
55459d04a5SEd Schouten * If we reach a node that has a non-zero
56459d04a5SEd Schouten * balance on the way, we know that we won't
57459d04a5SEd Schouten * need to perform any rotations above this
58459d04a5SEd Schouten * point. In this case rotations are always
59459d04a5SEd Schouten * capable of keeping the subtree in balance.
60*4ef9bd22SEd Schouten * Make this the root node and reset the path.
61459d04a5SEd Schouten */
62*4ef9bd22SEd Schouten rootp = leaf;
63459d04a5SEd Schouten path_init(&path);
64459d04a5SEd Schouten }
65459d04a5SEd Schouten cmp = compar(key, (*leaf)->key);
66459d04a5SEd Schouten if (cmp < 0) {
67459d04a5SEd Schouten path_taking_left(&path);
68459d04a5SEd Schouten leaf = &(*leaf)->llink;
69459d04a5SEd Schouten } else if (cmp > 0) {
70459d04a5SEd Schouten path_taking_right(&path);
71459d04a5SEd Schouten leaf = &(*leaf)->rlink;
72459d04a5SEd Schouten } else {
73*4ef9bd22SEd Schouten return (*leaf);
74459d04a5SEd Schouten }
7564566a3eSAlfred Perlstein }
7664566a3eSAlfred Perlstein
77459d04a5SEd Schouten /* Did not find a matching key in the tree. Insert a new node. */
78459d04a5SEd Schouten result = *leaf = malloc(sizeof(**leaf));
79459d04a5SEd Schouten if (result == NULL)
80459d04a5SEd Schouten return (NULL);
81459d04a5SEd Schouten result->key = (void *)key;
82459d04a5SEd Schouten result->llink = NULL;
83459d04a5SEd Schouten result->rlink = NULL;
84459d04a5SEd Schouten result->balance = 0;
85459d04a5SEd Schouten
86459d04a5SEd Schouten /*
87459d04a5SEd Schouten * Walk along the same path a second time and adjust the
88459d04a5SEd Schouten * balances. Except for the first node, all of these nodes must
89459d04a5SEd Schouten * have a balance of zero, meaning that these nodes will not get
90459d04a5SEd Schouten * out of balance.
91459d04a5SEd Schouten */
92*4ef9bd22SEd Schouten for (n = *rootp; n != *leaf;) {
93459d04a5SEd Schouten if (path_took_left(&path)) {
94459d04a5SEd Schouten n->balance += 1;
95459d04a5SEd Schouten n = n->llink;
96459d04a5SEd Schouten } else {
97459d04a5SEd Schouten n->balance -= 1;
98459d04a5SEd Schouten n = n->rlink;
9964566a3eSAlfred Perlstein }
100459d04a5SEd Schouten }
101459d04a5SEd Schouten
102459d04a5SEd Schouten /*
103459d04a5SEd Schouten * Adjusting the balances may have pushed the balance of the
104*4ef9bd22SEd Schouten * root node out of range. Perform a rotation to bring the
105459d04a5SEd Schouten * balance back in range.
106459d04a5SEd Schouten */
107*4ef9bd22SEd Schouten x = *rootp;
108459d04a5SEd Schouten if (x->balance > 1) {
109459d04a5SEd Schouten y = x->llink;
110459d04a5SEd Schouten if (y->balance < 0) {
111459d04a5SEd Schouten /*
112459d04a5SEd Schouten * Left-right case.
113459d04a5SEd Schouten *
114459d04a5SEd Schouten * x
115459d04a5SEd Schouten * / \ z
116459d04a5SEd Schouten * y D / \
117459d04a5SEd Schouten * / \ --> y x
118459d04a5SEd Schouten * A z /| |\
119459d04a5SEd Schouten * / \ A B C D
120459d04a5SEd Schouten * B C
121459d04a5SEd Schouten */
122459d04a5SEd Schouten z = y->rlink;
123459d04a5SEd Schouten y->rlink = z->llink;
124459d04a5SEd Schouten z->llink = y;
125459d04a5SEd Schouten x->llink = z->rlink;
126459d04a5SEd Schouten z->rlink = x;
127*4ef9bd22SEd Schouten *rootp = z;
128459d04a5SEd Schouten
129459d04a5SEd Schouten x->balance = z->balance > 0 ? -1 : 0;
130459d04a5SEd Schouten y->balance = z->balance < 0 ? 1 : 0;
131459d04a5SEd Schouten z->balance = 0;
132459d04a5SEd Schouten } else {
133459d04a5SEd Schouten /*
134459d04a5SEd Schouten * Left-left case.
135459d04a5SEd Schouten *
136459d04a5SEd Schouten * x y
137459d04a5SEd Schouten * / \ / \
138459d04a5SEd Schouten * y C --> A x
139459d04a5SEd Schouten * / \ / \
140459d04a5SEd Schouten * A B B C
141459d04a5SEd Schouten */
142459d04a5SEd Schouten x->llink = y->rlink;
143459d04a5SEd Schouten y->rlink = x;
144*4ef9bd22SEd Schouten *rootp = y;
145459d04a5SEd Schouten
146459d04a5SEd Schouten x->balance = 0;
147459d04a5SEd Schouten y->balance = 0;
148459d04a5SEd Schouten }
149459d04a5SEd Schouten } else if (x->balance < -1) {
150459d04a5SEd Schouten y = x->rlink;
151459d04a5SEd Schouten if (y->balance > 0) {
152459d04a5SEd Schouten /*
153459d04a5SEd Schouten * Right-left case.
154459d04a5SEd Schouten *
155459d04a5SEd Schouten * x
156459d04a5SEd Schouten * / \ z
157459d04a5SEd Schouten * A y / \
158459d04a5SEd Schouten * / \ --> x y
159459d04a5SEd Schouten * z D /| |\
160459d04a5SEd Schouten * / \ A B C D
161459d04a5SEd Schouten * B C
162459d04a5SEd Schouten */
163*4ef9bd22SEd Schouten posix_tnode *z = y->llink;
164459d04a5SEd Schouten x->rlink = z->llink;
165459d04a5SEd Schouten z->llink = x;
166459d04a5SEd Schouten y->llink = z->rlink;
167459d04a5SEd Schouten z->rlink = y;
168*4ef9bd22SEd Schouten *rootp = z;
169459d04a5SEd Schouten
170459d04a5SEd Schouten x->balance = z->balance < 0 ? 1 : 0;
171459d04a5SEd Schouten y->balance = z->balance > 0 ? -1 : 0;
172459d04a5SEd Schouten z->balance = 0;
173459d04a5SEd Schouten } else {
174459d04a5SEd Schouten /*
175459d04a5SEd Schouten * Right-right case.
176459d04a5SEd Schouten *
177459d04a5SEd Schouten * x y
178459d04a5SEd Schouten * / \ / \
179459d04a5SEd Schouten * A y --> x C
180459d04a5SEd Schouten * / \ / \
181459d04a5SEd Schouten * B C A B
182459d04a5SEd Schouten */
183459d04a5SEd Schouten x->rlink = y->llink;
184459d04a5SEd Schouten y->llink = x;
185*4ef9bd22SEd Schouten *rootp = y;
186459d04a5SEd Schouten
187459d04a5SEd Schouten x->balance = 0;
188459d04a5SEd Schouten y->balance = 0;
189459d04a5SEd Schouten }
190459d04a5SEd Schouten }
191459d04a5SEd Schouten
192459d04a5SEd Schouten /* Return the new entry. */
193*4ef9bd22SEd Schouten return (result);
19464566a3eSAlfred Perlstein }
195