mirror of
https://github.com/JGRennison/OpenTTD-patches.git
synced 2024-11-16 00:12:51 +00:00
(svn r19238) -Codechange: Unify the HeapifyDown code (skidd13)
This commit is contained in:
parent
b7d2c74b0b
commit
baf9c32900
@ -108,8 +108,8 @@ public:
|
|||||||
uint gap = 1;
|
uint gap = 1;
|
||||||
|
|
||||||
/* Heapify down:
|
/* Heapify down:
|
||||||
* last item becomes a candidate for the head. Call it new_item. */
|
* last item becomes a candidate for the head. Call it last. */
|
||||||
T& new_item = *m_items[m_size--];
|
T& last = *m_items[m_size--];
|
||||||
|
|
||||||
/* now we must maintain relation between parent and its children:
|
/* now we must maintain relation between parent and its children:
|
||||||
* parent <= any child
|
* parent <= any child
|
||||||
@ -122,7 +122,7 @@ public:
|
|||||||
if (child < m_size && *m_items[child + 1] < *m_items[child])
|
if (child < m_size && *m_items[child + 1] < *m_items[child])
|
||||||
child++;
|
child++;
|
||||||
/* is it smaller than our parent? */
|
/* is it smaller than our parent? */
|
||||||
if (!(*m_items[child] < new_item)) {
|
if (!(*m_items[child] < last)) {
|
||||||
/* the smaller child is still bigger or same as parent => we are done */
|
/* the smaller child is still bigger or same as parent => we are done */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -133,7 +133,7 @@ public:
|
|||||||
child = gap * 2;
|
child = gap * 2;
|
||||||
}
|
}
|
||||||
/* move last item to the proper place */
|
/* move last item to the proper place */
|
||||||
if (m_size > 0) m_items[gap] = &new_item;
|
if (m_size > 0) m_items[gap] = &last;
|
||||||
CheckConsistency();
|
CheckConsistency();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,11 +161,9 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint child = gap * 2;
|
||||||
/* Heapify (move gap) down: */
|
/* Heapify (move gap) down: */
|
||||||
while (true) {
|
while (child <= m_size) {
|
||||||
/* where we do have our children? */
|
|
||||||
uint child = gap * 2; // first child is at [parent * 2]
|
|
||||||
if (child > m_size) break;
|
|
||||||
/* choose the smaller child */
|
/* choose the smaller child */
|
||||||
if (child < m_size && *m_items[child + 1] < *m_items[child])
|
if (child < m_size && *m_items[child + 1] < *m_items[child])
|
||||||
child++;
|
child++;
|
||||||
@ -177,6 +175,8 @@ public:
|
|||||||
/* if smaller child is smaller than parent, it will become new parent */
|
/* if smaller child is smaller than parent, it will become new parent */
|
||||||
m_items[gap] = m_items[child];
|
m_items[gap] = m_items[child];
|
||||||
gap = child;
|
gap = child;
|
||||||
|
/* where do we have our new children? */
|
||||||
|
child = gap * 2;
|
||||||
}
|
}
|
||||||
/* move parent to the proper place */
|
/* move parent to the proper place */
|
||||||
if (m_size > 0) m_items[gap] = &last;
|
if (m_size > 0) m_items[gap] = &last;
|
||||||
|
Loading…
Reference in New Issue
Block a user