34 auto size()
const {
return parents_.size(); }
41 parents_.reserve(
size );
42 for (
I i{ size_t( 0 ) }; i <
size; ++i )
43 parents_.push_back( i );
45 sizes_.resize(
size, 1 );
50 std::pair<I,bool>
unite(
I first,
I second )
52 auto firstRoot = updateRoot_( first );
53 auto secondRoot = updateRoot_( second );
54 if ( firstRoot == secondRoot )
55 return { firstRoot,
false };
57 if ( sizes_[firstRoot] < sizes_[secondRoot] )
59 parents_[firstRoot] = secondRoot;
60 sizes_[secondRoot] += sizes_[firstRoot];
61 return { secondRoot,
true };
65 parents_[secondRoot] = firstRoot;
66 sizes_[firstRoot] += sizes_[secondRoot];
67 return { firstRoot,
true };
74 auto firstRoot = updateRoot_( first );
75 auto secondRoot = updateRoot_( second );
76 return firstRoot == secondRoot;
80 bool isRoot(
I a )
const {
return parents_[a] == a; }
86 I find(
I a ) {
return updateRoot_( a ); }
91 return updateRootInRange_( a, findRootNoUpdate_( a ),
begin,
end );
97 for (
I i{ size_t( 0 ) }; i < parents_.size(); ++i )
98 updateRoot_( i, findRootNoUpdate_( i ) );
110 I findRootNoUpdate_(
I a )
const
113 for (
I e = a; e != r; r = parents_[e = r] ) {}
118 I updateRoot_(
I a,
const I r )
124 std::swap( parents_[a], b );
131 I updateRoot_(
I a ) {
return updateRoot_( a, findRootNoUpdate_( a ) ); }
143 std::swap( parents_[a], b );
153 Vector<I, I> parents_;
156 Vector<SizeType, I> sizes_;
#define MR_TIMER
FUNCTION in GCC/Clang returns only short function name without class name and template parameters
Definition MRTimer.h:56
std::vector<T>-like container that requires specific indexing type,
Definition MRVector.h:23
SizeType sizeOfComp(I a)
returns the number of elements in the set containing given element
Definition MRUnionFind.h:106
std::pair< I, bool > unite(I first, I second)
Definition MRUnionFind.h:50
I find(I a)
finds the root of the set containing given element with optimizing data structure updates
Definition MRUnionFind.h:86
const Vector< I, I > & roots()
sets the root of corresponding set as the parent of each element, then returns the vector
Definition MRUnionFind.h:95
UnionFind(size_t size)
creates union-find with given number of elements, each element is the only one in its disjoint set
Definition MRUnionFind.h:31
void reset(size_t size)
resets union-find to represent given number of elements, each element is the only one in its disjoint...
Definition MRUnionFind.h:37
bool isRoot(I a) const
returns true if given element is the root of some set
Definition MRUnionFind.h:80
class MRMESH_CLASS I
Definition MRMeshFwd.h:137
bool united(I first, I second)
returns true if given two elements are from one set
Definition MRUnionFind.h:72
I parent(I a) const
return parent element of this element, which is equal to given element only for set's root
Definition MRUnionFind.h:83
auto size() const
returns the number of elements in union-find
Definition MRUnionFind.h:34
I findUpdateRange(I a, I begin, I end)
finds the root of the set containing given element with optimizing data structure in the range [begin...
Definition MRUnionFind.h:89
typename I::ValueType SizeType
the type that can hold the number of elements of the maximal set (e.g. int for FaceId and size_t for ...
Definition MRUnionFind.h:26
const Vector< I, I > & parents() const
gets the parents of all elements as is
Definition MRUnionFind.h:103
auto begin(ViewportMask mask)
Definition MRViewportId.h:122
auto end(ViewportMask)
Definition MRViewportId.h:124
only for bindings generation
Definition MRCameraOrientationPlugin.h:8