splay-tree.h revision 1.1.1.3
1/* A splay-tree datatype. 2 Copyright (C) 1998-2015 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 34extern "C" { 35#endif /* __cplusplus */ 36 37#include "ansidecl.h" 38 39#ifdef HAVE_STDINT_H 40#include <stdint.h> 41#endif 42#ifdef HAVE_INTTYPES_H 43#include <inttypes.h> 44#endif 45 46#ifndef GTY 47#define GTY(X) 48#endif 49 50/* Use typedefs for the key and data types to facilitate changing 51 these types, if necessary. These types should be sufficiently wide 52 that any pointer or scalar can be cast to these types, and then 53 cast back, without loss of precision. */ 54typedef uintptr_t splay_tree_key; 55typedef uintptr_t splay_tree_value; 56 57/* Forward declaration for a node in the tree. */ 58typedef struct splay_tree_node_s *splay_tree_node; 59 60/* The type of a function which compares two splay-tree keys. The 61 function should return values as for qsort. */ 62typedef int (*splay_tree_compare_fn) (splay_tree_key, splay_tree_key); 63 64/* The type of a function used to deallocate any resources associated 65 with the key. */ 66typedef void (*splay_tree_delete_key_fn) (splay_tree_key); 67 68/* The type of a function used to deallocate any resources associated 69 with the value. */ 70typedef void (*splay_tree_delete_value_fn) (splay_tree_value); 71 72/* The type of a function used to iterate over the tree. */ 73typedef int (*splay_tree_foreach_fn) (splay_tree_node, void*); 74 75/* The type of a function used to allocate memory for tree root and 76 node structures. The first argument is the number of bytes needed; 77 the second is a data pointer the splay tree functions pass through 78 to the allocator. This function must never return zero. */ 79typedef void *(*splay_tree_allocate_fn) (int, void *); 80 81/* The type of a function used to free memory allocated using the 82 corresponding splay_tree_allocate_fn. The first argument is the 83 memory to be freed; the latter is a data pointer the splay tree 84 functions pass through to the freer. */ 85typedef void (*splay_tree_deallocate_fn) (void *, void *); 86 87/* The nodes in the splay tree. */ 88struct GTY(()) splay_tree_node_s { 89 /* The key. */ 90 splay_tree_key GTY ((use_param1)) key; 91 92 /* The value. */ 93 splay_tree_value GTY ((use_param2)) value; 94 95 /* The left and right children, respectively. */ 96 splay_tree_node GTY ((use_params)) left; 97 splay_tree_node GTY ((use_params)) right; 98}; 99 100/* The splay tree itself. */ 101struct GTY(()) splay_tree_s { 102 /* The root of the tree. */ 103 splay_tree_node GTY ((use_params)) root; 104 105 /* The comparision function. */ 106 splay_tree_compare_fn comp; 107 108 /* The deallocate-key function. NULL if no cleanup is necessary. */ 109 splay_tree_delete_key_fn delete_key; 110 111 /* The deallocate-value function. NULL if no cleanup is necessary. */ 112 splay_tree_delete_value_fn delete_value; 113 114 /* Node allocate function. Takes allocate_data as a parameter. */ 115 splay_tree_allocate_fn allocate; 116 117 /* Free function for nodes and trees. Takes allocate_data as a parameter. */ 118 splay_tree_deallocate_fn deallocate; 119 120 /* Parameter for allocate/free functions. */ 121 void * GTY((skip)) allocate_data; 122}; 123 124typedef struct splay_tree_s *splay_tree; 125 126extern splay_tree splay_tree_new (splay_tree_compare_fn, 127 splay_tree_delete_key_fn, 128 splay_tree_delete_value_fn); 129extern splay_tree splay_tree_new_with_allocator (splay_tree_compare_fn, 130 splay_tree_delete_key_fn, 131 splay_tree_delete_value_fn, 132 splay_tree_allocate_fn, 133 splay_tree_deallocate_fn, 134 void *); 135extern splay_tree splay_tree_new_typed_alloc (splay_tree_compare_fn, 136 splay_tree_delete_key_fn, 137 splay_tree_delete_value_fn, 138 splay_tree_allocate_fn, 139 splay_tree_allocate_fn, 140 splay_tree_deallocate_fn, 141 void *); 142extern void splay_tree_delete (splay_tree); 143extern splay_tree_node splay_tree_insert (splay_tree, 144 splay_tree_key, 145 splay_tree_value); 146extern void splay_tree_remove (splay_tree, splay_tree_key); 147extern splay_tree_node splay_tree_lookup (splay_tree, splay_tree_key); 148extern splay_tree_node splay_tree_predecessor (splay_tree, splay_tree_key); 149extern splay_tree_node splay_tree_successor (splay_tree, splay_tree_key); 150extern splay_tree_node splay_tree_max (splay_tree); 151extern splay_tree_node splay_tree_min (splay_tree); 152extern int splay_tree_foreach (splay_tree, splay_tree_foreach_fn, void*); 153extern int splay_tree_compare_ints (splay_tree_key, splay_tree_key); 154extern int splay_tree_compare_pointers (splay_tree_key, splay_tree_key); 155 156#ifdef __cplusplus 157} 158#endif /* __cplusplus */ 159 160#endif /* _SPLAY_TREE_H */ 161