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 #ifndef TSEARCH_PATH_H
27 #define TSEARCH_PATH_H
34 * Bookkeeping for storing a path in a balanced binary search tree from
35 * the root to a leaf node.
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
48 /* Initializes the path structure with a zero-length path. */
50 path_init(struct path *p)
56 #define STEPS_BIT (sizeof(uintptr_t) * CHAR_BIT)
58 /* Pushes a step to the left to the end of the path. */
60 path_taking_left(struct path *p)
63 p->steps[p->nsteps / STEPS_BIT] |=
64 (uintptr_t)1 << (p->nsteps % STEPS_BIT);
68 /* Pushes a step to the right to the end of the path. */
70 path_taking_right(struct path *p)
73 p->steps[p->nsteps / STEPS_BIT] &=
74 ~((uintptr_t)1 << (p->nsteps % STEPS_BIT));
79 * Pops the first step from the path and returns whether it was a step
83 path_took_left(struct path *p)
87 result = p->steps[0] & 0x1;
88 p->steps[0] = (p->steps[0] >> 1) | (p->steps[1] << (STEPS_BIT - 1));