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