disjoint_set.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*************************************************************************/
  2. /* disjoint_set.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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/map.h"
  33. #include "core/vector.h"
  34. /**
  35. @author Marios Staikopoulos <marios@staik.net>
  36. */
  37. /* This DisjointSet class uses Find with path compression and Union by rank */
  38. template <typename T, class C = Comparator<T>, class AL = DefaultAllocator>
  39. class DisjointSet {
  40. struct Element {
  41. T object;
  42. Element *parent = nullptr;
  43. int rank = 0;
  44. };
  45. typedef Map<T, Element *, C, AL> MapT;
  46. MapT elements;
  47. Element *get_parent(Element *element);
  48. _FORCE_INLINE_ Element *insert_or_get(T object);
  49. public:
  50. ~DisjointSet();
  51. _FORCE_INLINE_ void insert(T object) { (void)insert_or_get(object); }
  52. void create_union(T a, T b);
  53. void get_representatives(Vector<T> &out_roots);
  54. void get_members(Vector<T> &out_members, T representative);
  55. };
  56. /* FUNCTIONS */
  57. template <typename T, class C, class AL>
  58. DisjointSet<T, C, AL>::~DisjointSet() {
  59. for (typename MapT::Element *itr = elements.front(); itr != nullptr; itr = itr->next()) {
  60. memdelete_allocator<Element, AL>(itr->value());
  61. }
  62. }
  63. template <typename T, class C, class AL>
  64. typename DisjointSet<T, C, AL>::Element *DisjointSet<T, C, AL>::get_parent(Element *element) {
  65. if (element->parent != element) {
  66. element->parent = get_parent(element->parent);
  67. }
  68. return element->parent;
  69. }
  70. template <typename T, class C, class AL>
  71. typename DisjointSet<T, C, AL>::Element *DisjointSet<T, C, AL>::insert_or_get(T object) {
  72. typename MapT::Element *itr = elements.find(object);
  73. if (itr != nullptr) {
  74. return itr->value();
  75. }
  76. Element *new_element = memnew_allocator(Element, AL);
  77. new_element->object = object;
  78. new_element->parent = new_element;
  79. elements.insert(object, new_element);
  80. return new_element;
  81. }
  82. template <typename T, class C, class AL>
  83. void DisjointSet<T, C, AL>::create_union(T a, T b) {
  84. Element *x = insert_or_get(a);
  85. Element *y = insert_or_get(b);
  86. Element *x_root = get_parent(x);
  87. Element *y_root = get_parent(y);
  88. // Already in the same set
  89. if (x_root == y_root)
  90. return;
  91. // Not in the same set, merge
  92. if (x_root->rank < y_root->rank) {
  93. SWAP(x_root, y_root);
  94. }
  95. // Merge y_root into x_root
  96. y_root->parent = x_root;
  97. if (x_root->rank == y_root->rank) {
  98. ++x_root->rank;
  99. }
  100. }
  101. template <typename T, class C, class AL>
  102. void DisjointSet<T, C, AL>::get_representatives(Vector<T> &out_representatives) {
  103. for (typename MapT::Element *itr = elements.front(); itr != nullptr; itr = itr->next()) {
  104. Element *element = itr->value();
  105. if (element->parent == element) {
  106. out_representatives.push_back(element->object);
  107. }
  108. }
  109. }
  110. template <typename T, class C, class AL>
  111. void DisjointSet<T, C, AL>::get_members(Vector<T> &out_members, T representative) {
  112. typename MapT::Element *rep_itr = elements.find(representative);
  113. ERR_FAIL_COND(rep_itr == nullptr);
  114. Element *rep_element = rep_itr->value();
  115. ERR_FAIL_COND(rep_element->parent != rep_element);
  116. for (typename MapT::Element *itr = elements.front(); itr != nullptr; itr = itr->next()) {
  117. Element *parent = get_parent(itr->value());
  118. if (parent == rep_element) {
  119. out_members.push_back(itr->key());
  120. }
  121. }
  122. }
  123. #endif