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