1*459d04a5SEd Schouten /*-
2*459d04a5SEd Schouten * Copyright (c) 2015 Nuxi, https://nuxi.nl/
3*459d04a5SEd Schouten *
4*459d04a5SEd Schouten * Redistribution and use in source and binary forms, with or without
5*459d04a5SEd Schouten * modification, are permitted provided that the following conditions
6*459d04a5SEd Schouten * are met:
7*459d04a5SEd Schouten * 1. Redistributions of source code must retain the above copyright
8*459d04a5SEd Schouten * notice, this list of conditions and the following disclaimer.
9*459d04a5SEd Schouten * 2. Redistributions in binary form must reproduce the above copyright
10*459d04a5SEd Schouten * notice, this list of conditions and the following disclaimer in the
11*459d04a5SEd Schouten * documentation and/or other materials provided with the distribution.
12*459d04a5SEd Schouten *
13*459d04a5SEd Schouten * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14*459d04a5SEd Schouten * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15*459d04a5SEd Schouten * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16*459d04a5SEd Schouten * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17*459d04a5SEd Schouten * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18*459d04a5SEd Schouten * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19*459d04a5SEd Schouten * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20*459d04a5SEd Schouten * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21*459d04a5SEd Schouten * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22*459d04a5SEd Schouten * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23*459d04a5SEd Schouten * SUCH DAMAGE.
24*459d04a5SEd Schouten */
25*459d04a5SEd Schouten
26*459d04a5SEd Schouten #ifndef TSEARCH_PATH_H
27*459d04a5SEd Schouten #define TSEARCH_PATH_H
28*459d04a5SEd Schouten
29*459d04a5SEd Schouten #include <limits.h>
30*459d04a5SEd Schouten #include <stdbool.h>
31*459d04a5SEd Schouten #include <stdint.h>
32*459d04a5SEd Schouten
33*459d04a5SEd Schouten /*
34*459d04a5SEd Schouten * Bookkeeping for storing a path in a balanced binary search tree from
35*459d04a5SEd Schouten * the root to a leaf node.
36*459d04a5SEd Schouten *
37*459d04a5SEd Schouten * For an AVL tree we know that its maximum height of a tree is bounded
38*459d04a5SEd Schouten * by approximately 1.44 * log2(n) - 0.328. Given that the number of
39*459d04a5SEd Schouten * entries of the tree is constrained by the size of the address space,
40*459d04a5SEd Schouten * two uintptr_t's provide sufficient space to store the path from the
41*459d04a5SEd Schouten * root to any leaf.
42*459d04a5SEd Schouten */
43*459d04a5SEd Schouten struct path {
44*459d04a5SEd Schouten uintptr_t steps[2];
45*459d04a5SEd Schouten unsigned int nsteps;
46*459d04a5SEd Schouten };
47*459d04a5SEd Schouten
48*459d04a5SEd Schouten /* Initializes the path structure with a zero-length path. */
49*459d04a5SEd Schouten static inline void
path_init(struct path * p)50*459d04a5SEd Schouten path_init(struct path *p)
51*459d04a5SEd Schouten {
52*459d04a5SEd Schouten
53*459d04a5SEd Schouten p->nsteps = 0;
54*459d04a5SEd Schouten }
55*459d04a5SEd Schouten
56*459d04a5SEd Schouten #define STEPS_BIT (sizeof(uintptr_t) * CHAR_BIT)
57*459d04a5SEd Schouten
58*459d04a5SEd Schouten /* Pushes a step to the left to the end of the path. */
59*459d04a5SEd Schouten static inline void
path_taking_left(struct path * p)60*459d04a5SEd Schouten path_taking_left(struct path *p)
61*459d04a5SEd Schouten {
62*459d04a5SEd Schouten
63*459d04a5SEd Schouten p->steps[p->nsteps / STEPS_BIT] |=
64*459d04a5SEd Schouten (uintptr_t)1 << (p->nsteps % STEPS_BIT);
65*459d04a5SEd Schouten ++p->nsteps;
66*459d04a5SEd Schouten }
67*459d04a5SEd Schouten
68*459d04a5SEd Schouten /* Pushes a step to the right to the end of the path. */
69*459d04a5SEd Schouten static inline void
path_taking_right(struct path * p)70*459d04a5SEd Schouten path_taking_right(struct path *p)
71*459d04a5SEd Schouten {
72*459d04a5SEd Schouten
73*459d04a5SEd Schouten p->steps[p->nsteps / STEPS_BIT] &=
74*459d04a5SEd Schouten ~((uintptr_t)1 << (p->nsteps % STEPS_BIT));
75*459d04a5SEd Schouten ++p->nsteps;
76*459d04a5SEd Schouten }
77*459d04a5SEd Schouten
78*459d04a5SEd Schouten /*
79*459d04a5SEd Schouten * Pops the first step from the path and returns whether it was a step
80*459d04a5SEd Schouten * to the left.
81*459d04a5SEd Schouten */
82*459d04a5SEd Schouten static inline bool
path_took_left(struct path * p)83*459d04a5SEd Schouten path_took_left(struct path *p)
84*459d04a5SEd Schouten {
85*459d04a5SEd Schouten bool result;
86*459d04a5SEd Schouten
87*459d04a5SEd Schouten result = p->steps[0] & 0x1;
88*459d04a5SEd Schouten p->steps[0] = (p->steps[0] >> 1) | (p->steps[1] << (STEPS_BIT - 1));
89*459d04a5SEd Schouten p->steps[1] >>= 1;
90*459d04a5SEd Schouten return (result);
91*459d04a5SEd Schouten }
92*459d04a5SEd Schouten
93*459d04a5SEd Schouten #undef STEPS_BIT
94*459d04a5SEd Schouten
95*459d04a5SEd Schouten #endif
96