1 /* ET-trees data structure implementation.
2 Contributed by Pavel Nejedly
3 Copyright (C) 2002, 2003 Free Software Foundation, Inc.
5 This file is part of the libiberty library.
6 Libiberty is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 Libiberty is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with libiberty; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 The ET-forest structure is described in:
22 D. D. Sleator and R. E. Tarjan. A data structure for dynamic trees.
23 J. G'omput. System Sci., 26(3):362 381, 1983.
28 #include "coretypes.h"
30 #include "et-forest.h"
31 #include "alloc-pool.h"
33 struct et_forest_occurrence
;
34 typedef struct et_forest_occurrence
* et_forest_occurrence_t
;
36 /* The ET-forest type. */
39 /* Linked list of nodes is used to destroy the structure. */
42 alloc_pool occur_pool
;
45 /* Single occurrence of node in ET-forest.
46 A single node may have multiple occurrences.
48 struct et_forest_occurrence
50 /* Parent in the splay-tree. */
51 et_forest_occurrence_t parent
;
53 /* Children in the splay-tree. */
54 et_forest_occurrence_t left
, right
;
56 /* Counts of vertices in the two splay-subtrees. */
57 int count_left
, count_right
;
59 /* Next occurrence of this node in the sequence. */
60 et_forest_occurrence_t next
;
62 /* The node, which this occurrence is of. */
63 et_forest_node_t node
;
73 /* First and last occurrence of this node in the sequence. */
74 et_forest_occurrence_t first
, last
;
78 static et_forest_occurrence_t
splay (et_forest_occurrence_t
);
79 static void remove_all_occurrences (et_forest_t
, et_forest_node_t
);
80 static inline et_forest_occurrence_t find_leftmost_node
81 (et_forest_occurrence_t
);
82 static inline et_forest_occurrence_t find_rightmost_node
83 (et_forest_occurrence_t
);
84 static int calculate_value (et_forest_occurrence_t
);
86 /* Return leftmost node present in the tree roted by OCC. */
87 static inline et_forest_occurrence_t
88 find_leftmost_node (et_forest_occurrence_t occ
)
96 /* Return rightmost node present in the tree roted by OCC. */
97 static inline et_forest_occurrence_t
98 find_rightmost_node (et_forest_occurrence_t occ
)
106 /* Operation splay for splay tree structure representing occurrences. */
107 static et_forest_occurrence_t
108 splay (et_forest_occurrence_t node
)
110 et_forest_occurrence_t parent
;
111 et_forest_occurrence_t grandparent
;
115 parent
= node
->parent
;
118 return node
; /* node == root. */
120 grandparent
= parent
->parent
;
125 /* Now there are four possible combinations: */
127 if (node
== parent
->left
)
129 if (parent
== grandparent
->left
)
131 et_forest_occurrence_t node1
, node2
;
135 count1
= node
->count_right
;
136 node2
= parent
->right
;
137 count2
= parent
->count_right
;
139 grandparent
->left
= node2
;
140 grandparent
->count_left
= count2
;
142 node2
->parent
= grandparent
;
143 parent
->left
= node1
;
144 parent
->count_left
= count1
;
146 node1
->parent
= parent
;
147 parent
->right
= grandparent
;
148 parent
->count_right
= count2
+ grandparent
->count_right
+ 1;
149 node
->right
= parent
;
150 node
->count_right
= count1
+ parent
->count_right
+ 1;
152 node
->parent
= grandparent
->parent
;
153 parent
->parent
= node
;
154 grandparent
->parent
= parent
;
158 if (node
->parent
->left
== grandparent
)
159 node
->parent
->left
= node
;
161 node
->parent
->right
= node
;
166 /* parent == grandparent->right && node == parent->left*/
167 et_forest_occurrence_t node1
, node2
;
171 count1
= node
->count_left
;
173 count2
= node
->count_right
;
175 grandparent
->right
= node1
;
176 grandparent
->count_right
= count1
;
178 node1
->parent
= grandparent
;
179 parent
->left
= node2
;
180 parent
->count_left
= count2
;
182 node2
->parent
= parent
;
183 node
->left
= grandparent
;
184 node
->count_left
= grandparent
->count_left
+ count1
+ 1;
185 node
->right
= parent
;
186 node
->count_right
= parent
->count_right
+ count2
+ 1;
188 node
->parent
= grandparent
->parent
;
189 parent
->parent
= node
;
190 grandparent
->parent
= node
;
194 if (node
->parent
->left
== grandparent
)
195 node
->parent
->left
= node
;
197 node
->parent
->right
= node
;
203 /* node == parent->right. */
204 if (parent
== grandparent
->left
)
206 et_forest_occurrence_t node1
, node2
;
210 count1
= node
->count_left
;
212 count2
= node
->count_right
;
214 parent
->right
= node1
;
215 parent
->count_right
= count1
;
217 node1
->parent
= parent
;
218 grandparent
->left
= node2
;
219 grandparent
->count_left
= count2
;
221 node2
->parent
= grandparent
;
223 node
->count_left
= parent
->count_left
+ count1
+ 1;
224 node
->right
= grandparent
;
225 node
->count_right
= grandparent
->count_right
+ count2
+ 1;
227 node
->parent
= grandparent
->parent
;
228 parent
->parent
= node
;
229 grandparent
->parent
= node
;
233 if (node
->parent
->left
== grandparent
)
234 node
->parent
->left
= node
;
236 node
->parent
->right
= node
;
241 /* parent == grandparent->right && node == parent->right*/
242 et_forest_occurrence_t node1
, node2
;
246 count1
= node
->count_left
;
247 node2
= parent
->left
;
248 count2
= parent
->count_left
;
250 grandparent
->right
= node2
;
251 grandparent
->count_right
= count2
;
253 node2
->parent
= grandparent
;
254 parent
->right
= node1
;
255 parent
->count_right
= count1
;
257 node1
->parent
= parent
;
258 parent
->left
= grandparent
;
259 parent
->count_left
= count2
+ grandparent
->count_left
+ 1;
261 node
->count_left
= count1
+ parent
->count_left
+ 1;
263 node
->parent
= grandparent
->parent
;
264 parent
->parent
= node
;
265 grandparent
->parent
= parent
;
269 if (node
->parent
->left
== grandparent
)
270 node
->parent
->left
= node
;
272 node
->parent
->right
= node
;
279 /* parent == root. */
280 /* There are two possible combinations: */
282 if (node
== parent
->left
)
284 et_forest_occurrence_t node1
;
288 count1
= node
->count_right
;
290 parent
->left
= node1
;
291 parent
->count_left
= count1
;
293 node1
->parent
= parent
;
294 node
->right
= parent
;
295 node
->count_right
= parent
->count_right
+ 1 + count1
;
296 node
->parent
= parent
->parent
; /* the same as = 0; */
297 parent
->parent
= node
;
301 if (node
->parent
->left
== parent
)
302 node
->parent
->left
= node
;
304 node
->parent
->right
= node
;
309 /* node == parent->right. */
310 et_forest_occurrence_t node1
;
314 count1
= node
->count_left
;
316 parent
->right
= node1
;
317 parent
->count_right
= count1
;
319 node1
->parent
= parent
;
321 node
->count_left
= parent
->count_left
+ 1 + count1
;
322 node
->parent
= parent
->parent
; /* the same as = 0; */
323 parent
->parent
= node
;
327 if (node
->parent
->left
== parent
)
328 node
->parent
->left
= node
;
330 node
->parent
->right
= node
;
337 /* Remove all occurrences of the given node before destroying the node. */
339 remove_all_occurrences (et_forest_t forest
, et_forest_node_t forest_node
)
341 et_forest_occurrence_t first
= forest_node
->first
;
342 et_forest_occurrence_t last
= forest_node
->last
;
343 et_forest_occurrence_t node
;
348 first
->left
->parent
= 0;
350 first
->right
->parent
= 0;
357 last
->left
->parent
= 0;
359 last
->right
->parent
= 0;
362 if (last
->right
&& first
->left
) /* actually, first->left would suffice. */
364 /* Need to join them. */
365 et_forest_occurrence_t prev_node
, next_node
;
367 prev_node
= splay (find_rightmost_node (first
->left
));
368 next_node
= splay (find_leftmost_node (last
->right
));
369 /* prev_node and next_node are consecutive occurrences
371 if (prev_node
->next
!= next_node
)
374 prev_node
->right
= next_node
->right
;
375 prev_node
->count_right
= next_node
->count_right
;
376 prev_node
->next
= next_node
->next
;
377 if (prev_node
->right
)
378 prev_node
->right
->parent
= prev_node
;
380 if (prev_node
->node
->last
== next_node
)
381 prev_node
->node
->last
= prev_node
;
383 pool_free (forest
->occur_pool
, next_node
);
392 et_forest_occurrence_t next_node
;
397 node
->left
->parent
= 0;
399 node
->right
->parent
= 0;
401 next_node
= node
->next
;
402 pool_free (forest
->occur_pool
, node
);
407 pool_free (forest
->occur_pool
, first
);
409 pool_free (forest
->occur_pool
, last
);
412 /* Calculate ET value of the given node. */
414 calculate_value (et_forest_occurrence_t node
)
416 int value
= node
->count_left
;
420 if (node
== node
->parent
->right
)
421 value
+= node
->parent
->count_left
+ 1;
432 /* Create ET-forest structure. */
434 et_forest_create (void)
436 et_forest_t forest
= xmalloc (sizeof (struct et_forest
));
439 forest
->occur_pool
= create_alloc_pool ("et_forest_occurrence pool", sizeof (struct et_forest_occurrence
), 300);
440 forest
->node_pool
= create_alloc_pool ("et_forest_node pool", sizeof (struct et_forest_node
), 300);
446 /* Deallocate the structure. */
448 et_forest_delete (et_forest_t forest
)
452 free_alloc_pool (forest
->occur_pool
);
453 free_alloc_pool (forest
->node_pool
);
457 /* Create new node with VALUE and return the edge.
458 Return NULL when memory allocation failed. */
460 et_forest_add_node (et_forest_t forest
, void *value
)
462 /* Create node with one occurrence. */
463 et_forest_node_t node
;
464 et_forest_occurrence_t occ
;
466 node
= pool_alloc (forest
->node_pool
);
467 occ
= pool_alloc (forest
->occur_pool
);
469 node
->first
= node
->last
= occ
;
474 occ
->left
= occ
->right
= occ
->parent
= 0;
476 occ
->count_left
= occ
->count_right
= 0;
480 /* Add new edge to the tree, return 1 if successful.
481 0 indicates that creation of the edge will close the cycle in graph. */
483 et_forest_add_edge (et_forest_t forest ATTRIBUTE_UNUSED
,
484 et_forest_node_t parent_node
, et_forest_node_t child_node
)
486 et_forest_occurrence_t new_occ
, parent_occ
, child_occ
;
488 if (! parent_node
|| ! child_node
)
491 parent_occ
= parent_node
->first
;
492 child_occ
= child_node
->first
;
497 if (parent_occ
->parent
)
498 return 0; /* Both child and parent are in the same tree. */
501 abort (); /* child must be root of its containing tree. */
503 new_occ
= pool_alloc (forest
->occur_pool
);
505 new_occ
->node
= parent_node
;
506 new_occ
->left
= child_occ
;
507 new_occ
->count_left
= child_occ
->count_right
+ 1; /* count_left is 0. */
508 new_occ
->right
= parent_occ
->right
;
509 new_occ
->count_right
= parent_occ
->count_right
;
510 new_occ
->parent
= parent_occ
;
511 new_occ
->next
= parent_occ
->next
;
512 child_occ
->parent
= new_occ
;
513 parent_occ
->right
= new_occ
;
514 parent_occ
->count_right
= new_occ
->count_left
+ new_occ
->count_right
+ 1;
515 parent_occ
->next
= new_occ
;
517 new_occ
->right
->parent
= new_occ
;
519 if (parent_node
->last
== parent_occ
)
520 parent_node
->last
= new_occ
;
524 /* Remove NODE from the tree and all connected edges. */
526 et_forest_remove_node (et_forest_t forest
, et_forest_node_t node
)
528 remove_all_occurrences (forest
, node
);
531 pool_free (forest
->node_pool
, node
);
534 /* Remove edge from the tree, return 1 if successful,
535 0 indicates nonexisting edge. */
537 et_forest_remove_edge (et_forest_t forest ATTRIBUTE_UNUSED
,
538 et_forest_node_t parent_node
,
539 et_forest_node_t child_node
)
541 et_forest_occurrence_t parent_pre_occ
, parent_post_occ
;
543 splay (child_node
->first
);
545 if (! child_node
->first
->left
)
548 parent_pre_occ
= find_rightmost_node (child_node
->first
->left
);
549 if (parent_pre_occ
->node
!= parent_node
)
552 splay (parent_pre_occ
);
553 parent_pre_occ
->right
->parent
= 0;
555 parent_post_occ
= parent_pre_occ
->next
;
556 splay (parent_post_occ
);
558 parent_post_occ
->left
->parent
= 0;
560 parent_pre_occ
->right
= parent_post_occ
->right
;
561 parent_pre_occ
->count_right
= parent_post_occ
->count_right
;
562 if (parent_post_occ
->right
)
563 parent_post_occ
->right
->parent
= parent_pre_occ
;
565 parent_pre_occ
->next
= parent_post_occ
->next
;
567 if (parent_post_occ
== parent_node
->last
)
568 parent_node
->last
= parent_pre_occ
;
570 pool_free (forest
->occur_pool
, parent_post_occ
);
574 /* Return the parent of the NODE if any, NULL otherwise. */
576 et_forest_parent (et_forest_t forest ATTRIBUTE_UNUSED
, et_forest_node_t node
)
580 if (node
->first
->left
)
581 return find_rightmost_node (node
->first
->left
)->node
;
587 /* Return nearest common ancestor of NODE1 and NODE2.
588 Return NULL of they are in different trees. */
590 et_forest_common_ancestor (et_forest_t forest ATTRIBUTE_UNUSED
,
591 et_forest_node_t node1
, et_forest_node_t node2
)
593 int value1
, value2
, max_value
;
594 et_forest_node_t ancestor
;
599 if (! node1
|| ! node2
)
602 splay (node1
->first
);
603 splay (node2
->first
);
605 if (! node1
->first
->parent
) /* The two vertices are in different trees. */
608 value2
= calculate_value (node2
->first
);
609 value1
= calculate_value (node1
->first
);
622 while (calculate_value (ancestor
->last
) < max_value
)
624 /* Find parent node. */
625 splay (ancestor
->first
);
626 ancestor
= find_rightmost_node (ancestor
->first
->left
) ->node
;
632 /* Return the value pointer of node set during it's creation. */
634 et_forest_node_value (et_forest_t forest ATTRIBUTE_UNUSED
,
635 et_forest_node_t node
)
637 /* Alloc threading NULL as a special node of the forest. */
643 /* Find all sons of NODE and store them into ARRAY allocated by the caller.
644 Return number of nodes found. */
646 et_forest_enumerate_sons (et_forest_t forest ATTRIBUTE_UNUSED
,
647 et_forest_node_t node
, et_forest_node_t
*array
)
650 et_forest_occurrence_t occ
= node
->first
, stop
= node
->last
, occ1
;
652 /* Parent is the rightmost node of the left successor.
653 Look for all occurrences having no right successor
654 and lookup the sons. */
660 occ1
= find_leftmost_node (occ
->right
);
661 if (occ1
->node
->first
== occ1
)
662 array
[n
++] = occ1
->node
;