disjoint_set.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #pragma once
  31. #include "core/templates/hash_map.h"
  32. #include "core/templates/vector.h"
  33. /* This DisjointSet class uses Find with path compression and Union by rank */
  34. template <typename T, typename H = HashMapHasherDefault, typename C = HashMapComparatorDefault<T>, typename AL = DefaultAllocator>
  35. class DisjointSet {
  36. struct Element {
  37. T object;
  38. Element *parent = nullptr;
  39. int rank = 0;
  40. };
  41. typedef HashMap<T, Element *, H, C> MapT;
  42. MapT elements;
  43. Element *get_parent(Element *element);
  44. _FORCE_INLINE_ Element *insert_or_get(T object);
  45. public:
  46. ~DisjointSet();
  47. _FORCE_INLINE_ void insert(T object) { (void)insert_or_get(object); }
  48. void create_union(T a, T b);
  49. void get_representatives(Vector<T> &out_roots);
  50. void get_members(Vector<T> &out_members, T representative);
  51. };
  52. /* FUNCTIONS */
  53. template <typename T, typename H, typename C, typename AL>
  54. DisjointSet<T, H, C, AL>::~DisjointSet() {
  55. for (KeyValue<T, Element *> &E : elements) {
  56. memdelete_allocator<Element, AL>(E.value);
  57. }
  58. }
  59. template <typename T, typename H, typename C, typename AL>
  60. typename DisjointSet<T, H, C, AL>::Element *DisjointSet<T, H, C, AL>::get_parent(Element *element) {
  61. if (element->parent != element) {
  62. element->parent = get_parent(element->parent);
  63. }
  64. return element->parent;
  65. }
  66. template <typename T, typename H, typename C, typename AL>
  67. typename DisjointSet<T, H, C, AL>::Element *DisjointSet<T, H, C, AL>::insert_or_get(T object) {
  68. typename MapT::Iterator itr = elements.find(object);
  69. if (itr != nullptr) {
  70. return itr->value;
  71. }
  72. Element *new_element = memnew_allocator(Element, AL);
  73. new_element->object = object;
  74. new_element->parent = new_element;
  75. elements.insert(object, new_element);
  76. return new_element;
  77. }
  78. template <typename T, typename H, typename C, typename AL>
  79. void DisjointSet<T, H, C, AL>::create_union(T a, T b) {
  80. Element *x = insert_or_get(a);
  81. Element *y = insert_or_get(b);
  82. Element *x_root = get_parent(x);
  83. Element *y_root = get_parent(y);
  84. // Already in the same set
  85. if (x_root == y_root) {
  86. return;
  87. }
  88. // Not in the same set, merge
  89. if (x_root->rank < y_root->rank) {
  90. SWAP(x_root, y_root);
  91. }
  92. // Merge y_root into x_root
  93. y_root->parent = x_root;
  94. if (x_root->rank == y_root->rank) {
  95. ++x_root->rank;
  96. }
  97. }
  98. template <typename T, typename H, typename C, typename AL>
  99. void DisjointSet<T, H, C, AL>::get_representatives(Vector<T> &out_representatives) {
  100. for (KeyValue<T, Element *> &E : elements) {
  101. Element *element = E.value;
  102. if (element->parent == element) {
  103. out_representatives.push_back(element->object);
  104. }
  105. }
  106. }
  107. template <typename T, typename H, typename C, typename AL>
  108. void DisjointSet<T, H, C, AL>::get_members(Vector<T> &out_members, T representative) {
  109. typename MapT::Iterator rep_itr = elements.find(representative);
  110. ERR_FAIL_NULL(rep_itr);
  111. Element *rep_element = rep_itr->value;
  112. ERR_FAIL_COND(rep_element->parent != rep_element);
  113. for (KeyValue<T, Element *> &E : elements) {
  114. Element *parent = get_parent(E.value);
  115. if (parent == rep_element) {
  116. out_members.push_back(E.key);
  117. }
  118. }
  119. }