198b9484cSchristos /* A splay-tree datatype. 2*e663ba6eSchristos Copyright (C) 1998-2024 Free Software Foundation, Inc. 398b9484cSchristos Contributed by Mark Mitchell (mark@markmitchell.com). 498b9484cSchristos 598b9484cSchristos This file is part of GCC. 698b9484cSchristos 798b9484cSchristos GCC is free software; you can redistribute it and/or modify it 898b9484cSchristos under the terms of the GNU General Public License as published by 998b9484cSchristos the Free Software Foundation; either version 2, or (at your option) 1098b9484cSchristos any later version. 1198b9484cSchristos 1298b9484cSchristos GCC is distributed in the hope that it will be useful, but 1398b9484cSchristos WITHOUT ANY WARRANTY; without even the implied warranty of 1498b9484cSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1598b9484cSchristos General Public License for more details. 1698b9484cSchristos 1798b9484cSchristos You should have received a copy of the GNU General Public License 1898b9484cSchristos along with GCC; see the file COPYING. If not, write to 1998b9484cSchristos the Free Software Foundation, 51 Franklin Street - Fifth Floor, 2098b9484cSchristos Boston, MA 02110-1301, USA. */ 2198b9484cSchristos 2298b9484cSchristos /* For an easily readable description of splay-trees, see: 2398b9484cSchristos 2498b9484cSchristos Lewis, Harry R. and Denenberg, Larry. Data Structures and Their 2598b9484cSchristos Algorithms. Harper-Collins, Inc. 1991. 2698b9484cSchristos 2798b9484cSchristos The major feature of splay trees is that all basic tree operations 2898b9484cSchristos are amortized O(log n) time for a tree with n nodes. */ 2998b9484cSchristos 3098b9484cSchristos #ifndef _SPLAY_TREE_H 3198b9484cSchristos #define _SPLAY_TREE_H 3298b9484cSchristos 3398b9484cSchristos #ifdef __cplusplus 3498b9484cSchristos extern "C" { 3598b9484cSchristos #endif /* __cplusplus */ 3698b9484cSchristos 3798b9484cSchristos #include "ansidecl.h" 3898b9484cSchristos 39a2e2270fSchristos #ifdef HAVE_STDINT_H 40a2e2270fSchristos #include <stdint.h> 4198b9484cSchristos #endif 42a2e2270fSchristos #ifdef HAVE_INTTYPES_H 43a2e2270fSchristos #include <inttypes.h> 4498b9484cSchristos #endif 4598b9484cSchristos 4698b9484cSchristos /* Use typedefs for the key and data types to facilitate changing 4798b9484cSchristos these types, if necessary. These types should be sufficiently wide 4898b9484cSchristos that any pointer or scalar can be cast to these types, and then 4998b9484cSchristos cast back, without loss of precision. */ 50a2e2270fSchristos typedef uintptr_t splay_tree_key; 51a2e2270fSchristos typedef uintptr_t splay_tree_value; 5298b9484cSchristos 5398b9484cSchristos /* Forward declaration for a node in the tree. */ 5498b9484cSchristos typedef struct splay_tree_node_s *splay_tree_node; 5598b9484cSchristos 5698b9484cSchristos /* The type of a function which compares two splay-tree keys. The 5798b9484cSchristos function should return values as for qsort. */ 5898b9484cSchristos typedef int (*splay_tree_compare_fn) (splay_tree_key, splay_tree_key); 5998b9484cSchristos 6098b9484cSchristos /* The type of a function used to deallocate any resources associated 614559860eSchristos with the key. If you provide this function, the splay tree 624559860eSchristos will take the ownership of the memory of the splay_tree_key arg 634559860eSchristos of splay_tree_insert. This function is called to release the keys 644559860eSchristos present in the tree when calling splay_tree_delete or splay_tree_remove. 654559860eSchristos If splay_tree_insert is called with a key equal to a key already 664559860eSchristos present in the tree, the old key and old value will be released. */ 6798b9484cSchristos typedef void (*splay_tree_delete_key_fn) (splay_tree_key); 6898b9484cSchristos 6998b9484cSchristos /* The type of a function used to deallocate any resources associated 704559860eSchristos with the value. If you provide this function, the memory of the 714559860eSchristos splay_tree_value arg of splay_tree_insert is managed similarly to 724559860eSchristos the splay_tree_key memory: see splay_tree_delete_key_fn. */ 7398b9484cSchristos typedef void (*splay_tree_delete_value_fn) (splay_tree_value); 7498b9484cSchristos 7598b9484cSchristos /* The type of a function used to iterate over the tree. */ 7698b9484cSchristos typedef int (*splay_tree_foreach_fn) (splay_tree_node, void*); 7798b9484cSchristos 7898b9484cSchristos /* The type of a function used to allocate memory for tree root and 7998b9484cSchristos node structures. The first argument is the number of bytes needed; 8098b9484cSchristos the second is a data pointer the splay tree functions pass through 8198b9484cSchristos to the allocator. This function must never return zero. */ 8298b9484cSchristos typedef void *(*splay_tree_allocate_fn) (int, void *); 8398b9484cSchristos 8498b9484cSchristos /* The type of a function used to free memory allocated using the 8598b9484cSchristos corresponding splay_tree_allocate_fn. The first argument is the 8698b9484cSchristos memory to be freed; the latter is a data pointer the splay tree 8798b9484cSchristos functions pass through to the freer. */ 8898b9484cSchristos typedef void (*splay_tree_deallocate_fn) (void *, void *); 8998b9484cSchristos 9098b9484cSchristos /* The nodes in the splay tree. */ 91ba340e45Schristos struct splay_tree_node_s { 9298b9484cSchristos /* The key. */ 93ba340e45Schristos splay_tree_key key; 9498b9484cSchristos 9598b9484cSchristos /* The value. */ 96ba340e45Schristos splay_tree_value value; 9798b9484cSchristos 9898b9484cSchristos /* The left and right children, respectively. */ 99ba340e45Schristos splay_tree_node left; 100ba340e45Schristos splay_tree_node right; 10198b9484cSchristos }; 10298b9484cSchristos 10398b9484cSchristos /* The splay tree itself. */ 104ba340e45Schristos struct splay_tree_s { 10598b9484cSchristos /* The root of the tree. */ 106ba340e45Schristos splay_tree_node root; 10798b9484cSchristos 10898b9484cSchristos /* The comparision function. */ 10998b9484cSchristos splay_tree_compare_fn comp; 11098b9484cSchristos 11198b9484cSchristos /* The deallocate-key function. NULL if no cleanup is necessary. */ 11298b9484cSchristos splay_tree_delete_key_fn delete_key; 11398b9484cSchristos 11498b9484cSchristos /* The deallocate-value function. NULL if no cleanup is necessary. */ 11598b9484cSchristos splay_tree_delete_value_fn delete_value; 11698b9484cSchristos 11798b9484cSchristos /* Node allocate function. Takes allocate_data as a parameter. */ 11898b9484cSchristos splay_tree_allocate_fn allocate; 11998b9484cSchristos 12098b9484cSchristos /* Free function for nodes and trees. Takes allocate_data as a parameter. */ 12198b9484cSchristos splay_tree_deallocate_fn deallocate; 12298b9484cSchristos 12398b9484cSchristos /* Parameter for allocate/free functions. */ 124ba340e45Schristos void *allocate_data; 12598b9484cSchristos }; 12698b9484cSchristos 12798b9484cSchristos typedef struct splay_tree_s *splay_tree; 12898b9484cSchristos 12998b9484cSchristos extern splay_tree splay_tree_new (splay_tree_compare_fn, 13098b9484cSchristos splay_tree_delete_key_fn, 13198b9484cSchristos splay_tree_delete_value_fn); 13298b9484cSchristos extern splay_tree splay_tree_new_with_allocator (splay_tree_compare_fn, 13398b9484cSchristos splay_tree_delete_key_fn, 13498b9484cSchristos splay_tree_delete_value_fn, 13598b9484cSchristos splay_tree_allocate_fn, 13698b9484cSchristos splay_tree_deallocate_fn, 13798b9484cSchristos void *); 13898b9484cSchristos extern splay_tree splay_tree_new_typed_alloc (splay_tree_compare_fn, 13998b9484cSchristos splay_tree_delete_key_fn, 14098b9484cSchristos splay_tree_delete_value_fn, 14198b9484cSchristos splay_tree_allocate_fn, 14298b9484cSchristos splay_tree_allocate_fn, 14398b9484cSchristos splay_tree_deallocate_fn, 14498b9484cSchristos void *); 14598b9484cSchristos extern void splay_tree_delete (splay_tree); 14698b9484cSchristos extern splay_tree_node splay_tree_insert (splay_tree, 14798b9484cSchristos splay_tree_key, 14898b9484cSchristos splay_tree_value); 14998b9484cSchristos extern void splay_tree_remove (splay_tree, splay_tree_key); 15098b9484cSchristos extern splay_tree_node splay_tree_lookup (splay_tree, splay_tree_key); 15198b9484cSchristos extern splay_tree_node splay_tree_predecessor (splay_tree, splay_tree_key); 15298b9484cSchristos extern splay_tree_node splay_tree_successor (splay_tree, splay_tree_key); 15398b9484cSchristos extern splay_tree_node splay_tree_max (splay_tree); 15498b9484cSchristos extern splay_tree_node splay_tree_min (splay_tree); 15598b9484cSchristos extern int splay_tree_foreach (splay_tree, splay_tree_foreach_fn, void*); 15698b9484cSchristos extern int splay_tree_compare_ints (splay_tree_key, splay_tree_key); 15798b9484cSchristos extern int splay_tree_compare_pointers (splay_tree_key, splay_tree_key); 1584559860eSchristos extern int splay_tree_compare_strings (splay_tree_key, splay_tree_key); 1594559860eSchristos extern void splay_tree_delete_pointers (splay_tree_value); 16098b9484cSchristos 16198b9484cSchristos #ifdef __cplusplus 16298b9484cSchristos } 16398b9484cSchristos #endif /* __cplusplus */ 16498b9484cSchristos 16598b9484cSchristos #endif /* _SPLAY_TREE_H */ 166