# CBinaryHeapT< Titem_ > Class Template Reference

Binary Heap as C++ template. More...

`#include <binaryheap.hpp>`

## Public Types
typedef Titem_ * | ItemPtr
## Public Member Functions
| CBinaryHeapT (int max_items=102400)
FORCEINLINE int | Size () const
| Return the number of items stored in the priority queue.
FORCEINLINE bool | IsEmpty () const
| Test if the priority queue is empty.
FORCEINLINE bool | IsFull () const
| Test if the priority queue is full.
FORCEINLINE Titem_ & | GetHead ()
| Find the smallest item in the priority queue.
bool | Push (Titem_ &new_item)
| Insert new item into the priority queue, maintaining heap order.
FORCEINLINE Titem_ & | PopHead ()
| Remove and return the smallest item from the priority queue.
void | RemoveHead ()
| Remove the smallest item from the priority queue.
void | RemoveByIdx (int idx)
| Remove item specified by index.
int | FindLinear (const Titem_ &item) const
| return index of the item that matches (using &item1 == &item2) the given item.
void | Clear ()
| Make the priority queue empty.
void | CheckConsistency ()
| verifies the heap consistency (added during first YAPF debug phase)
## Private Attributes
int | m_size
| Number of items in the heap.
int | m_max_size
| Maximum number of items the heap can hold.
ItemPtr * | m_items
| The heap item pointers.

## Detailed Description

### template<class Titem_>
class CBinaryHeapT< Titem_ >

Binary Heap as C++ template.

For information about Binary Heap algotithm, see: http://www.policyalmanac.org/games/binaryHeaps.htm

Implementation specific notes:

1) It allocates space for item pointers (array). Items are allocated elsewhere.

2) ItemPtr [0] is never used. Total array size is max_items + 1, because we use indices 1..max_items instead of zero based C indexing.

3) Item of the binary heap should support these public members:

- 'lower-than' operator '<' - used for comparing items before moving

Definition at line 27 of file binaryheap.hpp.

## Member Function Documentation

template<class Titem_>

FORCEINLINE int CBinaryHeapT< Titem_ >::Size | ( |  | ) | const`[inline]`

Return the number of items stored in the priority queue.

Returns:
  number of items in the queue

Definition at line 53 of file binaryheap.hpp.

References CBinaryHeapT< Titem_ >::m_size.

template<class Titem_>

FORCEINLINE bool CBinaryHeapT< Titem_ >::IsEmpty | ( |  | ) | const`[inline]`

Test if the priority queue is empty.

Returns:
  true if empty

Definition at line 57 of file binaryheap.hpp.

References CBinaryHeapT< Titem_ >::m_size.

Referenced by CBinaryHeapT< Titem_ >::FindLinear(), CNodeList_HashTableT< Titem_, Thash_bits_open_, Thash_bits_closed_ >::GetBestOpenNode(), CBinaryHeapT< Titem_ >::GetHead(), CNodeList_HashTableT< Titem_, Thash_bits_open_, Thash_bits_closed_ >::PopBestOpenNode(), and CBinaryHeapT< Titem_ >::RemoveHead().

template<class Titem_>

FORCEINLINE bool CBinaryHeapT< Titem_ >::IsFull | ( |  | ) | const`[inline]`

Test if the priority queue is full.

Returns:
  true if full.

Definition at line 61 of file binaryheap.hpp.

References CBinaryHeapT< Titem_ >::m_max_size, and CBinaryHeapT< Titem_ >::m_size.

Referenced by CNodeList_HashTableT< Titem_, Thash_bits_open_, Thash_bits_closed_ >::InsertOpenNode(), and CBinaryHeapT< Titem_ >::Push().

template<class Titem_>

FORCEINLINE Titem_& CBinaryHeapT< Titem_ >::GetHead | ( |  | ) | `[inline]`

Find the smallest item in the priority queue.

Return the smallest item, or throw assert if empty.

Definition at line 65 of file binaryheap.hpp.

References CBinaryHeapT< Titem_ >::IsEmpty(), and CBinaryHeapT< Titem_ >::m_items.

Referenced by CNodeList_HashTableT< Titem_, Thash_bits_open_, Thash_bits_closed_ >::GetBestOpenNode(), and CBinaryHeapT< Titem_ >::PopHead().

template<class Titem_>

FORCEINLINE bool CBinaryHeapT< Titem_ >::Push | ( | Titem_ & | new_item | ) | `[inline]`

Insert new item into the priority queue, maintaining heap order.

Returns:
  false if the queue is full.

Definition at line 93 of file binaryheap.hpp.

References CBinaryHeapT< Titem_ >::CheckConsistency(), CBinaryHeapT< Titem_ >::IsFull(), CBinaryHeapT< Titem_ >::m_items, and CBinaryHeapT< Titem_ >::m_size.

Referenced by CNodeList_HashTableT< Titem_, Thash_bits_open_, Thash_bits_closed_ >::InsertOpenNode().

template<class Titem_>

FORCEINLINE Titem_& CBinaryHeapT< Titem_ >::PopHead | ( |  | ) | `[inline]`

Remove and return the smallest item from the priority queue.

Definition at line 72 of file binaryheap.hpp.

References CBinaryHeapT< Titem_ >::GetHead(), and CBinaryHeapT< Titem_ >::RemoveHead().

Referenced by CNodeList_HashTableT< Titem_, Thash_bits_open_, Thash_bits_closed_ >::PopBestOpenNode().

template<class Titem_>

FORCEINLINE void CBinaryHeapT< Titem_ >::RemoveHead | ( |  | ) | `[inline]`

Remove the smallest item from the priority queue.

Definition at line 108 of file binaryheap.hpp.

References CBinaryHeapT< Titem_ >::CheckConsistency(), CBinaryHeapT< Titem_ >::IsEmpty(), CBinaryHeapT< Titem_ >::m_items, and CBinaryHeapT< Titem_ >::m_size.

Referenced by CBinaryHeapT< Titem_ >::PopHead().

template<class Titem_>

int CBinaryHeapT< Titem_ >::FindLinear | ( | const Titem_ & | item | ) | const`[inline]`

return index of the item that matches (using &item1 == &item2) the given item.

Definition at line 197 of file binaryheap.hpp.

References CBinaryHeapT< Titem_ >::IsEmpty(), CBinaryHeapT< Titem_ >::m_items, and CBinaryHeapT< Titem_ >::m_size.

Referenced by CNodeList_HashTableT< Titem_, Thash_bits_open_, Thash_bits_closed_ >::PopOpenNode().

template<class Titem_>

void CBinaryHeapT< Titem_ >::Clear | ( |  | ) | `[inline]`

Make the priority queue empty.

All remaining items will remain untouched.

Definition at line 85 of file binaryheap.hpp.

References CBinaryHeapT< Titem_ >::m_size.

The documentation for this class was generated from the following file:

- src/misc/binaryheap.hpp