AyStar: Reduce redundant GetElement calls in BinaryHeap
This commit is contained in:
@@ -80,26 +80,23 @@ bool BinaryHeap::Push(uint32 item, int priority)
|
||||
}
|
||||
|
||||
/* Add the item at the end of the array */
|
||||
this->GetElement(this->size + 1).priority = priority;
|
||||
this->GetElement(this->size + 1).item = item;
|
||||
BinaryHeapNode &new_item = this->GetElement(this->size + 1);
|
||||
new_item.priority = priority;
|
||||
new_item.item = item;
|
||||
this->size++;
|
||||
|
||||
/* Now we are going to check where it belongs. As long as the parent is
|
||||
* bigger, we switch with the parent */
|
||||
{
|
||||
BinaryHeapNode temp;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = this->size;
|
||||
int i = this->size;
|
||||
while (i > 1) {
|
||||
/* Get the parent of this object (divide by 2) */
|
||||
j = i / 2;
|
||||
int j = i / 2;
|
||||
/* Is the parent bigger than the current, switch them */
|
||||
if (this->GetElement(i).priority <= this->GetElement(j).priority) {
|
||||
temp = this->GetElement(j);
|
||||
this->GetElement(j) = this->GetElement(i);
|
||||
this->GetElement(i) = temp;
|
||||
BinaryHeapNode &elem_i = this->GetElement(i);
|
||||
BinaryHeapNode &elem_j = this->GetElement(j);
|
||||
if (elem_i.priority <= elem_j.priority) {
|
||||
std::swap(elem_i, elem_j);
|
||||
i = j;
|
||||
} else {
|
||||
/* It is not, we're done! */
|
||||
@@ -135,15 +132,13 @@ bool BinaryHeap::Delete(uint32 item, int priority)
|
||||
/* 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 */
|
||||
{
|
||||
uint j;
|
||||
BinaryHeapNode temp;
|
||||
/* Because of the fact that Binary Heap uses array from 1 to n, we need to
|
||||
* increase i by 1
|
||||
*/
|
||||
i++;
|
||||
|
||||
for (;;) {
|
||||
j = i;
|
||||
uint j = i;
|
||||
/* Check if we have 2 children */
|
||||
if (2 * j + 1 <= this->size) {
|
||||
/* Is this child smaller than the parent? */
|
||||
@@ -158,9 +153,7 @@ bool BinaryHeap::Delete(uint32 item, int priority)
|
||||
|
||||
/* One of our children is smaller than we are, switch */
|
||||
if (i != j) {
|
||||
temp = this->GetElement(j);
|
||||
this->GetElement(j) = this->GetElement(i);
|
||||
this->GetElement(i) = temp;
|
||||
std::swap(this->GetElement(i), this->GetElement(j));
|
||||
} else {
|
||||
/* None of our children is smaller, so we stay here.. stop :) */
|
||||
break;
|
||||
@@ -180,11 +173,11 @@ uint32 BinaryHeap::Pop()
|
||||
if (this->size == 0) return UINT32_MAX;
|
||||
|
||||
/* The best item is always on top, so give that as result */
|
||||
uint32 result = this->GetElement(1).item;
|
||||
BinaryHeapNode result = this->GetElement(1);
|
||||
/* And now we should get rid of this item... */
|
||||
this->Delete(this->GetElement(1).item, this->GetElement(1).priority);
|
||||
this->Delete(result.item, result.priority);
|
||||
|
||||
return result;
|
||||
return result.item;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user