... | ... |
@@ -640,193 +640,193 @@ |
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; |
0 comments (0 inline)