OpenTTD Source: BinaryHeap Struct Reference

OpenTTD Source 12.2

Public Member Functions | Data Fields | Static Public Attributes
BinaryHeap Struct Reference

Binary Heap. More...

`#include <queue.h>`

## Public Member Functions
void | Init (uint max_size)
| Initializes a binary heap and allocates internal memory for maximum of max_size elements.
bool | Push (void *item, int priority)
| Pushes an element into the queue, at the appropriate place for the queue. More...
void * | Pop ()
| Pops the first element from the queue. More...
bool | Delete (void *item, int priority)
| Deletes the item from the queue. More...
void | Clear (bool free_values)
| Clears the queue, by removing all values from it. More...
void | Free (bool free_values)
| Frees the queue, by reclaiming all memory allocated by it. More...
BinaryHeapNode & | GetElement (uint i)
| Get an element from the #elements. More...

## Data Fields
uint | max_size
uint | size
uint | blocks
| The amount of blocks for which space is reserved in elements.
BinaryHeapNode ** | elements

## Static Public Attributes
static const int | BINARY_HEAP_BLOCKSIZE = 1 << BinaryHeap::BINARY_HEAP_BLOCKSIZE_BITS
static const int | BINARY_HEAP_BLOCKSIZE_BITS = 10
| The number of elements that will be malloc'd at a time.
static const int | BINARY_HEAP_BLOCKSIZE_MASK = BinaryHeap::BINARY_HEAP_BLOCKSIZE - 1

## Detailed Description

Binary Heap.

For information, see: http://www.policyalmanac.org/games/binaryHeaps.htm

Definition at line 26 of file queue.h.

## Member Function Documentation

## ◆ Clear()

void BinaryHeap::Clear | ( | bool | free_values | ) |

Clears the queue, by removing all values from it.

Its state is effectively reset. If free_items is true, each of the items cleared in this way are free()'d.

Definition at line 31 of file queue.cpp.

References BINARY_HEAP_BLOCKSIZE_BITS, blocks, and free().

Referenced by AyStar::Clear(), and Free().

## ◆ Delete()

bool BinaryHeap::Delete | ( | void * | item,
|  | int | priority
| ) |  |

Deletes the item from the queue.

priority should be specified if known, which speeds up the deleting for some queue's. Should be -1 if not known.

Definition at line 133 of file queue.cpp.

References GetElement().

Referenced by AyStar::CheckTile(), and Pop().

## ◆ Free()

void BinaryHeap::Free | ( | bool | free_values | ) |

Frees the queue, by reclaiming all memory allocated by it.

After this it is no longer usable. If free_items is true, any remaining items are free()'d too.

Definition at line 68 of file queue.cpp.

References blocks, Clear(), and free().

Referenced by AyStar::Free().

## ◆ GetElement()

BinaryHeapNode& BinaryHeap::GetElement | ( | uint | i | ) |
| inline

Get an element from the #elements.

Parameters

  i | Element to access (starts at offset `1`).

Returns
  Value of the element.

Definition at line 44 of file queue.h.

References BINARY_HEAP_BLOCKSIZE_BITS.

Referenced by Delete(), Pop(), and Push().

## ◆ Pop()

void * BinaryHeap::Pop | ( |  | ) |

Pops the first element from the queue.

What exactly is the first element, is defined by the exact type of queue.

Definition at line 192 of file queue.cpp.

References Delete(), and GetElement().

Referenced by AyStar::OpenListPop().

## ◆ Push()

bool BinaryHeap::Push | ( | void * | item,
|  | int | priority
| ) |  |

Pushes an element into the queue, at the appropriate place for the queue.

Requires the queue pointer to be of an appropriate type, of course.

Definition at line 84 of file queue.cpp.

References BINARY_HEAP_BLOCKSIZE_BITS, blocks, and GetElement().

Referenced by AyStar::CheckTile(), and AyStar::OpenListAdd().

The documentation for this struct was generated from the following files:

- src/pathfinder/npf/queue.h
- src/pathfinder/npf/queue.cpp