MeshLib C++ Docs
Loading...
Searching...
No Matches
MRUnionFind.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRVector.h"
4#include "MRTimer.h"
5#include "MRId.h"
6#include <atomic>
7#include <limits>
8#include <utility>
9
10namespace MR
11{
14
15
22template <typename I>
24{
25public:
27 using SizeType = typename I::ValueType;
28
29 BaseUnionFind() = default;
30 BaseUnionFind( const BaseUnionFind & ) = default;
31 BaseUnionFind( BaseUnionFind && ) noexcept = default;
32 BaseUnionFind & operator =( const BaseUnionFind & ) = default;
33 BaseUnionFind & operator =( BaseUnionFind && ) noexcept = default;
34
36 auto size() const { return parents_.size(); }
37
39 void reset( size_t size )
40 {
42 if constexpr ( sizeof( SizeType ) < sizeof( size_t ) )
43 assert( size <= size_t( std::numeric_limits<SizeType>::max() ) );
45 parents_.clear();
46 parents_.resizeNoInit( size );
47 I * const p = parents_.data();
48 for ( size_t i = 0; i < size; ++i )
49 p[i] = I( i );
50 }
51
53 I add()
54 {
55 const I res( parents_.size() );
56 parents_.push_back( res );
57 return res;
58 }
59
64 std::pair<I, bool> uniteUnbalanced( I first, I second )
65 {
66 auto firstRoot = updateRoot_( first );
67 auto secondRoot = updateRoot_( second );
68 if ( firstRoot == secondRoot )
69 return { firstRoot, false };
70 parents_[firstRoot] = secondRoot;
71 return { secondRoot, true };
72 }
73
75 bool united( I first, I second )
76 {
77 return updateRoot_( first ) == updateRoot_( second );
78 }
79
81 bool isRoot( I a ) const { return parents_[a] == a; }
82
84 I parent( I a ) const { return parents_[a]; }
85
87 I find( I a ) { return updateRoot_( a ); }
88
90 I findUpdateRange( I a, I begin, I end )
91 {
93 }
94
97 {
98 for ( I i{ size_t( 0 ) }; i < parents_.size(); ++i )
100 return parents_;
101 }
102
104 const Vector<I, I> & parents() const { return parents_; }
105
106protected:
108 I findRootNoUpdate_( I a ) const
109 {
110 I r = parents_[a];
111 for ( I e = a; e != r; r = parents_[e = r] ) {}
112 return r;
113 }
114
116 I updateRoot_( I a, const I r )
117 {
119 while ( a != r )
120 {
121 I b = r;
122 std::swap( parents_[a], b );
123 a = b;
124 }
125 return r;
126 }
127
129 I updateRoot_( I a ) { return updateRoot_( a, findRootNoUpdate_( a ) ); }
130
132 I updateRootInRange_( I a, const I r, I begin, I end )
133 {
134 assert( begin < end );
136 while ( a != r )
137 {
138 if ( a >= begin && a < end )
139 {
140 I b = r;
141 std::swap( parents_[a], b );
142 a = b;
143 }
144 else
145 a = parents_[a];
146 }
147 return r;
148 }
149
152};
153
163template <typename I>
164class UnionFind : public BaseUnionFind<I>
165{
166public:
168 using SizeType = typename I::ValueType;
169
170 UnionFind() = default;
171
173 explicit UnionFind( size_t size ) { reset( size ); }
174
177 explicit UnionFind( BaseUnionFind<I> && base ) : BaseUnionFind<I>( std::move( base ) )
178 {
179 sizes_.resize( this->parents_.size(), SizeType( 0 ) );
180 for ( I i{ size_t( 0 ) }; i < this->parents_.size(); ++i )
181 ++sizes_[ this->findRootNoUpdate_( i ) ];
182 }
183
185 void reset( size_t size )
186 {
188 sizes_.clear();
189 sizes_.resize( size, 1 );
190 }
191
193 I add()
194 {
195 sizes_.push_back( 1 );
196 return BaseUnionFind<I>::add();
197 }
198
201 std::pair<I,bool> unite( I first, I second )
202 {
203 auto firstRoot = this->updateRoot_( first );
204 auto secondRoot = this->updateRoot_( second );
205 if ( firstRoot == secondRoot )
206 return { firstRoot, false };
208 if ( sizes_[firstRoot] < sizes_[secondRoot] )
209 {
210 this->parents_[firstRoot] = secondRoot;
211 sizes_[secondRoot] += sizes_[firstRoot];
212 return { secondRoot, true };
213 }
214 else
215 {
216 this->parents_[secondRoot] = firstRoot;
217 sizes_[firstRoot] += sizes_[secondRoot];
218 return { firstRoot, true };
219 }
220 }
221
223 SizeType sizeOfComp( I a ) { return sizes_[ this->find( a ) ]; }
224
225private:
227 Vector<SizeType, I> sizes_;
228};
229
237template <typename I>
239{
240public:
241 ParallelUnionFind() = default;
242
244 explicit ParallelUnionFind( size_t size ) { this->reset( size ); }
245
248 void uniteAtomic( I first, I second )
249 {
250 for ( ;; )
251 {
252 first = findAtomic_( first );
253 second = findAtomic_( second );
254 if ( first == second )
255 return;
257 if ( first < second )
258 std::swap( first, second );
260 if ( casStrong_( this->parents_[first], first, second ) )
261 return;
263 }
264 }
265
266private:
270
272 static I loadAtomic_( I& slot )
273 {
274#ifdef __cpp_lib_atomic_ref
275 return I( std::atomic_ref<typename I::ValueType>( slot.get() ).load( std::memory_order_relaxed ) );
276#else
277 return I( __atomic_load_n( &slot.get(), __ATOMIC_RELAXED ) );
278#endif
279 }
280
282 static void casWeak_( I& slot, I expected, I desired )
283 {
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 );
288#else
289 __atomic_compare_exchange_n( &slot.get(), &exp, desired.get(), true, __ATOMIC_RELAXED, __ATOMIC_RELAXED );
290#endif
291 }
292
294 static bool casStrong_( I& slot, I expected, I desired )
295 {
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 );
300#else
301 return __atomic_compare_exchange_n( &slot.get(), &exp, desired.get(), false, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED );
302#endif
303 }
304
306 I findAtomic_( I a )
307 {
308 for ( ;; )
309 {
310 I p = loadAtomic_( this->parents_[a] );
311 if ( p == a )
312 return a;
313 I gp = loadAtomic_( this->parents_[p] );
314 if ( p == gp )
315 return p;
317 casWeak_( this->parents_[a], p, gp );
318 a = gp;
319 }
320 }
321};
322
326template<typename T>
327static std::pair<Vector<RegionId, Id<T>>, int> getUniqueRootIds( const Vector<Id<T>, Id<T>>& allRoots, const TaggedBitSet<T>& region )
328{
329 MR_TIMER;
330 Vector<RegionId, Id<T>> uniqueRootsMap( allRoots.size() );
331 int k = 0;
332 for ( auto f : region )
333 {
334 auto& uniqIndex = uniqueRootsMap[allRoots[f]];
335 if ( uniqIndex < 0 )
336 {
337 uniqIndex = RegionId( k );
338 ++k;
339 }
340 uniqueRootsMap[f] = uniqIndex;
341 }
342 return { std::move( uniqueRootsMap ), k };
343}
344
345}
#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
BaseUnionFind()=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
UnionFind()=default
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