[core] splaytree: splaytree_insert_splayed()

This commit is contained in:
Glenn Strauss 2023-09-11 12:48:02 -04:00
parent 5f1b0a1ec2
commit 0523c65ad8
2 changed files with 14 additions and 13 deletions

View File

@ -107,20 +107,10 @@ splay_tree * splaytree_splay_nonnull (splay_tree *t, int i) {
return t;
}
splay_tree * splaytree_insert(splay_tree * t, int i, void *data) {
/* Insert key i into the tree t, if it is not already there. */
splay_tree * splaytree_insert_splayed(splay_tree * t, int i, void *data) {
/* Insert key i into (already) splayed tree t. */
/* Return a pointer to the resulting tree. */
splay_tree * new;
if (t != NULL) {
/*(caller likely already verified that entry does not exist,
* so skip splaytree_splay() short-circuit check)*/
t = splaytree_splay_nonnull(t, i);
if (i == t->key) {
return t; /* it's already there */
}
}
new = (splay_tree *) malloc (sizeof (splay_tree));
splay_tree * const new = (splay_tree *) malloc (sizeof (splay_tree));
assert(new);
if (t == NULL) {
new->left = new->right = NULL;
@ -138,6 +128,14 @@ splay_tree * splaytree_insert(splay_tree * t, int i, void *data) {
return new;
}
splay_tree * splaytree_insert(splay_tree * t, int i, void *data) {
/* Insert key i into the tree t, if it is not already there. */
/* Return a pointer to the resulting tree. */
return (t != NULL && (t = splaytree_splay_nonnull(t, i))->key == i)
? t
: splaytree_insert_splayed(t, i, data);
}
__attribute_noinline__
__attribute_nonnull__()
splay_tree * splaytree_delete_splayed_node(splay_tree *t) {

View File

@ -18,6 +18,9 @@ static inline splay_tree * splaytree_splay (splay_tree *t, int key) {
return splaytree_splay_nonnull(t, key);
}
__attribute_returns_nonnull__
splay_tree * splaytree_insert_splayed(splay_tree * t, int key, void *data);
__attribute_returns_nonnull__
splay_tree * splaytree_insert(splay_tree *t, int key, void *data);