nav_heap.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**************************************************************************/
  2. /* nav_heap.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/local_vector.h"
  32. template <typename T>
  33. struct NoopIndexer {
  34. void operator()(const T &p_value, uint32_t p_index) {}
  35. };
  36. /**
  37. * A max-heap implementation that notifies of element index changes.
  38. */
  39. template <typename T, typename LessThan = Comparator<T>, typename Indexer = NoopIndexer<T>>
  40. class Heap {
  41. LocalVector<T> _buffer;
  42. LessThan _less_than;
  43. Indexer _indexer;
  44. public:
  45. static constexpr uint32_t INVALID_INDEX = UINT32_MAX;
  46. void reserve(uint32_t p_size) {
  47. _buffer.reserve(p_size);
  48. }
  49. uint32_t size() const {
  50. return _buffer.size();
  51. }
  52. bool is_empty() const {
  53. return _buffer.is_empty();
  54. }
  55. void push(const T &p_element) {
  56. _buffer.push_back(p_element);
  57. _indexer(p_element, _buffer.size() - 1);
  58. _shift_up(_buffer.size() - 1);
  59. }
  60. T pop() {
  61. ERR_FAIL_COND_V_MSG(_buffer.is_empty(), T(), "Can't pop an empty heap.");
  62. T value = _buffer[0];
  63. _indexer(value, INVALID_INDEX);
  64. if (_buffer.size() > 1) {
  65. _buffer[0] = _buffer[_buffer.size() - 1];
  66. _indexer(_buffer[0], 0);
  67. _buffer.remove_at(_buffer.size() - 1);
  68. _shift_down(0);
  69. } else {
  70. _buffer.remove_at(_buffer.size() - 1);
  71. }
  72. return value;
  73. }
  74. /**
  75. * Update the position of the element in the heap if necessary.
  76. */
  77. void shift(uint32_t p_index) {
  78. ERR_FAIL_UNSIGNED_INDEX_MSG(p_index, _buffer.size(), "Heap element index is out of range.");
  79. if (!_shift_up(p_index)) {
  80. _shift_down(p_index);
  81. }
  82. }
  83. void clear() {
  84. for (const T &value : _buffer) {
  85. _indexer(value, INVALID_INDEX);
  86. }
  87. _buffer.clear();
  88. }
  89. Heap() {}
  90. Heap(const LessThan &p_less_than) :
  91. _less_than(p_less_than) {}
  92. Heap(const Indexer &p_indexer) :
  93. _indexer(p_indexer) {}
  94. Heap(const LessThan &p_less_than, const Indexer &p_indexer) :
  95. _less_than(p_less_than), _indexer(p_indexer) {}
  96. private:
  97. bool _shift_up(uint32_t p_index) {
  98. T value = _buffer[p_index];
  99. uint32_t current_index = p_index;
  100. uint32_t parent_index = (current_index - 1) / 2;
  101. while (current_index > 0 && _less_than(_buffer[parent_index], value)) {
  102. _buffer[current_index] = _buffer[parent_index];
  103. _indexer(_buffer[current_index], current_index);
  104. current_index = parent_index;
  105. parent_index = (current_index - 1) / 2;
  106. }
  107. if (current_index != p_index) {
  108. _buffer[current_index] = value;
  109. _indexer(value, current_index);
  110. return true;
  111. } else {
  112. return false;
  113. }
  114. }
  115. bool _shift_down(uint32_t p_index) {
  116. T value = _buffer[p_index];
  117. uint32_t current_index = p_index;
  118. uint32_t child_index = 2 * current_index + 1;
  119. while (child_index < _buffer.size()) {
  120. if (child_index + 1 < _buffer.size() &&
  121. _less_than(_buffer[child_index], _buffer[child_index + 1])) {
  122. child_index++;
  123. }
  124. if (_less_than(_buffer[child_index], value)) {
  125. break;
  126. }
  127. _buffer[current_index] = _buffer[child_index];
  128. _indexer(_buffer[current_index], current_index);
  129. current_index = child_index;
  130. child_index = 2 * current_index + 1;
  131. }
  132. if (current_index != p_index) {
  133. _buffer[current_index] = value;
  134. _indexer(value, current_index);
  135. return true;
  136. } else {
  137. return false;
  138. }
  139. }
  140. };