mirror of https://github.com/OpenTTD/OpenTTD
(svn r19236) -Codechange: move method code into class definition (skidd13)
parent
f9709a6f50
commit
cc413b8f6e
|
@ -69,34 +69,15 @@ public:
|
||||||
|
|
||||||
/** Find the smallest item in the priority queue.
|
/** Find the smallest item in the priority queue.
|
||||||
* 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.
|
||||||
void Push(Titem_& new_item);
|
* @return false if the queue is full. */
|
||||||
|
FORCEINLINE void Push(Titem_& new_item)
|
||||||
/** Remove and return the smallest item from the priority queue. */
|
|
||||||
FORCEINLINE Titem_& PopHead() {Titem_& ret = GetHead(); RemoveHead(); return ret;};
|
|
||||||
|
|
||||||
/** Remove the smallest item from the priority queue. */
|
|
||||||
void RemoveHead();
|
|
||||||
|
|
||||||
/** Remove item specified by index */
|
|
||||||
void RemoveByIdx(int idx);
|
|
||||||
|
|
||||||
/** return index of the item that matches (using &item1 == &item2) the given item. */
|
|
||||||
int FindLinear(const Titem_& item) const;
|
|
||||||
|
|
||||||
/** Make the priority queue empty.
|
|
||||||
* All remaining items will remain untouched. */
|
|
||||||
void Clear() {m_size = 0;};
|
|
||||||
|
|
||||||
/** verifies the heap consistency (added during first YAPF debug phase) */
|
|
||||||
void CheckConsistency();
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
template <class Titem_>
|
|
||||||
FORCEINLINE void CBinaryHeapT<Titem_>::Push(Titem_& new_item)
|
|
||||||
{
|
{
|
||||||
if (IsFull()) {
|
if (IsFull()) {
|
||||||
m_max_size *= 2;
|
m_max_size *= 2;
|
||||||
|
@ -112,8 +93,16 @@ FORCEINLINE void CBinaryHeapT<Titem_>::Push(Titem_& new_item)
|
||||||
CheckConsistency();
|
CheckConsistency();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Titem_>
|
/** Remove and return the smallest item from the priority queue. */
|
||||||
FORCEINLINE void CBinaryHeapT<Titem_>::RemoveHead()
|
FORCEINLINE Titem_& PopHead()
|
||||||
|
{
|
||||||
|
Titem_& ret = GetHead();
|
||||||
|
RemoveHead();
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Remove the smallest item from the priority queue. */
|
||||||
|
FORCEINLINE void RemoveHead()
|
||||||
{
|
{
|
||||||
assert(!IsEmpty());
|
assert(!IsEmpty());
|
||||||
|
|
||||||
|
@ -150,8 +139,8 @@ FORCEINLINE void CBinaryHeapT<Titem_>::RemoveHead()
|
||||||
CheckConsistency();
|
CheckConsistency();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Titem_>
|
/** Remove item specified by index */
|
||||||
inline void CBinaryHeapT<Titem_>::RemoveByIdx(int idx)
|
FORCEINLINE void RemoveByIdx(int idx)
|
||||||
{
|
{
|
||||||
/* at position idx we have a gap now */
|
/* at position idx we have a gap now */
|
||||||
int gap = idx;
|
int gap = idx;
|
||||||
|
@ -200,8 +189,8 @@ inline void CBinaryHeapT<Titem_>::RemoveByIdx(int idx)
|
||||||
CheckConsistency();
|
CheckConsistency();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Titem_>
|
/** return index of the item that matches (using &item1 == &item2) the given item. */
|
||||||
inline int CBinaryHeapT<Titem_>::FindLinear(const Titem_& item) const
|
FORCEINLINE int FindLinear(const Titem_& item) const
|
||||||
{
|
{
|
||||||
if (IsEmpty()) return 0;
|
if (IsEmpty()) return 0;
|
||||||
for (ItemPtr *ppI = m_items + 1, *ppLast = ppI + m_size; ppI <= ppLast; ppI++) {
|
for (ItemPtr *ppI = m_items + 1, *ppLast = ppI + m_size; ppI <= ppLast; ppI++) {
|
||||||
|
@ -212,8 +201,12 @@ inline int CBinaryHeapT<Titem_>::FindLinear(const Titem_& item) const
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Titem_>
|
/** Make the priority queue empty.
|
||||||
FORCEINLINE void CBinaryHeapT<Titem_>::CheckConsistency()
|
* All remaining items will remain untouched. */
|
||||||
|
FORCEINLINE void Clear() {m_size = 0;}
|
||||||
|
|
||||||
|
/** verifies the heap consistency (added during first YAPF debug phase) */
|
||||||
|
FORCEINLINE void CheckConsistency()
|
||||||
{
|
{
|
||||||
/* enable it if you suspect binary heap doesn't work well */
|
/* enable it if you suspect binary heap doesn't work well */
|
||||||
#if 0
|
#if 0
|
||||||
|
@ -223,6 +216,6 @@ FORCEINLINE void CBinaryHeapT<Titem_>::CheckConsistency()
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
#endif /* BINARYHEAP_HPP */
|
#endif /* BINARYHEAP_HPP */
|
||||||
|
|
Loading…
Reference in New Issue