42 for ( I i{ size_t( 0 ) }; i <
size; ++i )
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; }
83 I
parent( I a )
const {
return parents_[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 ) ); }
134 I updateRootInRange_( I a,
const I r, I
begin, I
end )
143 std::swap( parents_[a], b );
153 Vector<I, I> parents_;
156 Vector<SizeType, I> sizes_;
#define MR_TIMER
namespace MR
Definition MRTimer.h:56
Union-find data structure for representing disjoin sets of elements with few very quick operations: 1...
Definition MRUnionFind.h:23
std::vector<T>-like container that requires specific indexing type,
Definition MRVector.h:23
MR_BIND_IGNORE_PY auto end(const BitSet &)
Definition MRBitSet.h:397
MR_BIND_IGNORE_PY auto begin(const BitSet &a)
Definition MRBitSet.h:395
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
void clear()
Definition MRVector.h:51
I find(I a)
finds the root of the set containing given element with optimizing data structure updates
Definition MRUnionFind.h:86
std::size_t size() const
Definition MRVector.h:55
void resize(size_t newSize) MR_REQUIRES_IF_SUPPORTED(sizeof(T)>0 &&std
Definition MRVector.h:57
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
void push_back(const T &t MR_LIFETIME_CAPTURE_BY_NESTED(this))
Definition MRVector.h:132
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
void reserve(size_t capacity)
Definition MRVector.h:65
bool isRoot(I a) const
returns true if given element is the root of some set
Definition MRUnionFind.h:80
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
only for bindings generation
Definition MRCameraOrientationPlugin.h:8