Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/math/disjoint_set.h
9896 views
1
/**************************************************************************/
2
/* disjoint_set.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/templates/hash_map.h"
34
#include "core/templates/vector.h"
35
36
/* This DisjointSet class uses Find with path compression and Union by rank */
37
template <typename T, typename H = HashMapHasherDefault, typename C = HashMapComparatorDefault<T>, typename AL = DefaultAllocator>
38
class DisjointSet {
39
struct Element {
40
T object;
41
Element *parent = nullptr;
42
int rank = 0;
43
};
44
45
typedef HashMap<T, Element *, H, C> MapT;
46
47
MapT elements;
48
49
Element *get_parent(Element *element);
50
51
_FORCE_INLINE_ Element *insert_or_get(T object);
52
53
public:
54
~DisjointSet();
55
56
_FORCE_INLINE_ void insert(T object) { (void)insert_or_get(object); }
57
58
void create_union(T a, T b);
59
60
void get_representatives(Vector<T> &out_roots);
61
62
void get_members(Vector<T> &out_members, T representative);
63
};
64
65
/* FUNCTIONS */
66
67
template <typename T, typename H, typename C, typename AL>
68
DisjointSet<T, H, C, AL>::~DisjointSet() {
69
for (KeyValue<T, Element *> &E : elements) {
70
memdelete_allocator<Element, AL>(E.value);
71
}
72
}
73
74
template <typename T, typename H, typename C, typename AL>
75
typename DisjointSet<T, H, C, AL>::Element *DisjointSet<T, H, C, AL>::get_parent(Element *element) {
76
if (element->parent != element) {
77
element->parent = get_parent(element->parent);
78
}
79
80
return element->parent;
81
}
82
83
template <typename T, typename H, typename C, typename AL>
84
typename DisjointSet<T, H, C, AL>::Element *DisjointSet<T, H, C, AL>::insert_or_get(T object) {
85
typename MapT::Iterator itr = elements.find(object);
86
if (itr != nullptr) {
87
return itr->value;
88
}
89
90
Element *new_element = memnew_allocator(Element, AL);
91
new_element->object = object;
92
new_element->parent = new_element;
93
elements.insert(object, new_element);
94
95
return new_element;
96
}
97
98
template <typename T, typename H, typename C, typename AL>
99
void DisjointSet<T, H, C, AL>::create_union(T a, T b) {
100
Element *x = insert_or_get(a);
101
Element *y = insert_or_get(b);
102
103
Element *x_root = get_parent(x);
104
Element *y_root = get_parent(y);
105
106
// Already in the same set
107
if (x_root == y_root) {
108
return;
109
}
110
111
// Not in the same set, merge
112
if (x_root->rank < y_root->rank) {
113
SWAP(x_root, y_root);
114
}
115
116
// Merge y_root into x_root
117
y_root->parent = x_root;
118
if (x_root->rank == y_root->rank) {
119
++x_root->rank;
120
}
121
}
122
123
template <typename T, typename H, typename C, typename AL>
124
void DisjointSet<T, H, C, AL>::get_representatives(Vector<T> &out_representatives) {
125
for (KeyValue<T, Element *> &E : elements) {
126
Element *element = E.value;
127
if (element->parent == element) {
128
out_representatives.push_back(element->object);
129
}
130
}
131
}
132
133
template <typename T, typename H, typename C, typename AL>
134
void DisjointSet<T, H, C, AL>::get_members(Vector<T> &out_members, T representative) {
135
typename MapT::Iterator rep_itr = elements.find(representative);
136
ERR_FAIL_NULL(rep_itr);
137
138
Element *rep_element = rep_itr->value;
139
ERR_FAIL_COND(rep_element->parent != rep_element);
140
141
for (KeyValue<T, Element *> &E : elements) {
142
Element *parent = get_parent(E.value);
143
if (parent == rep_element) {
144
out_members.push_back(E.key);
145
}
146
}
147
}
148
149