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