| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/templates/hash_map.h" |
| 34 |
#include "core/templates/vector.h" |
| 35 |
|
| 36 |
|
| 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 |
|
| 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 |
|
| 107 |
if (x_root == y_root) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
if (x_root->rank < y_root->rank) { |
| 113 |
SWAP(x_root, y_root); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 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 |
} |