1*38fd1498Szrj /* A splay-tree datatype. 2*38fd1498Szrj Copyright (C) 1998-2018 Free Software Foundation, Inc. 3*38fd1498Szrj Contributed by Mark Mitchell (mark@markmitchell.com). 4*38fd1498Szrj 5*38fd1498Szrj This file is part of GCC. 6*38fd1498Szrj 7*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it 8*38fd1498Szrj under the terms of the GNU General Public License as published by 9*38fd1498Szrj the Free Software Foundation; either version 2, or (at your option) 10*38fd1498Szrj any later version. 11*38fd1498Szrj 12*38fd1498Szrj GCC is distributed in the hope that it will be useful, but 13*38fd1498Szrj WITHOUT ANY WARRANTY; without even the implied warranty of 14*38fd1498Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15*38fd1498Szrj General Public License for more details. 16*38fd1498Szrj 17*38fd1498Szrj You should have received a copy of the GNU General Public License 18*38fd1498Szrj along with GCC; see the file COPYING. If not, write to 19*38fd1498Szrj the Free Software Foundation, 51 Franklin Street - Fifth Floor, 20*38fd1498Szrj Boston, MA 02110-1301, USA. */ 21*38fd1498Szrj 22*38fd1498Szrj /* For an easily readable description of splay-trees, see: 23*38fd1498Szrj 24*38fd1498Szrj Lewis, Harry R. and Denenberg, Larry. Data Structures and Their 25*38fd1498Szrj Algorithms. Harper-Collins, Inc. 1991. 26*38fd1498Szrj 27*38fd1498Szrj The major feature of splay trees is that all basic tree operations 28*38fd1498Szrj are amortized O(log n) time for a tree with n nodes. */ 29*38fd1498Szrj 30*38fd1498Szrj #ifndef _SPLAY_TREE_H 31*38fd1498Szrj #define _SPLAY_TREE_H 32*38fd1498Szrj 33*38fd1498Szrj #ifdef __cplusplus 34*38fd1498Szrj extern "C" { 35*38fd1498Szrj #endif /* __cplusplus */ 36*38fd1498Szrj 37*38fd1498Szrj #include "ansidecl.h" 38*38fd1498Szrj 39*38fd1498Szrj #ifdef HAVE_STDINT_H 40*38fd1498Szrj #include <stdint.h> 41*38fd1498Szrj #endif 42*38fd1498Szrj #ifdef HAVE_INTTYPES_H 43*38fd1498Szrj #include <inttypes.h> 44*38fd1498Szrj #endif 45*38fd1498Szrj 46*38fd1498Szrj /* Use typedefs for the key and data types to facilitate changing 47*38fd1498Szrj these types, if necessary. These types should be sufficiently wide 48*38fd1498Szrj that any pointer or scalar can be cast to these types, and then 49*38fd1498Szrj cast back, without loss of precision. */ 50*38fd1498Szrj typedef uintptr_t splay_tree_key; 51*38fd1498Szrj typedef uintptr_t splay_tree_value; 52*38fd1498Szrj 53*38fd1498Szrj /* Forward declaration for a node in the tree. */ 54*38fd1498Szrj typedef struct splay_tree_node_s *splay_tree_node; 55*38fd1498Szrj 56*38fd1498Szrj /* The type of a function which compares two splay-tree keys. The 57*38fd1498Szrj function should return values as for qsort. */ 58*38fd1498Szrj typedef int (*splay_tree_compare_fn) (splay_tree_key, splay_tree_key); 59*38fd1498Szrj 60*38fd1498Szrj /* The type of a function used to deallocate any resources associated 61*38fd1498Szrj with the key. */ 62*38fd1498Szrj typedef void (*splay_tree_delete_key_fn) (splay_tree_key); 63*38fd1498Szrj 64*38fd1498Szrj /* The type of a function used to deallocate any resources associated 65*38fd1498Szrj with the value. */ 66*38fd1498Szrj typedef void (*splay_tree_delete_value_fn) (splay_tree_value); 67*38fd1498Szrj 68*38fd1498Szrj /* The type of a function used to iterate over the tree. */ 69*38fd1498Szrj typedef int (*splay_tree_foreach_fn) (splay_tree_node, void*); 70*38fd1498Szrj 71*38fd1498Szrj /* The type of a function used to allocate memory for tree root and 72*38fd1498Szrj node structures. The first argument is the number of bytes needed; 73*38fd1498Szrj the second is a data pointer the splay tree functions pass through 74*38fd1498Szrj to the allocator. This function must never return zero. */ 75*38fd1498Szrj typedef void *(*splay_tree_allocate_fn) (int, void *); 76*38fd1498Szrj 77*38fd1498Szrj /* The type of a function used to free memory allocated using the 78*38fd1498Szrj corresponding splay_tree_allocate_fn. The first argument is the 79*38fd1498Szrj memory to be freed; the latter is a data pointer the splay tree 80*38fd1498Szrj functions pass through to the freer. */ 81*38fd1498Szrj typedef void (*splay_tree_deallocate_fn) (void *, void *); 82*38fd1498Szrj 83*38fd1498Szrj /* The nodes in the splay tree. */ 84*38fd1498Szrj struct splay_tree_node_s { 85*38fd1498Szrj /* The key. */ 86*38fd1498Szrj splay_tree_key key; 87*38fd1498Szrj 88*38fd1498Szrj /* The value. */ 89*38fd1498Szrj splay_tree_value value; 90*38fd1498Szrj 91*38fd1498Szrj /* The left and right children, respectively. */ 92*38fd1498Szrj splay_tree_node left; 93*38fd1498Szrj splay_tree_node right; 94*38fd1498Szrj }; 95*38fd1498Szrj 96*38fd1498Szrj /* The splay tree itself. */ 97*38fd1498Szrj struct splay_tree_s { 98*38fd1498Szrj /* The root of the tree. */ 99*38fd1498Szrj splay_tree_node root; 100*38fd1498Szrj 101*38fd1498Szrj /* The comparision function. */ 102*38fd1498Szrj splay_tree_compare_fn comp; 103*38fd1498Szrj 104*38fd1498Szrj /* The deallocate-key function. NULL if no cleanup is necessary. */ 105*38fd1498Szrj splay_tree_delete_key_fn delete_key; 106*38fd1498Szrj 107*38fd1498Szrj /* The deallocate-value function. NULL if no cleanup is necessary. */ 108*38fd1498Szrj splay_tree_delete_value_fn delete_value; 109*38fd1498Szrj 110*38fd1498Szrj /* Node allocate function. Takes allocate_data as a parameter. */ 111*38fd1498Szrj splay_tree_allocate_fn allocate; 112*38fd1498Szrj 113*38fd1498Szrj /* Free function for nodes and trees. Takes allocate_data as a parameter. */ 114*38fd1498Szrj splay_tree_deallocate_fn deallocate; 115*38fd1498Szrj 116*38fd1498Szrj /* Parameter for allocate/free functions. */ 117*38fd1498Szrj void *allocate_data; 118*38fd1498Szrj }; 119*38fd1498Szrj 120*38fd1498Szrj typedef struct splay_tree_s *splay_tree; 121*38fd1498Szrj 122*38fd1498Szrj extern splay_tree splay_tree_new (splay_tree_compare_fn, 123*38fd1498Szrj splay_tree_delete_key_fn, 124*38fd1498Szrj splay_tree_delete_value_fn); 125*38fd1498Szrj extern splay_tree splay_tree_new_with_allocator (splay_tree_compare_fn, 126*38fd1498Szrj splay_tree_delete_key_fn, 127*38fd1498Szrj splay_tree_delete_value_fn, 128*38fd1498Szrj splay_tree_allocate_fn, 129*38fd1498Szrj splay_tree_deallocate_fn, 130*38fd1498Szrj void *); 131*38fd1498Szrj extern splay_tree splay_tree_new_typed_alloc (splay_tree_compare_fn, 132*38fd1498Szrj splay_tree_delete_key_fn, 133*38fd1498Szrj splay_tree_delete_value_fn, 134*38fd1498Szrj splay_tree_allocate_fn, 135*38fd1498Szrj splay_tree_allocate_fn, 136*38fd1498Szrj splay_tree_deallocate_fn, 137*38fd1498Szrj void *); 138*38fd1498Szrj extern void splay_tree_delete (splay_tree); 139*38fd1498Szrj extern splay_tree_node splay_tree_insert (splay_tree, 140*38fd1498Szrj splay_tree_key, 141*38fd1498Szrj splay_tree_value); 142*38fd1498Szrj extern void splay_tree_remove (splay_tree, splay_tree_key); 143*38fd1498Szrj extern splay_tree_node splay_tree_lookup (splay_tree, splay_tree_key); 144*38fd1498Szrj extern splay_tree_node splay_tree_predecessor (splay_tree, splay_tree_key); 145*38fd1498Szrj extern splay_tree_node splay_tree_successor (splay_tree, splay_tree_key); 146*38fd1498Szrj extern splay_tree_node splay_tree_max (splay_tree); 147*38fd1498Szrj extern splay_tree_node splay_tree_min (splay_tree); 148*38fd1498Szrj extern int splay_tree_foreach (splay_tree, splay_tree_foreach_fn, void*); 149*38fd1498Szrj extern int splay_tree_compare_ints (splay_tree_key, splay_tree_key); 150*38fd1498Szrj extern int splay_tree_compare_pointers (splay_tree_key, splay_tree_key); 151*38fd1498Szrj 152*38fd1498Szrj #ifdef __cplusplus 153*38fd1498Szrj } 154*38fd1498Szrj #endif /* __cplusplus */ 155*38fd1498Szrj 156*38fd1498Szrj #endif /* _SPLAY_TREE_H */ 157