* c-ada-spec.c (dump_number): Add FLOAT_P parameter.
[official-gcc.git] / gcc / typed-splay-tree.h
blob95c2f5f39b33bb461169c94a5210d3eef0eb605e
1 /* A typesafe wrapper around libiberty's splay-tree.h.
2 Copyright (C) 2015-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #ifndef GCC_TYPED_SPLAY_TREE_H
21 #define GCC_TYPED_SPLAY_TREE_H
23 #include "splay-tree.h"
25 /* Typesafe wrapper around libiberty's splay-tree.h. */
26 template <typename KEY_TYPE, typename VALUE_TYPE>
27 class typed_splay_tree
29 public:
30 typedef KEY_TYPE key_type;
31 typedef VALUE_TYPE value_type;
33 typedef int (*compare_fn) (key_type, key_type);
34 typedef void (*delete_key_fn) (key_type);
35 typedef void (*delete_value_fn) (value_type);
36 typedef int (*foreach_fn) (key_type, value_type, void *);
38 typed_splay_tree (compare_fn,
39 delete_key_fn,
40 delete_value_fn);
41 ~typed_splay_tree ();
43 value_type lookup (key_type k);
44 value_type predecessor (key_type k);
45 value_type successor (key_type k);
46 void insert (key_type k, value_type v);
47 value_type max ();
48 value_type min ();
49 int foreach (foreach_fn, void *);
51 private:
52 /* Helper type for typed_splay_tree::foreach. */
53 struct closure
55 closure (foreach_fn outer_cb, void *outer_user_data)
56 : m_outer_cb (outer_cb), m_outer_user_data (outer_user_data) {}
58 foreach_fn m_outer_cb;
59 void *m_outer_user_data;
62 static int inner_foreach_fn (splay_tree_node node, void *user_data);
64 static value_type node_to_value (splay_tree_node node);
66 private:
67 ::splay_tree m_inner;
70 /* Constructor for typed_splay_tree <K, V>. */
72 template <typename KEY_TYPE, typename VALUE_TYPE>
73 inline typed_splay_tree<KEY_TYPE, VALUE_TYPE>::
74 typed_splay_tree (compare_fn compare_fn,
75 delete_key_fn delete_key_fn,
76 delete_value_fn delete_value_fn)
78 m_inner = splay_tree_new ((splay_tree_compare_fn)
79 (void (*) (void)) compare_fn,
80 (splay_tree_delete_key_fn)
81 (void (*) (void)) delete_key_fn,
82 (splay_tree_delete_value_fn)
83 (void (*) (void)) delete_value_fn);
86 /* Destructor for typed_splay_tree <K, V>. */
88 template <typename KEY_TYPE, typename VALUE_TYPE>
89 inline typed_splay_tree<KEY_TYPE, VALUE_TYPE>::
90 ~typed_splay_tree ()
92 splay_tree_delete (m_inner);
95 /* Lookup KEY, returning a value if present, and NULL
96 otherwise. */
98 template <typename KEY_TYPE, typename VALUE_TYPE>
99 inline VALUE_TYPE
100 typed_splay_tree<KEY_TYPE, VALUE_TYPE>::lookup (key_type key)
102 splay_tree_node node = splay_tree_lookup (m_inner, (splay_tree_key)key);
103 return node_to_value (node);
106 /* Return the immediate predecessor of KEY, or NULL if there is no
107 predecessor. KEY need not be present in the tree. */
109 template <typename KEY_TYPE, typename VALUE_TYPE>
110 inline VALUE_TYPE
111 typed_splay_tree<KEY_TYPE, VALUE_TYPE>::predecessor (key_type key)
113 splay_tree_node node = splay_tree_predecessor (m_inner, (splay_tree_key)key);
114 return node_to_value (node);
117 /* Return the immediate successor of KEY, or NULL if there is no
118 successor. KEY need not be present in the tree. */
120 template <typename KEY_TYPE, typename VALUE_TYPE>
121 inline VALUE_TYPE
122 typed_splay_tree<KEY_TYPE, VALUE_TYPE>::successor (key_type k)
124 splay_tree_node node = splay_tree_successor (m_inner, (splay_tree_key)k);
125 return node_to_value (node);
128 /* Insert a new node (associating KEY with VALUE). If a
129 previous node with the indicated KEY exists, its data is replaced
130 with the new value. */
132 template <typename KEY_TYPE, typename VALUE_TYPE>
133 inline void
134 typed_splay_tree<KEY_TYPE, VALUE_TYPE>::insert (key_type key,
135 value_type value)
137 splay_tree_insert (m_inner,
138 (splay_tree_key)key,
139 (splay_tree_value)value);
142 /* Get the value with maximal key. */
144 template <typename KEY_TYPE, typename VALUE_TYPE>
145 inline VALUE_TYPE
146 typed_splay_tree<KEY_TYPE, VALUE_TYPE>::max ()
148 return node_to_value (splay_tree_max (m_inner));
151 /* Get the value with minimal key. */
153 template <typename KEY_TYPE, typename VALUE_TYPE>
154 inline VALUE_TYPE
155 typed_splay_tree<KEY_TYPE, VALUE_TYPE>::min ()
157 return node_to_value (splay_tree_min (m_inner));
160 /* Call OUTER_CB, passing it the OUTER_USER_DATA, for every node,
161 following an in-order traversal. If OUTER_CB ever returns a non-zero
162 value, the iteration ceases immediately, and the value is returned.
163 Otherwise, this function returns 0. */
165 template <typename KEY_TYPE, typename VALUE_TYPE>
166 inline int
167 typed_splay_tree<KEY_TYPE, VALUE_TYPE>::foreach (foreach_fn outer_cb,
168 void *outer_user_data)
170 closure c (outer_cb, outer_user_data);
172 return splay_tree_foreach (m_inner, inner_foreach_fn, &c);
175 /* Helper function for typed_splay_tree::foreach. */
177 template <typename KEY_TYPE, typename VALUE_TYPE>
179 typed_splay_tree<KEY_TYPE, VALUE_TYPE>::inner_foreach_fn (splay_tree_node node,
180 void *user_data)
182 closure *c = (closure *)user_data;
184 return c->m_outer_cb ((KEY_TYPE)node->key, (VALUE_TYPE)node->value,
185 c->m_outer_user_data);
188 /* Internal function for converting from splay_tree_node to
189 VALUE_TYPE. */
190 template <typename KEY_TYPE, typename VALUE_TYPE>
191 inline VALUE_TYPE
192 typed_splay_tree<KEY_TYPE, VALUE_TYPE>::node_to_value (splay_tree_node node)
194 if (node)
195 return (value_type)node->value;
196 else
197 return 0;
200 #endif /* GCC_TYPED_SPLAY_TREE_H */