1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
|
/* ET-trees datastructure implementation.
Contributed by Pavel Nejedly
Copyright (C) 2002 Free Software Foundation, Inc.
This file is part of the libiberty library.
Libiberty is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
Libiberty is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with libiberty; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
The ET-forest structure is described in:
D. D. Sleator and R. E. Tarjan. A data structure for dynamic trees.
J. G'omput. System Sci., 26(3):362 381, 1983.
*/
#include "config.h"
#include "system.h"
#include "et-forest.h"
struct et_forest_occurrence;
typedef struct et_forest_occurrence* et_forest_occurrence_t;
/* The ET-forest type. */
struct et_forest
{
/* Linked list of nodes is used to destroy the structure. */
int nnodes;
};
/* Single occurrence of node in ET-forest.
A single node may have multiple occurrences.
*/
struct et_forest_occurrence
{
/* Parent in the splay-tree. */
et_forest_occurrence_t parent;
/* Children in the splay-tree. */
et_forest_occurrence_t left, right;
/* Counts of vertices in the two splay-subtrees. */
int count_left, count_right;
/* Next occurrence of this node in the sequence. */
et_forest_occurrence_t next;
/* The node, which this occurrence is of. */
et_forest_node_t node;
};
/* ET-forest node. */
struct et_forest_node
{
et_forest_t forest;
void *value;
/* First and last occurrence of this node in the sequence. */
et_forest_occurrence_t first, last;
};
static et_forest_occurrence_t splay PARAMS ((et_forest_occurrence_t));
static void remove_all_occurrences PARAMS ((et_forest_node_t));
static inline et_forest_occurrence_t find_leftmost_node
PARAMS ((et_forest_occurrence_t));
static inline et_forest_occurrence_t find_rightmost_node
PARAMS ((et_forest_occurrence_t));
static int calculate_value PARAMS ((et_forest_occurrence_t));
/* Return leftmost node present in the tree roted by OCC. */
static inline et_forest_occurrence_t
find_leftmost_node (occ)
et_forest_occurrence_t occ;
{
while (occ->left)
occ = occ->left;
return occ;
}
/* Return rightmost node present in the tree roted by OCC. */
static inline et_forest_occurrence_t
find_rightmost_node (occ)
et_forest_occurrence_t occ;
{
while (occ->right)
occ = occ->right;
return occ;
}
/* Operation splay for splay tree structure representing ocuurences. */
static et_forest_occurrence_t
splay (node)
et_forest_occurrence_t node;
{
et_forest_occurrence_t parent;
et_forest_occurrence_t grandparent;
while (1)
{
parent = node->parent;
if (! parent)
return node; /* node == root. */
grandparent = parent->parent;
if (! grandparent)
break;
/* Now there are four possible combinations: */
if (node == parent->left)
{
if (parent == grandparent->left)
{
et_forest_occurrence_t node1, node2;
int count1, count2;
node1 = node->right;
count1 = node->count_right;
node2 = parent->right;
count2 = parent->count_right;
grandparent->left = node2;
grandparent->count_left = count2;
if (node2)
node2->parent = grandparent;
parent->left = node1;
parent->count_left = count1;
if (node1)
node1->parent = parent;
parent->right = grandparent;
parent->count_right = count2 + grandparent->count_right + 1;
node->right = parent;
node->count_right = count1 + parent->count_right + 1;
node->parent = grandparent->parent;
parent->parent = node;
grandparent->parent = parent;
if (node->parent)
{
if (node->parent->left == grandparent)
node->parent->left = node;
else
node->parent->right = node;
}
}
else
{
/* parent == grandparent->right && node == parent->left*/
et_forest_occurrence_t node1, node2;
int count1, count2;
node1 = node->left;
count1 = node->count_left;
node2 = node->right;
count2 = node->count_right;
grandparent->right = node1;
grandparent->count_right = count1;
if (node1)
node1->parent = grandparent;
parent->left = node2;
parent->count_left = count2;
if (node2)
node2->parent = parent;
node->left = grandparent;
node->count_left = grandparent->count_left + count1 + 1;
node->right = parent;
node->count_right = parent->count_right + count2 + 1;
node->parent = grandparent->parent;
parent->parent = node;
grandparent->parent = node;
if (node->parent)
{
if (node->parent->left == grandparent)
node->parent->left = node;
else
node->parent->right = node;
}
}
}
else
{
/* node == parent->right. */
if (parent == grandparent->left)
{
et_forest_occurrence_t node1, node2;
int count1, count2;
node1 = node->left;
count1 = node->count_left;
node2 = node->right;
count2 = node->count_right;
parent->right = node1;
parent->count_right = count1;
if (node1)
node1->parent = parent;
grandparent->left = node2;
grandparent->count_left = count2;
if (node2)
node2->parent = grandparent;
node->left = parent;
node->count_left = parent->count_left + count1 + 1;
node->right = grandparent;
node->count_right = grandparent->count_right + count2 + 1;
node->parent = grandparent->parent;
parent->parent = node;
grandparent->parent = node;
if (node->parent)
{
if (node->parent->left == grandparent)
node->parent->left = node;
else
node->parent->right = node;
}
}
else
{
/* parent == grandparent->right && node == parent->right*/
et_forest_occurrence_t node1, node2;
int count1, count2;
node1 = node->left;
count1 = node->count_left;
node2 = parent->left;
count2 = parent->count_left;
grandparent->right = node2;
grandparent->count_right = count2;
if (node2)
node2->parent = grandparent;
parent->right = node1;
parent->count_right = count1;
if (node1)
node1->parent = parent;
parent->left = grandparent;
parent->count_left = count2 + grandparent->count_left + 1;
node->left = parent;
node->count_left = count1 + parent->count_left + 1;
node->parent = grandparent->parent;
parent->parent = node;
grandparent->parent = parent;
if (node->parent)
{
if (node->parent->left == grandparent)
node->parent->left = node;
else
node->parent->right = node;
}
}
}
}
/* parent == root. */
/* There are two possible combinations: */
if (node == parent->left)
{
et_forest_occurrence_t node1;
int count1;
node1 = node->right;
count1 = node->count_right;
parent->left = node1;
parent->count_left = count1;
if (node1)
node1->parent = parent;
node->right = parent;
node->count_right = parent->count_right + 1 + count1;
node->parent = parent->parent; /* the same as = 0; */
parent->parent = node;
if (node->parent)
{
if (node->parent->left == parent)
node->parent->left = node;
else
node->parent->right = node;
}
}
else
{
/* node == parent->right. */
et_forest_occurrence_t node1;
int count1;
node1 = node->left;
count1 = node->count_left;
parent->right = node1;
parent->count_right = count1;
if (node1)
node1->parent = parent;
node->left = parent;
node->count_left = parent->count_left + 1 + count1;
node->parent = parent->parent; /* the same as = 0; */
parent->parent = node;
if (node->parent)
{
if (node->parent->left == parent)
node->parent->left = node;
else
node->parent->right = node;
}
}
return node;
}
/* Remove all occurences of the given node before destroying the node. */
static void
remove_all_occurrences (forest_node)
et_forest_node_t forest_node;
{
et_forest_occurrence_t first = forest_node->first;
et_forest_occurrence_t last = forest_node->last;
et_forest_occurrence_t node;
splay (first);
if (first->left)
first->left->parent = 0;
if (first->right)
first->right->parent = 0;
if (last != first)
{
splay (last);
if (last->left)
last->left->parent = 0;
if (last->right)
last->right->parent = 0;
}
if (last->right && first->left) /* actually, first->left would suffice. */
{
/* Need to join them. */
et_forest_occurrence_t prev_node, next_node;
prev_node = splay (find_rightmost_node (first->left));
next_node = splay (find_leftmost_node (last->right));
/* prev_node and next_node are consecutive occurencies
of the same node. */
if (prev_node->next != next_node)
abort ();
prev_node->right = next_node->right;
prev_node->count_right = next_node->count_right;
prev_node->next = next_node->next;
if (prev_node->right)
prev_node->right->parent = prev_node;
if (prev_node->node->last == next_node)
prev_node->node->last = prev_node;
free (next_node);
}
if (first != last)
{
node = first->next;
while (node != last)
{
et_forest_occurrence_t next_node;
splay (node);
if (node->left)
node->left->parent = 0;
if (node->right)
node->right->parent = 0;
next_node = node->next;
free (node);
node = next_node;
}
}
free (first);
if (first != last)
free (last);
}
/* Calculate ET value of the given node. */
static inline int
calculate_value (node)
et_forest_occurrence_t node;
{
int value = node->count_left;
while (node->parent)
{
if (node == node->parent->right)
value += node->parent->count_left + 1;
node = node->parent;
}
return value;
}
/* Create ET-forest structure. */
et_forest_t
et_forest_create ()
{
et_forest_t forest = xmalloc (sizeof (struct et_forest));
forest->nnodes = 0;
return forest;
}
/* Deallocate the structure. */
void
et_forest_delete (forest)
et_forest_t forest;
{
if (forest->nnodes)
abort ();
free (forest);
}
/* Create new node with VALUE and return the edge.
Return NULL when memory allocation failed. */
et_forest_node_t
et_forest_add_node (forest, value)
et_forest_t forest;
void *value;
{
/* Create node with one occurrence. */
et_forest_node_t node;
et_forest_occurrence_t occ;
node = xmalloc (sizeof (struct et_forest_node));
occ = xmalloc (sizeof (struct et_forest_occurrence));
node->first = node->last = occ;
node->value = value;
forest->nnodes++;
occ->node = node;
occ->left = occ->right = occ->parent = 0;
occ->next = 0;
occ->count_left = occ->count_right = 0;
return node;
}
/* Add new edge to the tree, return 1 if succesfull.
0 indicates that creation of the edge will close the cycle in graph. */
int
et_forest_add_edge (forest, parent_node, child_node)
et_forest_t forest ATTRIBUTE_UNUSED;
et_forest_node_t parent_node;
et_forest_node_t child_node;
{
et_forest_occurrence_t new_occ, parent_occ, child_occ;
if (! parent_node || ! child_node)
abort ();
parent_occ = parent_node->first;
child_occ = child_node->first;
splay (parent_occ);
splay (child_occ);
if (parent_occ->parent)
return 0; /* Both child and parent are in the same tree. */
if (child_occ->left)
abort (); /* child must be root of its containing tree. */
new_occ = xmalloc (sizeof (struct et_forest_occurrence));
new_occ->node = parent_node;
new_occ->left = child_occ;
new_occ->count_left = child_occ->count_right + 1; /* count_left is 0. */
new_occ->right = parent_occ->right;
new_occ->count_right = parent_occ->count_right;
new_occ->parent = parent_occ;
new_occ->next = parent_occ->next;
child_occ->parent = new_occ;
parent_occ->right = new_occ;
parent_occ->count_right = new_occ->count_left + new_occ->count_right + 1;
parent_occ->next = new_occ;
if (new_occ->right)
new_occ->right->parent = new_occ;
if (parent_node->last == parent_occ)
parent_node->last = new_occ;
return 1;
}
/* Remove NODE from the tree and all connected edges. */
void
et_forest_remove_node (forest, node)
et_forest_t forest;
et_forest_node_t node;
{
remove_all_occurrences (node);
forest->nnodes--;
free (node);
}
/* Remove edge from the tree, return 1 if sucesfull,
0 indicates nonexisting edge. */
int
et_forest_remove_edge (forest, parent_node, child_node)
et_forest_t forest ATTRIBUTE_UNUSED;
et_forest_node_t parent_node;
et_forest_node_t child_node;
{
et_forest_occurrence_t parent_pre_occ, parent_post_occ;
splay (child_node->first);
if (! child_node->first->left)
return 0;
parent_pre_occ = find_rightmost_node (child_node->first->left);
if (parent_pre_occ->node != parent_node)
abort ();
splay (parent_pre_occ);
parent_pre_occ->right->parent = 0;
parent_post_occ = parent_pre_occ->next;
splay (parent_post_occ);
parent_post_occ->left->parent = 0;
parent_pre_occ->right = parent_post_occ->right;
parent_pre_occ->count_right = parent_post_occ->count_right;
if (parent_post_occ->right)
parent_post_occ->right->parent = parent_pre_occ;
parent_pre_occ->next = parent_post_occ->next;
if (parent_post_occ == parent_node->last)
parent_node->last = parent_pre_occ;
free (parent_post_occ);
return 1;
}
/* Return the parent of the NODE if any, NULL otherwise. */
et_forest_node_t
et_forest_parent (forest, node)
et_forest_t forest ATTRIBUTE_UNUSED;
et_forest_node_t node;
{
splay (node->first);
if (node->first->left)
return find_rightmost_node (node->first->left)->node;
else
return 0;
}
/* Return nearest common ancestor of NODE1 and NODE2.
Return NULL of they are in different trees. */
et_forest_node_t
et_forest_common_ancestor (forest, node1, node2)
et_forest_t forest ATTRIBUTE_UNUSED;
et_forest_node_t node1;
et_forest_node_t node2;
{
int value1, value2, max_value;
et_forest_node_t ancestor;
if (node1 == node2)
return node1;
if (! node1 || ! node2)
abort ();
splay (node1->first);
splay (node2->first);
if (! node1->first->parent) /* The two vertices are in different trees. */
return 0;
value2 = calculate_value (node2->first);
value1 = calculate_value (node1->first);
if (value1 < value2)
{
ancestor = node1;
max_value = value2;
}
else
{
ancestor = node2;
max_value = value1;
}
while (calculate_value (ancestor->last) < max_value)
{
/* Find parent node. */
splay (ancestor->first);
ancestor = find_rightmost_node (ancestor->first->left) ->node;
}
return ancestor;
}
/* Return the value pointer of node set during it's creation. */
void *
et_forest_node_value (forest, node)
et_forest_t forest ATTRIBUTE_UNUSED;
et_forest_node_t node;
{
/* Alloc threading NULL as a special node of the forest. */
if (!node)
return NULL;
return node->value;
}
/* Find all sons of NODE and store them into ARRAY allocated by the caller.
Return number of nodes found. */
int
et_forest_enumerate_sons (forest, node, array)
et_forest_t forest ATTRIBUTE_UNUSED;
et_forest_node_t node;
et_forest_node_t *array;
{
int n = 0;
et_forest_occurrence_t occ = node->first, stop = node->last, occ1;
/* Parent is the rightmost node of the left successor.
Look for all occurences having no right succesor
and lookup the sons. */
while (occ != stop)
{
splay (occ);
if (occ->right)
{
occ1 = find_leftmost_node (occ->right);
if (occ1->node->first == occ1)
array[n++] = occ1->node;
}
occ = occ->next;
}
return n;
}
|