MeshLib Documentation
Loading...
Searching...
No Matches
MRUnionFind.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRVector.h"
4#include <utility>
5
6namespace MR
7{
8
14template <typename I>
16{
17public:
18 UnionFind() = default;
19 explicit UnionFind( size_t size ) { reset( size ); }
20 auto size() const { return roots_.size(); }
21
23 void reset( size_t size )
24 {
25 roots_.clear();
26 roots_.reserve( size );
27 for ( I i{ size_t( 0 ) }; i < size; ++i )
28 roots_.push_back( i );
29 sizes_.clear();
30 sizes_.resize( size, 1 );
31 }
32
35 std::pair<I,bool> unite( I first, I second )
36 {
37 auto firstRoot = updateRoot_( first );
38 auto secondRoot = updateRoot_( second );
39 if ( firstRoot == secondRoot )
40 return { firstRoot, false };
42 if ( sizes_[firstRoot] < sizes_[secondRoot] )
43 {
44 roots_[firstRoot] = secondRoot;
45 sizes_[secondRoot] += sizes_[firstRoot];
46 return { secondRoot, true };
47 }
48 else
49 {
50 roots_[secondRoot] = firstRoot;
51 sizes_[firstRoot] += sizes_[secondRoot];
52 return { firstRoot, true };
53 }
54 }
55
57 bool united( I first, I second )
58 {
59 auto firstRoot = updateRoot_( first );
60 auto secondRoot = updateRoot_( second );
61 return firstRoot == secondRoot;
62 }
63
65 I find( I a )
66 {
67 return updateRoot_( a, findRootNoUpdate_( a ) );
68 }
69
72 {
73 return updateRootInRange_( a, findRootNoUpdate_( a ), begin, end );
74 }
75
78 {
79 for ( I i{ size_t( 0 ) }; i < roots_.size(); ++i )
80 updateRoot_( i, findRootNoUpdate_( i ) );
81 return roots_;
82 }
83
85 const Vector<I, I> & parents() const { return roots_; }
86
88 size_t sizeOfComp( I a ) { return sizes_[ find( a ) ]; }
89
90private:
92 I findRootNoUpdate_( I a ) const
93 {
94 I r = roots_[a];
95 for ( I e = a; e != r; r = roots_[e = r] ) {}
96 return r;
97 }
99 I updateRoot_( I a, const I r )
100 {
101 // update parents
102 while ( a != r )
103 {
104 I b = r;
105 std::swap( roots_[a], b );
106 a = b;
107 }
108 return r;
109 }
110 // find the root of given element, and set it as parent for it and other parents
111 I updateRoot_( I a ) { return updateRoot_( a, findRootNoUpdate_( a ) ); }
113 I updateRootInRange_( I a, const I r, I begin, I end )
114 {
115 assert( begin < end );
116 // update parents
117 while ( a != r )
118 {
119 if ( a >= begin && a < end )
120 {
121 I b = r;
122 std::swap( roots_[a], b );
123 a = b;
124 }
125 else
126 a = roots_[a];
127 }
128 return r;
129 }
131 Vector<I, I> roots_;
133 Vector<size_t, I> sizes_;
134};
135
136}
Simple union find data structure.
Definition MRUnionFind.h:16
std::pair< I, bool > unite(I first, I second)
Definition MRUnionFind.h:35
I find(I a)
finds the root of the set containing given element with optimizing data structure updates
Definition MRUnionFind.h:65
const Vector< I, I > & roots()
sets the root as the parent of each element, then returns the vector
Definition MRUnionFind.h:77
UnionFind(size_t size)
Definition MRUnionFind.h:19
UnionFind()=default
void reset(size_t size)
reset roots to represent each element as disjoint set of rank 0
Definition MRUnionFind.h:23
size_t sizeOfComp(I a)
returns the size of component containing given element
Definition MRUnionFind.h:88
bool united(I first, I second)
returns true if given two elements are from one component
Definition MRUnionFind.h:57
auto size() const
Definition MRUnionFind.h:20
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:71
const Vector< I, I > & parents() const
gets the parents of all elements as in
Definition MRUnionFind.h:85
std::vector<T>-like container that requires specific indexing type,
Definition MRMesh/MRVector.h:20
void clear()
Definition MRMesh/MRVector.h:39
std::size_t size() const
Definition MRMesh/MRVector.h:42
void resize(size_t newSize) MR_REQUIRES_IF_SUPPORTED(sizeof(T)>0 &&std
Definition MRMesh/MRVector.h:44
void reserve(size_t capacity)
Definition MRMesh/MRVector.h:60
void push_back(const T &t)
Definition MRMesh/MRVector.h:111
auto begin(const BitSet &a)
Definition MRMesh/MRBitSet.h:280
auto end(const BitSet &)
Definition MRMesh/MRBitSet.h:282
Definition MRCameraOrientationPlugin.h:8
I
Definition MRMesh/MRMeshFwd.h:88