OSDN Git Service

9389ddafe1b72212acbe808757be321e7065c321
[pf3gnuchains/gcc-fork.git] / include / splay-tree.h
1 /* A splay-tree datatype.  
2    Copyright (C) 1998 Free Software Foundation, Inc.
3    Contributed by Mark Mitchell (mark@markmitchell.com).
4
5    This file is part of GNU CC.
6    
7    GNU CC is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GNU CC is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GNU CC; see the file COPYING.  If not, write to the Free
19    Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
20
21    For an easily readable description of splay-trees, see:
22
23      Lewis, Harry R. and Denenberg, Larry.  Data Structures and Their
24      Algorithms.  Harper-Collins, Inc.  1991.  
25
26    The major feature of splay trees is that all basic tree operations
27    are amortized O(log n) time for a tree with n nodes.  */
28
29 #ifndef _SPLAY_TREE_H
30 #define _SPLAY_TREE_H
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif /* __cplusplus */
35
36 #ifdef IN_GCC
37 #define PARAMS(ARGS) PROTO(ARGS)
38 #else /* ! IN_GCC */
39 #include <ansidecl.h>
40 #endif /* IN_GCC */
41
42 /* Use typedefs for the key and data types to facilitate changing
43    these types, if necessary.  These types should be sufficiently wide
44    that any pointer or scalar can be cast to these types, and then
45    cast back, without loss of precision.  */
46 typedef unsigned long int splay_tree_key;
47 typedef unsigned long int splay_tree_value;
48
49 /* Forward declaration for a node in the tree.  */
50 typedef struct splay_tree_node *splay_tree_node;
51
52 /* The type of a function which compares two splay-tree keys.  The
53    function should return values as for qsort.  */
54 typedef int (*splay_tree_compare_fn) PARAMS((splay_tree_key, splay_tree_key));
55
56 /* The type of a function used to deallocate any resources associated
57    with the key.  */
58 typedef void (*splay_tree_delete_key_fn) PARAMS((splay_tree_key));
59
60 /* The type of a function used to deallocate any resources associated
61    with the value.  */
62 typedef void (*splay_tree_delete_value_fn) PARAMS((splay_tree_value));
63
64 /* The type of a function used to iterate over the tree.  */
65 typedef int (*splay_tree_foreach_fn) PARAMS((splay_tree_node, void*));
66
67 /* The nodes in the splay tree.  */
68 struct splay_tree_node
69 {
70   /* The key.  */
71   splay_tree_key key;
72
73   /* The value.  */
74   splay_tree_value value;
75
76   /* The left and right children, respectively.  */
77   splay_tree_node left;
78   splay_tree_node right;
79 };
80
81 /* The splay tree itself.  */
82 typedef struct splay_tree
83 {
84   /* The root of the tree.  */
85   splay_tree_node root;
86
87   /* The comparision function.  */
88   splay_tree_compare_fn comp;
89
90   /* The deallocate-key function.  NULL if no cleanup is necessary.  */
91   splay_tree_delete_key_fn delete_key;
92
93   /* The deallocate-value function.  NULL if no cleanup is necessary.  */
94   splay_tree_delete_value_fn delete_value;
95 } *splay_tree;
96
97 extern splay_tree splay_tree_new        PARAMS((splay_tree_compare_fn,
98                                                 splay_tree_delete_key_fn,
99                                                 splay_tree_delete_value_fn));
100 extern void splay_tree_delete           PARAMS((splay_tree));
101 extern void splay_tree_insert           PARAMS((splay_tree,
102                                                 splay_tree_key,
103                                                 splay_tree_value));
104 extern splay_tree_node splay_tree_lookup   
105                                         PARAMS((splay_tree,
106                                                 splay_tree_key));
107 extern int splay_tree_foreach           PARAMS((splay_tree,
108                                                 splay_tree_foreach_fn,
109                                                 void*));
110                                                
111 #ifdef __cplusplus
112 }
113 #endif /* __cplusplus */
114
115 #endif /* _SPLAY_TREE_H */