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
28 #ifndef TSEARCH_PATH_H
29 #define TSEARCH_PATH_H
36 * Bookkeeping for storing a path in a balanced binary search tree from
37 * the root to a leaf node.
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
50 /* Initializes the path structure with a zero-length path. */
52 path_init(struct path *p)
58 #define STEPS_BIT (sizeof(uintptr_t) * CHAR_BIT)
60 /* Pushes a step to the left to the end of the path. */
62 path_taking_left(struct path *p)
65 p->steps[p->nsteps / STEPS_BIT] |=
66 (uintptr_t)1 << (p->nsteps % STEPS_BIT);
70 /* Pushes a step to the right to the end of the path. */
72 path_taking_right(struct path *p)
75 p->steps[p->nsteps / STEPS_BIT] &=
76 ~((uintptr_t)1 << (p->nsteps % STEPS_BIT));
81 * Pops the first step from the path and returns whether it was a step
85 path_took_left(struct path *p)
89 result = p->steps[0] & 0x1;
90 p->steps[0] = (p->steps[0] >> 1) | (p->steps[1] << (STEPS_BIT - 1));