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