1 /* A typesafe wrapper around libiberty's splay-tree.h.
2 Copyright (C) 2015-2016 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
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
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
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
,
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
);
49 int foreach (foreach_fn
, void *);
52 /* Helper type for typed_splay_tree::foreach. */
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
);
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
)compare_fn
,
79 (splay_tree_delete_key_fn
)delete_key_fn
,
80 (splay_tree_delete_value_fn
)delete_value_fn
);
83 /* Destructor for typed_splay_tree <K, V>. */
85 template <typename KEY_TYPE
, typename VALUE_TYPE
>
86 inline typed_splay_tree
<KEY_TYPE
, VALUE_TYPE
>::
89 splay_tree_delete (m_inner
);
92 /* Lookup KEY, returning a value if present, and NULL
95 template <typename KEY_TYPE
, typename VALUE_TYPE
>
97 typed_splay_tree
<KEY_TYPE
, VALUE_TYPE
>::lookup (key_type key
)
99 splay_tree_node node
= splay_tree_lookup (m_inner
, (splay_tree_key
)key
);
100 return node_to_value (node
);
103 /* Return the immediate predecessor of KEY, or NULL if there is no
104 predecessor. KEY need not be present in the tree. */
106 template <typename KEY_TYPE
, typename VALUE_TYPE
>
108 typed_splay_tree
<KEY_TYPE
, VALUE_TYPE
>::predecessor (key_type key
)
110 splay_tree_node node
= splay_tree_predecessor (m_inner
, (splay_tree_key
)key
);
111 return node_to_value (node
);
114 /* Return the immediate successor of KEY, or NULL if there is no
115 successor. KEY need not be present in the tree. */
117 template <typename KEY_TYPE
, typename VALUE_TYPE
>
119 typed_splay_tree
<KEY_TYPE
, VALUE_TYPE
>::successor (key_type k
)
121 splay_tree_node node
= splay_tree_successor (m_inner
, (splay_tree_key
)k
);
122 return node_to_value (node
);
125 /* Insert a new node (associating KEY with VALUE). If a
126 previous node with the indicated KEY exists, its data is replaced
127 with the new value. */
129 template <typename KEY_TYPE
, typename VALUE_TYPE
>
131 typed_splay_tree
<KEY_TYPE
, VALUE_TYPE
>::insert (key_type key
,
134 splay_tree_insert (m_inner
,
136 (splay_tree_value
)value
);
139 /* Get the value with maximal key. */
141 template <typename KEY_TYPE
, typename VALUE_TYPE
>
143 typed_splay_tree
<KEY_TYPE
, VALUE_TYPE
>::max ()
145 return node_to_value (splay_tree_max (m_inner
));
148 /* Get the value with minimal key. */
150 template <typename KEY_TYPE
, typename VALUE_TYPE
>
152 typed_splay_tree
<KEY_TYPE
, VALUE_TYPE
>::min ()
154 return node_to_value (splay_tree_min (m_inner
));
157 /* Call OUTER_CB, passing it the OUTER_USER_DATA, for every node,
158 following an in-order traversal. If OUTER_CB ever returns a non-zero
159 value, the iteration ceases immediately, and the value is returned.
160 Otherwise, this function returns 0. */
162 template <typename KEY_TYPE
, typename VALUE_TYPE
>
164 typed_splay_tree
<KEY_TYPE
, VALUE_TYPE
>::foreach (foreach_fn outer_cb
,
165 void *outer_user_data
)
167 closure
c (outer_cb
, outer_user_data
);
169 return splay_tree_foreach (m_inner
, inner_foreach_fn
, &c
);
172 /* Helper function for typed_splay_tree::foreach. */
174 template <typename KEY_TYPE
, typename VALUE_TYPE
>
176 typed_splay_tree
<KEY_TYPE
, VALUE_TYPE
>::inner_foreach_fn (splay_tree_node node
,
179 closure
*c
= (closure
*)user_data
;
181 return c
->m_outer_cb ((KEY_TYPE
)node
->key
, (VALUE_TYPE
)node
->value
,
182 c
->m_outer_user_data
);
185 /* Internal function for converting from splay_tree_node to
187 template <typename KEY_TYPE
, typename VALUE_TYPE
>
189 typed_splay_tree
<KEY_TYPE
, VALUE_TYPE
>::node_to_value (splay_tree_node node
)
192 return (value_type
)node
->value
;
197 #endif /* GCC_TYPED_SPLAY_TREE_H */