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