1 /* Balanced binary trees using treaps.
2 Copyright (C) 2000-2023 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* The idea is to balance the tree using pseudorandom numbers. The
22 main constraint on this implementation is that we have several
23 distinct structures that have to be arranged in a binary tree.
24 These structures all contain a BBT_HEADER() in front that gives the
25 treap-related information. The key and value are assumed to reside
26 in the rest of the structure.
28 When calling, we are also passed a comparison function that
29 compares two nodes. We don't implement a separate 'find' function
30 here, but rather use separate functions for each variety of tree.
31 We are also restricted to not copy treap structures, which most
32 implementations find convenient, because we otherwise would need to
33 know how long the structure is.
35 This implementation is based on Stefan Nilsson's article in the
36 July 1997 Doctor Dobb's Journal, "Treaps in Java". */
40 #include "coretypes.h"
43 typedef struct gfc_treap
45 BBT_HEADER (gfc_treap
);
49 /* Simple linear congruential pseudorandom number generator. The
50 period of this generator is 44071, which is plenty for our
58 x0
= (22611 * x0
+ 10) % 44071;
63 /* Rotate the treap left. */
66 rotate_left (gfc_bbt
*t
)
71 t
->right
= t
->right
->left
;
78 /* Rotate the treap right. */
81 rotate_right (gfc_bbt
*t
)
86 t
->left
= t
->left
->right
;
93 /* Recursive insertion function. Returns the updated treap, or
94 aborts if we find a duplicate key. */
97 insert (gfc_bbt
*new_bbt
, gfc_bbt
*t
, compare_fn compare
)
104 c
= (*compare
) (new_bbt
, t
);
108 t
->left
= insert (new_bbt
, t
->left
, compare
);
109 if (t
->priority
< t
->left
->priority
)
110 t
= rotate_right (t
);
114 t
->right
= insert (new_bbt
, t
->right
, compare
);
115 if (t
->priority
< t
->right
->priority
)
118 else /* if (c == 0) */
119 gfc_internal_error("insert_bbt(): Duplicate key found");
125 /* Given root pointer, a new node and a comparison function, insert
126 the new node into the treap. It is an error to insert a key that
130 gfc_insert_bbt (void *root
, void *new_node
, compare_fn compare
)
134 r
= (gfc_bbt
**) root
;
135 n
= (gfc_bbt
*) new_node
;
136 n
->priority
= pseudo_random ();
137 *r
= insert (n
, *r
, compare
);
141 delete_root (gfc_bbt
*t
)
147 if (t
->right
== NULL
)
150 if (t
->left
->priority
> t
->right
->priority
)
152 temp
= rotate_right (t
);
153 temp
->right
= delete_root (t
);
157 temp
= rotate_left (t
);
158 temp
->left
= delete_root (t
);
165 /* Delete an element from a tree, returning the new root node of the tree.
166 The OLD value does not necessarily have to point to the element to be
167 deleted, it must just point to a treap structure with the key to be deleted.
168 The REMOVED argument, if non-null, is set to the removed element from the
172 delete_treap (gfc_bbt
*old
, gfc_bbt
*t
, compare_fn compare
, gfc_bbt
**removed
)
183 c
= (*compare
) (old
, t
);
186 t
->left
= delete_treap (old
, t
->left
, compare
, removed
);
188 t
->right
= delete_treap (old
, t
->right
, compare
, removed
);
200 /* Delete the element from the tree at *ROOT that matches the OLD element
201 according to the COMPARE_FN function. This updates the *ROOT pointer to
202 point to the new tree root (if different from the original) and returns the
206 gfc_delete_bbt (void *root
, void *old
, compare_fn compare
)
211 t
= (gfc_bbt
**) root
;
212 *t
= delete_treap ((gfc_bbt
*) old
, *t
, compare
, &removed
);
214 return (void *) removed
;