]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/config/rs6000/rbtree.cc
1 /* Partial red-black tree implementation for rs6000-gen-builtins.cc.
2 Copyright (C) 2020-2023 Free Software Foundation, Inc.
3 Contributed by Bill Schmidt, IBM <wschmidt@linux.ibm.com>
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/>. */
27 /* Initialize a red-black tree. */
29 rbt_new (struct rbt_strings
*t
)
31 t
->rbt_nil
= (rbt_string_node
*) malloc (sizeof (rbt_string_node
));
32 t
->rbt_nil
->color
= RBT_BLACK
;
33 t
->rbt_root
= t
->rbt_nil
;
36 /* Create a new node to be inserted into the red-black tree. An inserted
37 node starts out red. */
38 static struct rbt_string_node
*
39 rbt_create_node (struct rbt_strings
*t
, char *str
)
41 struct rbt_string_node
*nodeptr
42 = (struct rbt_string_node
*) malloc (sizeof (rbt_string_node
));
44 nodeptr
->left
= t
->rbt_nil
;
45 nodeptr
->right
= t
->rbt_nil
;
47 nodeptr
->color
= RBT_RED
;
51 /* Perform a left-rotate operation on NODE in the red-black tree. */
53 rbt_left_rotate (struct rbt_strings
*t
, struct rbt_string_node
*node
)
55 struct rbt_string_node
*right
= node
->right
;
58 /* Turn RIGHT's left subtree into NODE's right subtree. */
59 node
->right
= right
->left
;
60 if (right
->left
!= t
->rbt_nil
)
61 right
->left
->par
= node
;
63 /* Link NODE's parent to RIGHT. */
64 right
->par
= node
->par
;
66 if (node
->par
== t
->rbt_nil
)
68 else if (node
== node
->par
->left
)
69 node
->par
->left
= right
;
71 node
->par
->right
= right
;
73 /* Put NODE on RIGHT's left. */
78 /* Perform a right-rotate operation on NODE in the red-black tree. */
80 rbt_right_rotate (struct rbt_strings
*t
, struct rbt_string_node
*node
)
82 struct rbt_string_node
*left
= node
->left
;
85 /* Turn LEFT's right subtree into NODE's left subtree. */
86 node
->left
= left
->right
;
87 if (left
->right
!= t
->rbt_nil
)
88 left
->right
->par
= node
;
90 /* Link NODE's parent to LEFT. */
91 left
->par
= node
->par
;
93 if (node
->par
== t
->rbt_nil
)
95 else if (node
== node
->par
->right
)
96 node
->par
->right
= left
;
98 node
->par
->left
= left
;
100 /* Put NODE on LEFT's right. */
105 /* Insert STR into the tree, returning 1 for success and 0 if STR already
106 appears in the tree. */
108 rbt_insert (struct rbt_strings
*t
, char *str
)
110 struct rbt_string_node
*curr
= t
->rbt_root
;
111 struct rbt_string_node
*trail
= t
->rbt_nil
;
113 while (curr
!= t
->rbt_nil
)
116 int cmp
= strcmp (str
, curr
->str
);
125 struct rbt_string_node
*fresh
= rbt_create_node (t
, str
);
128 if (trail
== t
->rbt_nil
)
130 else if (strcmp (fresh
->str
, trail
->str
) < 0)
133 trail
->right
= fresh
;
135 fresh
->left
= t
->rbt_nil
;
136 fresh
->right
= t
->rbt_nil
;
138 /* FRESH has now been inserted as a red leaf. If we have invalidated
139 one of the following preconditions, we must fix things up:
140 (a) If a node is red, both of its children are black.
141 (b) The root must be black.
142 Note that only (a) or (b) applies at any given time during the
143 process. This algorithm works up the tree from NEW looking
144 for a red child with a red parent, and cleaning that up. If the
145 root ends up red, it gets turned black at the end. */
147 while (curr
->par
->color
== RBT_RED
)
148 if (curr
->par
== curr
->par
->par
->left
)
150 struct rbt_string_node
*uncle
= curr
->par
->par
->right
;
151 if (uncle
->color
== RBT_RED
)
153 curr
->par
->color
= RBT_BLACK
;
154 uncle
->color
= RBT_BLACK
;
155 curr
->par
->par
->color
= RBT_RED
;
156 curr
= curr
->par
->par
;
158 else if (curr
== curr
->par
->right
)
161 rbt_left_rotate (t
, curr
);
165 curr
->par
->color
= RBT_BLACK
;
166 curr
->par
->par
->color
= RBT_RED
;
167 rbt_right_rotate (t
, curr
->par
->par
);
170 else /* curr->par == curr->par->par->right */
172 /* Gender-neutral formations are awkward, so let's be fair. ;-)
173 ("Parent-sibling" is just awful.) */
174 struct rbt_string_node
*aunt
= curr
->par
->par
->left
;
175 if (aunt
->color
== RBT_RED
)
177 curr
->par
->color
= RBT_BLACK
;
178 aunt
->color
= RBT_BLACK
;
179 curr
->par
->par
->color
= RBT_RED
;
180 curr
= curr
->par
->par
;
182 else if (curr
== curr
->par
->left
)
185 rbt_right_rotate (t
, curr
);
189 curr
->par
->color
= RBT_BLACK
;
190 curr
->par
->par
->color
= RBT_RED
;
191 rbt_left_rotate (t
, curr
->par
->par
);
195 t
->rbt_root
->color
= RBT_BLACK
;
199 /* Return 1 if STR is in the red-black tree, else 0. */
201 rbt_find (struct rbt_strings
*t
, char *str
)
203 struct rbt_string_node
*curr
= t
->rbt_root
;
205 while (curr
!= t
->rbt_nil
)
207 int cmp
= strcmp (str
, curr
->str
);
219 /* Inorder dump of the binary search tree. */
221 rbt_dump (struct rbt_strings
*t
, struct rbt_string_node
*subtree
)
223 if (subtree
!= t
->rbt_nil
)
225 rbt_dump (t
, subtree
->left
);
226 fprintf (stderr
, "%s\n", subtree
->str
);
227 rbt_dump (t
, subtree
->right
);
231 /* Inorder call-back for iteration over the tree. */
233 rbt_inorder_callback (struct rbt_strings
*t
, struct rbt_string_node
*subtree
,
236 if (subtree
!= t
->rbt_nil
)
238 rbt_inorder_callback (t
, subtree
->left
, fn
);
239 (*fn
) (subtree
->str
);
240 rbt_inorder_callback (t
, subtree
->right
, fn
);