mirror of https://github.com/OpenTTD/OpenTTD
(svn r19160) -Codechange: Enlarge a CBinaryHeapT if the heap is full instead of dropping the added item
-Fix: CBinaryHeapT::CheckConsistency compared pointers instead of the actual items (skidd13)release/1.1
parent
e2e2310e16
commit
1abc0db336
|
@ -44,13 +44,13 @@ public:
|
||||||
: m_size(0)
|
: m_size(0)
|
||||||
, m_max_size(max_items)
|
, m_max_size(max_items)
|
||||||
{
|
{
|
||||||
m_items = new ItemPtr[max_items + 1];
|
m_items = MallocT<ItemPtr>(max_items + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
~CBinaryHeapT()
|
~CBinaryHeapT()
|
||||||
{
|
{
|
||||||
Clear();
|
Clear();
|
||||||
delete [] m_items;
|
free(m_items);
|
||||||
m_items = NULL;
|
m_items = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,9 +71,8 @@ public:
|
||||||
* Return the smallest item, or throw assert if empty. */
|
* Return the smallest item, or throw assert if empty. */
|
||||||
FORCEINLINE Titem_& GetHead() {assert(!IsEmpty()); return *m_items[1];}
|
FORCEINLINE Titem_& GetHead() {assert(!IsEmpty()); return *m_items[1];}
|
||||||
|
|
||||||
/** Insert new item into the priority queue, maintaining heap order.
|
/** Insert new item into the priority queue, maintaining heap order. */
|
||||||
* @return false if the queue is full. */
|
void Push(Titem_& new_item);
|
||||||
bool Push(Titem_& new_item);
|
|
||||||
|
|
||||||
/** Remove and return the smallest item from the priority queue. */
|
/** Remove and return the smallest item from the priority queue. */
|
||||||
FORCEINLINE Titem_& PopHead() {Titem_& ret = GetHead(); RemoveHead(); return ret;};
|
FORCEINLINE Titem_& PopHead() {Titem_& ret = GetHead(); RemoveHead(); return ret;};
|
||||||
|
@ -97,9 +96,12 @@ public:
|
||||||
|
|
||||||
|
|
||||||
template <class Titem_>
|
template <class Titem_>
|
||||||
FORCEINLINE bool CBinaryHeapT<Titem_>::Push(Titem_& new_item)
|
FORCEINLINE void CBinaryHeapT<Titem_>::Push(Titem_& new_item)
|
||||||
{
|
{
|
||||||
if (IsFull()) return false;
|
if (IsFull()) {
|
||||||
|
m_max_size *= 2;
|
||||||
|
m_items = ReallocT<ItemPtr>(m_items, m_max_size + 1);
|
||||||
|
}
|
||||||
|
|
||||||
/* make place for new item */
|
/* make place for new item */
|
||||||
int gap = ++m_size;
|
int gap = ++m_size;
|
||||||
|
@ -108,7 +110,6 @@ FORCEINLINE bool CBinaryHeapT<Titem_>::Push(Titem_& new_item)
|
||||||
m_items[gap] = m_items[parent];
|
m_items[gap] = m_items[parent];
|
||||||
m_items[gap] = &new_item;
|
m_items[gap] = &new_item;
|
||||||
CheckConsistency();
|
CheckConsistency();
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Titem_>
|
template <class Titem_>
|
||||||
|
@ -218,7 +219,7 @@ FORCEINLINE void CBinaryHeapT<Titem_>::CheckConsistency()
|
||||||
#if 0
|
#if 0
|
||||||
for (int child = 2; child <= m_size; child++) {
|
for (int child = 2; child <= m_size; child++) {
|
||||||
int parent = child / 2;
|
int parent = child / 2;
|
||||||
assert(!(m_items[child] < m_items[parent]));
|
assert(!(*m_items[child] < *m_items[parent]));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,8 +93,6 @@ public:
|
||||||
{
|
{
|
||||||
assert(m_closed.Find(item.GetKey()) == NULL);
|
assert(m_closed.Find(item.GetKey()) == NULL);
|
||||||
m_open.Push(item);
|
m_open.Push(item);
|
||||||
/* TODO: check if m_open_queue is not full */
|
|
||||||
assert(!m_open_queue.IsFull());
|
|
||||||
m_open_queue.Push(item);
|
m_open_queue.Push(item);
|
||||||
if (&item == m_new_node) {
|
if (&item == m_new_node) {
|
||||||
m_new_node = NULL;
|
m_new_node = NULL;
|
||||||
|
|
Loading…
Reference in New Issue