Blender V4.3
disjoint_set.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#ifndef __UTIL_DISJOINT_SET_H__
6#define __UTIL_DISJOINT_SET_H__
7
8#include "util/array.h"
9#include <utility>
10
12
14 private:
15 array<size_t> parents;
16 array<size_t> ranks;
17
18 public:
19 DisjointSet(size_t size) : parents(size), ranks(size)
20 {
21 for (size_t i = 0; i < size; i++) {
22 parents[i] = i;
23 ranks[i] = 0;
24 }
25 }
26
27 size_t find(size_t x)
28 {
29 size_t root = x;
30 while (parents[root] != root) {
31 root = parents[root];
32 }
33 while (parents[x] != root) {
34 size_t parent = parents[x];
35 parents[x] = root;
36 x = parent;
37 }
38 return root;
39 }
40
41 void join(size_t x, size_t y)
42 {
43 size_t x_root = find(x);
44 size_t y_root = find(y);
45
46 if (x_root == y_root) {
47 return;
48 }
49
50 if (ranks[x_root] < ranks[y_root]) {
51 std::swap(x_root, y_root);
52 }
53 parents[y_root] = x_root;
54
55 if (ranks[x_root] == ranks[y_root]) {
56 ranks[x_root]++;
57 }
58 }
59};
60
62
63#endif /* __UTIL_DISJOINT_SET_H__ */
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
void join(size_t x, size_t y)
DisjointSet(size_t size)
size_t find(size_t x)
#define CCL_NAMESPACE_END