15796c8dcSSimon Schubert /* A splay-tree datatype. 2cf7f2e2dSJohn Marino Copyright 1998, 1999, 2000, 2002, 2005, 2007, 2009, 2010 35796c8dcSSimon Schubert Free Software Foundation, Inc. 45796c8dcSSimon Schubert Contributed by Mark Mitchell (mark@markmitchell.com). 55796c8dcSSimon Schubert 65796c8dcSSimon Schubert This file is part of GCC. 75796c8dcSSimon Schubert 85796c8dcSSimon Schubert GCC is free software; you can redistribute it and/or modify it 95796c8dcSSimon Schubert under the terms of the GNU General Public License as published by 105796c8dcSSimon Schubert the Free Software Foundation; either version 2, or (at your option) 115796c8dcSSimon Schubert any later version. 125796c8dcSSimon Schubert 135796c8dcSSimon Schubert GCC is distributed in the hope that it will be useful, but 145796c8dcSSimon Schubert WITHOUT ANY WARRANTY; without even the implied warranty of 155796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 165796c8dcSSimon Schubert General Public License for more details. 175796c8dcSSimon Schubert 185796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 195796c8dcSSimon Schubert along with GCC; see the file COPYING. If not, write to 205796c8dcSSimon Schubert the Free Software Foundation, 51 Franklin Street - Fifth Floor, 215796c8dcSSimon Schubert Boston, MA 02110-1301, USA. */ 225796c8dcSSimon Schubert 235796c8dcSSimon Schubert /* For an easily readable description of splay-trees, see: 245796c8dcSSimon Schubert 255796c8dcSSimon Schubert Lewis, Harry R. and Denenberg, Larry. Data Structures and Their 265796c8dcSSimon Schubert Algorithms. Harper-Collins, Inc. 1991. 275796c8dcSSimon Schubert 285796c8dcSSimon Schubert The major feature of splay trees is that all basic tree operations 295796c8dcSSimon Schubert are amortized O(log n) time for a tree with n nodes. */ 305796c8dcSSimon Schubert 315796c8dcSSimon Schubert #ifndef _SPLAY_TREE_H 325796c8dcSSimon Schubert #define _SPLAY_TREE_H 335796c8dcSSimon Schubert 345796c8dcSSimon Schubert #ifdef __cplusplus 355796c8dcSSimon Schubert extern "C" { 365796c8dcSSimon Schubert #endif /* __cplusplus */ 375796c8dcSSimon Schubert 385796c8dcSSimon Schubert #include "ansidecl.h" 395796c8dcSSimon Schubert 40*ef5ccd6cSJohn Marino #ifdef HAVE_STDINT_H 41*ef5ccd6cSJohn Marino #include <stdint.h> 42cf7f2e2dSJohn Marino #endif 43*ef5ccd6cSJohn Marino #ifdef HAVE_INTTYPES_H 44*ef5ccd6cSJohn Marino #include <inttypes.h> 455796c8dcSSimon Schubert #endif 465796c8dcSSimon Schubert 475796c8dcSSimon Schubert #ifndef GTY 485796c8dcSSimon Schubert #define GTY(X) 495796c8dcSSimon Schubert #endif 505796c8dcSSimon Schubert 515796c8dcSSimon Schubert /* Use typedefs for the key and data types to facilitate changing 525796c8dcSSimon Schubert these types, if necessary. These types should be sufficiently wide 535796c8dcSSimon Schubert that any pointer or scalar can be cast to these types, and then 545796c8dcSSimon Schubert cast back, without loss of precision. */ 55*ef5ccd6cSJohn Marino typedef uintptr_t splay_tree_key; 56*ef5ccd6cSJohn Marino typedef uintptr_t splay_tree_value; 575796c8dcSSimon Schubert 585796c8dcSSimon Schubert /* Forward declaration for a node in the tree. */ 595796c8dcSSimon Schubert typedef struct splay_tree_node_s *splay_tree_node; 605796c8dcSSimon Schubert 615796c8dcSSimon Schubert /* The type of a function which compares two splay-tree keys. The 625796c8dcSSimon Schubert function should return values as for qsort. */ 635796c8dcSSimon Schubert typedef int (*splay_tree_compare_fn) (splay_tree_key, splay_tree_key); 645796c8dcSSimon Schubert 655796c8dcSSimon Schubert /* The type of a function used to deallocate any resources associated 665796c8dcSSimon Schubert with the key. */ 675796c8dcSSimon Schubert typedef void (*splay_tree_delete_key_fn) (splay_tree_key); 685796c8dcSSimon Schubert 695796c8dcSSimon Schubert /* The type of a function used to deallocate any resources associated 705796c8dcSSimon Schubert with the value. */ 715796c8dcSSimon Schubert typedef void (*splay_tree_delete_value_fn) (splay_tree_value); 725796c8dcSSimon Schubert 735796c8dcSSimon Schubert /* The type of a function used to iterate over the tree. */ 745796c8dcSSimon Schubert typedef int (*splay_tree_foreach_fn) (splay_tree_node, void*); 755796c8dcSSimon Schubert 765796c8dcSSimon Schubert /* The type of a function used to allocate memory for tree root and 775796c8dcSSimon Schubert node structures. The first argument is the number of bytes needed; 785796c8dcSSimon Schubert the second is a data pointer the splay tree functions pass through 795796c8dcSSimon Schubert to the allocator. This function must never return zero. */ 805796c8dcSSimon Schubert typedef void *(*splay_tree_allocate_fn) (int, void *); 815796c8dcSSimon Schubert 825796c8dcSSimon Schubert /* The type of a function used to free memory allocated using the 835796c8dcSSimon Schubert corresponding splay_tree_allocate_fn. The first argument is the 845796c8dcSSimon Schubert memory to be freed; the latter is a data pointer the splay tree 855796c8dcSSimon Schubert functions pass through to the freer. */ 865796c8dcSSimon Schubert typedef void (*splay_tree_deallocate_fn) (void *, void *); 875796c8dcSSimon Schubert 885796c8dcSSimon Schubert /* The nodes in the splay tree. */ 895796c8dcSSimon Schubert struct GTY(()) splay_tree_node_s { 905796c8dcSSimon Schubert /* The key. */ 915796c8dcSSimon Schubert splay_tree_key GTY ((use_param1)) key; 925796c8dcSSimon Schubert 935796c8dcSSimon Schubert /* The value. */ 945796c8dcSSimon Schubert splay_tree_value GTY ((use_param2)) value; 955796c8dcSSimon Schubert 965796c8dcSSimon Schubert /* The left and right children, respectively. */ 975796c8dcSSimon Schubert splay_tree_node GTY ((use_params)) left; 985796c8dcSSimon Schubert splay_tree_node GTY ((use_params)) right; 995796c8dcSSimon Schubert }; 1005796c8dcSSimon Schubert 1015796c8dcSSimon Schubert /* The splay tree itself. */ 1025796c8dcSSimon Schubert struct GTY(()) splay_tree_s { 1035796c8dcSSimon Schubert /* The root of the tree. */ 1045796c8dcSSimon Schubert splay_tree_node GTY ((use_params)) root; 1055796c8dcSSimon Schubert 1065796c8dcSSimon Schubert /* The comparision function. */ 1075796c8dcSSimon Schubert splay_tree_compare_fn comp; 1085796c8dcSSimon Schubert 1095796c8dcSSimon Schubert /* The deallocate-key function. NULL if no cleanup is necessary. */ 1105796c8dcSSimon Schubert splay_tree_delete_key_fn delete_key; 1115796c8dcSSimon Schubert 1125796c8dcSSimon Schubert /* The deallocate-value function. NULL if no cleanup is necessary. */ 1135796c8dcSSimon Schubert splay_tree_delete_value_fn delete_value; 1145796c8dcSSimon Schubert 115cf7f2e2dSJohn Marino /* Node allocate function. Takes allocate_data as a parameter. */ 1165796c8dcSSimon Schubert splay_tree_allocate_fn allocate; 117cf7f2e2dSJohn Marino 118cf7f2e2dSJohn Marino /* Free function for nodes and trees. Takes allocate_data as a parameter. */ 1195796c8dcSSimon Schubert splay_tree_deallocate_fn deallocate; 120cf7f2e2dSJohn Marino 121cf7f2e2dSJohn Marino /* Parameter for allocate/free functions. */ 1225796c8dcSSimon Schubert void * GTY((skip)) allocate_data; 1235796c8dcSSimon Schubert }; 1245796c8dcSSimon Schubert 1255796c8dcSSimon Schubert typedef struct splay_tree_s *splay_tree; 1265796c8dcSSimon Schubert 1275796c8dcSSimon Schubert extern splay_tree splay_tree_new (splay_tree_compare_fn, 1285796c8dcSSimon Schubert splay_tree_delete_key_fn, 1295796c8dcSSimon Schubert splay_tree_delete_value_fn); 1305796c8dcSSimon Schubert extern splay_tree splay_tree_new_with_allocator (splay_tree_compare_fn, 1315796c8dcSSimon Schubert splay_tree_delete_key_fn, 1325796c8dcSSimon Schubert splay_tree_delete_value_fn, 1335796c8dcSSimon Schubert splay_tree_allocate_fn, 1345796c8dcSSimon Schubert splay_tree_deallocate_fn, 1355796c8dcSSimon Schubert void *); 136cf7f2e2dSJohn Marino extern splay_tree splay_tree_new_typed_alloc (splay_tree_compare_fn, 137cf7f2e2dSJohn Marino splay_tree_delete_key_fn, 138cf7f2e2dSJohn Marino splay_tree_delete_value_fn, 139cf7f2e2dSJohn Marino splay_tree_allocate_fn, 140cf7f2e2dSJohn Marino splay_tree_allocate_fn, 141cf7f2e2dSJohn Marino splay_tree_deallocate_fn, 142cf7f2e2dSJohn Marino void *); 1435796c8dcSSimon Schubert extern void splay_tree_delete (splay_tree); 1445796c8dcSSimon Schubert extern splay_tree_node splay_tree_insert (splay_tree, 1455796c8dcSSimon Schubert splay_tree_key, 1465796c8dcSSimon Schubert splay_tree_value); 1475796c8dcSSimon Schubert extern void splay_tree_remove (splay_tree, splay_tree_key); 1485796c8dcSSimon Schubert extern splay_tree_node splay_tree_lookup (splay_tree, splay_tree_key); 1495796c8dcSSimon Schubert extern splay_tree_node splay_tree_predecessor (splay_tree, splay_tree_key); 1505796c8dcSSimon Schubert extern splay_tree_node splay_tree_successor (splay_tree, splay_tree_key); 1515796c8dcSSimon Schubert extern splay_tree_node splay_tree_max (splay_tree); 1525796c8dcSSimon Schubert extern splay_tree_node splay_tree_min (splay_tree); 1535796c8dcSSimon Schubert extern int splay_tree_foreach (splay_tree, splay_tree_foreach_fn, void*); 1545796c8dcSSimon Schubert extern int splay_tree_compare_ints (splay_tree_key, splay_tree_key); 1555796c8dcSSimon Schubert extern int splay_tree_compare_pointers (splay_tree_key, splay_tree_key); 1565796c8dcSSimon Schubert 1575796c8dcSSimon Schubert #ifdef __cplusplus 1585796c8dcSSimon Schubert } 1595796c8dcSSimon Schubert #endif /* __cplusplus */ 1605796c8dcSSimon Schubert 1615796c8dcSSimon Schubert #endif /* _SPLAY_TREE_H */ 162