1 /* ET-trees datastructure implementation.
2 Contributed by Pavel Nejedly
3 Copyright (C) 2002 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 "et-forest.h"
30 struct et_forest_occurrence
;
31 typedef struct et_forest_occurrence
* et_forest_occurrence_t
;
33 /* The ET-forest type. */
36 /* Linked list of nodes is used to destroy the structure. */
40 /* Single occurrence of node in ET-forest.
41 A single node may have multiple occurrences.
43 struct et_forest_occurrence
45 /* Parent in the splay-tree. */
46 et_forest_occurrence_t parent
;
48 /* Children in the splay-tree. */
49 et_forest_occurrence_t left
, right
;
51 /* Counts of vertices in the two splay-subtrees. */
52 int count_left
, count_right
;
54 /* Next occurrence of this node in the sequence. */
55 et_forest_occurrence_t next
;
57 /* The node, which this occurrence is of. */
58 et_forest_node_t node
;
68 /* First and last occurrence of this node in the sequence. */
69 et_forest_occurrence_t first
, last
;
73 static et_forest_occurrence_t splay
PARAMS ((et_forest_occurrence_t
));
74 static void remove_all_occurrences
PARAMS ((et_forest_node_t
));
75 static inline et_forest_occurrence_t find_leftmost_node
76 PARAMS ((et_forest_occurrence_t
));
77 static inline et_forest_occurrence_t find_rightmost_node
78 PARAMS ((et_forest_occurrence_t
));
79 static int calculate_value
PARAMS ((et_forest_occurrence_t
));
81 /* Return leftmost node present in the tree roted by OCC. */
82 static inline et_forest_occurrence_t
83 find_leftmost_node (occ
)
84 et_forest_occurrence_t occ
;
92 /* Return rightmost node present in the tree roted by OCC. */
93 static inline et_forest_occurrence_t
94 find_rightmost_node (occ
)
95 et_forest_occurrence_t occ
;
103 /* Operation splay for splay tree structure representing ocuurences. */
104 static et_forest_occurrence_t
106 et_forest_occurrence_t node
;
108 et_forest_occurrence_t parent
;
109 et_forest_occurrence_t grandparent
;
113 parent
= node
->parent
;
116 return node
; /* node == root. */
118 grandparent
= parent
->parent
;
123 /* Now there are four possible combinations: */
125 if (node
== parent
->left
)
127 if (parent
== grandparent
->left
)
129 et_forest_occurrence_t node1
, node2
;
133 count1
= node
->count_right
;
134 node2
= parent
->right
;
135 count2
= parent
->count_right
;
137 grandparent
->left
= node2
;
138 grandparent
->count_left
= count2
;
140 node2
->parent
= grandparent
;
141 parent
->left
= node1
;
142 parent
->count_left
= count1
;
144 node1
->parent
= parent
;
145 parent
->right
= grandparent
;
146 parent
->count_right
= count2
+ grandparent
->count_right
+ 1;
147 node
->right
= parent
;
148 node
->count_right
= count1
+ parent
->count_right
+ 1;
150 node
->parent
= grandparent
->parent
;
151 parent
->parent
= node
;
152 grandparent
->parent
= parent
;
156 if (node
->parent
->left
== grandparent
)
157 node
->parent
->left
= node
;
159 node
->parent
->right
= node
;
164 /* parent == grandparent->right && node == parent->left*/
165 et_forest_occurrence_t node1
, node2
;
169 count1
= node
->count_left
;
171 count2
= node
->count_right
;
173 grandparent
->right
= node1
;
174 grandparent
->count_right
= count1
;
176 node1
->parent
= grandparent
;
177 parent
->left
= node2
;
178 parent
->count_left
= count2
;
180 node2
->parent
= parent
;
181 node
->left
= grandparent
;
182 node
->count_left
= grandparent
->count_left
+ count1
+ 1;
183 node
->right
= parent
;
184 node
->count_right
= parent
->count_right
+ count2
+ 1;
186 node
->parent
= grandparent
->parent
;
187 parent
->parent
= node
;
188 grandparent
->parent
= node
;
192 if (node
->parent
->left
== grandparent
)
193 node
->parent
->left
= node
;
195 node
->parent
->right
= node
;
201 /* node == parent->right. */
202 if (parent
== grandparent
->left
)
204 et_forest_occurrence_t node1
, node2
;
208 count1
= node
->count_left
;
210 count2
= node
->count_right
;
212 parent
->right
= node1
;
213 parent
->count_right
= count1
;
215 node1
->parent
= parent
;
216 grandparent
->left
= node2
;
217 grandparent
->count_left
= count2
;
219 node2
->parent
= grandparent
;
221 node
->count_left
= parent
->count_left
+ count1
+ 1;
222 node
->right
= grandparent
;
223 node
->count_right
= grandparent
->count_right
+ count2
+ 1;
225 node
->parent
= grandparent
->parent
;
226 parent
->parent
= node
;
227 grandparent
->parent
= node
;
231 if (node
->parent
->left
== grandparent
)
232 node
->parent
->left
= node
;
234 node
->parent
->right
= node
;
239 /* parent == grandparent->right && node == parent->right*/
240 et_forest_occurrence_t node1
, node2
;
244 count1
= node
->count_left
;
245 node2
= parent
->left
;
246 count2
= parent
->count_left
;
248 grandparent
->right
= node2
;
249 grandparent
->count_right
= count2
;
251 node2
->parent
= grandparent
;
252 parent
->right
= node1
;
253 parent
->count_right
= count1
;
255 node1
->parent
= parent
;
256 parent
->left
= grandparent
;
257 parent
->count_left
= count2
+ grandparent
->count_left
+ 1;
259 node
->count_left
= count1
+ parent
->count_left
+ 1;
261 node
->parent
= grandparent
->parent
;
262 parent
->parent
= node
;
263 grandparent
->parent
= parent
;
267 if (node
->parent
->left
== grandparent
)
268 node
->parent
->left
= node
;
270 node
->parent
->right
= node
;
277 /* parent == root. */
278 /* There are two possible combinations: */
280 if (node
== parent
->left
)
282 et_forest_occurrence_t node1
;
286 count1
= node
->count_right
;
288 parent
->left
= node1
;
289 parent
->count_left
= count1
;
291 node1
->parent
= parent
;
292 node
->right
= parent
;
293 node
->count_right
= parent
->count_right
+ 1 + count1
;
294 node
->parent
= parent
->parent
; /* the same as = 0; */
295 parent
->parent
= node
;
299 if (node
->parent
->left
== parent
)
300 node
->parent
->left
= node
;
302 node
->parent
->right
= node
;
307 /* node == parent->right. */
308 et_forest_occurrence_t node1
;
312 count1
= node
->count_left
;
314 parent
->right
= node1
;
315 parent
->count_right
= count1
;
317 node1
->parent
= parent
;
319 node
->count_left
= parent
->count_left
+ 1 + count1
;
320 node
->parent
= parent
->parent
; /* the same as = 0; */
321 parent
->parent
= node
;
325 if (node
->parent
->left
== parent
)
326 node
->parent
->left
= node
;
328 node
->parent
->right
= node
;
335 /* Remove all occurences of the given node before destroying the node. */
337 remove_all_occurrences (forest_node
)
338 et_forest_node_t forest_node
;
340 et_forest_occurrence_t first
= forest_node
->first
;
341 et_forest_occurrence_t last
= forest_node
->last
;
342 et_forest_occurrence_t node
;
347 first
->left
->parent
= 0;
349 first
->right
->parent
= 0;
356 last
->left
->parent
= 0;
358 last
->right
->parent
= 0;
361 if (last
->right
&& first
->left
) /* actually, first->left would suffice. */
363 /* Need to join them. */
364 et_forest_occurrence_t prev_node
, next_node
;
366 prev_node
= splay (find_rightmost_node (first
->left
));
367 next_node
= splay (find_leftmost_node (last
->right
));
368 /* prev_node and next_node are consecutive occurencies
370 if (prev_node
->next
!= next_node
)
373 prev_node
->right
= next_node
->right
;
374 prev_node
->count_right
= next_node
->count_right
;
375 prev_node
->next
= next_node
->next
;
376 if (prev_node
->right
)
377 prev_node
->right
->parent
= prev_node
;
379 if (prev_node
->node
->last
== next_node
)
380 prev_node
->node
->last
= prev_node
;
391 et_forest_occurrence_t next_node
;
396 node
->left
->parent
= 0;
398 node
->right
->parent
= 0;
400 next_node
= node
->next
;
411 /* Calculate ET value of the given node. */
413 calculate_value (node
)
414 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. */
437 et_forest_t forest
= xmalloc (sizeof (struct et_forest
));
445 /* Deallocate the structure. */
447 et_forest_delete (forest
)
456 /* Create new node with VALUE and return the edge.
457 Return NULL when memory allocation failed. */
459 et_forest_add_node (forest
, value
)
463 /* Create node with one occurrence. */
464 et_forest_node_t node
;
465 et_forest_occurrence_t occ
;
467 node
= xmalloc (sizeof (struct et_forest_node
));
468 occ
= xmalloc (sizeof (struct et_forest_occurrence
));
470 node
->first
= node
->last
= occ
;
475 occ
->left
= occ
->right
= occ
->parent
= 0;
477 occ
->count_left
= occ
->count_right
= 0;
481 /* Add new edge to the tree, return 1 if succesfull.
482 0 indicates that creation of the edge will close the cycle in graph. */
484 et_forest_add_edge (forest
, parent_node
, child_node
)
485 et_forest_t forest ATTRIBUTE_UNUSED
;
486 et_forest_node_t parent_node
;
487 et_forest_node_t child_node
;
489 et_forest_occurrence_t new_occ
, parent_occ
, child_occ
;
491 if (! parent_node
|| ! child_node
)
494 parent_occ
= parent_node
->first
;
495 child_occ
= child_node
->first
;
500 if (parent_occ
->parent
)
501 return 0; /* Both child and parent are in the same tree. */
504 abort (); /* child must be root of its containing tree. */
506 new_occ
= xmalloc (sizeof (struct et_forest_occurrence
));
508 new_occ
->node
= parent_node
;
509 new_occ
->left
= child_occ
;
510 new_occ
->count_left
= child_occ
->count_right
+ 1; /* count_left is 0. */
511 new_occ
->right
= parent_occ
->right
;
512 new_occ
->count_right
= parent_occ
->count_right
;
513 new_occ
->parent
= parent_occ
;
514 new_occ
->next
= parent_occ
->next
;
515 child_occ
->parent
= new_occ
;
516 parent_occ
->right
= new_occ
;
517 parent_occ
->count_right
= new_occ
->count_left
+ new_occ
->count_right
+ 1;
518 parent_occ
->next
= new_occ
;
520 new_occ
->right
->parent
= new_occ
;
522 if (parent_node
->last
== parent_occ
)
523 parent_node
->last
= new_occ
;
527 /* Remove NODE from the tree and all connected edges. */
529 et_forest_remove_node (forest
, node
)
531 et_forest_node_t node
;
533 remove_all_occurrences (node
);
539 /* Remove edge from the tree, return 1 if sucesfull,
540 0 indicates nonexisting edge. */
542 et_forest_remove_edge (forest
, parent_node
, child_node
)
543 et_forest_t forest ATTRIBUTE_UNUSED
;
544 et_forest_node_t parent_node
;
545 et_forest_node_t child_node
;
547 et_forest_occurrence_t parent_pre_occ
, parent_post_occ
;
549 splay (child_node
->first
);
551 if (! child_node
->first
->left
)
554 parent_pre_occ
= find_rightmost_node (child_node
->first
->left
);
555 if (parent_pre_occ
->node
!= parent_node
)
558 splay (parent_pre_occ
);
559 parent_pre_occ
->right
->parent
= 0;
561 parent_post_occ
= parent_pre_occ
->next
;
562 splay (parent_post_occ
);
564 parent_post_occ
->left
->parent
= 0;
566 parent_pre_occ
->right
= parent_post_occ
->right
;
567 parent_pre_occ
->count_right
= parent_post_occ
->count_right
;
568 if (parent_post_occ
->right
)
569 parent_post_occ
->right
->parent
= parent_pre_occ
;
571 parent_pre_occ
->next
= parent_post_occ
->next
;
573 if (parent_post_occ
== parent_node
->last
)
574 parent_node
->last
= parent_pre_occ
;
576 free (parent_post_occ
);
580 /* Return the parent of the NODE if any, NULL otherwise. */
582 et_forest_parent (forest
, node
)
583 et_forest_t forest ATTRIBUTE_UNUSED
;
584 et_forest_node_t node
;
588 if (node
->first
->left
)
589 return find_rightmost_node (node
->first
->left
)->node
;
595 /* Return nearest common ancestor of NODE1 and NODE2.
596 Return NULL of they are in different trees. */
598 et_forest_common_ancestor (forest
, node1
, node2
)
599 et_forest_t forest ATTRIBUTE_UNUSED
;
600 et_forest_node_t node1
;
601 et_forest_node_t node2
;
603 int value1
, value2
, max_value
;
604 et_forest_node_t ancestor
;
609 if (! node1
|| ! node2
)
612 splay (node1
->first
);
613 splay (node2
->first
);
615 if (! node1
->first
->parent
) /* The two vertices are in different trees. */
618 value2
= calculate_value (node2
->first
);
619 value1
= calculate_value (node1
->first
);
632 while (calculate_value (ancestor
->last
) < max_value
)
634 /* Find parent node. */
635 splay (ancestor
->first
);
636 ancestor
= find_rightmost_node (ancestor
->first
->left
) ->node
;
642 /* Return the value pointer of node set during it's creation. */
644 et_forest_node_value (forest
, node
)
645 et_forest_t forest ATTRIBUTE_UNUSED
;
646 et_forest_node_t node
;
648 /* Alloc threading NULL as a special node of the forest. */
654 /* Find all sons of NODE and store them into ARRAY allocated by the caller.
655 Return number of nodes found. */
657 et_forest_enumerate_sons (forest
, node
, array
)
658 et_forest_t forest ATTRIBUTE_UNUSED
;
659 et_forest_node_t node
;
660 et_forest_node_t
*array
;
663 et_forest_occurrence_t occ
= node
->first
, stop
= node
->last
, occ1
;
665 /* Parent is the rightmost node of the left successor.
666 Look for all occurences having no right succesor
667 and lookup the sons. */
673 occ1
= find_leftmost_node (occ
->right
);
674 if (occ1
->node
->first
== occ1
)
675 array
[n
++] = occ1
->node
;