annotate gcc/typed-splay-tree.h @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* A typesafe wrapper around libiberty's splay-tree.h.
kono
parents:
diff changeset
2 Copyright (C) 2015-2017 Free Software Foundation, Inc.
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of GCC.
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
7 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
8 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
9 version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
14 for more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
17 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
18 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 #ifndef GCC_TYPED_SPLAY_TREE_H
kono
parents:
diff changeset
21 #define GCC_TYPED_SPLAY_TREE_H
kono
parents:
diff changeset
22
kono
parents:
diff changeset
23 #include "splay-tree.h"
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 /* Typesafe wrapper around libiberty's splay-tree.h. */
kono
parents:
diff changeset
26 template <typename KEY_TYPE, typename VALUE_TYPE>
kono
parents:
diff changeset
27 class typed_splay_tree
kono
parents:
diff changeset
28 {
kono
parents:
diff changeset
29 public:
kono
parents:
diff changeset
30 typedef KEY_TYPE key_type;
kono
parents:
diff changeset
31 typedef VALUE_TYPE value_type;
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 typedef int (*compare_fn) (key_type, key_type);
kono
parents:
diff changeset
34 typedef void (*delete_key_fn) (key_type);
kono
parents:
diff changeset
35 typedef void (*delete_value_fn) (value_type);
kono
parents:
diff changeset
36 typedef int (*foreach_fn) (key_type, value_type, void *);
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 typed_splay_tree (compare_fn,
kono
parents:
diff changeset
39 delete_key_fn,
kono
parents:
diff changeset
40 delete_value_fn);
kono
parents:
diff changeset
41 ~typed_splay_tree ();
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 value_type lookup (key_type k);
kono
parents:
diff changeset
44 value_type predecessor (key_type k);
kono
parents:
diff changeset
45 value_type successor (key_type k);
kono
parents:
diff changeset
46 void insert (key_type k, value_type v);
kono
parents:
diff changeset
47 value_type max ();
kono
parents:
diff changeset
48 value_type min ();
kono
parents:
diff changeset
49 int foreach (foreach_fn, void *);
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 private:
kono
parents:
diff changeset
52 /* Helper type for typed_splay_tree::foreach. */
kono
parents:
diff changeset
53 struct closure
kono
parents:
diff changeset
54 {
kono
parents:
diff changeset
55 closure (foreach_fn outer_cb, void *outer_user_data)
kono
parents:
diff changeset
56 : m_outer_cb (outer_cb), m_outer_user_data (outer_user_data) {}
kono
parents:
diff changeset
57
kono
parents:
diff changeset
58 foreach_fn m_outer_cb;
kono
parents:
diff changeset
59 void *m_outer_user_data;
kono
parents:
diff changeset
60 };
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62 static int inner_foreach_fn (splay_tree_node node, void *user_data);
kono
parents:
diff changeset
63
kono
parents:
diff changeset
64 static value_type node_to_value (splay_tree_node node);
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 private:
kono
parents:
diff changeset
67 ::splay_tree m_inner;
kono
parents:
diff changeset
68 };
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 /* Constructor for typed_splay_tree <K, V>. */
kono
parents:
diff changeset
71
kono
parents:
diff changeset
72 template <typename KEY_TYPE, typename VALUE_TYPE>
kono
parents:
diff changeset
73 inline typed_splay_tree<KEY_TYPE, VALUE_TYPE>::
kono
parents:
diff changeset
74 typed_splay_tree (compare_fn compare_fn,
kono
parents:
diff changeset
75 delete_key_fn delete_key_fn,
kono
parents:
diff changeset
76 delete_value_fn delete_value_fn)
kono
parents:
diff changeset
77 {
kono
parents:
diff changeset
78 m_inner = splay_tree_new ((splay_tree_compare_fn)compare_fn,
kono
parents:
diff changeset
79 (splay_tree_delete_key_fn)delete_key_fn,
kono
parents:
diff changeset
80 (splay_tree_delete_value_fn)delete_value_fn);
kono
parents:
diff changeset
81 }
kono
parents:
diff changeset
82
kono
parents:
diff changeset
83 /* Destructor for typed_splay_tree <K, V>. */
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 template <typename KEY_TYPE, typename VALUE_TYPE>
kono
parents:
diff changeset
86 inline typed_splay_tree<KEY_TYPE, VALUE_TYPE>::
kono
parents:
diff changeset
87 ~typed_splay_tree ()
kono
parents:
diff changeset
88 {
kono
parents:
diff changeset
89 splay_tree_delete (m_inner);
kono
parents:
diff changeset
90 }
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 /* Lookup KEY, returning a value if present, and NULL
kono
parents:
diff changeset
93 otherwise. */
kono
parents:
diff changeset
94
kono
parents:
diff changeset
95 template <typename KEY_TYPE, typename VALUE_TYPE>
kono
parents:
diff changeset
96 inline VALUE_TYPE
kono
parents:
diff changeset
97 typed_splay_tree<KEY_TYPE, VALUE_TYPE>::lookup (key_type key)
kono
parents:
diff changeset
98 {
kono
parents:
diff changeset
99 splay_tree_node node = splay_tree_lookup (m_inner, (splay_tree_key)key);
kono
parents:
diff changeset
100 return node_to_value (node);
kono
parents:
diff changeset
101 }
kono
parents:
diff changeset
102
kono
parents:
diff changeset
103 /* Return the immediate predecessor of KEY, or NULL if there is no
kono
parents:
diff changeset
104 predecessor. KEY need not be present in the tree. */
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 template <typename KEY_TYPE, typename VALUE_TYPE>
kono
parents:
diff changeset
107 inline VALUE_TYPE
kono
parents:
diff changeset
108 typed_splay_tree<KEY_TYPE, VALUE_TYPE>::predecessor (key_type key)
kono
parents:
diff changeset
109 {
kono
parents:
diff changeset
110 splay_tree_node node = splay_tree_predecessor (m_inner, (splay_tree_key)key);
kono
parents:
diff changeset
111 return node_to_value (node);
kono
parents:
diff changeset
112 }
kono
parents:
diff changeset
113
kono
parents:
diff changeset
114 /* Return the immediate successor of KEY, or NULL if there is no
kono
parents:
diff changeset
115 successor. KEY need not be present in the tree. */
kono
parents:
diff changeset
116
kono
parents:
diff changeset
117 template <typename KEY_TYPE, typename VALUE_TYPE>
kono
parents:
diff changeset
118 inline VALUE_TYPE
kono
parents:
diff changeset
119 typed_splay_tree<KEY_TYPE, VALUE_TYPE>::successor (key_type k)
kono
parents:
diff changeset
120 {
kono
parents:
diff changeset
121 splay_tree_node node = splay_tree_successor (m_inner, (splay_tree_key)k);
kono
parents:
diff changeset
122 return node_to_value (node);
kono
parents:
diff changeset
123 }
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 /* Insert a new node (associating KEY with VALUE). If a
kono
parents:
diff changeset
126 previous node with the indicated KEY exists, its data is replaced
kono
parents:
diff changeset
127 with the new value. */
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 template <typename KEY_TYPE, typename VALUE_TYPE>
kono
parents:
diff changeset
130 inline void
kono
parents:
diff changeset
131 typed_splay_tree<KEY_TYPE, VALUE_TYPE>::insert (key_type key,
kono
parents:
diff changeset
132 value_type value)
kono
parents:
diff changeset
133 {
kono
parents:
diff changeset
134 splay_tree_insert (m_inner,
kono
parents:
diff changeset
135 (splay_tree_key)key,
kono
parents:
diff changeset
136 (splay_tree_value)value);
kono
parents:
diff changeset
137 }
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 /* Get the value with maximal key. */
kono
parents:
diff changeset
140
kono
parents:
diff changeset
141 template <typename KEY_TYPE, typename VALUE_TYPE>
kono
parents:
diff changeset
142 inline VALUE_TYPE
kono
parents:
diff changeset
143 typed_splay_tree<KEY_TYPE, VALUE_TYPE>::max ()
kono
parents:
diff changeset
144 {
kono
parents:
diff changeset
145 return node_to_value (splay_tree_max (m_inner));
kono
parents:
diff changeset
146 }
kono
parents:
diff changeset
147
kono
parents:
diff changeset
148 /* Get the value with minimal key. */
kono
parents:
diff changeset
149
kono
parents:
diff changeset
150 template <typename KEY_TYPE, typename VALUE_TYPE>
kono
parents:
diff changeset
151 inline VALUE_TYPE
kono
parents:
diff changeset
152 typed_splay_tree<KEY_TYPE, VALUE_TYPE>::min ()
kono
parents:
diff changeset
153 {
kono
parents:
diff changeset
154 return node_to_value (splay_tree_min (m_inner));
kono
parents:
diff changeset
155 }
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 /* Call OUTER_CB, passing it the OUTER_USER_DATA, for every node,
kono
parents:
diff changeset
158 following an in-order traversal. If OUTER_CB ever returns a non-zero
kono
parents:
diff changeset
159 value, the iteration ceases immediately, and the value is returned.
kono
parents:
diff changeset
160 Otherwise, this function returns 0. */
kono
parents:
diff changeset
161
kono
parents:
diff changeset
162 template <typename KEY_TYPE, typename VALUE_TYPE>
kono
parents:
diff changeset
163 inline int
kono
parents:
diff changeset
164 typed_splay_tree<KEY_TYPE, VALUE_TYPE>::foreach (foreach_fn outer_cb,
kono
parents:
diff changeset
165 void *outer_user_data)
kono
parents:
diff changeset
166 {
kono
parents:
diff changeset
167 closure c (outer_cb, outer_user_data);
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 return splay_tree_foreach (m_inner, inner_foreach_fn, &c);
kono
parents:
diff changeset
170 }
kono
parents:
diff changeset
171
kono
parents:
diff changeset
172 /* Helper function for typed_splay_tree::foreach. */
kono
parents:
diff changeset
173
kono
parents:
diff changeset
174 template <typename KEY_TYPE, typename VALUE_TYPE>
kono
parents:
diff changeset
175 int
kono
parents:
diff changeset
176 typed_splay_tree<KEY_TYPE, VALUE_TYPE>::inner_foreach_fn (splay_tree_node node,
kono
parents:
diff changeset
177 void *user_data)
kono
parents:
diff changeset
178 {
kono
parents:
diff changeset
179 closure *c = (closure *)user_data;
kono
parents:
diff changeset
180
kono
parents:
diff changeset
181 return c->m_outer_cb ((KEY_TYPE)node->key, (VALUE_TYPE)node->value,
kono
parents:
diff changeset
182 c->m_outer_user_data);
kono
parents:
diff changeset
183 }
kono
parents:
diff changeset
184
kono
parents:
diff changeset
185 /* Internal function for converting from splay_tree_node to
kono
parents:
diff changeset
186 VALUE_TYPE. */
kono
parents:
diff changeset
187 template <typename KEY_TYPE, typename VALUE_TYPE>
kono
parents:
diff changeset
188 inline VALUE_TYPE
kono
parents:
diff changeset
189 typed_splay_tree<KEY_TYPE, VALUE_TYPE>::node_to_value (splay_tree_node node)
kono
parents:
diff changeset
190 {
kono
parents:
diff changeset
191 if (node)
kono
parents:
diff changeset
192 return (value_type)node->value;
kono
parents:
diff changeset
193 else
kono
parents:
diff changeset
194 return 0;
kono
parents:
diff changeset
195 }
kono
parents:
diff changeset
196
kono
parents:
diff changeset
197 #endif /* GCC_TYPED_SPLAY_TREE_H */