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