gravatar
deba@inf.elte.hu
deba@inf.elte.hu
Fix clear() function in ExtendFindEnum (#335), backport of [28c7ad6f8d91]
0 1 0
1.0
1 file changed with 1 insertions and 1 deletions:
↑ Collapse diff ↑
Ignore white space 768 line context
... ...
@@ -352,769 +352,769 @@
352 352
      items[idx].parent = rdx;
353 353

	
354 354
      ++classes[~(items[rdx].parent)].size;
355 355
    }
356 356

	
357 357
    /// \brief Clears the union-find data structure
358 358
    ///
359 359
    /// Erase each item from the data structure.
360 360
    void clear() {
361 361
      items.clear();
362 362
      firstClass = -1;
363 363
      firstFreeItem = -1;
364 364
    }
365 365

	
366 366
    /// \brief Finds the component of the given element.
367 367
    ///
368 368
    /// The method returns the component id of the given element.
369 369
    int find(const Item &item) const {
370 370
      return ~(items[repIndex(index[item])].parent);
371 371
    }
372 372

	
373 373
    /// \brief Joining the component of element \e a and element \e b.
374 374
    ///
375 375
    /// This is the \e union operation of the Union-Find structure.
376 376
    /// Joins the component of element \e a and component of
377 377
    /// element \e b. If \e a and \e b are in the same component then
378 378
    /// returns -1 else returns the remaining class.
379 379
    int join(const Item& a, const Item& b) {
380 380

	
381 381
      int ak = repIndex(index[a]);
382 382
      int bk = repIndex(index[b]);
383 383

	
384 384
      if (ak == bk) {
385 385
        return -1;
386 386
      }
387 387

	
388 388
      int acx = ~(items[ak].parent);
389 389
      int bcx = ~(items[bk].parent);
390 390

	
391 391
      int rcx;
392 392

	
393 393
      if (classes[acx].size > classes[bcx].size) {
394 394
        classes[acx].size += classes[bcx].size;
395 395
        items[bk].parent = ak;
396 396
        unlaceClass(bcx);
397 397
        rcx = acx;
398 398
      } else {
399 399
        classes[bcx].size += classes[acx].size;
400 400
        items[ak].parent = bk;
401 401
        unlaceClass(acx);
402 402
        rcx = bcx;
403 403
      }
404 404
      spliceItems(ak, bk);
405 405

	
406 406
      return rcx;
407 407
    }
408 408

	
409 409
    /// \brief Returns the size of the class.
410 410
    ///
411 411
    /// Returns the size of the class.
412 412
    int size(int cls) const {
413 413
      return classes[cls].size;
414 414
    }
415 415

	
416 416
    /// \brief Splits up the component.
417 417
    ///
418 418
    /// Splitting the component into singleton components (component
419 419
    /// of size one).
420 420
    void split(int cls) {
421 421
      int fdx = classes[cls].firstItem;
422 422
      int idx = items[fdx].next;
423 423
      while (idx != fdx) {
424 424
        int next = items[idx].next;
425 425

	
426 426
        singletonItem(idx);
427 427

	
428 428
        int cdx = newClass();
429 429
        items[idx].parent = ~cdx;
430 430

	
431 431
        laceClass(cdx);
432 432
        classes[cdx].size = 1;
433 433
        classes[cdx].firstItem = idx;
434 434

	
435 435
        idx = next;
436 436
      }
437 437

	
438 438
      items[idx].prev = idx;
439 439
      items[idx].next = idx;
440 440

	
441 441
      classes[~(items[idx].parent)].size = 1;
442 442

	
443 443
    }
444 444

	
445 445
    /// \brief Removes the given element from the structure.
446 446
    ///
447 447
    /// Removes the element from its component and if the component becomes
448 448
    /// empty then removes that component from the component list.
449 449
    ///
450 450
    /// \warning It is an error to remove an element which is not in
451 451
    /// the structure.
452 452
    /// \warning This running time of this operation is proportional to the
453 453
    /// number of the items in this class.
454 454
    void erase(const Item& item) {
455 455
      int idx = index[item];
456 456
      int fdx = items[idx].next;
457 457

	
458 458
      int cdx = classIndex(idx);
459 459
      if (idx == fdx) {
460 460
        unlaceClass(cdx);
461 461
        items[idx].next = firstFreeItem;
462 462
        firstFreeItem = idx;
463 463
        return;
464 464
      } else {
465 465
        classes[cdx].firstItem = fdx;
466 466
        --classes[cdx].size;
467 467
        items[fdx].parent = ~cdx;
468 468

	
469 469
        unlaceItem(idx);
470 470
        idx = items[fdx].next;
471 471
        while (idx != fdx) {
472 472
          items[idx].parent = fdx;
473 473
          idx = items[idx].next;
474 474
        }
475 475

	
476 476
      }
477 477

	
478 478
    }
479 479

	
480 480
    /// \brief Gives back a representant item of the component.
481 481
    ///
482 482
    /// Gives back a representant item of the component.
483 483
    Item item(int cls) const {
484 484
      return items[classes[cls].firstItem].item;
485 485
    }
486 486

	
487 487
    /// \brief Removes the component of the given element from the structure.
488 488
    ///
489 489
    /// Removes the component of the given element from the structure.
490 490
    ///
491 491
    /// \warning It is an error to give an element which is not in the
492 492
    /// structure.
493 493
    void eraseClass(int cls) {
494 494
      int fdx = classes[cls].firstItem;
495 495
      unlaceClass(cls);
496 496
      items[items[fdx].prev].next = firstFreeItem;
497 497
      firstFreeItem = fdx;
498 498
    }
499 499

	
500 500
    /// \brief LEMON style iterator for the representant items.
501 501
    ///
502 502
    /// ClassIt is a lemon style iterator for the components. It iterates
503 503
    /// on the ids of the classes.
504 504
    class ClassIt {
505 505
    public:
506 506
      /// \brief Constructor of the iterator
507 507
      ///
508 508
      /// Constructor of the iterator
509 509
      ClassIt(const UnionFindEnum& ufe) : unionFind(&ufe) {
510 510
        cdx = unionFind->firstClass;
511 511
      }
512 512

	
513 513
      /// \brief Constructor to get invalid iterator
514 514
      ///
515 515
      /// Constructor to get invalid iterator
516 516
      ClassIt(Invalid) : unionFind(0), cdx(-1) {}
517 517

	
518 518
      /// \brief Increment operator
519 519
      ///
520 520
      /// It steps to the next representant item.
521 521
      ClassIt& operator++() {
522 522
        cdx = unionFind->classes[cdx].next;
523 523
        return *this;
524 524
      }
525 525

	
526 526
      /// \brief Conversion operator
527 527
      ///
528 528
      /// It converts the iterator to the current representant item.
529 529
      operator int() const {
530 530
        return cdx;
531 531
      }
532 532

	
533 533
      /// \brief Equality operator
534 534
      ///
535 535
      /// Equality operator
536 536
      bool operator==(const ClassIt& i) {
537 537
        return i.cdx == cdx;
538 538
      }
539 539

	
540 540
      /// \brief Inequality operator
541 541
      ///
542 542
      /// Inequality operator
543 543
      bool operator!=(const ClassIt& i) {
544 544
        return i.cdx != cdx;
545 545
      }
546 546

	
547 547
    private:
548 548
      const UnionFindEnum* unionFind;
549 549
      int cdx;
550 550
    };
551 551

	
552 552
    /// \brief LEMON style iterator for the items of a component.
553 553
    ///
554 554
    /// ClassIt is a lemon style iterator for the components. It iterates
555 555
    /// on the items of a class. By example if you want to iterate on
556 556
    /// each items of each classes then you may write the next code.
557 557
    ///\code
558 558
    /// for (ClassIt cit(ufe); cit != INVALID; ++cit) {
559 559
    ///   std::cout << "Class: ";
560 560
    ///   for (ItemIt iit(ufe, cit); iit != INVALID; ++iit) {
561 561
    ///     std::cout << toString(iit) << ' ' << std::endl;
562 562
    ///   }
563 563
    ///   std::cout << std::endl;
564 564
    /// }
565 565
    ///\endcode
566 566
    class ItemIt {
567 567
    public:
568 568
      /// \brief Constructor of the iterator
569 569
      ///
570 570
      /// Constructor of the iterator. The iterator iterates
571 571
      /// on the class of the \c item.
572 572
      ItemIt(const UnionFindEnum& ufe, int cls) : unionFind(&ufe) {
573 573
        fdx = idx = unionFind->classes[cls].firstItem;
574 574
      }
575 575

	
576 576
      /// \brief Constructor to get invalid iterator
577 577
      ///
578 578
      /// Constructor to get invalid iterator
579 579
      ItemIt(Invalid) : unionFind(0), idx(-1) {}
580 580

	
581 581
      /// \brief Increment operator
582 582
      ///
583 583
      /// It steps to the next item in the class.
584 584
      ItemIt& operator++() {
585 585
        idx = unionFind->items[idx].next;
586 586
        if (idx == fdx) idx = -1;
587 587
        return *this;
588 588
      }
589 589

	
590 590
      /// \brief Conversion operator
591 591
      ///
592 592
      /// It converts the iterator to the current item.
593 593
      operator const Item&() const {
594 594
        return unionFind->items[idx].item;
595 595
      }
596 596

	
597 597
      /// \brief Equality operator
598 598
      ///
599 599
      /// Equality operator
600 600
      bool operator==(const ItemIt& i) {
601 601
        return i.idx == idx;
602 602
      }
603 603

	
604 604
      /// \brief Inequality operator
605 605
      ///
606 606
      /// Inequality operator
607 607
      bool operator!=(const ItemIt& i) {
608 608
        return i.idx != idx;
609 609
      }
610 610

	
611 611
    private:
612 612
      const UnionFindEnum* unionFind;
613 613
      int idx, fdx;
614 614
    };
615 615

	
616 616
  };
617 617

	
618 618
  /// \ingroup auxdat
619 619
  ///
620 620
  /// \brief A \e Extend-Find data structure implementation which
621 621
  /// is able to enumerate the components.
622 622
  ///
623 623
  /// The class implements an \e Extend-Find data structure which is
624 624
  /// able to enumerate the components and the items in a
625 625
  /// component. The data structure is a simplification of the
626 626
  /// Union-Find structure, and it does not allow to merge two components.
627 627
  ///
628 628
  /// \pre You need to add all the elements by the \ref insert()
629 629
  /// method.
630 630
  template <typename _ItemIntMap>
631 631
  class ExtendFindEnum {
632 632
  public:
633 633

	
634 634
    typedef _ItemIntMap ItemIntMap;
635 635
    typedef typename ItemIntMap::Key Item;
636 636

	
637 637
  private:
638 638

	
639 639
    ItemIntMap& index;
640 640

	
641 641
    struct ItemT {
642 642
      int cls;
643 643
      Item item;
644 644
      int next, prev;
645 645
    };
646 646

	
647 647
    std::vector<ItemT> items;
648 648
    int firstFreeItem;
649 649

	
650 650
    struct ClassT {
651 651
      int firstItem;
652 652
      int next, prev;
653 653
    };
654 654

	
655 655
    std::vector<ClassT> classes;
656 656

	
657 657
    int firstClass, firstFreeClass;
658 658

	
659 659
    int newClass() {
660 660
      if (firstFreeClass != -1) {
661 661
        int cdx = firstFreeClass;
662 662
        firstFreeClass = classes[cdx].next;
663 663
        return cdx;
664 664
      } else {
665 665
        classes.push_back(ClassT());
666 666
        return classes.size() - 1;
667 667
      }
668 668
    }
669 669

	
670 670
    int newItem() {
671 671
      if (firstFreeItem != -1) {
672 672
        int idx = firstFreeItem;
673 673
        firstFreeItem = items[idx].next;
674 674
        return idx;
675 675
      } else {
676 676
        items.push_back(ItemT());
677 677
        return items.size() - 1;
678 678
      }
679 679
    }
680 680

	
681 681
  public:
682 682

	
683 683
    /// \brief Constructor
684 684
    ExtendFindEnum(ItemIntMap& _index)
685 685
      : index(_index), items(), firstFreeItem(-1),
686 686
        classes(), firstClass(-1), firstFreeClass(-1) {}
687 687

	
688 688
    /// \brief Inserts the given element into a new component.
689 689
    ///
690 690
    /// This method creates a new component consisting only of the
691 691
    /// given element.
692 692
    int insert(const Item& item) {
693 693
      int cdx = newClass();
694 694
      classes[cdx].prev = -1;
695 695
      classes[cdx].next = firstClass;
696 696
      if (firstClass != -1) {
697 697
        classes[firstClass].prev = cdx;
698 698
      }
699 699
      firstClass = cdx;
700 700

	
701 701
      int idx = newItem();
702 702
      items[idx].item = item;
703 703
      items[idx].cls = cdx;
704 704
      items[idx].prev = idx;
705 705
      items[idx].next = idx;
706 706

	
707 707
      classes[cdx].firstItem = idx;
708 708

	
709 709
      index.set(item, idx);
710 710

	
711 711
      return cdx;
712 712
    }
713 713

	
714 714
    /// \brief Inserts the given element into the given component.
715 715
    ///
716 716
    /// This methods inserts the element \e item a into the \e cls class.
717 717
    void insert(const Item& item, int cls) {
718 718
      int idx = newItem();
719 719
      int rdx = classes[cls].firstItem;
720 720
      items[idx].item = item;
721 721
      items[idx].cls = cls;
722 722

	
723 723
      items[idx].prev = rdx;
724 724
      items[idx].next = items[rdx].next;
725 725
      items[items[rdx].next].prev = idx;
726 726
      items[rdx].next = idx;
727 727

	
728 728
      index.set(item, idx);
729 729
    }
730 730

	
731 731
    /// \brief Clears the union-find data structure
732 732
    ///
733 733
    /// Erase each item from the data structure.
734 734
    void clear() {
735 735
      items.clear();
736
      classes.clear;
736
      classes.clear();
737 737
      firstClass = firstFreeClass = firstFreeItem = -1;
738 738
    }
739 739

	
740 740
    /// \brief Gives back the class of the \e item.
741 741
    ///
742 742
    /// Gives back the class of the \e item.
743 743
    int find(const Item &item) const {
744 744
      return items[index[item]].cls;
745 745
    }
746 746

	
747 747
    /// \brief Gives back a representant item of the component.
748 748
    ///
749 749
    /// Gives back a representant item of the component.
750 750
    Item item(int cls) const {
751 751
      return items[classes[cls].firstItem].item;
752 752
    }
753 753

	
754 754
    /// \brief Removes the given element from the structure.
755 755
    ///
756 756
    /// Removes the element from its component and if the component becomes
757 757
    /// empty then removes that component from the component list.
758 758
    ///
759 759
    /// \warning It is an error to remove an element which is not in
760 760
    /// the structure.
761 761
    void erase(const Item &item) {
762 762
      int idx = index[item];
763 763
      int cdx = items[idx].cls;
764 764

	
765 765
      if (idx == items[idx].next) {
766 766
        if (classes[cdx].prev != -1) {
767 767
          classes[classes[cdx].prev].next = classes[cdx].next;
768 768
        } else {
769 769
          firstClass = classes[cdx].next;
770 770
        }
771 771
        if (classes[cdx].next != -1) {
772 772
          classes[classes[cdx].next].prev = classes[cdx].prev;
773 773
        }
774 774
        classes[cdx].next = firstFreeClass;
775 775
        firstFreeClass = cdx;
776 776
      } else {
777 777
        classes[cdx].firstItem = items[idx].next;
778 778
        items[items[idx].next].prev = items[idx].prev;
779 779
        items[items[idx].prev].next = items[idx].next;
780 780
      }
781 781
      items[idx].next = firstFreeItem;
782 782
      firstFreeItem = idx;
783 783

	
784 784
    }
785 785

	
786 786

	
787 787
    /// \brief Removes the component of the given element from the structure.
788 788
    ///
789 789
    /// Removes the component of the given element from the structure.
790 790
    ///
791 791
    /// \warning It is an error to give an element which is not in the
792 792
    /// structure.
793 793
    void eraseClass(int cdx) {
794 794
      int idx = classes[cdx].firstItem;
795 795
      items[items[idx].prev].next = firstFreeItem;
796 796
      firstFreeItem = idx;
797 797

	
798 798
      if (classes[cdx].prev != -1) {
799 799
        classes[classes[cdx].prev].next = classes[cdx].next;
800 800
      } else {
801 801
        firstClass = classes[cdx].next;
802 802
      }
803 803
      if (classes[cdx].next != -1) {
804 804
        classes[classes[cdx].next].prev = classes[cdx].prev;
805 805
      }
806 806
      classes[cdx].next = firstFreeClass;
807 807
      firstFreeClass = cdx;
808 808
    }
809 809

	
810 810
    /// \brief LEMON style iterator for the classes.
811 811
    ///
812 812
    /// ClassIt is a lemon style iterator for the components. It iterates
813 813
    /// on the ids of classes.
814 814
    class ClassIt {
815 815
    public:
816 816
      /// \brief Constructor of the iterator
817 817
      ///
818 818
      /// Constructor of the iterator
819 819
      ClassIt(const ExtendFindEnum& ufe) : extendFind(&ufe) {
820 820
        cdx = extendFind->firstClass;
821 821
      }
822 822

	
823 823
      /// \brief Constructor to get invalid iterator
824 824
      ///
825 825
      /// Constructor to get invalid iterator
826 826
      ClassIt(Invalid) : extendFind(0), cdx(-1) {}
827 827

	
828 828
      /// \brief Increment operator
829 829
      ///
830 830
      /// It steps to the next representant item.
831 831
      ClassIt& operator++() {
832 832
        cdx = extendFind->classes[cdx].next;
833 833
        return *this;
834 834
      }
835 835

	
836 836
      /// \brief Conversion operator
837 837
      ///
838 838
      /// It converts the iterator to the current class id.
839 839
      operator int() const {
840 840
        return cdx;
841 841
      }
842 842

	
843 843
      /// \brief Equality operator
844 844
      ///
845 845
      /// Equality operator
846 846
      bool operator==(const ClassIt& i) {
847 847
        return i.cdx == cdx;
848 848
      }
849 849

	
850 850
      /// \brief Inequality operator
851 851
      ///
852 852
      /// Inequality operator
853 853
      bool operator!=(const ClassIt& i) {
854 854
        return i.cdx != cdx;
855 855
      }
856 856

	
857 857
    private:
858 858
      const ExtendFindEnum* extendFind;
859 859
      int cdx;
860 860
    };
861 861

	
862 862
    /// \brief LEMON style iterator for the items of a component.
863 863
    ///
864 864
    /// ClassIt is a lemon style iterator for the components. It iterates
865 865
    /// on the items of a class. By example if you want to iterate on
866 866
    /// each items of each classes then you may write the next code.
867 867
    ///\code
868 868
    /// for (ClassIt cit(ufe); cit != INVALID; ++cit) {
869 869
    ///   std::cout << "Class: ";
870 870
    ///   for (ItemIt iit(ufe, cit); iit != INVALID; ++iit) {
871 871
    ///     std::cout << toString(iit) << ' ' << std::endl;
872 872
    ///   }
873 873
    ///   std::cout << std::endl;
874 874
    /// }
875 875
    ///\endcode
876 876
    class ItemIt {
877 877
    public:
878 878
      /// \brief Constructor of the iterator
879 879
      ///
880 880
      /// Constructor of the iterator. The iterator iterates
881 881
      /// on the class of the \c item.
882 882
      ItemIt(const ExtendFindEnum& ufe, int cls) : extendFind(&ufe) {
883 883
        fdx = idx = extendFind->classes[cls].firstItem;
884 884
      }
885 885

	
886 886
      /// \brief Constructor to get invalid iterator
887 887
      ///
888 888
      /// Constructor to get invalid iterator
889 889
      ItemIt(Invalid) : extendFind(0), idx(-1) {}
890 890

	
891 891
      /// \brief Increment operator
892 892
      ///
893 893
      /// It steps to the next item in the class.
894 894
      ItemIt& operator++() {
895 895
        idx = extendFind->items[idx].next;
896 896
        if (fdx == idx) idx = -1;
897 897
        return *this;
898 898
      }
899 899

	
900 900
      /// \brief Conversion operator
901 901
      ///
902 902
      /// It converts the iterator to the current item.
903 903
      operator const Item&() const {
904 904
        return extendFind->items[idx].item;
905 905
      }
906 906

	
907 907
      /// \brief Equality operator
908 908
      ///
909 909
      /// Equality operator
910 910
      bool operator==(const ItemIt& i) {
911 911
        return i.idx == idx;
912 912
      }
913 913

	
914 914
      /// \brief Inequality operator
915 915
      ///
916 916
      /// Inequality operator
917 917
      bool operator!=(const ItemIt& i) {
918 918
        return i.idx != idx;
919 919
      }
920 920

	
921 921
    private:
922 922
      const ExtendFindEnum* extendFind;
923 923
      int idx, fdx;
924 924
    };
925 925

	
926 926
  };
927 927

	
928 928
  /// \ingroup auxdat
929 929
  ///
930 930
  /// \brief A \e Union-Find data structure implementation which
931 931
  /// is able to store a priority for each item and retrieve the minimum of
932 932
  /// each class.
933 933
  ///
934 934
  /// A \e Union-Find data structure implementation which is able to
935 935
  /// store a priority for each item and retrieve the minimum of each
936 936
  /// class. In addition, it supports the joining and splitting the
937 937
  /// components. If you don't need this feature then you makes
938 938
  /// better to use the \ref UnionFind class which is more efficient.
939 939
  ///
940 940
  /// The union-find data strcuture based on a (2, 16)-tree with a
941 941
  /// tournament minimum selection on the internal nodes. The insert
942 942
  /// operation takes O(1), the find, set, decrease and increase takes
943 943
  /// O(log(n)), where n is the number of nodes in the current
944 944
  /// component.  The complexity of join and split is O(log(n)*k),
945 945
  /// where n is the sum of the number of the nodes and k is the
946 946
  /// number of joined components or the number of the components
947 947
  /// after the split.
948 948
  ///
949 949
  /// \pre You need to add all the elements by the \ref insert()
950 950
  /// method.
951 951
  ///
952 952
  template <typename _Value, typename _ItemIntMap,
953 953
            typename _Comp = std::less<_Value> >
954 954
  class HeapUnionFind {
955 955
  public:
956 956

	
957 957
    typedef _Value Value;
958 958
    typedef typename _ItemIntMap::Key Item;
959 959

	
960 960
    typedef _ItemIntMap ItemIntMap;
961 961

	
962 962
    typedef _Comp Comp;
963 963

	
964 964
  private:
965 965

	
966 966
    static const int cmax = 16;
967 967

	
968 968
    ItemIntMap& index;
969 969

	
970 970
    struct ClassNode {
971 971
      int parent;
972 972
      int depth;
973 973

	
974 974
      int left, right;
975 975
      int next, prev;
976 976
    };
977 977

	
978 978
    int first_class;
979 979
    int first_free_class;
980 980
    std::vector<ClassNode> classes;
981 981

	
982 982
    int newClass() {
983 983
      if (first_free_class < 0) {
984 984
        int id = classes.size();
985 985
        classes.push_back(ClassNode());
986 986
        return id;
987 987
      } else {
988 988
        int id = first_free_class;
989 989
        first_free_class = classes[id].next;
990 990
        return id;
991 991
      }
992 992
    }
993 993

	
994 994
    void deleteClass(int id) {
995 995
      classes[id].next = first_free_class;
996 996
      first_free_class = id;
997 997
    }
998 998

	
999 999
    struct ItemNode {
1000 1000
      int parent;
1001 1001
      Item item;
1002 1002
      Value prio;
1003 1003
      int next, prev;
1004 1004
      int left, right;
1005 1005
      int size;
1006 1006
    };
1007 1007

	
1008 1008
    int first_free_node;
1009 1009
    std::vector<ItemNode> nodes;
1010 1010

	
1011 1011
    int newNode() {
1012 1012
      if (first_free_node < 0) {
1013 1013
        int id = nodes.size();
1014 1014
        nodes.push_back(ItemNode());
1015 1015
        return id;
1016 1016
      } else {
1017 1017
        int id = first_free_node;
1018 1018
        first_free_node = nodes[id].next;
1019 1019
        return id;
1020 1020
      }
1021 1021
    }
1022 1022

	
1023 1023
    void deleteNode(int id) {
1024 1024
      nodes[id].next = first_free_node;
1025 1025
      first_free_node = id;
1026 1026
    }
1027 1027

	
1028 1028
    Comp comp;
1029 1029

	
1030 1030
    int findClass(int id) const {
1031 1031
      int kd = id;
1032 1032
      while (kd >= 0) {
1033 1033
        kd = nodes[kd].parent;
1034 1034
      }
1035 1035
      return ~kd;
1036 1036
    }
1037 1037

	
1038 1038
    int leftNode(int id) const {
1039 1039
      int kd = ~(classes[id].parent);
1040 1040
      for (int i = 0; i < classes[id].depth; ++i) {
1041 1041
        kd = nodes[kd].left;
1042 1042
      }
1043 1043
      return kd;
1044 1044
    }
1045 1045

	
1046 1046
    int nextNode(int id) const {
1047 1047
      int depth = 0;
1048 1048
      while (id >= 0 && nodes[id].next == -1) {
1049 1049
        id = nodes[id].parent;
1050 1050
        ++depth;
1051 1051
      }
1052 1052
      if (id < 0) {
1053 1053
        return -1;
1054 1054
      }
1055 1055
      id = nodes[id].next;
1056 1056
      while (depth--) {
1057 1057
        id = nodes[id].left;
1058 1058
      }
1059 1059
      return id;
1060 1060
    }
1061 1061

	
1062 1062

	
1063 1063
    void setPrio(int id) {
1064 1064
      int jd = nodes[id].left;
1065 1065
      nodes[id].prio = nodes[jd].prio;
1066 1066
      nodes[id].item = nodes[jd].item;
1067 1067
      jd = nodes[jd].next;
1068 1068
      while (jd != -1) {
1069 1069
        if (comp(nodes[jd].prio, nodes[id].prio)) {
1070 1070
          nodes[id].prio = nodes[jd].prio;
1071 1071
          nodes[id].item = nodes[jd].item;
1072 1072
        }
1073 1073
        jd = nodes[jd].next;
1074 1074
      }
1075 1075
    }
1076 1076

	
1077 1077
    void push(int id, int jd) {
1078 1078
      nodes[id].size = 1;
1079 1079
      nodes[id].left = nodes[id].right = jd;
1080 1080
      nodes[jd].next = nodes[jd].prev = -1;
1081 1081
      nodes[jd].parent = id;
1082 1082
    }
1083 1083

	
1084 1084
    void pushAfter(int id, int jd) {
1085 1085
      int kd = nodes[id].parent;
1086 1086
      if (nodes[id].next != -1) {
1087 1087
        nodes[nodes[id].next].prev = jd;
1088 1088
        if (kd >= 0) {
1089 1089
          nodes[kd].size += 1;
1090 1090
        }
1091 1091
      } else {
1092 1092
        if (kd >= 0) {
1093 1093
          nodes[kd].right = jd;
1094 1094
          nodes[kd].size += 1;
1095 1095
        }
1096 1096
      }
1097 1097
      nodes[jd].next = nodes[id].next;
1098 1098
      nodes[jd].prev = id;
1099 1099
      nodes[id].next = jd;
1100 1100
      nodes[jd].parent = kd;
1101 1101
    }
1102 1102

	
1103 1103
    void pushRight(int id, int jd) {
1104 1104
      nodes[id].size += 1;
1105 1105
      nodes[jd].prev = nodes[id].right;
1106 1106
      nodes[jd].next = -1;
1107 1107
      nodes[nodes[id].right].next = jd;
1108 1108
      nodes[id].right = jd;
1109 1109
      nodes[jd].parent = id;
1110 1110
    }
1111 1111

	
1112 1112
    void popRight(int id) {
1113 1113
      nodes[id].size -= 1;
1114 1114
      int jd = nodes[id].right;
1115 1115
      nodes[nodes[jd].prev].next = -1;
1116 1116
      nodes[id].right = nodes[jd].prev;
1117 1117
    }
1118 1118

	
1119 1119
    void splice(int id, int jd) {
1120 1120
      nodes[id].size += nodes[jd].size;
0 comments (0 inline)