Skip to content
0.2.0-dev Download ZIP
147 lines ·5.3 KB Raw
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 */
37template <typename T, typename H = HashMapHasherDefault, typename C = HashMapComparatorDefault<T>, typename AL = DefaultAllocator>
38class 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 *r_element);
50
51 _FORCE_INLINE_ Element *insert_or_get(T p_object);
52
53public:
54 ~DisjointSet();
55
56 _FORCE_INLINE_ void insert(T p_object) { (void)insert_or_get(p_object); }
57
58 void create_union(T p_left, T p_right);
59
60 void get_representatives(Vector<T> &r_out_roots);
61
62 void get_members(Vector<T> &r_out_members, T p_representative);
63};
64
65/* FUNCTIONS */
66
67template <typename T, typename H, typename C, typename AL>
68DisjointSet<T, H, C, AL>::~DisjointSet() {
69 for (KeyValue<T, Element *> &E : elements) {
70 memdelete_allocator<Element, AL>(E.value);
71 }
72}
73
74template <typename T, typename H, typename C, typename AL>
75typename DisjointSet<T, H, C, AL>::Element *DisjointSet<T, H, C, AL>::get_parent(Element *r_element) {
76 if (r_element->parent != r_element) {
77 r_element->parent = get_parent(r_element->parent);
78 }
79
80 return r_element->parent;
81}
82
83template <typename T, typename H, typename C, typename AL>
84typename DisjointSet<T, H, C, AL>::Element *DisjointSet<T, H, C, AL>::insert_or_get(T p_object) {
85 typename MapT::Iterator itr = elements.find(p_object);
86 if (itr != nullptr) {
87 return itr->value;
88 }
89
90 Element *new_element = memnew_allocator(Element, AL);
91 new_element->object = p_object;
92 new_element->parent = new_element;
93 elements.insert(p_object, new_element);
94
95 return new_element;
96}
97
98template <typename T, typename H, typename C, typename AL>
99void DisjointSet<T, H, C, AL>::create_union(T p_left, T p_right) {
100 Element *x = insert_or_get(p_left);
101 Element *y = insert_or_get(p_right);
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
123template <typename T, typename H, typename C, typename AL>
124void DisjointSet<T, H, C, AL>::get_representatives(Vector<T> &r_out_representatives) {
125 for (KeyValue<T, Element *> &E : elements) {
126 Element *element = E.value;
127 if (element->parent == element) {
128 r_out_representatives.push_back(element->object);
129 }
130 }
131}
132
133template <typename T, typename H, typename C, typename AL>
134void DisjointSet<T, H, C, AL>::get_members(Vector<T> &r_out_members, T p_representative) {
135 typename MapT::Iterator rep_itr = elements.find(p_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 r_out_members.push_back(E.key);
145 }
146 }
147}