xref: /freebsd/lib/libc/stdlib/tsearch_path.h (revision f5f40dd63bc7acbb5312b26ac1ea1103c12352a6)
1 /*-
2  * Copyright (c) 2015 Nuxi, https://nuxi.nl/
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  */
25 
26 #ifndef TSEARCH_PATH_H
27 #define	TSEARCH_PATH_H
28 
29 #include <limits.h>
30 #include <stdbool.h>
31 #include <stdint.h>
32 
33 /*
34  * Bookkeeping for storing a path in a balanced binary search tree from
35  * the root to a leaf node.
36  *
37  * For an AVL tree we know that its maximum height of a tree is bounded
38  * by approximately 1.44 * log2(n) - 0.328. Given that the number of
39  * entries of the tree is constrained by the size of the address space,
40  * two uintptr_t's provide sufficient space to store the path from the
41  * root to any leaf.
42  */
43 struct path {
44 	uintptr_t steps[2];
45 	unsigned int nsteps;
46 };
47 
48 /* Initializes the path structure with a zero-length path. */
49 static inline void
50 path_init(struct path *p)
51 {
52 
53 	p->nsteps = 0;
54 }
55 
56 #define	STEPS_BIT (sizeof(uintptr_t) * CHAR_BIT)
57 
58 /* Pushes a step to the left to the end of the path. */
59 static inline void
60 path_taking_left(struct path *p)
61 {
62 
63 	p->steps[p->nsteps / STEPS_BIT] |=
64 	    (uintptr_t)1 << (p->nsteps % STEPS_BIT);
65 	++p->nsteps;
66 }
67 
68 /* Pushes a step to the right to the end of the path. */
69 static inline void
70 path_taking_right(struct path *p)
71 {
72 
73 	p->steps[p->nsteps / STEPS_BIT] &=
74 	    ~((uintptr_t)1 << (p->nsteps % STEPS_BIT));
75 	++p->nsteps;
76 }
77 
78 /*
79  * Pops the first step from the path and returns whether it was a step
80  * to the left.
81  */
82 static inline bool
83 path_took_left(struct path *p)
84 {
85 	bool result;
86 
87 	result = p->steps[0] & 0x1;
88 	p->steps[0] = (p->steps[0] >> 1) | (p->steps[1] << (STEPS_BIT - 1));
89 	p->steps[1] >>= 1;
90 	return (result);
91 }
92 
93 #undef STEPS_BIT
94 
95 #endif
96