annotate gcc/fortran/bbt.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Balanced binary trees using treaps.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 2000-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3 Contributed by Andy Vaught
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 This file is part of GCC.
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
8 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
9 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
10 version.
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
15 for more details.
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
18 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
19 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 /* The idea is to balance the tree using pseudorandom numbers. The
kono
parents:
diff changeset
22 main constraint on this implementation is that we have several
kono
parents:
diff changeset
23 distinct structures that have to be arranged in a binary tree.
kono
parents:
diff changeset
24 These structures all contain a BBT_HEADER() in front that gives the
kono
parents:
diff changeset
25 treap-related information. The key and value are assumed to reside
kono
parents:
diff changeset
26 in the rest of the structure.
kono
parents:
diff changeset
27
kono
parents:
diff changeset
28 When calling, we are also passed a comparison function that
kono
parents:
diff changeset
29 compares two nodes. We don't implement a separate 'find' function
kono
parents:
diff changeset
30 here, but rather use separate functions for each variety of tree.
kono
parents:
diff changeset
31 We are also restricted to not copy treap structures, which most
kono
parents:
diff changeset
32 implementations find convenient, because we otherwise would need to
kono
parents:
diff changeset
33 know how long the structure is.
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 This implementation is based on Stefan Nilsson's article in the
kono
parents:
diff changeset
36 July 1997 Doctor Dobb's Journal, "Treaps in Java". */
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 #include "config.h"
kono
parents:
diff changeset
39 #include "system.h"
kono
parents:
diff changeset
40 #include "coretypes.h"
kono
parents:
diff changeset
41 #include "gfortran.h"
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 typedef struct gfc_treap
kono
parents:
diff changeset
44 {
kono
parents:
diff changeset
45 BBT_HEADER (gfc_treap);
kono
parents:
diff changeset
46 }
kono
parents:
diff changeset
47 gfc_bbt;
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 /* Simple linear congruential pseudorandom number generator. The
kono
parents:
diff changeset
50 period of this generator is 44071, which is plenty for our
kono
parents:
diff changeset
51 purposes. */
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 static int
kono
parents:
diff changeset
54 pseudo_random (void)
kono
parents:
diff changeset
55 {
kono
parents:
diff changeset
56 static int x0 = 5341;
kono
parents:
diff changeset
57
kono
parents:
diff changeset
58 x0 = (22611 * x0 + 10) % 44071;
kono
parents:
diff changeset
59 return x0;
kono
parents:
diff changeset
60 }
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 /* Rotate the treap left. */
kono
parents:
diff changeset
64
kono
parents:
diff changeset
65 static gfc_bbt *
kono
parents:
diff changeset
66 rotate_left (gfc_bbt *t)
kono
parents:
diff changeset
67 {
kono
parents:
diff changeset
68 gfc_bbt *temp;
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 temp = t->right;
kono
parents:
diff changeset
71 t->right = t->right->left;
kono
parents:
diff changeset
72 temp->left = t;
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 return temp;
kono
parents:
diff changeset
75 }
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 /* Rotate the treap right. */
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 static gfc_bbt *
kono
parents:
diff changeset
81 rotate_right (gfc_bbt *t)
kono
parents:
diff changeset
82 {
kono
parents:
diff changeset
83 gfc_bbt *temp;
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 temp = t->left;
kono
parents:
diff changeset
86 t->left = t->left->right;
kono
parents:
diff changeset
87 temp->right = t;
kono
parents:
diff changeset
88
kono
parents:
diff changeset
89 return temp;
kono
parents:
diff changeset
90 }
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 /* Recursive insertion function. Returns the updated treap, or
kono
parents:
diff changeset
94 aborts if we find a duplicate key. */
kono
parents:
diff changeset
95
kono
parents:
diff changeset
96 static gfc_bbt *
kono
parents:
diff changeset
97 insert (gfc_bbt *new_bbt, gfc_bbt *t, compare_fn compare)
kono
parents:
diff changeset
98 {
kono
parents:
diff changeset
99 int c;
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 if (t == NULL)
kono
parents:
diff changeset
102 return new_bbt;
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 c = (*compare) (new_bbt, t);
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 if (c < 0)
kono
parents:
diff changeset
107 {
kono
parents:
diff changeset
108 t->left = insert (new_bbt, t->left, compare);
kono
parents:
diff changeset
109 if (t->priority < t->left->priority)
kono
parents:
diff changeset
110 t = rotate_right (t);
kono
parents:
diff changeset
111 }
kono
parents:
diff changeset
112 else if (c > 0)
kono
parents:
diff changeset
113 {
kono
parents:
diff changeset
114 t->right = insert (new_bbt, t->right, compare);
kono
parents:
diff changeset
115 if (t->priority < t->right->priority)
kono
parents:
diff changeset
116 t = rotate_left (t);
kono
parents:
diff changeset
117 }
kono
parents:
diff changeset
118 else /* if (c == 0) */
kono
parents:
diff changeset
119 gfc_internal_error("insert_bbt(): Duplicate key found");
kono
parents:
diff changeset
120
kono
parents:
diff changeset
121 return t;
kono
parents:
diff changeset
122 }
kono
parents:
diff changeset
123
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 /* Given root pointer, a new node and a comparison function, insert
kono
parents:
diff changeset
126 the new node into the treap. It is an error to insert a key that
kono
parents:
diff changeset
127 already exists. */
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 void
kono
parents:
diff changeset
130 gfc_insert_bbt (void *root, void *new_node, compare_fn compare)
kono
parents:
diff changeset
131 {
kono
parents:
diff changeset
132 gfc_bbt **r, *n;
kono
parents:
diff changeset
133
kono
parents:
diff changeset
134 r = (gfc_bbt **) root;
kono
parents:
diff changeset
135 n = (gfc_bbt *) new_node;
kono
parents:
diff changeset
136 n->priority = pseudo_random ();
kono
parents:
diff changeset
137 *r = insert (n, *r, compare);
kono
parents:
diff changeset
138 }
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 static gfc_bbt *
kono
parents:
diff changeset
141 delete_root (gfc_bbt *t)
kono
parents:
diff changeset
142 {
kono
parents:
diff changeset
143 gfc_bbt *temp;
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 if (t->left == NULL)
kono
parents:
diff changeset
146 return t->right;
kono
parents:
diff changeset
147 if (t->right == NULL)
kono
parents:
diff changeset
148 return t->left;
kono
parents:
diff changeset
149
kono
parents:
diff changeset
150 if (t->left->priority > t->right->priority)
kono
parents:
diff changeset
151 {
kono
parents:
diff changeset
152 temp = rotate_right (t);
kono
parents:
diff changeset
153 temp->right = delete_root (t);
kono
parents:
diff changeset
154 }
kono
parents:
diff changeset
155 else
kono
parents:
diff changeset
156 {
kono
parents:
diff changeset
157 temp = rotate_left (t);
kono
parents:
diff changeset
158 temp->left = delete_root (t);
kono
parents:
diff changeset
159 }
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 return temp;
kono
parents:
diff changeset
162 }
kono
parents:
diff changeset
163
kono
parents:
diff changeset
164
kono
parents:
diff changeset
165 /* Delete an element from a tree. The 'old' value does not
kono
parents:
diff changeset
166 necessarily have to point to the element to be deleted, it must
kono
parents:
diff changeset
167 just point to a treap structure with the key to be deleted.
kono
parents:
diff changeset
168 Returns the new root node of the tree. */
kono
parents:
diff changeset
169
kono
parents:
diff changeset
170 static gfc_bbt *
kono
parents:
diff changeset
171 delete_treap (gfc_bbt *old, gfc_bbt *t, compare_fn compare)
kono
parents:
diff changeset
172 {
kono
parents:
diff changeset
173 int c;
kono
parents:
diff changeset
174
kono
parents:
diff changeset
175 if (t == NULL)
kono
parents:
diff changeset
176 return NULL;
kono
parents:
diff changeset
177
kono
parents:
diff changeset
178 c = (*compare) (old, t);
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180 if (c < 0)
kono
parents:
diff changeset
181 t->left = delete_treap (old, t->left, compare);
kono
parents:
diff changeset
182 if (c > 0)
kono
parents:
diff changeset
183 t->right = delete_treap (old, t->right, compare);
kono
parents:
diff changeset
184 if (c == 0)
kono
parents:
diff changeset
185 t = delete_root (t);
kono
parents:
diff changeset
186
kono
parents:
diff changeset
187 return t;
kono
parents:
diff changeset
188 }
kono
parents:
diff changeset
189
kono
parents:
diff changeset
190
kono
parents:
diff changeset
191 void
kono
parents:
diff changeset
192 gfc_delete_bbt (void *root, void *old, compare_fn compare)
kono
parents:
diff changeset
193 {
kono
parents:
diff changeset
194 gfc_bbt **t;
kono
parents:
diff changeset
195
kono
parents:
diff changeset
196 t = (gfc_bbt **) root;
kono
parents:
diff changeset
197 *t = delete_treap ((gfc_bbt *) old, *t, compare);
kono
parents:
diff changeset
198 }