xref: /netbsd-src/external/gpl3/binutils/dist/include/splay-tree.h (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
12a6b7db3Sskrll /* A splay-tree datatype.
2*cb63e24eSchristos    Copyright (C) 1998-2024 Free Software Foundation, Inc.
32a6b7db3Sskrll    Contributed by Mark Mitchell (mark@markmitchell.com).
42a6b7db3Sskrll 
52a6b7db3Sskrll    This file is part of GCC.
62a6b7db3Sskrll 
72a6b7db3Sskrll    GCC is free software; you can redistribute it and/or modify it
82a6b7db3Sskrll    under the terms of the GNU General Public License as published by
92a6b7db3Sskrll    the Free Software Foundation; either version 2, or (at your option)
102a6b7db3Sskrll    any later version.
112a6b7db3Sskrll 
122a6b7db3Sskrll    GCC is distributed in the hope that it will be useful, but
132a6b7db3Sskrll    WITHOUT ANY WARRANTY; without even the implied warranty of
142a6b7db3Sskrll    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
152a6b7db3Sskrll    General Public License for more details.
162a6b7db3Sskrll 
172a6b7db3Sskrll    You should have received a copy of the GNU General Public License
182a6b7db3Sskrll    along with GCC; see the file COPYING.  If not, write to
192a6b7db3Sskrll    the Free Software Foundation, 51 Franklin Street - Fifth Floor,
202a6b7db3Sskrll    Boston, MA 02110-1301, USA.  */
212a6b7db3Sskrll 
222a6b7db3Sskrll /* For an easily readable description of splay-trees, see:
232a6b7db3Sskrll 
242a6b7db3Sskrll      Lewis, Harry R. and Denenberg, Larry.  Data Structures and Their
252a6b7db3Sskrll      Algorithms.  Harper-Collins, Inc.  1991.
262a6b7db3Sskrll 
272a6b7db3Sskrll    The major feature of splay trees is that all basic tree operations
282a6b7db3Sskrll    are amortized O(log n) time for a tree with n nodes.  */
292a6b7db3Sskrll 
302a6b7db3Sskrll #ifndef _SPLAY_TREE_H
312a6b7db3Sskrll #define _SPLAY_TREE_H
322a6b7db3Sskrll 
332a6b7db3Sskrll #ifdef __cplusplus
342a6b7db3Sskrll extern "C" {
352a6b7db3Sskrll #endif /* __cplusplus */
362a6b7db3Sskrll 
372a6b7db3Sskrll #include "ansidecl.h"
382a6b7db3Sskrll 
39883529b6Schristos #ifdef HAVE_STDINT_H
40883529b6Schristos #include <stdint.h>
4145548106Schristos #endif
42883529b6Schristos #ifdef HAVE_INTTYPES_H
43883529b6Schristos #include <inttypes.h>
442a6b7db3Sskrll #endif
452a6b7db3Sskrll 
462a6b7db3Sskrll /* Use typedefs for the key and data types to facilitate changing
472a6b7db3Sskrll    these types, if necessary.  These types should be sufficiently wide
482a6b7db3Sskrll    that any pointer or scalar can be cast to these types, and then
492a6b7db3Sskrll    cast back, without loss of precision.  */
50883529b6Schristos typedef uintptr_t splay_tree_key;
51883529b6Schristos typedef uintptr_t splay_tree_value;
522a6b7db3Sskrll 
532a6b7db3Sskrll /* Forward declaration for a node in the tree.  */
542a6b7db3Sskrll typedef struct splay_tree_node_s *splay_tree_node;
552a6b7db3Sskrll 
562a6b7db3Sskrll /* The type of a function which compares two splay-tree keys.  The
572a6b7db3Sskrll    function should return values as for qsort.  */
582a6b7db3Sskrll typedef int (*splay_tree_compare_fn) (splay_tree_key, splay_tree_key);
592a6b7db3Sskrll 
602a6b7db3Sskrll /* The type of a function used to deallocate any resources associated
616f4ced0bSchristos    with the key.  If you provide this function, the splay tree
626f4ced0bSchristos    will take the ownership of the memory of the splay_tree_key arg
636f4ced0bSchristos    of splay_tree_insert.  This function is called to release the keys
646f4ced0bSchristos    present in the tree when calling splay_tree_delete or splay_tree_remove.
656f4ced0bSchristos    If splay_tree_insert is called with a key equal to a key already
666f4ced0bSchristos    present in the tree, the old key and old value will be released.  */
672a6b7db3Sskrll typedef void (*splay_tree_delete_key_fn) (splay_tree_key);
682a6b7db3Sskrll 
692a6b7db3Sskrll /* The type of a function used to deallocate any resources associated
706f4ced0bSchristos    with the value.  If you provide this function, the memory of the
716f4ced0bSchristos    splay_tree_value arg of splay_tree_insert is managed similarly to
726f4ced0bSchristos    the splay_tree_key memory: see splay_tree_delete_key_fn.  */
732a6b7db3Sskrll typedef void (*splay_tree_delete_value_fn) (splay_tree_value);
742a6b7db3Sskrll 
752a6b7db3Sskrll /* The type of a function used to iterate over the tree.  */
762a6b7db3Sskrll typedef int (*splay_tree_foreach_fn) (splay_tree_node, void*);
772a6b7db3Sskrll 
782a6b7db3Sskrll /* The type of a function used to allocate memory for tree root and
792a6b7db3Sskrll    node structures.  The first argument is the number of bytes needed;
802a6b7db3Sskrll    the second is a data pointer the splay tree functions pass through
812a6b7db3Sskrll    to the allocator.  This function must never return zero.  */
822a6b7db3Sskrll typedef void *(*splay_tree_allocate_fn) (int, void *);
832a6b7db3Sskrll 
842a6b7db3Sskrll /* The type of a function used to free memory allocated using the
852a6b7db3Sskrll    corresponding splay_tree_allocate_fn.  The first argument is the
862a6b7db3Sskrll    memory to be freed; the latter is a data pointer the splay tree
872a6b7db3Sskrll    functions pass through to the freer.  */
882a6b7db3Sskrll typedef void (*splay_tree_deallocate_fn) (void *, void *);
892a6b7db3Sskrll 
902a6b7db3Sskrll /* The nodes in the splay tree.  */
919573673dSchristos struct splay_tree_node_s {
922a6b7db3Sskrll   /* The key.  */
939573673dSchristos   splay_tree_key key;
942a6b7db3Sskrll 
952a6b7db3Sskrll   /* The value.  */
969573673dSchristos   splay_tree_value value;
972a6b7db3Sskrll 
982a6b7db3Sskrll   /* The left and right children, respectively.  */
999573673dSchristos   splay_tree_node left;
1009573673dSchristos   splay_tree_node right;
1012a6b7db3Sskrll };
1022a6b7db3Sskrll 
1032a6b7db3Sskrll /* The splay tree itself.  */
1049573673dSchristos struct splay_tree_s {
1052a6b7db3Sskrll   /* The root of the tree.  */
1069573673dSchristos   splay_tree_node root;
1072a6b7db3Sskrll 
1082a6b7db3Sskrll   /* The comparision function.  */
1092a6b7db3Sskrll   splay_tree_compare_fn comp;
1102a6b7db3Sskrll 
1112a6b7db3Sskrll   /* The deallocate-key function.  NULL if no cleanup is necessary.  */
1122a6b7db3Sskrll   splay_tree_delete_key_fn delete_key;
1132a6b7db3Sskrll 
1142a6b7db3Sskrll   /* The deallocate-value function.  NULL if no cleanup is necessary.  */
1152a6b7db3Sskrll   splay_tree_delete_value_fn delete_value;
1162a6b7db3Sskrll 
11745548106Schristos   /* Node allocate function.  Takes allocate_data as a parameter. */
1182a6b7db3Sskrll   splay_tree_allocate_fn allocate;
11945548106Schristos 
12045548106Schristos   /* Free function for nodes and trees.  Takes allocate_data as a parameter.  */
1212a6b7db3Sskrll   splay_tree_deallocate_fn deallocate;
12245548106Schristos 
12345548106Schristos   /* Parameter for allocate/free functions.  */
1249573673dSchristos   void *allocate_data;
1252a6b7db3Sskrll };
1262a6b7db3Sskrll 
1272a6b7db3Sskrll typedef struct splay_tree_s *splay_tree;
1282a6b7db3Sskrll 
1292a6b7db3Sskrll extern splay_tree splay_tree_new (splay_tree_compare_fn,
1302a6b7db3Sskrll 				  splay_tree_delete_key_fn,
1312a6b7db3Sskrll 				  splay_tree_delete_value_fn);
1322a6b7db3Sskrll extern splay_tree splay_tree_new_with_allocator (splay_tree_compare_fn,
1332a6b7db3Sskrll 						 splay_tree_delete_key_fn,
1342a6b7db3Sskrll 						 splay_tree_delete_value_fn,
1352a6b7db3Sskrll 						 splay_tree_allocate_fn,
1362a6b7db3Sskrll 						 splay_tree_deallocate_fn,
1372a6b7db3Sskrll 						 void *);
13845548106Schristos extern splay_tree splay_tree_new_typed_alloc (splay_tree_compare_fn,
13945548106Schristos 					      splay_tree_delete_key_fn,
14045548106Schristos 					      splay_tree_delete_value_fn,
14145548106Schristos 					      splay_tree_allocate_fn,
14245548106Schristos 					      splay_tree_allocate_fn,
14345548106Schristos 					      splay_tree_deallocate_fn,
14445548106Schristos 					      void *);
1452a6b7db3Sskrll extern void splay_tree_delete (splay_tree);
1462a6b7db3Sskrll extern splay_tree_node splay_tree_insert (splay_tree,
1472a6b7db3Sskrll 					  splay_tree_key,
1482a6b7db3Sskrll 					  splay_tree_value);
1492a6b7db3Sskrll extern void splay_tree_remove	(splay_tree, splay_tree_key);
1502a6b7db3Sskrll extern splay_tree_node splay_tree_lookup (splay_tree, splay_tree_key);
1512a6b7db3Sskrll extern splay_tree_node splay_tree_predecessor (splay_tree, splay_tree_key);
1522a6b7db3Sskrll extern splay_tree_node splay_tree_successor (splay_tree, splay_tree_key);
1532a6b7db3Sskrll extern splay_tree_node splay_tree_max (splay_tree);
1542a6b7db3Sskrll extern splay_tree_node splay_tree_min (splay_tree);
1552a6b7db3Sskrll extern int splay_tree_foreach (splay_tree, splay_tree_foreach_fn, void*);
1562a6b7db3Sskrll extern int splay_tree_compare_ints (splay_tree_key, splay_tree_key);
1572a6b7db3Sskrll extern int splay_tree_compare_pointers (splay_tree_key, splay_tree_key);
158c1a20988Schristos extern int splay_tree_compare_strings (splay_tree_key, splay_tree_key);
159c1a20988Schristos extern void splay_tree_delete_pointers (splay_tree_value);
1602a6b7db3Sskrll 
1612a6b7db3Sskrll #ifdef __cplusplus
1622a6b7db3Sskrll }
1632a6b7db3Sskrll #endif /* __cplusplus */
1642a6b7db3Sskrll 
1652a6b7db3Sskrll #endif /* _SPLAY_TREE_H */
166