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