ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ConcurrentUnionFind.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------ File ConcurrentUnionFind.h ----------------------*/
3/*--------------------------------------------------------------------------*/
55/*--------------------------------------------------------------------------*/
56/*----------------------------- DEFINITIONS --------------------------------*/
57/*--------------------------------------------------------------------------*/
58
59#ifndef __ADS_ConcurrentUnionFind
60 #define __ADS_ConcurrentUnionFind
61/* self-identification: #endif constant */
62
63/*--------------------------------------------------------------------------*/
64/*------------------------------ INCLUDES ----------------------------------*/
65/*--------------------------------------------------------------------------*/
66
67#include "ADS/Contract.h"
68
69#include <atomic>
70#include <cstddef>
71#include <cstdint>
72#include <memory>
73#include <utility>
74
75/*--------------------------------------------------------------------------*/
76/*--------------------------- NAMESPACE ------------------------------------*/
77/*--------------------------------------------------------------------------*/
78
79namespace ADS
80{
81
82/*--------------------------------------------------------------------------*/
83/*------------------------ CLASS ConcurrentUnionFind -----------------------*/
84/*--------------------------------------------------------------------------*/
85
89{
90
91/*--------------------------------------------------------------------------*/
92 public:
95 explicit ConcurrentUnionFind( std::size_t n )
96 : f_n( checked( n ) ) , f_p( new std::atomic< std::int32_t >[ n ] ) {
97 for( std::size_t x = 0 ; x < n ; ++x )
98 f_p[ x ].store( std::int32_t( x ) , std::memory_order_relaxed );
99 }
100
105
107 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
108
127 [[nodiscard]] std::int32_t find( std::int32_t x ) {
128 ADS_PRE( ( x >= 0 ) && ( std::size_t( x ) < f_n ) );
129 for( ; ; ) {
130 std::int32_t p = f_p[ x ].load( std::memory_order_acquire );
131 const std::int32_t g = f_p[ p ].load( std::memory_order_acquire );
132 if( p == g )
133 return( p );
134 f_p[ x ].compare_exchange_weak( p , g , std::memory_order_release ,
135 std::memory_order_relaxed );
136 x = p;
137 }
138 }
139
142 [[nodiscard]] std::int32_t parent( std::int32_t x ) const {
143 ADS_PRE( ( x >= 0 ) && ( std::size_t( x ) < f_n ) );
144 return( f_p[ x ].load( std::memory_order_acquire ) );
145 }
146
161 void compress( std::int32_t x ) {
162 ADS_PRE( ( x >= 0 ) && ( std::size_t( x ) < f_n ) );
163 std::int32_t p = f_p[ x ].load( std::memory_order_acquire );
164 if( p == x )
165 return;
166 const std::int32_t r = find( p );
167 if( r != p )
168 f_p[ x ].compare_exchange_strong( p , r , std::memory_order_release ,
169 std::memory_order_relaxed );
170 }
171
191 bool unite( std::int32_t x , std::int32_t y ) {
192 for( ; ; ) {
193 x = find( x );
194 y = find( y );
195 if( x == y )
196 return( false );
197 if( x < y )
198 std::swap( x , y ); // hook x, the larger root
199 std::int32_t r = x;
200 if( f_p[ x ].compare_exchange_strong( r , y , std::memory_order_acq_rel ,
201 std::memory_order_relaxed ) )
202 return( true );
203 }
204 }
205
220 [[nodiscard]] bool sameSet( std::int32_t x , std::int32_t y ) {
221 for( ; ; ) {
222 x = find( x );
223 y = find( y );
224 if( x == y )
225 return( true );
226 if( f_p[ x ].load( std::memory_order_acquire ) == x )
227 return( false ); // x was a root after y found
228 }
229 }
230
231/*--------------------------------------------------------------------------*/
232 private:
235 static std::size_t checked( std::size_t n ) {
236 ADS_PRE( n < ( std::size_t( 1 ) << 31 ) );
237 return( n );
238 }
239
240 std::size_t f_n;
241 std::unique_ptr< std::atomic< std::int32_t >[] > f_p;
242 }; // end( class ConcurrentUnionFind )
243
244/*--------------------------------------------------------------------------*/
245
246} // end( namespace ADS )
247
248/*--------------------------------------------------------------------------*/
249
250#endif /* ConcurrentUnionFind.h included */
251
252/*--------------------------------------------------------------------------*/
253/*--------------------- End File ConcurrentUnionFind.h ---------------------*/
254/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
ConcurrentUnionFind & operator=(const ConcurrentUnionFind &)=delete
no copy, since the threads share it by its address
std::int32_t find(std::int32_t x)
the root of , the least element of its set when no union is running
Definition ConcurrentUnionFind.h:127
ConcurrentUnionFind(std::size_t n)
the singletons
Definition ConcurrentUnionFind.h:95
void compress(std::int32_t x)
pointed to its root, unless a union moved first
Definition ConcurrentUnionFind.h:161
bool sameSet(std::int32_t x, std::int32_t y)
whether and are in the same set
Definition ConcurrentUnionFind.h:220
bool unite(std::int32_t x, std::int32_t y)
the union of the sets of and ; false if they were already one
Definition ConcurrentUnionFind.h:191
std::int32_t parent(std::int32_t x) const
the parent of at a moment of the call ( if a root)
Definition ConcurrentUnionFind.h:142
std::size_t size(void) const
the number of the elements
Definition ConcurrentUnionFind.h:107
ConcurrentUnionFind(const ConcurrentUnionFind &)=delete
no copy, since the threads share it by its address
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
void load(Ar &a, T &x)
the structure read from the archive of cereal
Definition Cereal.h:238
Definition ADT.h:96