... | ... |
@@ -678,129 +678,129 @@ |
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 { |
0 comments (0 inline)