59#ifndef __ADS_ConcurrentUnionFind
60 #define __ADS_ConcurrentUnionFind
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 );
107 [[nodiscard]] std::size_t
size(
void )
const {
return( f_n ); }
127 [[nodiscard]] std::int32_t
find( std::int32_t x ) {
128 ADS_PRE( ( x >= 0 ) && ( std::size_t( x ) < f_n ) );
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 );
134 f_p[ x ].compare_exchange_weak( p , g , std::memory_order_release ,
135 std::memory_order_relaxed );
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 ) );
162 ADS_PRE( ( x >= 0 ) && ( std::size_t( x ) < f_n ) );
163 std::int32_t p = f_p[ x ].load( std::memory_order_acquire );
166 const std::int32_t r =
find( p );
168 f_p[ x ].compare_exchange_strong( p , r , std::memory_order_release ,
169 std::memory_order_relaxed );
191 bool unite( std::int32_t x , std::int32_t y ) {
200 if( f_p[ x ].compare_exchange_strong( r , y , std::memory_order_acq_rel ,
201 std::memory_order_relaxed ) )
220 [[nodiscard]]
bool sameSet( std::int32_t x , std::int32_t y ) {
226 if( f_p[ x ].
load( std::memory_order_acquire ) == x )
235 static std::size_t checked( std::size_t n ) {
236 ADS_PRE( n < ( std::size_t( 1 ) << 31 ) );
241 std::unique_ptr< std::atomic< std::int32_t >[] > f_p;
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