|
ADS
Algorithms and Data Structures
|
the union-find of the elements \( [ 0 , n ) \) that threads change at once More...
#include <ADS/concurrent/ConcurrentUnionFind.h>
Public member functions | |
| ConcurrentUnionFind (std::size_t n) | |
| the \( n \) singletons \( \{ 0 \} , \ldots , \{ n - 1 \} \) | |
| ConcurrentUnionFind (const ConcurrentUnionFind &)=delete | |
| no copy, since the threads share it by its address | |
| ConcurrentUnionFind & | operator= (const ConcurrentUnionFind &)=delete |
| no copy, since the threads share it by its address | |
| std::size_t | size (void) const |
| the number \( n \) of the elements | |
| std::int32_t | find (std::int32_t x) |
| the root of \( x \), the least element of its set when no union is running | |
| std::int32_t | parent (std::int32_t x) const |
| the parent of \( x \) at a moment of the call (\( x \) if a root) | |
| void | compress (std::int32_t x) |
| \( x \) pointed to its root, unless a union moved \( x \) first | |
| bool | unite (std::int32_t x, std::int32_t y) |
| the union of the sets of \( x \) and \( y \); false if they were already one | |
| bool | sameSet (std::int32_t x, std::int32_t y) |
| whether \( x \) and \( y \) are in the same set | |
the union-find of the elements \( [ 0 , n ) \) that threads change at once
|
inlineexplicit |
the \( n \) singletons \( \{ 0 \} , \ldots , \{ n - 1 \} \)
|
delete |
no copy, since the threads share it by its address
|
inline |
\( x \) pointed to its root, unless a union moved \( x \) first
In pseudocode (beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{compress}( x ) \cr 2 & \quad u \gets p[ x ] \cr 3 & \quad \textbf{if}\ u \ne x\ \textbf{then} \cr 4 & \qquad r \gets \mathrm{find}( u ) \cr 5 & \qquad \textbf{if}\ r \ne u\ \textbf{then}\ \mathrm{CAS}( p[ x ] , u , r ) \end{array}} \]
|
inlinenodiscard |
the root of \( x \), the least element of its set when no union is running
In pseudocode (the splitting of Jayanti and Tarjan, beyond the book), \( p[ x ] \) the parent of \( x \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{find}( x ) \cr 2 & \quad \textbf{loop} \cr 3 & \qquad u \gets p[ x ];\ g \gets p[ u ]\quad \triangleright \ \text{acquire} \cr 4 & \qquad \textbf{if}\ u = g\ \textbf{then return}\ u \cr 5 & \qquad \mathrm{CAS}( p[ x ] , u , g )\quad \triangleright \ \text{release, its failure harmless} \cr 6 & \qquad x \gets u \end{array}} \]
|
delete |
no copy, since the threads share it by its address
|
inlinenodiscard |
the parent of \( x \) at a moment of the call (\( x \) if a root)
|
inlinenodiscard |
whether \( x \) and \( y \) are in the same set
In pseudocode (Jayanti and Tarjan, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{sameSet}( x , y ) \cr 2 & \quad \textbf{loop} \cr 3 & \qquad x \gets \mathrm{find}( x );\ y \gets \mathrm{find}( y ) \cr 4 & \qquad \textbf{if}\ x = y\ \textbf{then return true} \cr 5 & \qquad \textbf{if}\ p[ x ] = x\ \textbf{then return false}\quad \triangleright\ x \text{ still a root after } y \text{ was found} \end{array}} \]
|
inlinenodiscard |
the number \( n \) of the elements
|
inline |
the union of the sets of \( x \) and \( y \); false if they were already one
In pseudocode (the linking by index of Jayanti and Tarjan, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{unite}( x , y ) \cr 2 & \quad \textbf{loop} \cr 3 & \qquad x \gets \mathrm{find}( x );\ y \gets \mathrm{find}( y ) \cr 4 & \qquad \textbf{if}\ x = y\ \textbf{then return false} \cr 5 & \qquad \textbf{if}\ x < y\ \textbf{then}\ \text{swap } x \text{ and } y \cr 6 & \qquad \textbf{if}\ \mathrm{CAS}( p[ x ] , x , y ) \ \textbf{then return true}\quad \triangleright\ \text{the larger} \text{ root hooked} \end{array}} \]