(svn r20683) -Codechange: Make BinaryHeap_Delete() a method.

pull/155/head
alberth 14 years ago
parent 4e9c7f489a
commit e03d069f3f

@ -127,7 +127,7 @@ static int AyStarMain_CheckTile(AyStar *aystar, AyStarNode *current, OpenListNod
uint i; uint i;
/* Yes, check if this g value is lower.. */ /* Yes, check if this g value is lower.. */
if (new_g > check->g) return AYSTAR_DONE; if (new_g > check->g) return AYSTAR_DONE;
aystar->OpenListQueue.del(&aystar->OpenListQueue, check, 0); aystar->OpenListQueue.Delete(check, 0);
/* It is lower, so change it to this item */ /* It is lower, so change it to this item */
check->g = new_g; check->g = new_g;
check->path.parent = closedlist_parent; check->path.parent = closedlist_parent;

@ -129,25 +129,30 @@ bool Queue::Push(void *item, int priority)
return true; return true;
} }
static bool BinaryHeap_Delete(Queue *q, void *item, int priority) /**
* Deletes the item from the queue. priority should be specified if
* known, which speeds up the deleting for some queue's. Should be -1
* if not known.
*/
bool Queue::Delete(void *item, int priority)
{ {
uint i = 0; uint i = 0;
#ifdef QUEUE_DEBUG #ifdef QUEUE_DEBUG
printf("[BinaryHeap] Deleting an element. There are %d elements left\n", q->size); printf("[BinaryHeap] Deleting an element. There are %d elements left\n", this->size);
#endif #endif
/* First, we try to find the item.. */ /* First, we try to find the item.. */
do { do {
if (BIN_HEAP_ARR(i + 1).item == item) break; if (THISBIN_HEAP_ARR(i + 1).item == item) break;
i++; i++;
} while (i < q->size); } while (i < this->size);
/* We did not find the item, so we return false */ /* We did not find the item, so we return false */
if (i == q->size) return false; if (i == this->size) return false;
/* Now we put the last item over the current item while decreasing the size of the elements */ /* Now we put the last item over the current item while decreasing the size of the elements */
q->size--; this->size--;
BIN_HEAP_ARR(i + 1) = BIN_HEAP_ARR(q->size + 1); THISBIN_HEAP_ARR(i + 1) = THISBIN_HEAP_ARR(this->size + 1);
/* Now the only thing we have to do, is resort it.. /* Now the only thing we have to do, is resort it..
* On place i there is the item to be sorted.. let's start there */ * On place i there is the item to be sorted.. let's start there */
@ -162,22 +167,22 @@ static bool BinaryHeap_Delete(Queue *q, void *item, int priority)
for (;;) { for (;;) {
j = i; j = i;
/* Check if we have 2 childs */ /* Check if we have 2 childs */
if (2 * j + 1 <= q->size) { if (2 * j + 1 <= this->size) {
/* Is this child smaller than the parent? */ /* Is this child smaller than the parent? */
if (BIN_HEAP_ARR(j).priority >= BIN_HEAP_ARR(2 * j).priority) i = 2 * j; if (THISBIN_HEAP_ARR(j).priority >= THISBIN_HEAP_ARR(2 * j).priority) i = 2 * j;
/* Yes, we _need_ to use i here, not j, because we want to have the smallest child /* Yes, we _need_ to use i here, not j, because we want to have the smallest child
* This way we get that straight away! */ * This way we get that straight away! */
if (BIN_HEAP_ARR(i).priority >= BIN_HEAP_ARR(2 * j + 1).priority) i = 2 * j + 1; if (THISBIN_HEAP_ARR(i).priority >= THISBIN_HEAP_ARR(2 * j + 1).priority) i = 2 * j + 1;
/* Do we have one child? */ /* Do we have one child? */
} else if (2 * j <= q->size) { } else if (2 * j <= this->size) {
if (BIN_HEAP_ARR(j).priority >= BIN_HEAP_ARR(2 * j).priority) i = 2 * j; if (THISBIN_HEAP_ARR(j).priority >= THISBIN_HEAP_ARR(2 * j).priority) i = 2 * j;
} }
/* One of our childs is smaller than we are, switch */ /* One of our childs is smaller than we are, switch */
if (i != j) { if (i != j) {
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;
} else { } else {
/* None of our childs is smaller, so we stay here.. stop :) */ /* None of our childs is smaller, so we stay here.. stop :) */
break; break;
@ -205,7 +210,7 @@ void *Queue::Pop()
/* The best item is always on top, so give that as result */ /* The best item is always on top, so give that as result */
result = THISBIN_HEAP_ARR(1).item; result = THISBIN_HEAP_ARR(1).item;
/* And now we should get rid of this item... */ /* And now we should get rid of this item... */
BinaryHeap_Delete(this, THISBIN_HEAP_ARR(1).item, THISBIN_HEAP_ARR(1).priority); this->Delete(THISBIN_HEAP_ARR(1).item, THISBIN_HEAP_ARR(1).priority);
return result; return result;
} }
@ -213,7 +218,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->del = BinaryHeap_Delete;
q->clear = BinaryHeap_Clear; q->clear = BinaryHeap_Clear;
q->free = BinaryHeap_Free; q->free = BinaryHeap_Free;
q->max_size = max_size; q->max_size = max_size;

@ -19,7 +19,6 @@
struct Queue; struct Queue;
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);
typedef void Queue_FreeProc(Queue *q, bool free_values); typedef void Queue_FreeProc(Queue *q, bool free_values);
@ -32,12 +31,7 @@ struct BinaryHeapNode {
struct Queue { struct Queue {
bool Push(void *item, int priority); bool Push(void *item, int priority);
void *Pop(); void *Pop();
/* bool Delete(void *item, int priority);
* Deletes the item from the queue. priority should be specified if
* known, which speeds up the deleting for some queue's. Should be -1
* if not known.
*/
Queue_DeleteProc *del;
/* Clears the queue, by removing all values from it. Its state is /* Clears the queue, by removing all values from it. Its state is
* effectively reset. If free_items is true, each of the items cleared * effectively reset. If free_items is true, each of the items cleared

Loading…
Cancel
Save