ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
UnionFind.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File UnionFind.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
58/*--------------------------------------------------------------------------*/
59/*----------------------------- DEFINITIONS --------------------------------*/
60/*--------------------------------------------------------------------------*/
61
62#ifndef __ADS_UnionFind
63 #define __ADS_UnionFind
64/* self-identification: #endif constant */
65
66/*--------------------------------------------------------------------------*/
67/*------------------------------ INCLUDES ----------------------------------*/
68/*--------------------------------------------------------------------------*/
69
70#include "ADS/Contract.h"
71#include "ADS/Modification.h"
72#include "ADS/Portable.h"
73
74#include <algorithm>
75#include <cstddef>
76#include <utility>
77#include <vector>
78
79/*--------------------------------------------------------------------------*/
80/*--------------------------- NAMESPACE ------------------------------------*/
81/*--------------------------------------------------------------------------*/
82
83namespace ADS
84{
85
86/*--------------------------------------------------------------------------*/
87/*--------------------------- CLASS DisjointSets ---------------------------*/
88/*--------------------------------------------------------------------------*/
89
92enum class Union {
95 };
96
100enum class Compression {
103 };
104
108 class Policy = NoObservers >
110{
111
112/*--------------------------------------------------------------------------*/
113 public:
120 using Subject = typename Policy::template bind< modifications ,
121 DisjointSets >;
122
125 explicit DisjointSets( int n = 0 );
126
130 template< class... O >
131 requires( sizeof...( O ) > 0 )
132 DisjointSets( int n , O &... o );
133
137 void reset( int n );
138
140 [[nodiscard]] int n( void ) const { return( int( f_p.size() ) ); }
141
143 [[nodiscard]] int count( void ) const { return( f_count ); }
144
162 int find( int x );
163
184 bool merge( int x , int y );
185
188 bool same( int x , int y );
189
192 int size( int x )
193 requires( L == Union::SIZE );
194
195/*--------------------------- THE OBSERVERS --------------------------------*/
196
198 [[nodiscard]] Subject & subject( void ) { return( f_sub ); }
200 [[nodiscard]] const Subject & subject( void ) const { return( f_sub ); }
201
204 template< class O >
205 int attach( O & o )
206 requires( Subject::dynamic )
207 {
208 return( f_sub.attach( o ) );
209 }
210
213 void detach( int id )
214 requires( Subject::dynamic )
215 {
216 f_sub.detach( id );
217 }
218
219/*--------------------------------------------------------------------------*/
220 private:
223 void link( int x , int y );
224
226 void linkTold( int x , int y );
227
229 ADS_COLD_PATH void linkCold( int x , int y ) { linkTold( x , y ); }
230
233 [[nodiscard]] bool forest( void ) const;
234
235 std::vector< int > f_p;
236 int f_count = 0;
238
239 }; // end( class DisjointSets )
240
243
244/*--------------------------------------------------------------------------*/
245
246} // end( namespace ADS )
247
248/*--------------------------------------------------------------------------*/
249/*-------------------------- IMPLEMENTATION --------------------------------*/
250/*--------------------------------------------------------------------------*/
251
252#include "ADS/mfset/impl/UnionFind.tpp"
253
254/*--------------------------------------------------------------------------*/
255
256#endif /* UnionFind.h included */
257
258/*--------------------------------------------------------------------------*/
259/*-------------------------- End File UnionFind.h --------------------------*/
260/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The typed modifications, beyond the book: the vocabulary by which a structure tells the observers att...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
the union of disjoint sets of , the roots joined by L, the paths shortened by C, the links of the roo...
Definition UnionFind.h:110
void reset(int n)
back to the components
int find(int x)
the representative of the component of
DisjointSets(int n, O &... o)
the components , the static observers o...
int count(void) const
the number of components
Definition UnionFind.h:143
DisjointSets(int n=0)
the components
typename Policy::template bind< modifications, DisjointSets > Subject
the subject
Definition UnionFind.h:120
const Subject & subject(void) const
the subject
Definition UnionFind.h:200
int attach(O &o)
attaches the observer and gives its number
Definition UnionFind.h:205
bool same(int x, int y)
whether and are in the same component
int size(int x)
the number of elements of the component of
Subject & subject(void)
the subject, for ADS::batch() and ADS::quiet()
Definition UnionFind.h:198
void detach(int id)
detaches the observer number
Definition UnionFind.h:213
mod::Changed< mod::Parent, int, int > Linked
the link of a root under another root: the parent of the handle from to the other root
Definition UnionFind.h:116
mod::list< Linked, mod::Rebuilt > modifications
the events: a root linked under another, the forest made anew
Definition UnionFind.h:118
bool merge(int x, int y)
joins the components of and , the smaller (of lower rank) under the larger, under the root of if th...
int n(void) const
the number of elements
Definition UnionFind.h:140
#define ADS_COLD_PATH
a function of a rare path: never inlined, laid out away from the hot code
Definition Portable.h:107
#define ADS_NO_UNIQUE_ADDRESS
an empty member that takes no storage, by the attribute that does so (the standard one is accepted bu...
Definition Portable.h:81
Union
how the roots of two trees are joined: the smaller tree under the root of the larger,...
Definition UnionFind.h:92
DisjointSets<> UnionFind
the union by size with path halving
Definition UnionFind.h:242
Compression
how find() shortens the path: halving it (every other node on the way made a child of its grandparent...
Definition UnionFind.h:100
@ RANK
the lower rank under the higher
Definition UnionFind.h:94
@ SIZE
the smaller tree under the root of the larger
Definition UnionFind.h:93
@ FULL
each node under the root
Definition UnionFind.h:102
@ HALVING
half the path under grandparents
Definition UnionFind.h:101
Definition ADT.h:96
the attribute A of the item of handle changed from from to to
Definition Modification.h:600
a list of types, the events of a structure [see Modification.h]
Definition Modification.h:482