mirror of https://github.com/OpenTTD/OpenTTD
(svn r20685) -Codechange: Make BinaryHeap_Clear() a method.
parent
b06cedc905
commit
2c962548e5
|
@ -221,7 +221,7 @@ void AyStarMain_Clear(AyStar *aystar)
|
||||||
{
|
{
|
||||||
/* Clean the Queue, but not the elements within. That will be done by
|
/* Clean the Queue, but not the elements within. That will be done by
|
||||||
* the hash. */
|
* the hash. */
|
||||||
aystar->OpenListQueue.clear(&aystar->OpenListQueue, false);
|
aystar->OpenListQueue.Clear(false);
|
||||||
/* Clean the hashes */
|
/* Clean the hashes */
|
||||||
clear_Hash(&aystar->OpenListHash, true);
|
clear_Hash(&aystar->OpenListHash, true);
|
||||||
clear_Hash(&aystar->ClosedListHash, true);
|
clear_Hash(&aystar->ClosedListHash, true);
|
||||||
|
|
|
@ -30,14 +30,19 @@
|
||||||
/** Temporary duplicate of #BIN_HEAP_ARR, except it uses 'this' instead of 'q'. */
|
/** Temporary duplicate of #BIN_HEAP_ARR, except it uses 'this' instead of 'q'. */
|
||||||
#define THISBIN_HEAP_ARR(i) this->elements[((i) - 1) >> BINARY_HEAP_BLOCKSIZE_BITS][((i) - 1) & BINARY_HEAP_BLOCKSIZE_MASK]
|
#define THISBIN_HEAP_ARR(i) this->elements[((i) - 1) >> BINARY_HEAP_BLOCKSIZE_BITS][((i) - 1) & BINARY_HEAP_BLOCKSIZE_MASK]
|
||||||
|
|
||||||
static void BinaryHeap_Clear(Queue *q, bool free_values)
|
/**
|
||||||
|
* Clears the queue, by removing all values from it. Its state is
|
||||||
|
* effectively reset. If free_items is true, each of the items cleared
|
||||||
|
* in this way are free()'d.
|
||||||
|
*/
|
||||||
|
void Queue::Clear(bool free_values)
|
||||||
{
|
{
|
||||||
/* Free all items if needed and free all but the first blocks of memory */
|
/* Free all items if needed and free all but the first blocks of memory */
|
||||||
uint i;
|
uint i;
|
||||||
uint j;
|
uint j;
|
||||||
|
|
||||||
for (i = 0; i < q->blocks; i++) {
|
for (i = 0; i < this->blocks; i++) {
|
||||||
if (q->elements[i] == NULL) {
|
if (this->elements[i] == NULL) {
|
||||||
/* No more allocated blocks */
|
/* No more allocated blocks */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -45,21 +50,21 @@ static void BinaryHeap_Clear(Queue *q, bool free_values)
|
||||||
if (free_values) {
|
if (free_values) {
|
||||||
for (j = 0; j < (1 << BINARY_HEAP_BLOCKSIZE_BITS); j++) {
|
for (j = 0; j < (1 << BINARY_HEAP_BLOCKSIZE_BITS); j++) {
|
||||||
/* For every element in the block */
|
/* For every element in the block */
|
||||||
if ((q->size >> BINARY_HEAP_BLOCKSIZE_BITS) == i &&
|
if ((this->size >> BINARY_HEAP_BLOCKSIZE_BITS) == i &&
|
||||||
(q->size & BINARY_HEAP_BLOCKSIZE_MASK) == j) {
|
(this->size & BINARY_HEAP_BLOCKSIZE_MASK) == j) {
|
||||||
break; // We're past the last element
|
break; // We're past the last element
|
||||||
}
|
}
|
||||||
free(q->elements[i][j].item);
|
free(this->elements[i][j].item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (i != 0) {
|
if (i != 0) {
|
||||||
/* Leave the first block of memory alone */
|
/* Leave the first block of memory alone */
|
||||||
free(q->elements[i]);
|
free(this->elements[i]);
|
||||||
q->elements[i] = NULL;
|
this->elements[i] = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
q->size = 0;
|
this->size = 0;
|
||||||
q->blocks = 1;
|
this->blocks = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -71,7 +76,7 @@ void Queue::Free(bool free_values)
|
||||||
{
|
{
|
||||||
uint i;
|
uint i;
|
||||||
|
|
||||||
this->clear(this, free_values);
|
this->Clear(free_values);
|
||||||
for (i = 0; i < this->blocks; i++) {
|
for (i = 0; i < this->blocks; i++) {
|
||||||
if (this->elements[i] == NULL) break;
|
if (this->elements[i] == NULL) break;
|
||||||
free(this->elements[i]);
|
free(this->elements[i]);
|
||||||
|
@ -223,7 +228,6 @@ void *Queue::Pop()
|
||||||
void init_BinaryHeap(Queue *q, uint max_size)
|
void init_BinaryHeap(Queue *q, uint max_size)
|
||||||
{
|
{
|
||||||
assert(q != NULL);
|
assert(q != NULL);
|
||||||
q->clear = BinaryHeap_Clear;
|
|
||||||
q->max_size = max_size;
|
q->max_size = max_size;
|
||||||
q->size = 0;
|
q->size = 0;
|
||||||
/* We malloc memory in block of BINARY_HEAP_BLOCKSIZE
|
/* We malloc memory in block of BINARY_HEAP_BLOCKSIZE
|
||||||
|
|
|
@ -18,9 +18,6 @@
|
||||||
//#define HASH_STATS
|
//#define HASH_STATS
|
||||||
|
|
||||||
|
|
||||||
struct Queue;
|
|
||||||
typedef void Queue_ClearProc(Queue *q, bool free_values);
|
|
||||||
|
|
||||||
struct BinaryHeapNode {
|
struct BinaryHeapNode {
|
||||||
void *item;
|
void *item;
|
||||||
int priority;
|
int priority;
|
||||||
|
@ -31,12 +28,7 @@ struct Queue {
|
||||||
bool Push(void *item, int priority);
|
bool Push(void *item, int priority);
|
||||||
void *Pop();
|
void *Pop();
|
||||||
bool Delete(void *item, int priority);
|
bool Delete(void *item, int priority);
|
||||||
|
void Clear(bool free_values);
|
||||||
/* Clears the queue, by removing all values from it. Its state is
|
|
||||||
* effectively reset. If free_items is true, each of the items cleared
|
|
||||||
* in this way are free()'d.
|
|
||||||
*/
|
|
||||||
Queue_ClearProc *clear;
|
|
||||||
void Free(bool free_values);
|
void Free(bool free_values);
|
||||||
|
|
||||||
uint max_size;
|
uint max_size;
|
||||||
|
|
Loading…
Reference in New Issue