mirror of https://github.com/OpenTTD/OpenTTD
(svn r20681) -Codechange: Make BinaryHeap_Push() a method, introduce temporary THISBIN_HEAP_ARR macro.
parent
68e2a07479
commit
92801ac718
|
@ -82,7 +82,7 @@ static void AyStarMain_OpenList_Add(AyStar *aystar, PathNode *parent, const AySt
|
||||||
Hash_Set(&aystar->OpenListHash, node->tile, node->direction, new_node);
|
Hash_Set(&aystar->OpenListHash, node->tile, node->direction, new_node);
|
||||||
|
|
||||||
/* Add it to the queue */
|
/* Add it to the queue */
|
||||||
aystar->OpenListQueue.push(&aystar->OpenListQueue, new_node, f);
|
aystar->OpenListQueue.Push(new_node, f);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -136,7 +136,7 @@ static int AyStarMain_CheckTile(AyStar *aystar, AyStarNode *current, OpenListNod
|
||||||
check->path.node.user_data[i] = current->user_data[i];
|
check->path.node.user_data[i] = current->user_data[i];
|
||||||
}
|
}
|
||||||
/* Readd him in the OpenListQueue */
|
/* Readd him in the OpenListQueue */
|
||||||
aystar->OpenListQueue.push(&aystar->OpenListQueue, check, new_f);
|
aystar->OpenListQueue.Push(check, new_f);
|
||||||
} else {
|
} else {
|
||||||
/* A new node, add him to the OpenList */
|
/* A new node, add him to the OpenList */
|
||||||
AyStarMain_OpenList_Add(aystar, closedlist_parent, current, new_f, new_g);
|
AyStarMain_OpenList_Add(aystar, closedlist_parent, current, new_f, new_g);
|
||||||
|
|
|
@ -27,6 +27,8 @@
|
||||||
* and C with array from 0 to n-1, and we don't like typing
|
* and C with array from 0 to n-1, and we don't like typing
|
||||||
* q->elements[i - 1] every time, we use this define. */
|
* q->elements[i - 1] every time, we use this define. */
|
||||||
#define BIN_HEAP_ARR(i) q->elements[((i) - 1) >> BINARY_HEAP_BLOCKSIZE_BITS][((i) - 1) & BINARY_HEAP_BLOCKSIZE_MASK]
|
#define BIN_HEAP_ARR(i) q->elements[((i) - 1) >> BINARY_HEAP_BLOCKSIZE_BITS][((i) - 1) & BINARY_HEAP_BLOCKSIZE_MASK]
|
||||||
|
/** 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]
|
||||||
|
|
||||||
static void BinaryHeap_Clear(Queue *q, bool free_values)
|
static void BinaryHeap_Clear(Queue *q, bool free_values)
|
||||||
{
|
{
|
||||||
|
@ -72,29 +74,33 @@ static void BinaryHeap_Free(Queue *q, bool free_values)
|
||||||
free(q->elements);
|
free(q->elements);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool BinaryHeap_Push(Queue *q, void *item, int priority)
|
/**
|
||||||
|
* Pushes an element into the queue, at the appropriate place for the queue.
|
||||||
|
* Requires the queue pointer to be of an appropriate type, of course.
|
||||||
|
*/
|
||||||
|
bool Queue::Push(void *item, int priority)
|
||||||
{
|
{
|
||||||
#ifdef QUEUE_DEBUG
|
#ifdef QUEUE_DEBUG
|
||||||
printf("[BinaryHeap] Pushing an element. There are %d elements left\n", q->size);
|
printf("[BinaryHeap] Pushing an element. There are %d elements left\n", this->size);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (q->size == q->max_size) return false;
|
if (this->size == this->max_size) return false;
|
||||||
assert(q->size < q->max_size);
|
assert(this->size < this->max_size);
|
||||||
|
|
||||||
if (q->elements[q->size >> BINARY_HEAP_BLOCKSIZE_BITS] == NULL) {
|
if (this->elements[this->size >> BINARY_HEAP_BLOCKSIZE_BITS] == NULL) {
|
||||||
/* The currently allocated blocks are full, allocate a new one */
|
/* The currently allocated blocks are full, allocate a new one */
|
||||||
assert((q->size & BINARY_HEAP_BLOCKSIZE_MASK) == 0);
|
assert((this->size & BINARY_HEAP_BLOCKSIZE_MASK) == 0);
|
||||||
q->elements[q->size >> BINARY_HEAP_BLOCKSIZE_BITS] = MallocT<BinaryHeapNode>(BINARY_HEAP_BLOCKSIZE);
|
this->elements[this->size >> BINARY_HEAP_BLOCKSIZE_BITS] = MallocT<BinaryHeapNode>(BINARY_HEAP_BLOCKSIZE);
|
||||||
q->blocks++;
|
this->blocks++;
|
||||||
#ifdef QUEUE_DEBUG
|
#ifdef QUEUE_DEBUG
|
||||||
printf("[BinaryHeap] Increasing size of elements to %d nodes\n", q->blocks * BINARY_HEAP_BLOCKSIZE);
|
printf("[BinaryHeap] Increasing size of elements to %d nodes\n", this->blocks * BINARY_HEAP_BLOCKSIZE);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add the item at the end of the array */
|
/* Add the item at the end of the array */
|
||||||
BIN_HEAP_ARR(q->size + 1).priority = priority;
|
THISBIN_HEAP_ARR(this->size + 1).priority = priority;
|
||||||
BIN_HEAP_ARR(q->size + 1).item = item;
|
THISBIN_HEAP_ARR(this->size + 1).item = item;
|
||||||
q->size++;
|
this->size++;
|
||||||
|
|
||||||
/* Now we are going to check where it belongs. As long as the parent is
|
/* Now we are going to check where it belongs. As long as the parent is
|
||||||
* bigger, we switch with the parent */
|
* bigger, we switch with the parent */
|
||||||
|
@ -103,15 +109,15 @@ static bool BinaryHeap_Push(Queue *q, void *item, int priority)
|
||||||
int i;
|
int i;
|
||||||
int j;
|
int j;
|
||||||
|
|
||||||
i = q->size;
|
i = this->size;
|
||||||
while (i > 1) {
|
while (i > 1) {
|
||||||
/* Get the parent of this object (divide by 2) */
|
/* Get the parent of this object (divide by 2) */
|
||||||
j = i / 2;
|
j = i / 2;
|
||||||
/* Is the parent bigger than the current, switch them */
|
/* Is the parent bigger than the current, switch them */
|
||||||
if (BIN_HEAP_ARR(i).priority <= BIN_HEAP_ARR(j).priority) {
|
if (THISBIN_HEAP_ARR(i).priority <= THISBIN_HEAP_ARR(j).priority) {
|
||||||
temp = BIN_HEAP_ARR(j);
|
temp = THISBIN_HEAP_ARR(j);
|
||||||
BIN_HEAP_ARR(j) = BIN_HEAP_ARR(i);
|
THISBIN_HEAP_ARR(j) = THISBIN_HEAP_ARR(i);
|
||||||
BIN_HEAP_ARR(i) = temp;
|
THISBIN_HEAP_ARR(i) = temp;
|
||||||
i = j;
|
i = j;
|
||||||
} else {
|
} else {
|
||||||
/* It is not, we're done! */
|
/* It is not, we're done! */
|
||||||
|
@ -203,7 +209,6 @@ static void *BinaryHeap_Pop(Queue *q)
|
||||||
void init_BinaryHeap(Queue *q, uint max_size)
|
void init_BinaryHeap(Queue *q, uint max_size)
|
||||||
{
|
{
|
||||||
assert(q != NULL);
|
assert(q != NULL);
|
||||||
q->push = BinaryHeap_Push;
|
|
||||||
q->pop = BinaryHeap_Pop;
|
q->pop = BinaryHeap_Pop;
|
||||||
q->del = BinaryHeap_Delete;
|
q->del = BinaryHeap_Delete;
|
||||||
q->clear = BinaryHeap_Clear;
|
q->clear = BinaryHeap_Clear;
|
||||||
|
|
|
@ -19,7 +19,6 @@
|
||||||
|
|
||||||
|
|
||||||
struct Queue;
|
struct Queue;
|
||||||
typedef bool Queue_PushProc(Queue *q, void *item, int priority);
|
|
||||||
typedef void *Queue_PopProc(Queue *q);
|
typedef void *Queue_PopProc(Queue *q);
|
||||||
typedef bool Queue_DeleteProc(Queue *q, void *item, int priority);
|
typedef bool Queue_DeleteProc(Queue *q, void *item, int priority);
|
||||||
typedef void Queue_ClearProc(Queue *q, bool free_values);
|
typedef void Queue_ClearProc(Queue *q, bool free_values);
|
||||||
|
@ -32,11 +31,7 @@ struct BinaryHeapNode {
|
||||||
|
|
||||||
|
|
||||||
struct Queue {
|
struct Queue {
|
||||||
/*
|
bool Push(void *item, int priority);
|
||||||
* Pushes an element into the queue, at the appropriate place for the queue.
|
|
||||||
* Requires the queue pointer to be of an appropriate type, of course.
|
|
||||||
*/
|
|
||||||
Queue_PushProc *push;
|
|
||||||
/*
|
/*
|
||||||
* Pops the first element from the queue. What exactly is the first element,
|
* Pops the first element from the queue. What exactly is the first element,
|
||||||
* is defined by the exact type of queue.
|
* is defined by the exact type of queue.
|
||||||
|
|
Loading…
Reference in New Issue