... | ... |
@@ -230,1025 +230,1025 @@ |
230 | 230 |
} else { |
231 | 231 |
int idx = firstFreeItem; |
232 | 232 |
firstFreeItem = items[firstFreeItem].next; |
233 | 233 |
return idx; |
234 | 234 |
} |
235 | 235 |
} |
236 | 236 |
|
237 | 237 |
|
238 | 238 |
bool rep(int idx) const { |
239 | 239 |
return items[idx].parent < 0; |
240 | 240 |
} |
241 | 241 |
|
242 | 242 |
int repIndex(int idx) const { |
243 | 243 |
int k = idx; |
244 | 244 |
while (!rep(k)) { |
245 | 245 |
k = items[k].parent; |
246 | 246 |
} |
247 | 247 |
while (idx != k) { |
248 | 248 |
int next = items[idx].parent; |
249 | 249 |
const_cast<int&>(items[idx].parent) = k; |
250 | 250 |
idx = next; |
251 | 251 |
} |
252 | 252 |
return k; |
253 | 253 |
} |
254 | 254 |
|
255 | 255 |
int classIndex(int idx) const { |
256 | 256 |
return ~(items[repIndex(idx)].parent); |
257 | 257 |
} |
258 | 258 |
|
259 | 259 |
void singletonItem(int idx) { |
260 | 260 |
items[idx].next = idx; |
261 | 261 |
items[idx].prev = idx; |
262 | 262 |
} |
263 | 263 |
|
264 | 264 |
void laceItem(int idx, int rdx) { |
265 | 265 |
items[idx].prev = rdx; |
266 | 266 |
items[idx].next = items[rdx].next; |
267 | 267 |
items[items[rdx].next].prev = idx; |
268 | 268 |
items[rdx].next = idx; |
269 | 269 |
} |
270 | 270 |
|
271 | 271 |
void unlaceItem(int idx) { |
272 | 272 |
items[items[idx].prev].next = items[idx].next; |
273 | 273 |
items[items[idx].next].prev = items[idx].prev; |
274 | 274 |
|
275 | 275 |
items[idx].next = firstFreeItem; |
276 | 276 |
firstFreeItem = idx; |
277 | 277 |
} |
278 | 278 |
|
279 | 279 |
void spliceItems(int ak, int bk) { |
280 | 280 |
items[items[ak].prev].next = bk; |
281 | 281 |
items[items[bk].prev].next = ak; |
282 | 282 |
int tmp = items[ak].prev; |
283 | 283 |
items[ak].prev = items[bk].prev; |
284 | 284 |
items[bk].prev = tmp; |
285 | 285 |
|
286 | 286 |
} |
287 | 287 |
|
288 | 288 |
void laceClass(int cls) { |
289 | 289 |
if (firstClass != -1) { |
290 | 290 |
classes[firstClass].prev = cls; |
291 | 291 |
} |
292 | 292 |
classes[cls].next = firstClass; |
293 | 293 |
classes[cls].prev = -1; |
294 | 294 |
firstClass = cls; |
295 | 295 |
} |
296 | 296 |
|
297 | 297 |
void unlaceClass(int cls) { |
298 | 298 |
if (classes[cls].prev != -1) { |
299 | 299 |
classes[classes[cls].prev].next = classes[cls].next; |
300 | 300 |
} else { |
301 | 301 |
firstClass = classes[cls].next; |
302 | 302 |
} |
303 | 303 |
if (classes[cls].next != -1) { |
304 | 304 |
classes[classes[cls].next].prev = classes[cls].prev; |
305 | 305 |
} |
306 | 306 |
|
307 | 307 |
classes[cls].next = firstFreeClass; |
308 | 308 |
firstFreeClass = cls; |
309 | 309 |
} |
310 | 310 |
|
311 | 311 |
public: |
312 | 312 |
|
313 | 313 |
UnionFindEnum(ItemIntMap& _index) |
314 | 314 |
: index(_index), items(), firstFreeItem(-1), |
315 | 315 |
firstClass(-1), firstFreeClass(-1) {} |
316 | 316 |
|
317 | 317 |
/// \brief Inserts the given element into a new component. |
318 | 318 |
/// |
319 | 319 |
/// This method creates a new component consisting only of the |
320 | 320 |
/// given element. |
321 | 321 |
/// |
322 | 322 |
int insert(const Item& item) { |
323 | 323 |
int idx = newItem(); |
324 | 324 |
|
325 | 325 |
index.set(item, idx); |
326 | 326 |
|
327 | 327 |
singletonItem(idx); |
328 | 328 |
items[idx].item = item; |
329 | 329 |
|
330 | 330 |
int cdx = newClass(); |
331 | 331 |
|
332 | 332 |
items[idx].parent = ~cdx; |
333 | 333 |
|
334 | 334 |
laceClass(cdx); |
335 | 335 |
classes[cdx].size = 1; |
336 | 336 |
classes[cdx].firstItem = idx; |
337 | 337 |
|
338 | 338 |
firstClass = cdx; |
339 | 339 |
|
340 | 340 |
return cdx; |
341 | 341 |
} |
342 | 342 |
|
343 | 343 |
/// \brief Inserts the given element into the component of the others. |
344 | 344 |
/// |
345 | 345 |
/// This methods inserts the element \e a into the component of the |
346 | 346 |
/// element \e comp. |
347 | 347 |
void insert(const Item& item, int cls) { |
348 | 348 |
int rdx = classes[cls].firstItem; |
349 | 349 |
int idx = newItem(); |
350 | 350 |
|
351 | 351 |
index.set(item, idx); |
352 | 352 |
|
353 | 353 |
laceItem(idx, rdx); |
354 | 354 |
|
355 | 355 |
items[idx].item = item; |
356 | 356 |
items[idx].parent = rdx; |
357 | 357 |
|
358 | 358 |
++classes[~(items[rdx].parent)].size; |
359 | 359 |
} |
360 | 360 |
|
361 | 361 |
/// \brief Clears the union-find data structure |
362 | 362 |
/// |
363 | 363 |
/// Erase each item from the data structure. |
364 | 364 |
void clear() { |
365 | 365 |
items.clear(); |
366 | 366 |
firstClass = -1; |
367 | 367 |
firstFreeItem = -1; |
368 | 368 |
} |
369 | 369 |
|
370 | 370 |
/// \brief Finds the component of the given element. |
371 | 371 |
/// |
372 | 372 |
/// The method returns the component id of the given element. |
373 | 373 |
int find(const Item &item) const { |
374 | 374 |
return ~(items[repIndex(index[item])].parent); |
375 | 375 |
} |
376 | 376 |
|
377 | 377 |
/// \brief Joining the component of element \e a and element \e b. |
378 | 378 |
/// |
379 | 379 |
/// This is the \e union operation of the Union-Find structure. |
380 | 380 |
/// Joins the component of element \e a and component of |
381 | 381 |
/// element \e b. If \e a and \e b are in the same component then |
382 | 382 |
/// returns -1 else returns the remaining class. |
383 | 383 |
int join(const Item& a, const Item& b) { |
384 | 384 |
|
385 | 385 |
int ak = repIndex(index[a]); |
386 | 386 |
int bk = repIndex(index[b]); |
387 | 387 |
|
388 | 388 |
if (ak == bk) { |
389 | 389 |
return -1; |
390 | 390 |
} |
391 | 391 |
|
392 | 392 |
int acx = ~(items[ak].parent); |
393 | 393 |
int bcx = ~(items[bk].parent); |
394 | 394 |
|
395 | 395 |
int rcx; |
396 | 396 |
|
397 | 397 |
if (classes[acx].size > classes[bcx].size) { |
398 | 398 |
classes[acx].size += classes[bcx].size; |
399 | 399 |
items[bk].parent = ak; |
400 | 400 |
unlaceClass(bcx); |
401 | 401 |
rcx = acx; |
402 | 402 |
} else { |
403 | 403 |
classes[bcx].size += classes[acx].size; |
404 | 404 |
items[ak].parent = bk; |
405 | 405 |
unlaceClass(acx); |
406 | 406 |
rcx = bcx; |
407 | 407 |
} |
408 | 408 |
spliceItems(ak, bk); |
409 | 409 |
|
410 | 410 |
return rcx; |
411 | 411 |
} |
412 | 412 |
|
413 | 413 |
/// \brief Returns the size of the class. |
414 | 414 |
/// |
415 | 415 |
/// Returns the size of the class. |
416 | 416 |
int size(int cls) const { |
417 | 417 |
return classes[cls].size; |
418 | 418 |
} |
419 | 419 |
|
420 | 420 |
/// \brief Splits up the component. |
421 | 421 |
/// |
422 | 422 |
/// Splitting the component into singleton components (component |
423 | 423 |
/// of size one). |
424 | 424 |
void split(int cls) { |
425 | 425 |
int fdx = classes[cls].firstItem; |
426 | 426 |
int idx = items[fdx].next; |
427 | 427 |
while (idx != fdx) { |
428 | 428 |
int next = items[idx].next; |
429 | 429 |
|
430 | 430 |
singletonItem(idx); |
431 | 431 |
|
432 | 432 |
int cdx = newClass(); |
433 | 433 |
items[idx].parent = ~cdx; |
434 | 434 |
|
435 | 435 |
laceClass(cdx); |
436 | 436 |
classes[cdx].size = 1; |
437 | 437 |
classes[cdx].firstItem = idx; |
438 | 438 |
|
439 | 439 |
idx = next; |
440 | 440 |
} |
441 | 441 |
|
442 | 442 |
items[idx].prev = idx; |
443 | 443 |
items[idx].next = idx; |
444 | 444 |
|
445 | 445 |
classes[~(items[idx].parent)].size = 1; |
446 | 446 |
|
447 | 447 |
} |
448 | 448 |
|
449 | 449 |
/// \brief Removes the given element from the structure. |
450 | 450 |
/// |
451 | 451 |
/// Removes the element from its component and if the component becomes |
452 | 452 |
/// empty then removes that component from the component list. |
453 | 453 |
/// |
454 | 454 |
/// \warning It is an error to remove an element which is not in |
455 | 455 |
/// the structure. |
456 | 456 |
/// \warning This running time of this operation is proportional to the |
457 | 457 |
/// number of the items in this class. |
458 | 458 |
void erase(const Item& item) { |
459 | 459 |
int idx = index[item]; |
460 | 460 |
int fdx = items[idx].next; |
461 | 461 |
|
462 | 462 |
int cdx = classIndex(idx); |
463 | 463 |
if (idx == fdx) { |
464 | 464 |
unlaceClass(cdx); |
465 | 465 |
items[idx].next = firstFreeItem; |
466 | 466 |
firstFreeItem = idx; |
467 | 467 |
return; |
468 | 468 |
} else { |
469 | 469 |
classes[cdx].firstItem = fdx; |
470 | 470 |
--classes[cdx].size; |
471 | 471 |
items[fdx].parent = ~cdx; |
472 | 472 |
|
473 | 473 |
unlaceItem(idx); |
474 | 474 |
idx = items[fdx].next; |
475 | 475 |
while (idx != fdx) { |
476 | 476 |
items[idx].parent = fdx; |
477 | 477 |
idx = items[idx].next; |
478 | 478 |
} |
479 | 479 |
|
480 | 480 |
} |
481 | 481 |
|
482 | 482 |
} |
483 | 483 |
|
484 | 484 |
/// \brief Gives back a representant item of the component. |
485 | 485 |
/// |
486 | 486 |
/// Gives back a representant item of the component. |
487 | 487 |
Item item(int cls) const { |
488 | 488 |
return items[classes[cls].firstItem].item; |
489 | 489 |
} |
490 | 490 |
|
491 | 491 |
/// \brief Removes the component of the given element from the structure. |
492 | 492 |
/// |
493 | 493 |
/// Removes the component of the given element from the structure. |
494 | 494 |
/// |
495 | 495 |
/// \warning It is an error to give an element which is not in the |
496 | 496 |
/// structure. |
497 | 497 |
void eraseClass(int cls) { |
498 | 498 |
int fdx = classes[cls].firstItem; |
499 | 499 |
unlaceClass(cls); |
500 | 500 |
items[items[fdx].prev].next = firstFreeItem; |
501 | 501 |
firstFreeItem = fdx; |
502 | 502 |
} |
503 | 503 |
|
504 | 504 |
/// \brief LEMON style iterator for the representant items. |
505 | 505 |
/// |
506 | 506 |
/// ClassIt is a lemon style iterator for the components. It iterates |
507 | 507 |
/// on the ids of the classes. |
508 | 508 |
class ClassIt { |
509 | 509 |
public: |
510 | 510 |
/// \brief Constructor of the iterator |
511 | 511 |
/// |
512 | 512 |
/// Constructor of the iterator |
513 | 513 |
ClassIt(const UnionFindEnum& ufe) : unionFind(&ufe) { |
514 | 514 |
cdx = unionFind->firstClass; |
515 | 515 |
} |
516 | 516 |
|
517 | 517 |
/// \brief Constructor to get invalid iterator |
518 | 518 |
/// |
519 | 519 |
/// Constructor to get invalid iterator |
520 | 520 |
ClassIt(Invalid) : unionFind(0), cdx(-1) {} |
521 | 521 |
|
522 | 522 |
/// \brief Increment operator |
523 | 523 |
/// |
524 | 524 |
/// It steps to the next representant item. |
525 | 525 |
ClassIt& operator++() { |
526 | 526 |
cdx = unionFind->classes[cdx].next; |
527 | 527 |
return *this; |
528 | 528 |
} |
529 | 529 |
|
530 | 530 |
/// \brief Conversion operator |
531 | 531 |
/// |
532 | 532 |
/// It converts the iterator to the current representant item. |
533 | 533 |
operator int() const { |
534 | 534 |
return cdx; |
535 | 535 |
} |
536 | 536 |
|
537 | 537 |
/// \brief Equality operator |
538 | 538 |
/// |
539 | 539 |
/// Equality operator |
540 | 540 |
bool operator==(const ClassIt& i) { |
541 | 541 |
return i.cdx == cdx; |
542 | 542 |
} |
543 | 543 |
|
544 | 544 |
/// \brief Inequality operator |
545 | 545 |
/// |
546 | 546 |
/// Inequality operator |
547 | 547 |
bool operator!=(const ClassIt& i) { |
548 | 548 |
return i.cdx != cdx; |
549 | 549 |
} |
550 | 550 |
|
551 | 551 |
private: |
552 | 552 |
const UnionFindEnum* unionFind; |
553 | 553 |
int cdx; |
554 | 554 |
}; |
555 | 555 |
|
556 | 556 |
/// \brief LEMON style iterator for the items of a component. |
557 | 557 |
/// |
558 | 558 |
/// ClassIt is a lemon style iterator for the components. It iterates |
559 | 559 |
/// on the items of a class. By example if you want to iterate on |
560 | 560 |
/// each items of each classes then you may write the next code. |
561 | 561 |
///\code |
562 | 562 |
/// for (ClassIt cit(ufe); cit != INVALID; ++cit) { |
563 | 563 |
/// std::cout << "Class: "; |
564 | 564 |
/// for (ItemIt iit(ufe, cit); iit != INVALID; ++iit) { |
565 | 565 |
/// std::cout << toString(iit) << ' ' << std::endl; |
566 | 566 |
/// } |
567 | 567 |
/// std::cout << std::endl; |
568 | 568 |
/// } |
569 | 569 |
///\endcode |
570 | 570 |
class ItemIt { |
571 | 571 |
public: |
572 | 572 |
/// \brief Constructor of the iterator |
573 | 573 |
/// |
574 | 574 |
/// Constructor of the iterator. The iterator iterates |
575 | 575 |
/// on the class of the \c item. |
576 | 576 |
ItemIt(const UnionFindEnum& ufe, int cls) : unionFind(&ufe) { |
577 | 577 |
fdx = idx = unionFind->classes[cls].firstItem; |
578 | 578 |
} |
579 | 579 |
|
580 | 580 |
/// \brief Constructor to get invalid iterator |
581 | 581 |
/// |
582 | 582 |
/// Constructor to get invalid iterator |
583 | 583 |
ItemIt(Invalid) : unionFind(0), idx(-1) {} |
584 | 584 |
|
585 | 585 |
/// \brief Increment operator |
586 | 586 |
/// |
587 | 587 |
/// It steps to the next item in the class. |
588 | 588 |
ItemIt& operator++() { |
589 | 589 |
idx = unionFind->items[idx].next; |
590 | 590 |
if (idx == fdx) idx = -1; |
591 | 591 |
return *this; |
592 | 592 |
} |
593 | 593 |
|
594 | 594 |
/// \brief Conversion operator |
595 | 595 |
/// |
596 | 596 |
/// It converts the iterator to the current item. |
597 | 597 |
operator const Item&() const { |
598 | 598 |
return unionFind->items[idx].item; |
599 | 599 |
} |
600 | 600 |
|
601 | 601 |
/// \brief Equality operator |
602 | 602 |
/// |
603 | 603 |
/// Equality operator |
604 | 604 |
bool operator==(const ItemIt& i) { |
605 | 605 |
return i.idx == idx; |
606 | 606 |
} |
607 | 607 |
|
608 | 608 |
/// \brief Inequality operator |
609 | 609 |
/// |
610 | 610 |
/// Inequality operator |
611 | 611 |
bool operator!=(const ItemIt& i) { |
612 | 612 |
return i.idx != idx; |
613 | 613 |
} |
614 | 614 |
|
615 | 615 |
private: |
616 | 616 |
const UnionFindEnum* unionFind; |
617 | 617 |
int idx, fdx; |
618 | 618 |
}; |
619 | 619 |
|
620 | 620 |
}; |
621 | 621 |
|
622 | 622 |
/// \ingroup auxdat |
623 | 623 |
/// |
624 | 624 |
/// \brief A \e Extend-Find data structure implementation which |
625 | 625 |
/// is able to enumerate the components. |
626 | 626 |
/// |
627 | 627 |
/// The class implements an \e Extend-Find data structure which is |
628 | 628 |
/// able to enumerate the components and the items in a |
629 | 629 |
/// component. The data structure is a simplification of the |
630 | 630 |
/// Union-Find structure, and it does not allow to merge two components. |
631 | 631 |
/// |
632 | 632 |
/// \pre You need to add all the elements by the \ref insert() |
633 | 633 |
/// method. |
634 | 634 |
template <typename IM> |
635 | 635 |
class ExtendFindEnum { |
636 | 636 |
public: |
637 | 637 |
|
638 | 638 |
///\e |
639 | 639 |
typedef IM ItemIntMap; |
640 | 640 |
///\e |
641 | 641 |
typedef typename ItemIntMap::Key Item; |
642 | 642 |
|
643 | 643 |
private: |
644 | 644 |
|
645 | 645 |
ItemIntMap& index; |
646 | 646 |
|
647 | 647 |
struct ItemT { |
648 | 648 |
int cls; |
649 | 649 |
Item item; |
650 | 650 |
int next, prev; |
651 | 651 |
}; |
652 | 652 |
|
653 | 653 |
std::vector<ItemT> items; |
654 | 654 |
int firstFreeItem; |
655 | 655 |
|
656 | 656 |
struct ClassT { |
657 | 657 |
int firstItem; |
658 | 658 |
int next, prev; |
659 | 659 |
}; |
660 | 660 |
|
661 | 661 |
std::vector<ClassT> classes; |
662 | 662 |
|
663 | 663 |
int firstClass, firstFreeClass; |
664 | 664 |
|
665 | 665 |
int newClass() { |
666 | 666 |
if (firstFreeClass != -1) { |
667 | 667 |
int cdx = firstFreeClass; |
668 | 668 |
firstFreeClass = classes[cdx].next; |
669 | 669 |
return cdx; |
670 | 670 |
} else { |
671 | 671 |
classes.push_back(ClassT()); |
672 | 672 |
return classes.size() - 1; |
673 | 673 |
} |
674 | 674 |
} |
675 | 675 |
|
676 | 676 |
int newItem() { |
677 | 677 |
if (firstFreeItem != -1) { |
678 | 678 |
int idx = firstFreeItem; |
679 | 679 |
firstFreeItem = items[idx].next; |
680 | 680 |
return idx; |
681 | 681 |
} else { |
682 | 682 |
items.push_back(ItemT()); |
683 | 683 |
return items.size() - 1; |
684 | 684 |
} |
685 | 685 |
} |
686 | 686 |
|
687 | 687 |
public: |
688 | 688 |
|
689 | 689 |
/// \brief Constructor |
690 | 690 |
ExtendFindEnum(ItemIntMap& _index) |
691 | 691 |
: index(_index), items(), firstFreeItem(-1), |
692 | 692 |
classes(), firstClass(-1), firstFreeClass(-1) {} |
693 | 693 |
|
694 | 694 |
/// \brief Inserts the given element into a new component. |
695 | 695 |
/// |
696 | 696 |
/// This method creates a new component consisting only of the |
697 | 697 |
/// given element. |
698 | 698 |
int insert(const Item& item) { |
699 | 699 |
int cdx = newClass(); |
700 | 700 |
classes[cdx].prev = -1; |
701 | 701 |
classes[cdx].next = firstClass; |
702 | 702 |
if (firstClass != -1) { |
703 | 703 |
classes[firstClass].prev = cdx; |
704 | 704 |
} |
705 | 705 |
firstClass = cdx; |
706 | 706 |
|
707 | 707 |
int idx = newItem(); |
708 | 708 |
items[idx].item = item; |
709 | 709 |
items[idx].cls = cdx; |
710 | 710 |
items[idx].prev = idx; |
711 | 711 |
items[idx].next = idx; |
712 | 712 |
|
713 | 713 |
classes[cdx].firstItem = idx; |
714 | 714 |
|
715 | 715 |
index.set(item, idx); |
716 | 716 |
|
717 | 717 |
return cdx; |
718 | 718 |
} |
719 | 719 |
|
720 | 720 |
/// \brief Inserts the given element into the given component. |
721 | 721 |
/// |
722 | 722 |
/// This methods inserts the element \e item a into the \e cls class. |
723 | 723 |
void insert(const Item& item, int cls) { |
724 | 724 |
int idx = newItem(); |
725 | 725 |
int rdx = classes[cls].firstItem; |
726 | 726 |
items[idx].item = item; |
727 | 727 |
items[idx].cls = cls; |
728 | 728 |
|
729 | 729 |
items[idx].prev = rdx; |
730 | 730 |
items[idx].next = items[rdx].next; |
731 | 731 |
items[items[rdx].next].prev = idx; |
732 | 732 |
items[rdx].next = idx; |
733 | 733 |
|
734 | 734 |
index.set(item, idx); |
735 | 735 |
} |
736 | 736 |
|
737 | 737 |
/// \brief Clears the union-find data structure |
738 | 738 |
/// |
739 | 739 |
/// Erase each item from the data structure. |
740 | 740 |
void clear() { |
741 | 741 |
items.clear(); |
742 |
classes.clear; |
|
742 |
classes.clear(); |
|
743 | 743 |
firstClass = firstFreeClass = firstFreeItem = -1; |
744 | 744 |
} |
745 | 745 |
|
746 | 746 |
/// \brief Gives back the class of the \e item. |
747 | 747 |
/// |
748 | 748 |
/// Gives back the class of the \e item. |
749 | 749 |
int find(const Item &item) const { |
750 | 750 |
return items[index[item]].cls; |
751 | 751 |
} |
752 | 752 |
|
753 | 753 |
/// \brief Gives back a representant item of the component. |
754 | 754 |
/// |
755 | 755 |
/// Gives back a representant item of the component. |
756 | 756 |
Item item(int cls) const { |
757 | 757 |
return items[classes[cls].firstItem].item; |
758 | 758 |
} |
759 | 759 |
|
760 | 760 |
/// \brief Removes the given element from the structure. |
761 | 761 |
/// |
762 | 762 |
/// Removes the element from its component and if the component becomes |
763 | 763 |
/// empty then removes that component from the component list. |
764 | 764 |
/// |
765 | 765 |
/// \warning It is an error to remove an element which is not in |
766 | 766 |
/// the structure. |
767 | 767 |
void erase(const Item &item) { |
768 | 768 |
int idx = index[item]; |
769 | 769 |
int cdx = items[idx].cls; |
770 | 770 |
|
771 | 771 |
if (idx == items[idx].next) { |
772 | 772 |
if (classes[cdx].prev != -1) { |
773 | 773 |
classes[classes[cdx].prev].next = classes[cdx].next; |
774 | 774 |
} else { |
775 | 775 |
firstClass = classes[cdx].next; |
776 | 776 |
} |
777 | 777 |
if (classes[cdx].next != -1) { |
778 | 778 |
classes[classes[cdx].next].prev = classes[cdx].prev; |
779 | 779 |
} |
780 | 780 |
classes[cdx].next = firstFreeClass; |
781 | 781 |
firstFreeClass = cdx; |
782 | 782 |
} else { |
783 | 783 |
classes[cdx].firstItem = items[idx].next; |
784 | 784 |
items[items[idx].next].prev = items[idx].prev; |
785 | 785 |
items[items[idx].prev].next = items[idx].next; |
786 | 786 |
} |
787 | 787 |
items[idx].next = firstFreeItem; |
788 | 788 |
firstFreeItem = idx; |
789 | 789 |
|
790 | 790 |
} |
791 | 791 |
|
792 | 792 |
|
793 | 793 |
/// \brief Removes the component of the given element from the structure. |
794 | 794 |
/// |
795 | 795 |
/// Removes the component of the given element from the structure. |
796 | 796 |
/// |
797 | 797 |
/// \warning It is an error to give an element which is not in the |
798 | 798 |
/// structure. |
799 | 799 |
void eraseClass(int cdx) { |
800 | 800 |
int idx = classes[cdx].firstItem; |
801 | 801 |
items[items[idx].prev].next = firstFreeItem; |
802 | 802 |
firstFreeItem = idx; |
803 | 803 |
|
804 | 804 |
if (classes[cdx].prev != -1) { |
805 | 805 |
classes[classes[cdx].prev].next = classes[cdx].next; |
806 | 806 |
} else { |
807 | 807 |
firstClass = classes[cdx].next; |
808 | 808 |
} |
809 | 809 |
if (classes[cdx].next != -1) { |
810 | 810 |
classes[classes[cdx].next].prev = classes[cdx].prev; |
811 | 811 |
} |
812 | 812 |
classes[cdx].next = firstFreeClass; |
813 | 813 |
firstFreeClass = cdx; |
814 | 814 |
} |
815 | 815 |
|
816 | 816 |
/// \brief LEMON style iterator for the classes. |
817 | 817 |
/// |
818 | 818 |
/// ClassIt is a lemon style iterator for the components. It iterates |
819 | 819 |
/// on the ids of classes. |
820 | 820 |
class ClassIt { |
821 | 821 |
public: |
822 | 822 |
/// \brief Constructor of the iterator |
823 | 823 |
/// |
824 | 824 |
/// Constructor of the iterator |
825 | 825 |
ClassIt(const ExtendFindEnum& ufe) : extendFind(&ufe) { |
826 | 826 |
cdx = extendFind->firstClass; |
827 | 827 |
} |
828 | 828 |
|
829 | 829 |
/// \brief Constructor to get invalid iterator |
830 | 830 |
/// |
831 | 831 |
/// Constructor to get invalid iterator |
832 | 832 |
ClassIt(Invalid) : extendFind(0), cdx(-1) {} |
833 | 833 |
|
834 | 834 |
/// \brief Increment operator |
835 | 835 |
/// |
836 | 836 |
/// It steps to the next representant item. |
837 | 837 |
ClassIt& operator++() { |
838 | 838 |
cdx = extendFind->classes[cdx].next; |
839 | 839 |
return *this; |
840 | 840 |
} |
841 | 841 |
|
842 | 842 |
/// \brief Conversion operator |
843 | 843 |
/// |
844 | 844 |
/// It converts the iterator to the current class id. |
845 | 845 |
operator int() const { |
846 | 846 |
return cdx; |
847 | 847 |
} |
848 | 848 |
|
849 | 849 |
/// \brief Equality operator |
850 | 850 |
/// |
851 | 851 |
/// Equality operator |
852 | 852 |
bool operator==(const ClassIt& i) { |
853 | 853 |
return i.cdx == cdx; |
854 | 854 |
} |
855 | 855 |
|
856 | 856 |
/// \brief Inequality operator |
857 | 857 |
/// |
858 | 858 |
/// Inequality operator |
859 | 859 |
bool operator!=(const ClassIt& i) { |
860 | 860 |
return i.cdx != cdx; |
861 | 861 |
} |
862 | 862 |
|
863 | 863 |
private: |
864 | 864 |
const ExtendFindEnum* extendFind; |
865 | 865 |
int cdx; |
866 | 866 |
}; |
867 | 867 |
|
868 | 868 |
/// \brief LEMON style iterator for the items of a component. |
869 | 869 |
/// |
870 | 870 |
/// ClassIt is a lemon style iterator for the components. It iterates |
871 | 871 |
/// on the items of a class. By example if you want to iterate on |
872 | 872 |
/// each items of each classes then you may write the next code. |
873 | 873 |
///\code |
874 | 874 |
/// for (ClassIt cit(ufe); cit != INVALID; ++cit) { |
875 | 875 |
/// std::cout << "Class: "; |
876 | 876 |
/// for (ItemIt iit(ufe, cit); iit != INVALID; ++iit) { |
877 | 877 |
/// std::cout << toString(iit) << ' ' << std::endl; |
878 | 878 |
/// } |
879 | 879 |
/// std::cout << std::endl; |
880 | 880 |
/// } |
881 | 881 |
///\endcode |
882 | 882 |
class ItemIt { |
883 | 883 |
public: |
884 | 884 |
/// \brief Constructor of the iterator |
885 | 885 |
/// |
886 | 886 |
/// Constructor of the iterator. The iterator iterates |
887 | 887 |
/// on the class of the \c item. |
888 | 888 |
ItemIt(const ExtendFindEnum& ufe, int cls) : extendFind(&ufe) { |
889 | 889 |
fdx = idx = extendFind->classes[cls].firstItem; |
890 | 890 |
} |
891 | 891 |
|
892 | 892 |
/// \brief Constructor to get invalid iterator |
893 | 893 |
/// |
894 | 894 |
/// Constructor to get invalid iterator |
895 | 895 |
ItemIt(Invalid) : extendFind(0), idx(-1) {} |
896 | 896 |
|
897 | 897 |
/// \brief Increment operator |
898 | 898 |
/// |
899 | 899 |
/// It steps to the next item in the class. |
900 | 900 |
ItemIt& operator++() { |
901 | 901 |
idx = extendFind->items[idx].next; |
902 | 902 |
if (fdx == idx) idx = -1; |
903 | 903 |
return *this; |
904 | 904 |
} |
905 | 905 |
|
906 | 906 |
/// \brief Conversion operator |
907 | 907 |
/// |
908 | 908 |
/// It converts the iterator to the current item. |
909 | 909 |
operator const Item&() const { |
910 | 910 |
return extendFind->items[idx].item; |
911 | 911 |
} |
912 | 912 |
|
913 | 913 |
/// \brief Equality operator |
914 | 914 |
/// |
915 | 915 |
/// Equality operator |
916 | 916 |
bool operator==(const ItemIt& i) { |
917 | 917 |
return i.idx == idx; |
918 | 918 |
} |
919 | 919 |
|
920 | 920 |
/// \brief Inequality operator |
921 | 921 |
/// |
922 | 922 |
/// Inequality operator |
923 | 923 |
bool operator!=(const ItemIt& i) { |
924 | 924 |
return i.idx != idx; |
925 | 925 |
} |
926 | 926 |
|
927 | 927 |
private: |
928 | 928 |
const ExtendFindEnum* extendFind; |
929 | 929 |
int idx, fdx; |
930 | 930 |
}; |
931 | 931 |
|
932 | 932 |
}; |
933 | 933 |
|
934 | 934 |
/// \ingroup auxdat |
935 | 935 |
/// |
936 | 936 |
/// \brief A \e Union-Find data structure implementation which |
937 | 937 |
/// is able to store a priority for each item and retrieve the minimum of |
938 | 938 |
/// each class. |
939 | 939 |
/// |
940 | 940 |
/// A \e Union-Find data structure implementation which is able to |
941 | 941 |
/// store a priority for each item and retrieve the minimum of each |
942 | 942 |
/// class. In addition, it supports the joining and splitting the |
943 | 943 |
/// components. If you don't need this feature then you makes |
944 | 944 |
/// better to use the \ref UnionFind class which is more efficient. |
945 | 945 |
/// |
946 | 946 |
/// The union-find data strcuture based on a (2, 16)-tree with a |
947 | 947 |
/// tournament minimum selection on the internal nodes. The insert |
948 | 948 |
/// operation takes O(1), the find, set, decrease and increase takes |
949 | 949 |
/// O(log(n)), where n is the number of nodes in the current |
950 | 950 |
/// component. The complexity of join and split is O(log(n)*k), |
951 | 951 |
/// where n is the sum of the number of the nodes and k is the |
952 | 952 |
/// number of joined components or the number of the components |
953 | 953 |
/// after the split. |
954 | 954 |
/// |
955 | 955 |
/// \pre You need to add all the elements by the \ref insert() |
956 | 956 |
/// method. |
957 | 957 |
template <typename V, typename IM, typename Comp = std::less<V> > |
958 | 958 |
class HeapUnionFind { |
959 | 959 |
public: |
960 | 960 |
|
961 | 961 |
///\e |
962 | 962 |
typedef V Value; |
963 | 963 |
///\e |
964 | 964 |
typedef typename IM::Key Item; |
965 | 965 |
///\e |
966 | 966 |
typedef IM ItemIntMap; |
967 | 967 |
///\e |
968 | 968 |
typedef Comp Compare; |
969 | 969 |
|
970 | 970 |
private: |
971 | 971 |
|
972 | 972 |
static const int cmax = 16; |
973 | 973 |
|
974 | 974 |
ItemIntMap& index; |
975 | 975 |
|
976 | 976 |
struct ClassNode { |
977 | 977 |
int parent; |
978 | 978 |
int depth; |
979 | 979 |
|
980 | 980 |
int left, right; |
981 | 981 |
int next, prev; |
982 | 982 |
}; |
983 | 983 |
|
984 | 984 |
int first_class; |
985 | 985 |
int first_free_class; |
986 | 986 |
std::vector<ClassNode> classes; |
987 | 987 |
|
988 | 988 |
int newClass() { |
989 | 989 |
if (first_free_class < 0) { |
990 | 990 |
int id = classes.size(); |
991 | 991 |
classes.push_back(ClassNode()); |
992 | 992 |
return id; |
993 | 993 |
} else { |
994 | 994 |
int id = first_free_class; |
995 | 995 |
first_free_class = classes[id].next; |
996 | 996 |
return id; |
997 | 997 |
} |
998 | 998 |
} |
999 | 999 |
|
1000 | 1000 |
void deleteClass(int id) { |
1001 | 1001 |
classes[id].next = first_free_class; |
1002 | 1002 |
first_free_class = id; |
1003 | 1003 |
} |
1004 | 1004 |
|
1005 | 1005 |
struct ItemNode { |
1006 | 1006 |
int parent; |
1007 | 1007 |
Item item; |
1008 | 1008 |
Value prio; |
1009 | 1009 |
int next, prev; |
1010 | 1010 |
int left, right; |
1011 | 1011 |
int size; |
1012 | 1012 |
}; |
1013 | 1013 |
|
1014 | 1014 |
int first_free_node; |
1015 | 1015 |
std::vector<ItemNode> nodes; |
1016 | 1016 |
|
1017 | 1017 |
int newNode() { |
1018 | 1018 |
if (first_free_node < 0) { |
1019 | 1019 |
int id = nodes.size(); |
1020 | 1020 |
nodes.push_back(ItemNode()); |
1021 | 1021 |
return id; |
1022 | 1022 |
} else { |
1023 | 1023 |
int id = first_free_node; |
1024 | 1024 |
first_free_node = nodes[id].next; |
1025 | 1025 |
return id; |
1026 | 1026 |
} |
1027 | 1027 |
} |
1028 | 1028 |
|
1029 | 1029 |
void deleteNode(int id) { |
1030 | 1030 |
nodes[id].next = first_free_node; |
1031 | 1031 |
first_free_node = id; |
1032 | 1032 |
} |
1033 | 1033 |
|
1034 | 1034 |
Comp comp; |
1035 | 1035 |
|
1036 | 1036 |
int findClass(int id) const { |
1037 | 1037 |
int kd = id; |
1038 | 1038 |
while (kd >= 0) { |
1039 | 1039 |
kd = nodes[kd].parent; |
1040 | 1040 |
} |
1041 | 1041 |
return ~kd; |
1042 | 1042 |
} |
1043 | 1043 |
|
1044 | 1044 |
int leftNode(int id) const { |
1045 | 1045 |
int kd = ~(classes[id].parent); |
1046 | 1046 |
for (int i = 0; i < classes[id].depth; ++i) { |
1047 | 1047 |
kd = nodes[kd].left; |
1048 | 1048 |
} |
1049 | 1049 |
return kd; |
1050 | 1050 |
} |
1051 | 1051 |
|
1052 | 1052 |
int nextNode(int id) const { |
1053 | 1053 |
int depth = 0; |
1054 | 1054 |
while (id >= 0 && nodes[id].next == -1) { |
1055 | 1055 |
id = nodes[id].parent; |
1056 | 1056 |
++depth; |
1057 | 1057 |
} |
1058 | 1058 |
if (id < 0) { |
1059 | 1059 |
return -1; |
1060 | 1060 |
} |
1061 | 1061 |
id = nodes[id].next; |
1062 | 1062 |
while (depth--) { |
1063 | 1063 |
id = nodes[id].left; |
1064 | 1064 |
} |
1065 | 1065 |
return id; |
1066 | 1066 |
} |
1067 | 1067 |
|
1068 | 1068 |
|
1069 | 1069 |
void setPrio(int id) { |
1070 | 1070 |
int jd = nodes[id].left; |
1071 | 1071 |
nodes[id].prio = nodes[jd].prio; |
1072 | 1072 |
nodes[id].item = nodes[jd].item; |
1073 | 1073 |
jd = nodes[jd].next; |
1074 | 1074 |
while (jd != -1) { |
1075 | 1075 |
if (comp(nodes[jd].prio, nodes[id].prio)) { |
1076 | 1076 |
nodes[id].prio = nodes[jd].prio; |
1077 | 1077 |
nodes[id].item = nodes[jd].item; |
1078 | 1078 |
} |
1079 | 1079 |
jd = nodes[jd].next; |
1080 | 1080 |
} |
1081 | 1081 |
} |
1082 | 1082 |
|
1083 | 1083 |
void push(int id, int jd) { |
1084 | 1084 |
nodes[id].size = 1; |
1085 | 1085 |
nodes[id].left = nodes[id].right = jd; |
1086 | 1086 |
nodes[jd].next = nodes[jd].prev = -1; |
1087 | 1087 |
nodes[jd].parent = id; |
1088 | 1088 |
} |
1089 | 1089 |
|
1090 | 1090 |
void pushAfter(int id, int jd) { |
1091 | 1091 |
int kd = nodes[id].parent; |
1092 | 1092 |
if (nodes[id].next != -1) { |
1093 | 1093 |
nodes[nodes[id].next].prev = jd; |
1094 | 1094 |
if (kd >= 0) { |
1095 | 1095 |
nodes[kd].size += 1; |
1096 | 1096 |
} |
1097 | 1097 |
} else { |
1098 | 1098 |
if (kd >= 0) { |
1099 | 1099 |
nodes[kd].right = jd; |
1100 | 1100 |
nodes[kd].size += 1; |
1101 | 1101 |
} |
1102 | 1102 |
} |
1103 | 1103 |
nodes[jd].next = nodes[id].next; |
1104 | 1104 |
nodes[jd].prev = id; |
1105 | 1105 |
nodes[id].next = jd; |
1106 | 1106 |
nodes[jd].parent = kd; |
1107 | 1107 |
} |
1108 | 1108 |
|
1109 | 1109 |
void pushRight(int id, int jd) { |
1110 | 1110 |
nodes[id].size += 1; |
1111 | 1111 |
nodes[jd].prev = nodes[id].right; |
1112 | 1112 |
nodes[jd].next = -1; |
1113 | 1113 |
nodes[nodes[id].right].next = jd; |
1114 | 1114 |
nodes[id].right = jd; |
1115 | 1115 |
nodes[jd].parent = id; |
1116 | 1116 |
} |
1117 | 1117 |
|
1118 | 1118 |
void popRight(int id) { |
1119 | 1119 |
nodes[id].size -= 1; |
1120 | 1120 |
int jd = nodes[id].right; |
1121 | 1121 |
nodes[nodes[jd].prev].next = -1; |
1122 | 1122 |
nodes[id].right = nodes[jd].prev; |
1123 | 1123 |
} |
1124 | 1124 |
|
1125 | 1125 |
void splice(int id, int jd) { |
1126 | 1126 |
nodes[id].size += nodes[jd].size; |
1127 | 1127 |
nodes[nodes[id].right].next = nodes[jd].left; |
1128 | 1128 |
nodes[nodes[jd].left].prev = nodes[id].right; |
1129 | 1129 |
int kd = nodes[jd].left; |
1130 | 1130 |
while (kd != -1) { |
1131 | 1131 |
nodes[kd].parent = id; |
1132 | 1132 |
kd = nodes[kd].next; |
1133 | 1133 |
} |
1134 | 1134 |
nodes[id].right = nodes[jd].right; |
1135 | 1135 |
} |
1136 | 1136 |
|
1137 | 1137 |
void split(int id, int jd) { |
1138 | 1138 |
int kd = nodes[id].parent; |
1139 | 1139 |
nodes[kd].right = nodes[id].prev; |
1140 | 1140 |
nodes[nodes[id].prev].next = -1; |
1141 | 1141 |
|
1142 | 1142 |
nodes[jd].left = id; |
1143 | 1143 |
nodes[id].prev = -1; |
1144 | 1144 |
int num = 0; |
1145 | 1145 |
while (id != -1) { |
1146 | 1146 |
nodes[id].parent = jd; |
1147 | 1147 |
nodes[jd].right = id; |
1148 | 1148 |
id = nodes[id].next; |
1149 | 1149 |
++num; |
1150 | 1150 |
} |
1151 | 1151 |
nodes[kd].size -= num; |
1152 | 1152 |
nodes[jd].size = num; |
1153 | 1153 |
} |
1154 | 1154 |
|
1155 | 1155 |
void pushLeft(int id, int jd) { |
1156 | 1156 |
nodes[id].size += 1; |
1157 | 1157 |
nodes[jd].next = nodes[id].left; |
1158 | 1158 |
nodes[jd].prev = -1; |
1159 | 1159 |
nodes[nodes[id].left].prev = jd; |
1160 | 1160 |
nodes[id].left = jd; |
1161 | 1161 |
nodes[jd].parent = id; |
1162 | 1162 |
} |
1163 | 1163 |
|
1164 | 1164 |
void popLeft(int id) { |
1165 | 1165 |
nodes[id].size -= 1; |
1166 | 1166 |
int jd = nodes[id].left; |
1167 | 1167 |
nodes[nodes[jd].next].prev = -1; |
1168 | 1168 |
nodes[id].left = nodes[jd].next; |
1169 | 1169 |
} |
1170 | 1170 |
|
1171 | 1171 |
void repairLeft(int id) { |
1172 | 1172 |
int jd = ~(classes[id].parent); |
1173 | 1173 |
while (nodes[jd].left != -1) { |
1174 | 1174 |
int kd = nodes[jd].left; |
1175 | 1175 |
if (nodes[jd].size == 1) { |
1176 | 1176 |
if (nodes[jd].parent < 0) { |
1177 | 1177 |
classes[id].parent = ~kd; |
1178 | 1178 |
classes[id].depth -= 1; |
1179 | 1179 |
nodes[kd].parent = ~id; |
1180 | 1180 |
deleteNode(jd); |
1181 | 1181 |
jd = kd; |
1182 | 1182 |
} else { |
1183 | 1183 |
int pd = nodes[jd].parent; |
1184 | 1184 |
if (nodes[nodes[jd].next].size < cmax) { |
1185 | 1185 |
pushLeft(nodes[jd].next, nodes[jd].left); |
1186 | 1186 |
if (less(jd, nodes[jd].next) || |
1187 | 1187 |
nodes[jd].item == nodes[pd].item) { |
1188 | 1188 |
nodes[nodes[jd].next].prio = nodes[jd].prio; |
1189 | 1189 |
nodes[nodes[jd].next].item = nodes[jd].item; |
1190 | 1190 |
} |
1191 | 1191 |
popLeft(pd); |
1192 | 1192 |
deleteNode(jd); |
1193 | 1193 |
jd = pd; |
1194 | 1194 |
} else { |
1195 | 1195 |
int ld = nodes[nodes[jd].next].left; |
1196 | 1196 |
popLeft(nodes[jd].next); |
1197 | 1197 |
pushRight(jd, ld); |
1198 | 1198 |
if (less(ld, nodes[jd].left) || |
1199 | 1199 |
nodes[ld].item == nodes[pd].item) { |
1200 | 1200 |
nodes[jd].item = nodes[ld].item; |
1201 | 1201 |
nodes[jd].prio = nodes[ld].prio; |
1202 | 1202 |
} |
1203 | 1203 |
if (nodes[nodes[jd].next].item == nodes[ld].item) { |
1204 | 1204 |
setPrio(nodes[jd].next); |
1205 | 1205 |
} |
1206 | 1206 |
jd = nodes[jd].left; |
1207 | 1207 |
} |
1208 | 1208 |
} |
1209 | 1209 |
} else { |
1210 | 1210 |
jd = nodes[jd].left; |
1211 | 1211 |
} |
1212 | 1212 |
} |
1213 | 1213 |
} |
1214 | 1214 |
|
1215 | 1215 |
void repairRight(int id) { |
1216 | 1216 |
int jd = ~(classes[id].parent); |
1217 | 1217 |
while (nodes[jd].right != -1) { |
1218 | 1218 |
int kd = nodes[jd].right; |
1219 | 1219 |
if (nodes[jd].size == 1) { |
1220 | 1220 |
if (nodes[jd].parent < 0) { |
1221 | 1221 |
classes[id].parent = ~kd; |
1222 | 1222 |
classes[id].depth -= 1; |
1223 | 1223 |
nodes[kd].parent = ~id; |
1224 | 1224 |
deleteNode(jd); |
1225 | 1225 |
jd = kd; |
1226 | 1226 |
} else { |
1227 | 1227 |
int pd = nodes[jd].parent; |
1228 | 1228 |
if (nodes[nodes[jd].prev].size < cmax) { |
1229 | 1229 |
pushRight(nodes[jd].prev, nodes[jd].right); |
1230 | 1230 |
if (less(jd, nodes[jd].prev) || |
1231 | 1231 |
nodes[jd].item == nodes[pd].item) { |
1232 | 1232 |
nodes[nodes[jd].prev].prio = nodes[jd].prio; |
1233 | 1233 |
nodes[nodes[jd].prev].item = nodes[jd].item; |
1234 | 1234 |
} |
1235 | 1235 |
popRight(pd); |
1236 | 1236 |
deleteNode(jd); |
1237 | 1237 |
jd = pd; |
1238 | 1238 |
} else { |
1239 | 1239 |
int ld = nodes[nodes[jd].prev].right; |
1240 | 1240 |
popRight(nodes[jd].prev); |
1241 | 1241 |
pushLeft(jd, ld); |
1242 | 1242 |
if (less(ld, nodes[jd].right) || |
1243 | 1243 |
nodes[ld].item == nodes[pd].item) { |
1244 | 1244 |
nodes[jd].item = nodes[ld].item; |
1245 | 1245 |
nodes[jd].prio = nodes[ld].prio; |
1246 | 1246 |
} |
1247 | 1247 |
if (nodes[nodes[jd].prev].item == nodes[ld].item) { |
1248 | 1248 |
setPrio(nodes[jd].prev); |
1249 | 1249 |
} |
1250 | 1250 |
jd = nodes[jd].right; |
1251 | 1251 |
} |
1252 | 1252 |
} |
1253 | 1253 |
} else { |
1254 | 1254 |
jd = nodes[jd].right; |
0 comments (0 inline)