42 if constexpr (
sizeof(
SizeType ) <
sizeof( size_t ) )
43 assert(
size <=
size_t( std::numeric_limits<SizeType>::max() ) );
48 for (
size_t i = 0; i <
size; ++i )
68 if ( firstRoot == secondRoot )
69 return { firstRoot,
false };
71 return { secondRoot,
true };
98 for ( I i{ size_t( 0 ) }; i <
parents_.size(); ++i )
111 for ( I e = a; e != r; r =
parents_[e = r] ) {}
180 for ( I i{ size_t( 0 ) }; i < this->
parents_.size(); ++i )
189 sizes_.resize(
size, 1 );
195 sizes_.push_back( 1 );
201 std::pair<I,bool>
unite( I first, I second )
205 if ( firstRoot == secondRoot )
206 return { firstRoot,
false };
208 if ( sizes_[firstRoot] < sizes_[secondRoot] )
210 this->
parents_[firstRoot] = secondRoot;
211 sizes_[secondRoot] += sizes_[firstRoot];
212 return { secondRoot,
true };
216 this->
parents_[secondRoot] = firstRoot;
217 sizes_[firstRoot] += sizes_[secondRoot];
218 return { firstRoot,
true };
252 first = findAtomic_( first );
253 second = findAtomic_( second );
254 if ( first == second )
257 if ( first < second )
258 std::swap( first, second );
260 if ( casStrong_( this->
parents_[first], first, second ) )
272 static I loadAtomic_( I& slot )
274#ifdef __cpp_lib_atomic_ref
275 return I( std::atomic_ref<typename I::ValueType>( slot.get() ).load( std::memory_order_relaxed ) );
277 return I( __atomic_load_n( &slot.get(), __ATOMIC_RELAXED ) );
282 static void casWeak_( I& slot, I expected, I desired )
284 using T =
typename I::ValueType;
285 T exp = expected.get();
286#ifdef __cpp_lib_atomic_ref
287 std::atomic_ref<T>( slot.get() ).compare_exchange_weak( exp, desired.get(), std::memory_order_relaxed );
289 __atomic_compare_exchange_n( &slot.get(), &exp, desired.get(),
true, __ATOMIC_RELAXED, __ATOMIC_RELAXED );
294 static bool casStrong_( I& slot, I expected, I desired )
296 using T =
typename I::ValueType;
297 T exp = expected.get();
298#ifdef __cpp_lib_atomic_ref
299 return std::atomic_ref<T>( slot.get() ).compare_exchange_strong( exp, desired.get(), std::memory_order_acq_rel, std::memory_order_relaxed );
301 return __atomic_compare_exchange_n( &slot.get(), &exp, desired.get(),
false, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED );
310 I p = loadAtomic_( this->
parents_[a] );
313 I gp = loadAtomic_( this->
parents_[p] );
317 casWeak_( this->
parents_[a], p, gp );
327static std::pair<Vector<RegionId, Id<T>>,
int> getUniqueRootIds(
const Vector<
Id<T>,
Id<T>>& allRoots,
const TaggedBitSet<T>& region )
332 for (
auto f : region )
334 auto& uniqIndex = uniqueRootsMap[allRoots[f]];
337 uniqIndex = RegionId( k );
340 uniqueRootsMap[f] = uniqIndex;
342 return { std::move( uniqueRootsMap ), k };
#define MR_TIMER
Definition MRTimer.h:60
stores index of some element, it is made as template class to avoid mixing faces, edges and vertices
Definition MRId.h:20
Vector<T> is indexed by size_t.
Definition MRVector.h:24
SizeType sizeOfComp(I a)
returns the number of elements in the set containing given element
Definition MRUnionFind.h:223
std::pair< I, bool > unite(I first, I second)
Definition MRUnionFind.h:201
I updateRootInRange_(I a, const I r, I begin, I end)
sets new root r for the element a and all its ancestors if they are in the range [begin,...
Definition MRUnionFind.h:132
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:244
BaseUnionFind(BaseUnionFind &&) noexcept=default
I updateRoot_(I a, const I r)
sets new root r for the element a and all its ancestors, returns new root r
Definition MRUnionFind.h:116
auto size() const
returns the number of elements in union-find
Definition MRUnionFind.h:36
bool isRoot(I a) const
returns true if given element is the root of some set
Definition MRUnionFind.h:81
const Vector< I, I > & parents() const
gets the parents of all elements as is
Definition MRUnionFind.h:104
std::pair< I, bool > uniteUnbalanced(I first, I second)
Definition MRUnionFind.h:64
I updateRoot_(I a)
find the root of given element, and set it as parent for it and other parents
Definition MRUnionFind.h:129
const Vector< I, I > & roots()
sets the root of corresponding set as the parent of each element, then returns the vector
Definition MRUnionFind.h:96
I add()
adds new element in its own disjoint set, returns its identifier
Definition MRUnionFind.h:193
ParallelUnionFind()=default
UnionFind(BaseUnionFind< I > &&base)
Definition MRUnionFind.h:177
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:173
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:185
bool united(I first, I second)
returns true if given two elements are from one set
Definition MRUnionFind.h:75
I find(I a)
finds the root of the set containing given element with optimizing data structure updates
Definition MRUnionFind.h:87
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:27
void uniteAtomic(I first, I second)
Definition MRUnionFind.h:248
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:84
I add()
adds new element in its own disjoint set, returns its identifier
Definition MRUnionFind.h:53
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:168
Vector< I, I > parents_
parent element of each element
Definition MRUnionFind.h:151
I findRootNoUpdate_(I a) const
finds the root of the set containing given element without optimizing data structure updates
Definition MRUnionFind.h:108
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:90
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:39
auto begin(ViewportMask mask)
Definition MRViewportId.h:122
auto end(ViewportMask)
Definition MRViewportId.h:124
Only strictly necessary for the bindings (to make MR::MeshIntersectionResult a complete type).
Definition MRCameraOrientationPlugin.h:8