Skip to content

Commit 638ba1e

Browse files
committed
docs: Enhance documentation for UnionFind class with detailed method descriptions
1 parent fcdc3be commit 638ba1e

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

weilycoder/ds/unionfind.hpp

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,61 @@
66
#include <vector>
77

88
namespace weilycoder {
9+
/**
10+
* @brief Union-Find (Disjoint Set Union) Data Structure
11+
* @tparam ptr_t Type for representing indices (default: size_t)
12+
*/
913
template <typename ptr_t = size_t> struct UnionFind {
10-
std::vector<ptr_t> parent, size;
14+
private:
15+
std::vector<ptr_t> parent, setSize;
1116

12-
UnionFind(ptr_t n) : parent(n), size(n, 1) {
17+
public:
18+
/**
19+
* @brief Constructs a UnionFind with n elements, each in its own set
20+
* @param n Number of elements
21+
*/
22+
UnionFind(ptr_t n) : parent(n), setSize(n, 1) {
1323
std::iota(parent.begin(), parent.end(), 0);
1424
}
1525

26+
/**
27+
* @brief Returns the number of elements
28+
* @return Number of elements
29+
*/
30+
size_t size() const { return parent.size(); }
31+
32+
/**
33+
* @brief Returns the size of the set containing x
34+
* @param x Element to query
35+
* @return Size of the set containing x
36+
*/
37+
size_t get_size(ptr_t x) { return setSize[getf(x)]; }
38+
39+
/**
40+
* @brief Finds the representative of the set containing x
41+
* @param x Element to find
42+
* @return Representative of the set containing x
43+
*/
1644
ptr_t getf(ptr_t x) {
1745
while (x != parent[x])
1846
x = parent[x] = parent[parent[x]];
1947
return x;
2048
}
2149

50+
/**
51+
* @brief Unites the sets containing x and y
52+
* @param x First element
53+
* @param y Second element
54+
* @return True if the sets were separate and have been united, false if they
55+
* were already in the same set
56+
*/
2257
bool unite(ptr_t x, ptr_t y) {
2358
x = getf(x), y = getf(y);
2459
if (x == y)
2560
return false;
26-
if (size[x] < size[y])
61+
if (setSize[x] < setSize[y])
2762
std::swap(x, y);
28-
parent[y] = x, size[x] += size[y];
63+
parent[y] = x, setSize[x] += setSize[y];
2964
return true;
3065
}
3166
};

0 commit comments

Comments
 (0)