Blender V5.0
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#pragma once
6
7#include "util/array.h"
8#include <utility>
9
11
13 private:
14 array<size_t> parents;
15 array<size_t> ranks;
16
17 public:
18 DisjointSet(const size_t size) : parents(size), ranks(size)
19 {
20 for (size_t i = 0; i < size; i++) {
21 parents[i] = i;
22 ranks[i] = 0;
23 }
24 }
25
26 size_t find(size_t x)
27 {
28 size_t root = x;
29 while (parents[root] != root) {
30 root = parents[root];
31 }
32 while (parents[x] != root) {
33 const size_t parent = parents[x];
34 parents[x] = root;
35 x = parent;
36 }
37 return root;
38 }
39
40 void join(const size_t x, const size_t y)
41 {
42 size_t x_root = find(x);
43 size_t y_root = find(y);
44
45 if (x_root == y_root) {
46 return;
47 }
48
49 if (ranks[x_root] < ranks[y_root]) {
50 std::swap(x_root, y_root);
51 }
52 parents[y_root] = x_root;
53
54 if (ranks[x_root] == ranks[y_root]) {
55 ranks[x_root]++;
56 }
57 }
58};
59
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
size_t find(size_t x)
void join(const size_t x, const size_t y)
DisjointSet(const size_t size)
#define CCL_NAMESPACE_END
i
Definition text_draw.cc:230