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