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