42 for (
I i{ size_t( 0 ) }; i <
size; ++i )
54 if ( firstRoot == secondRoot )
55 return { firstRoot,
false };
57 return { secondRoot,
true };
84 for (
I i{ size_t( 0 ) }; i <
parents_.size(); ++i )
97 for (
I e = a; e != r; r =
parents_[e = r] ) {}
166 for (
I i{ size_t( 0 ) }; i < this->
parents_.size(); ++i )
175 sizes_.resize(
size, 1 );
184 if ( firstRoot == secondRoot )
185 return { firstRoot,
false };
187 if ( sizes_[firstRoot] < sizes_[secondRoot] )
189 this->
parents_[firstRoot] = secondRoot;
190 sizes_[secondRoot] += sizes_[firstRoot];
191 return { secondRoot,
true };
195 this->
parents_[secondRoot] = firstRoot;
196 sizes_[firstRoot] += sizes_[secondRoot];
197 return { firstRoot,
true };
231 first = findAtomic_( first );
232 second = findAtomic_( second );
233 if ( first == second )
236 if ( first < second )
237 std::swap( first, second );
239 if ( casStrong_( this->
parents_[first], first, second ) )
251 static I loadAtomic_(
I& slot )
253#ifdef __cpp_lib_atomic_ref
254 return I( std::atomic_ref<typename I::ValueType>( slot.get() ).load( std::memory_order_relaxed ) );
256 return I( __atomic_load_n( &slot.get(), __ATOMIC_RELAXED ) );
261 static void casWeak_(
I& slot,
I expected,
I desired )
263 using T =
typename I::ValueType;
264 T exp = expected.get();
265#ifdef __cpp_lib_atomic_ref
266 std::atomic_ref<T>( slot.get() ).compare_exchange_weak( exp, desired.get(), std::memory_order_relaxed );
268 __atomic_compare_exchange_n( &slot.get(), &exp, desired.get(),
true, __ATOMIC_RELAXED, __ATOMIC_RELAXED );
273 static bool casStrong_(
I& slot,
I expected,
I desired )
275 using T =
typename I::ValueType;
276 T exp = expected.get();
277#ifdef __cpp_lib_atomic_ref
278 return std::atomic_ref<T>( slot.get() ).compare_exchange_strong( exp, desired.get(), std::memory_order_acq_rel, std::memory_order_relaxed );
280 return __atomic_compare_exchange_n( &slot.get(), &exp, desired.get(),
false, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED );
296 casWeak_( this->
parents_[a], p, gp );
#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:202
std::pair< I, bool > unite(I first, I second)
Definition MRUnionFind.h:180
I updateRootInRange_(I a, const I r, I begin, I end)
sets new root
Definition MRUnionFind.h:118
ParallelUnionFind(size_t size)
creates union-find with given number of elements, each element is the only one in its disjoint set
Definition MRUnionFind.h:223
BaseUnionFind(BaseUnionFind &&) noexcept=default
I updateRoot_(I a, const I r)
sets new root
Definition MRUnionFind.h:102
auto size() const
returns the number of elements in union-find
Definition MRUnionFind.h:34
bool isRoot(I a) const
returns true if given element is the root of some set
Definition MRUnionFind.h:67
const Vector< I, I > & parents() const
gets the parents of all elements as is
Definition MRUnionFind.h:90
std::pair< I, bool > uniteUnbalanced(I first, I second)
Definition MRUnionFind.h:50
I updateRoot_(I a)
find the root of given element, and set it as parent for it and other parents
Definition MRUnionFind.h:115
const Vector< I, I > & roots()
sets the root of corresponding set as the parent of each element, then returns the vector
Definition MRUnionFind.h:82
ParallelUnionFind()=default
UnionFind(BaseUnionFind< I > &&base)
Definition MRUnionFind.h:163
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:159
BaseUnionFind(const BaseUnionFind &)=default
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:171
class MRMESH_CLASS I
Definition MRMeshFwd.h:141
bool united(I first, I second)
returns true if given two elements are from one set
Definition MRUnionFind.h:61
I find(I a)
finds the root of the set containing given element with optimizing data structure updates
Definition MRUnionFind.h:73
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:25
void uniteAtomic(I first, I second)
Definition MRUnionFind.h:227
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:70
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:154
Vector< I, I > parents_
parent element of each element
Definition MRUnionFind.h:137
I findRootNoUpdate_(I a) const
finds the root of the set containing given element without optimizing data structure updates
Definition MRUnionFind.h:94
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:76
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
auto begin(ViewportMask mask)
Definition MRViewportId.h:122
auto end(ViewportMask)
Definition MRViewportId.h:124
only for bindings generation
Definition MRCameraOrientationPlugin.h:8