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
);
37 typed_splay_tree (compare_fn
,
42 value_type
lookup (key_type k
);
43 value_type
predecessor (key_type k
);
44 value_type
successor (key_type k
);
45 void insert (key_type k
, value_type v
);
48 static value_type
node_to_value (splay_tree_node node
);
54 /* Constructor for typed_splay_tree <K, V>. */
56 template <typename KEY_TYPE
, typename VALUE_TYPE
>
57 inline typed_splay_tree
<KEY_TYPE
, VALUE_TYPE
>::
58 typed_splay_tree (compare_fn compare_fn
,
59 delete_key_fn delete_key_fn
,
60 delete_value_fn delete_value_fn
)
62 m_inner
= splay_tree_new ((splay_tree_compare_fn
)compare_fn
,
63 (splay_tree_delete_key_fn
)delete_key_fn
,
64 (splay_tree_delete_value_fn
)delete_value_fn
);
67 /* Destructor for typed_splay_tree <K, V>. */
69 template <typename KEY_TYPE
, typename VALUE_TYPE
>
70 inline typed_splay_tree
<KEY_TYPE
, VALUE_TYPE
>::
73 splay_tree_delete (m_inner
);
76 /* Lookup KEY, returning a value if present, and NULL
79 template <typename KEY_TYPE
, typename VALUE_TYPE
>
81 typed_splay_tree
<KEY_TYPE
, VALUE_TYPE
>::lookup (key_type key
)
83 splay_tree_node node
= splay_tree_lookup (m_inner
, (splay_tree_key
)key
);
84 return node_to_value (node
);
87 /* Return the immediate predecessor of KEY, or NULL if there is no
88 predecessor. KEY need not be present in the tree. */
90 template <typename KEY_TYPE
, typename VALUE_TYPE
>
92 typed_splay_tree
<KEY_TYPE
, VALUE_TYPE
>::predecessor (key_type key
)
94 splay_tree_node node
= splay_tree_predecessor (m_inner
, (splay_tree_key
)key
);
95 return node_to_value (node
);
98 /* Return the immediate successor of KEY, or NULL if there is no
99 successor. KEY need not be present in the tree. */
101 template <typename KEY_TYPE
, typename VALUE_TYPE
>
103 typed_splay_tree
<KEY_TYPE
, VALUE_TYPE
>::successor (key_type k
)
105 splay_tree_node node
= splay_tree_successor (m_inner
, (splay_tree_key
)k
);
106 return node_to_value (node
);
109 /* Insert a new node (associating KEY with VALUE). If a
110 previous node with the indicated KEY exists, its data is replaced
111 with the new value. */
113 template <typename KEY_TYPE
, typename VALUE_TYPE
>
115 typed_splay_tree
<KEY_TYPE
, VALUE_TYPE
>::insert (key_type key
,
118 splay_tree_insert (m_inner
,
120 (splay_tree_value
)value
);
123 /* Internal function for converting from splay_tree_node to
125 template <typename KEY_TYPE
, typename VALUE_TYPE
>
127 typed_splay_tree
<KEY_TYPE
, VALUE_TYPE
>::node_to_value (splay_tree_node node
)
130 return (value_type
)node
->value
;
135 #endif /* GCC_TYPED_SPLAY_TREE_H */