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