disjoint_set.h 5.2 KB

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