OpenTTD Source: src/pathfinder/npf/queue.cpp Source File

OpenTTD Source 14.1

queue.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10 #include "../../stdafx.h"
11 #include "../../core/alloc_func.hpp"
12 #include "queue.h"
13
14 #include "../../safeguards.h"
15
16
17 /*
18 * Binary Heap
19 * For information, see: http://www.policyalmanac.org/games/binaryHeaps.htm
20 */
21
22 const int BinaryHeap::BINARY_HEAP_BLOCKSIZE_BITS = 10;
23 const int BinaryHeap::BINARY_HEAP_BLOCKSIZE = 1 << BinaryHeap::BINARY_HEAP_BLOCKSIZE_BITS;
24 const int BinaryHeap::BINARY_HEAP_BLOCKSIZE_MASK = BinaryHeap::BINARY_HEAP_BLOCKSIZE - 1;
25
31 void BinaryHeap::Clear(bool free_values)
32 {
33 /* Free all items if needed and free all but the first blocks of memory */
34 uint i;
35 uint j;
36
37 for (i = 0; i < this->blocks; i++) {
38 if (this->elements[i] == nullptr) {
39 /* No more allocated blocks */
40 break;
41 }
42 /* For every allocated block */
43 if (free_values) {
44 for (j = 0; j < (1 << BINARY_HEAP_BLOCKSIZE_BITS); j++) {
45 /* For every element in the block */
46 if ((this->size >> BINARY_HEAP_BLOCKSIZE_BITS) == i &&
47 (this->size & BINARY_HEAP_BLOCKSIZE_MASK) == j) {
48 break; // We're past the last element
49 }
50 free(this->elements[i][j].item);
51 }
52 }
53 if (i != 0) {
54 /* Leave the first block of memory alone */
55 free(this->elements[i]);
56 this->elements[i] = nullptr;
57 }
58 }
59 this->size = 0;
60 this->blocks = 1;
61 }
62
68 void BinaryHeap::Free(bool free_values)
69 {
70 uint i;
71
72 this->Clear(free_values);
73 for (i = 0; i < this->blocks; i++) {
74 if (this->elements[i] == nullptr) break;
75 free(this->elements[i]);
76 }
77 free(this->elements);
78 }
79
84 bool BinaryHeap::Push(void *item, int priority)
85 {
86 if (this->size == this->max_size) return false;
87 assert(this->size < this->max_size);
88
89 if (this->elements[this->size >> BINARY_HEAP_BLOCKSIZE_BITS] == nullptr) {
90 /* The currently allocated blocks are full, allocate a new one */
91 assert((this->size & BINARY_HEAP_BLOCKSIZE_MASK) == 0);
92 this->elements[this->size >> BINARY_HEAP_BLOCKSIZE_BITS] = MallocT<BinaryHeapNode>(BINARY_HEAP_BLOCKSIZE);
93 this->blocks++;
94 }
95
96 /* Add the item at the end of the array */
97 this->GetElement(this->size + 1).priority = priority;
98 this->GetElement(this->size + 1).item = item;
99 this->size++;
100
101 /* Now we are going to check where it belongs. As long as the parent is
102 * bigger, we switch with the parent */
103 {
104 BinaryHeapNode temp;
105 int i;
106 int j;
107
108 i = this->size;
109 while (i > 1) {
110 /* Get the parent of this object (divide by 2) */
111 j = i / 2;
112 /* Is the parent bigger than the current, switch them */
113 if (this->GetElement(i).priority <= this->GetElement(j).priority) {
114 temp = this->GetElement(j);
115 this->GetElement(j) = this->GetElement(i);
116 this->GetElement(i) = temp;
117 i = j;
118 } else {
119 /* It is not, we're done! */
120 break;
121 }
122 }
123 }
124
125 return true;
126 }
127
133 bool BinaryHeap::Delete(void *item, int)
134 {
135 uint i = 0;
136
137 /* First, we try to find the item.. */
138 do {
139 if (this->GetElement(i + 1).item == item) break;
140 i++;
141 } while (i < this->size);
142 /* We did not find the item, so we return false */
143 if (i == this->size) return false;
144
145 /* Now we put the last item over the current item while decreasing the size of the elements */
146 this->size--;
147 this->GetElement(i + 1) = this->GetElement(this->size + 1);
148
149 /* Now the only thing we have to do, is resort it..
150 * On place i there is the item to be sorted.. let's start there */
151 {
152 uint j;
153 BinaryHeapNode temp;
154 /* Because of the fact that Binary Heap uses array from 1 to n, we need to
155 * increase i by 1
156 */
157 i++;
158
159 for (;;) {
160 j = i;
161 /* Check if we have 2 children */
162 if (2 * j + 1 <= this->size) {
163 /* Is this child smaller than the parent? */
164 if (this->GetElement(j).priority >= this->GetElement(2 * j).priority) i = 2 * j;
165 /* Yes, we _need_ to use i here, not j, because we want to have the smallest child
166 * This way we get that straight away! */
167 if (this->GetElement(i).priority >= this->GetElement(2 * j + 1).priority) i = 2 * j + 1;
168 /* Do we have one child? */
169 } else if (2 * j <= this->size) {
170 if (this->GetElement(j).priority >= this->GetElement(2 * j).priority) i = 2 * j;
171 }
172
173 /* One of our children is smaller than we are, switch */
174 if (i != j) {
175 temp = this->GetElement(j);
176 this->GetElement(j) = this->GetElement(i);
177 this->GetElement(i) = temp;
178 } else {
179 /* None of our children is smaller, so we stay here.. stop :) */
180 break;
181 }
182 }
183 }
184
185 return true;
186 }
187
192 void *BinaryHeap::Pop()
193 {
194 void *result;
195
196 if (this->size == 0) return nullptr;
197
198 /* The best item is always on top, so give that as result */
199 result = this->GetElement(1).item;
200 /* And now we should get rid of this item... */
201 this->Delete(this->GetElement(1).item, this->GetElement(1).priority);
202
203 return result;
204 }
205
210 void BinaryHeap::Init(uint max_size)
211 {
212 this->max_size = max_size;
213 this->size = 0;
214 /* We malloc memory in block of BINARY_HEAP_BLOCKSIZE
215 * It autosizes when it runs out of memory */
216 this->elements = CallocT<BinaryHeapNode*>((max_size - 1) / BINARY_HEAP_BLOCKSIZE + 1);
217 this->elements[0] = MallocT<BinaryHeapNode>(BINARY_HEAP_BLOCKSIZE);
218 this->blocks = 1;
219 }
220
221 /* Because we don't want anyone else to bother with our defines */
222 #undef BIN_HEAP_ARR
223
224 /*
225 * Hash
226 */
227
232 void Hash::Init(Hash_HashProc *hash, uint num_buckets)
233 {
234 /* Allocate space for the Hash, the buckets and the bucket flags */
235 uint i;
236
237 /* Ensure the size won't overflow. */
238 CheckAllocationConstraints(sizeof(*this->buckets) + sizeof(*this->buckets_in_use), num_buckets);
239
240 this->hash = hash;
241 this->size = 0;
242 this->num_buckets = num_buckets;
243 this->buckets = (HashNode*)MallocT<byte>(num_buckets * (sizeof(*this->buckets) + sizeof(*this->buckets_in_use)));
244 this->buckets_in_use = (bool*)(this->buckets + num_buckets);
245 for (i = 0; i < num_buckets; i++) this->buckets_in_use[i] = false;
246 }
247
253 void Hash::Delete(bool free_values)
254 {
255 uint i;
256
257 /* Iterate all buckets */
258 for (i = 0; i < this->num_buckets; i++) {
259 if (this->buckets_in_use[i]) {
260 HashNode *node;
261
262 /* Free the first value */
263 if (free_values) free(this->buckets[i].value);
264 node = this->buckets[i].next;
265 while (node != nullptr) {
266 HashNode *prev = node;
267
268 node = node->next;
269 /* Free the value */
270 if (free_values) free(prev->value);
271 /* Free the node */
272 free(prev);
273 }
274 }
275 }
276 free(this->buckets);
277 /* No need to free buckets_in_use, it is always allocated in one
278 * malloc with buckets */
279 }
280
281 #ifdef HASH_STATS
282 void Hash::PrintStatistics() const
283 {
284 uint used_buckets = 0;
285 uint max_collision = 0;
286 uint max_usage = 0;
287 uint usage[200];
288 uint i;
289
290 for (i = 0; i < lengthof(usage); i++) usage[i] = 0;
291 for (i = 0; i < this->num_buckets; i++) {
292 uint collision = 0;
293 if (this->buckets_in_use[i]) {
294 const HashNode *node;
295
296 used_buckets++;
297 for (node = &this->buckets[i]; node != nullptr; node = node->next) collision++;
298 if (collision > max_collision) max_collision = collision;
299 }
300 if (collision >= lengthof(usage)) collision = lengthof(usage) - 1;
301 usage[collision]++;
302 if (collision > 0 && usage[collision] >= max_usage) {
303 max_usage = usage[collision];
304 }
305 }
306 Debug(misc, 0, "Hash size: {}, Nodes used: {}, Non empty buckets: {}, Max collision: {}",
307 this->num_buckets, this->size, used_buckets, max_collision
308 );
309 std::string line;
310 line += "{ ";
311 for (i = 0; i <= max_collision; i++) {
312 if (usage[i] > 0) {
313 fmt::format_to(std::back_inserter(line), "{}:{} ", i, usage[i]);
314 #if 0
315 if (i > 0) {
316 uint j;
317
318 for (j = 0; j < usage[i] * 160 / 800; j++) line += "#";
319 }
320 line += "\n";
321 #endif
322 }
323 }
324 line += "}";
325 Debug(misc, 0, "{}", line);
326 }
327 #endif
328
332 void Hash::Clear(bool free_values)
333 {
334 uint i;
335
336 #ifdef HASH_STATS
337 if (this->size > 2000) this->PrintStatistics();
338 #endif
339
340 /* Iterate all buckets */
341 for (i = 0; i < this->num_buckets; i++) {
342 if (this->buckets_in_use[i]) {
343 HashNode *node;
344
345 this->buckets_in_use[i] = false;
346 /* Free the first value */
347 if (free_values) free(this->buckets[i].value);
348 node = this->buckets[i].next;
349 while (node != nullptr) {
350 HashNode *prev = node;
351
352 node = node->next;
353 if (free_values) free(prev->value);
354 free(prev);
355 }
356 }
357 }
358 this->size = 0;
359 }
360
369 HashNode *Hash::FindNode(TileIndex tile, Trackdir dir, HashNode** prev_out) const
370 {
371 uint hash = this->hash(tile, dir);
372 HashNode *result = nullptr;
373
374 /* Check if the bucket is empty */
375 if (!this->buckets_in_use[hash]) {
376 if (prev_out != nullptr) *prev_out = nullptr;
377 result = nullptr;
378 /* Check the first node specially */
379 } else if (this->buckets[hash].tile == tile && this->buckets[hash].dir == dir) {
380 /* Save the value */
381 result = this->buckets + hash;
382 if (prev_out != nullptr) *prev_out = nullptr;
383 /* Check all other nodes */
384 } else {
385 HashNode *prev = this->buckets + hash;
386 HashNode *node;
387
388 for (node = prev->next; node != nullptr; node = node->next) {
389 if (node->tile == tile && node->dir == dir) {
390 /* Found it */
391 result = node;
392 break;
393 }
394 prev = node;
395 }
396 if (prev_out != nullptr) *prev_out = prev;
397 }
398 return result;
399 }
400
406 void *Hash::DeleteValue(TileIndex tile, Trackdir dir)
407 {
408 void *result;
409 HashNode *prev; // Used as output var for below function call
410 HashNode *node = this->FindNode(tile, dir, &prev);
411
412 if (node == nullptr) {
413 /* not found */
414 result = nullptr;
415 } else if (prev == nullptr) {
416 /* It is in the first node, we can't free that one, so we free
417 * the next one instead (if there is any)*/
418 /* Save the value */
419 result = node->value;
420 if (node->next != nullptr) {
421 HashNode *next = node->next;
422 /* Copy the second to the first */
423 *node = *next;
424 /* Free the second */
425 free(next);
426 } else {
427 /* This was the last in this bucket
428 * Mark it as empty */
429 uint hash = this->hash(tile, dir);
430 this->buckets_in_use[hash] = false;
431 }
432 } else {
433 /* It is in another node
434 * Save the value */
435 result = node->value;
436 /* Link previous and next nodes */
437 prev->next = node->next;
438 /* Free the node */
439 free(node);
440 }
441 if (result != nullptr) this->size--;
442 return result;
443 }
444
449 void *Hash::Set(TileIndex tile, Trackdir dir, void *value)
450 {
451 HashNode *prev;
452 HashNode *node = this->FindNode(tile, dir, &prev);
453
454 if (node != nullptr) {
455 /* Found it */
456 void *result = node->value;
457
458 node->value = value;
459 return result;
460 }
461 /* It is not yet present, let's add it */
462 if (prev == nullptr) {
463 /* The bucket is still empty */
464 uint hash = this->hash(tile, dir);
465 this->buckets_in_use[hash] = true;
466 node = this->buckets + hash;
467 } else {
468 /* Add it after prev */
469 node = MallocT<HashNode>(1);
470 prev->next = node;
471 }
472 node->next = nullptr;
473 node->tile = tile;
474 node->dir = dir;
475 node->value = value;
476 this->size++;
477 return nullptr;
478 }
479
484 void *Hash::Get(TileIndex tile, Trackdir dir) const
485 {
486 HashNode *node = this->FindNode(tile, dir, nullptr);
487
488 return (node != nullptr) ? node->value : nullptr;
489 }
Hash::FindNode
HashNode * FindNode(TileIndex tile, Trackdir dir, HashNode **prev_out) const
Finds the node that that saves this key pair.
Definition: queue.cpp:369
BinaryHeap::Clear
void Clear(bool free_values)
Clears the queue, by removing all values from it.
Definition: queue.cpp:31
Hash::Delete
void Delete(bool free_values)
Deletes the hash and cleans up.
Definition: queue.cpp:253
BinaryHeap::Delete
bool Delete(void *item, int priority)
Deletes the item from the queue.
Definition: queue.cpp:133
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
HashNode
Definition: queue.h:63
Hash::Set
void * Set(TileIndex tile, Trackdir dir, void *value)
Sets the value associated with the given key pair to the given value.
Definition: queue.cpp:449
Debug
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
BinaryHeapNode
Definition: queue.h:19
BinaryHeap::GetElement
BinaryHeapNode & GetElement(uint i)
Get an element from the #elements.
Definition: queue.h:47
free
void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:382
BinaryHeap::blocks
uint blocks
The amount of blocks for which space is reserved in elements.
Definition: queue.h:55
Hash::Init
void Init(Hash_HashProc *hash, uint num_buckets)
Builds a new hash in an existing struct.
Definition: queue.cpp:232
lengthof
#define lengthof(array)
Return the length of an fixed size array.
Definition: stdafx.h:303
queue.h
BinaryHeap::Pop
void * Pop()
Pops the first element from the queue.
Definition: queue.cpp:192
Hash::DeleteValue
void * DeleteValue(TileIndex tile, Trackdir dir)
Deletes the value with the specified key pair from the hash and returns that value.
Definition: queue.cpp:406
Hash_HashProc
uint Hash_HashProc(TileIndex tile, Trackdir dir)
Generates a hash code from the given key pair.
Definition: queue.h:73
BinaryHeap::BINARY_HEAP_BLOCKSIZE_BITS
static const int BINARY_HEAP_BLOCKSIZE_BITS
The number of elements that will be malloc'd at a time.
Definition: queue.h:31
CheckAllocationConstraints
void CheckAllocationConstraints(size_t element_size, size_t num_elements)
Checks whether allocating memory would overflow size_t.
Definition: alloc_func.hpp:29
BinaryHeap::Free
void Free(bool free_values)
Frees the queue, by reclaiming all memory allocated by it.
Definition: queue.cpp:68
Trackdir
Trackdir
Enumeration for tracks and directions.
Definition: track_type.h:67
BinaryHeap::Push
bool Push(void *item, int priority)
Pushes an element into the queue, at the appropriate place for the queue.
Definition: queue.cpp:84
BinaryHeap::Init
void Init(uint max_size)
Initializes a binary heap and allocates internal memory for maximum of max_size elements.
Definition: queue.cpp:210
Hash::Get
void * Get(TileIndex tile, Trackdir dir) const
Gets the value associated with the given key pair, or nullptr when it is not present.
Definition: queue.cpp:484
Hash::Clear
void Clear(bool free_values)
Cleans the hash, but keeps the memory allocated.
Definition: queue.cpp:332