OSDN Git Service

* splay-tree.h: New file.
[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 #include "gansidecl.h"
38 #define PARAMS(ARGS) PROTO(ARGS)
39 #else /* ! IN_GCC */
40 #include <ansidecl.h>
41 #endif /* IN_GCC */
42
43 /* Use typedefs for the key and data types to facilitate changing
44    these types, if necessary.  These types should be sufficiently wide
45    that any pointer or scalar can be cast to these types, and then
46    cast back, without loss of precision.  */
47 typedef unsigned long int splay_tree_key;
48 typedef unsigned long int splay_tree_value;
49
50 /* Forward declaration for a node in the tree.  */
51 typedef struct splay_tree_node *splay_tree_node;
52
53 /* The type of a function which compares two splay-tree keys.  The
54    function should return values as for qsort.  */
55 typedef int (*splay_tree_compare_fn)(splay_tree_key, splay_tree_key);
56
57 /* The type of a function used to deallocate any resources associated
58    with the key.  */
59 typedef void (*splay_tree_delete_key_fn)(splay_tree_key);
60
61 /* The type of a function used to deallocate any resources associated
62    with the value.  */
63 typedef void (*splay_tree_delete_value_fn)(splay_tree_value);
64
65 /* The type of a function used to iterate over the tree.  */
66 typedef int (*splay_tree_foreach_fn)(splay_tree_node, void*);
67
68 /* The nodes in the splay tree.  */
69 struct splay_tree_node
70 {
71   /* The key.  */
72   splay_tree_key key;
73
74   /* The value.  */
75   splay_tree_value value;
76
77   /* The left and right children, respectively.  */
78   splay_tree_node left;
79   splay_tree_node right;
80 };
81
82 /* The splay tree itself.  */
83 typedef struct splay_tree
84 {
85   /* The root of the tree.  */
86   splay_tree_node root;
87
88   /* The comparision function.  */
89   splay_tree_compare_fn comp;
90
91   /* The deallocate-key function.  NULL if no cleanup is necessary.  */
92   splay_tree_delete_key_fn delete_key;
93
94   /* The deallocate-value function.  NULL if no cleanup is necessary.  */
95   splay_tree_delete_value_fn delete_value;
96 } *splay_tree;
97
98 extern splay_tree splay_tree_new        PARAMS((splay_tree_compare_fn,
99                                                 splay_tree_delete_key_fn,
100                                                 splay_tree_delete_value_fn));
101 extern void splay_tree_delete           PARAMS((splay_tree));
102 extern void splay_tree_insert           PARAMS((splay_tree,
103                                                 splay_tree_key,
104                                                 splay_tree_value));
105 extern splay_tree_node splay_tree_lookup   
106                                         PARAMS((splay_tree,
107                                                 splay_tree_key));
108 extern int splay_tree_foreach           PARAMS((splay_tree,
109                                                 splay_tree_foreach_fn,
110                                                 void*));
111                                                
112 #ifdef __cplusplus
113 }
114 #endif /* __cplusplus */
115
116 #endif /* _SPLAY_TREE_H */