2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file binaryheap.hpp Binary heap implementation. */
|
2007-04-17 20:23:13 +00:00
|
|
|
|
2010-12-22 11:24:38 +00:00
|
|
|
#ifndef BINARYHEAP_HPP
|
|
|
|
#define BINARYHEAP_HPP
|
2006-05-27 16:12:16 +00:00
|
|
|
|
2010-08-26 22:01:16 +00:00
|
|
|
#include "../core/alloc_func.hpp"
|
|
|
|
|
2011-05-02 17:42:12 +00:00
|
|
|
/** Enable it if you suspect binary heap doesn't work well */
|
2010-02-25 11:50:30 +00:00
|
|
|
#define BINARYHEAP_CHECK 0
|
|
|
|
|
|
|
|
#if BINARYHEAP_CHECK
|
2011-05-02 17:42:12 +00:00
|
|
|
/** Check for consistency. */
|
2020-07-02 21:36:10 +00:00
|
|
|
# define CHECK_CONSISTY() this->CheckConsistency()
|
2010-02-25 11:50:30 +00:00
|
|
|
#else
|
2011-05-02 17:42:12 +00:00
|
|
|
/** Don't check for consistency. */
|
2020-07-02 21:36:10 +00:00
|
|
|
# define CHECK_CONSISTY() ;
|
2010-02-25 11:50:30 +00:00
|
|
|
#endif
|
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
/**
|
2006-09-04 20:40:33 +00:00
|
|
|
* Binary Heap as C++ template.
|
2010-07-31 21:02:56 +00:00
|
|
|
* A carrier which keeps its items automatically holds the smallest item at
|
2010-02-25 11:51:38 +00:00
|
|
|
* the first position. The order of items is maintained by using a binary tree.
|
|
|
|
* The implementation is used for priority queue's.
|
2006-09-04 20:40:33 +00:00
|
|
|
*
|
2010-02-25 11:51:38 +00:00
|
|
|
* @par Usage information:
|
|
|
|
* Item of the binary heap should support the 'lower-than' operator '<'.
|
|
|
|
* It is used for comparing items before moving them to their position.
|
2006-09-04 20:40:33 +00:00
|
|
|
*
|
2010-02-25 11:51:38 +00:00
|
|
|
* @par
|
|
|
|
* This binary heap allocates just the space for item pointers. The items
|
|
|
|
* are allocated elsewhere.
|
2006-09-04 20:40:33 +00:00
|
|
|
*
|
2010-02-25 11:51:38 +00:00
|
|
|
* @par Implementation notes:
|
2010-07-31 21:02:56 +00:00
|
|
|
* Internally the first item is never used, because that simplifies the
|
2010-02-25 11:51:38 +00:00
|
|
|
* implementation.
|
2006-09-04 20:40:33 +00:00
|
|
|
*
|
2010-02-25 11:51:38 +00:00
|
|
|
* @par
|
2013-01-08 22:46:42 +00:00
|
|
|
* For further information about the Binary Heap algorithm, see
|
2010-02-25 11:51:38 +00:00
|
|
|
* http://www.policyalmanac.org/games/binaryHeaps.htm
|
2006-09-04 20:40:33 +00:00
|
|
|
*
|
2010-02-25 11:51:38 +00:00
|
|
|
* @tparam T Type of the items stored in the binary heap
|
2006-09-04 20:40:33 +00:00
|
|
|
*/
|
2010-02-25 11:46:20 +00:00
|
|
|
template <class T>
|
2006-05-27 16:12:16 +00:00
|
|
|
class CBinaryHeapT {
|
|
|
|
private:
|
2010-02-25 11:49:17 +00:00
|
|
|
uint items; ///< Number of items in the heap
|
|
|
|
uint capacity; ///< Maximum number of items the heap can hold
|
2010-02-25 11:51:38 +00:00
|
|
|
T **data; ///< The pointer to the heap item pointers
|
2006-05-27 16:12:16 +00:00
|
|
|
|
|
|
|
public:
|
2011-05-02 17:42:12 +00:00
|
|
|
/**
|
|
|
|
* Create a binary heap.
|
|
|
|
* @param max_items The limit of the heap
|
|
|
|
*/
|
2010-02-25 11:46:20 +00:00
|
|
|
explicit CBinaryHeapT(uint max_items)
|
2010-02-25 11:49:17 +00:00
|
|
|
: items(0)
|
|
|
|
, capacity(max_items)
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
2010-02-25 11:50:58 +00:00
|
|
|
this->data = MallocT<T *>(max_items + 1);
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~CBinaryHeapT()
|
|
|
|
{
|
2010-02-25 11:50:58 +00:00
|
|
|
this->Clear();
|
|
|
|
free(this->data);
|
2019-04-10 21:07:06 +00:00
|
|
|
this->data = nullptr;
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 11:47:18 +00:00
|
|
|
protected:
|
2010-02-25 11:51:38 +00:00
|
|
|
/**
|
|
|
|
* Get position for fixing a gap (downwards).
|
|
|
|
* The gap is moved downwards in the binary tree until it
|
|
|
|
* is in order again.
|
|
|
|
*
|
|
|
|
* @param gap The position of the gap
|
|
|
|
* @param item The proposed item for filling the gap
|
|
|
|
* @return The (gap)position where the item fits
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline uint HeapifyDown(uint gap, T *item)
|
2010-02-25 11:47:18 +00:00
|
|
|
{
|
|
|
|
assert(gap != 0);
|
|
|
|
|
2010-02-25 11:51:38 +00:00
|
|
|
/* The first child of the gap is at [parent * 2] */
|
|
|
|
uint child = gap * 2;
|
2010-02-25 11:47:18 +00:00
|
|
|
|
|
|
|
/* while children are valid */
|
2010-02-25 11:50:58 +00:00
|
|
|
while (child <= this->items) {
|
2010-02-25 11:47:18 +00:00
|
|
|
/* choose the smaller child */
|
2010-07-24 10:14:39 +00:00
|
|
|
if (child < this->items && *this->data[child + 1] < *this->data[child]) {
|
2010-02-25 11:47:18 +00:00
|
|
|
child++;
|
2010-07-24 10:14:39 +00:00
|
|
|
}
|
2010-02-25 11:47:18 +00:00
|
|
|
/* is it smaller than our parent? */
|
2010-02-25 11:50:58 +00:00
|
|
|
if (!(*this->data[child] < *item)) {
|
2010-02-25 11:47:18 +00:00
|
|
|
/* the smaller child is still bigger or same as parent => we are done */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* if smaller child is smaller than parent, it will become new parent */
|
2010-02-25 11:50:58 +00:00
|
|
|
this->data[gap] = this->data[child];
|
2010-02-25 11:47:18 +00:00
|
|
|
gap = child;
|
|
|
|
/* where do we have our new children? */
|
|
|
|
child = gap * 2;
|
|
|
|
}
|
|
|
|
return gap;
|
|
|
|
}
|
|
|
|
|
2010-02-25 11:51:38 +00:00
|
|
|
/**
|
|
|
|
* Get position for fixing a gap (upwards).
|
|
|
|
* The gap is moved upwards in the binary tree until the
|
|
|
|
* is in order again.
|
|
|
|
*
|
|
|
|
* @param gap The position of the gap
|
|
|
|
* @param item The proposed item for filling the gap
|
|
|
|
* @return The (gap)position where the item fits
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline uint HeapifyUp(uint gap, T *item)
|
2010-02-25 11:48:09 +00:00
|
|
|
{
|
|
|
|
assert(gap != 0);
|
|
|
|
|
|
|
|
uint parent;
|
|
|
|
|
|
|
|
while (gap > 1) {
|
|
|
|
/* compare [gap] with its parent */
|
|
|
|
parent = gap / 2;
|
2010-02-25 11:50:58 +00:00
|
|
|
if (!(*item < *this->data[parent])) {
|
2010-02-25 11:48:09 +00:00
|
|
|
/* we don't need to continue upstairs */
|
|
|
|
break;
|
|
|
|
}
|
2010-02-25 11:50:58 +00:00
|
|
|
this->data[gap] = this->data[parent];
|
2010-02-25 11:48:09 +00:00
|
|
|
gap = parent;
|
|
|
|
}
|
|
|
|
return gap;
|
|
|
|
}
|
|
|
|
|
2010-02-25 11:50:30 +00:00
|
|
|
#if BINARYHEAP_CHECK
|
2010-02-25 11:51:38 +00:00
|
|
|
/** Verify the heap consistency */
|
2011-12-20 17:57:56 +00:00
|
|
|
inline void CheckConsistency()
|
2010-02-25 11:50:30 +00:00
|
|
|
{
|
2010-02-25 11:50:58 +00:00
|
|
|
for (uint child = 2; child <= this->items; child++) {
|
2010-02-25 11:50:30 +00:00
|
|
|
uint parent = child / 2;
|
2010-02-25 11:50:58 +00:00
|
|
|
assert(!(*this->data[child] < *this->data[parent]));
|
2010-02-25 11:50:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
public:
|
2010-02-25 11:51:38 +00:00
|
|
|
/**
|
|
|
|
* Get the number of items stored in the priority queue.
|
|
|
|
*
|
|
|
|
* @return The number of items in the queue
|
|
|
|
*/
|
2015-08-08 13:19:38 +00:00
|
|
|
inline uint Length() const
|
|
|
|
{
|
|
|
|
return this->items;
|
|
|
|
}
|
2006-05-27 16:12:16 +00:00
|
|
|
|
2010-02-25 11:51:38 +00:00
|
|
|
/**
|
|
|
|
* Test if the priority queue is empty.
|
|
|
|
*
|
|
|
|
* @return True if empty
|
|
|
|
*/
|
2015-08-08 13:19:38 +00:00
|
|
|
inline bool IsEmpty() const
|
|
|
|
{
|
|
|
|
return this->items == 0;
|
|
|
|
}
|
2006-05-27 16:12:16 +00:00
|
|
|
|
2010-02-25 11:51:38 +00:00
|
|
|
/**
|
|
|
|
* Test if the priority queue is full.
|
|
|
|
*
|
|
|
|
* @return True if full.
|
|
|
|
*/
|
2015-08-08 13:19:38 +00:00
|
|
|
inline bool IsFull() const
|
|
|
|
{
|
|
|
|
return this->items >= this->capacity;
|
|
|
|
}
|
2006-05-27 16:12:16 +00:00
|
|
|
|
2010-02-25 11:51:38 +00:00
|
|
|
/**
|
|
|
|
* Get the smallest item in the binary tree.
|
|
|
|
*
|
|
|
|
* @return The smallest item, or throw assert if empty.
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline T *Begin()
|
2010-02-25 11:45:47 +00:00
|
|
|
{
|
2010-02-25 11:50:58 +00:00
|
|
|
assert(!this->IsEmpty());
|
|
|
|
return this->data[1];
|
2010-02-25 11:45:47 +00:00
|
|
|
}
|
2006-05-27 16:12:16 +00:00
|
|
|
|
2010-02-25 11:51:38 +00:00
|
|
|
/**
|
|
|
|
* Get the LAST item in the binary tree.
|
|
|
|
*
|
2013-01-08 22:46:42 +00:00
|
|
|
* @note The last item is not necessary the biggest!
|
2010-02-25 11:51:38 +00:00
|
|
|
*
|
|
|
|
* @return The last item
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline T *End()
|
2010-02-25 11:47:18 +00:00
|
|
|
{
|
2010-02-25 11:50:58 +00:00
|
|
|
return this->data[1 + this->items];
|
2010-02-25 11:47:18 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 11:51:38 +00:00
|
|
|
/**
|
|
|
|
* Insert new item into the priority queue, maintaining heap order.
|
|
|
|
*
|
|
|
|
* @param new_item The pointer to the new item
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline void Include(T *new_item)
|
2010-02-25 11:45:47 +00:00
|
|
|
{
|
2010-02-25 11:50:58 +00:00
|
|
|
if (this->IsFull()) {
|
2011-09-02 20:54:51 +00:00
|
|
|
assert(this->capacity < UINT_MAX / 2);
|
|
|
|
|
2010-02-25 11:50:58 +00:00
|
|
|
this->capacity *= 2;
|
|
|
|
this->data = ReallocT<T*>(this->data, this->capacity + 1);
|
2010-02-25 11:45:47 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 11:51:38 +00:00
|
|
|
/* Make place for new item. A gap is now at the end of the tree. */
|
2010-02-25 11:50:58 +00:00
|
|
|
uint gap = this->HeapifyUp(++items, new_item);
|
|
|
|
this->data[gap] = new_item;
|
2010-02-25 11:50:30 +00:00
|
|
|
CHECK_CONSISTY();
|
2010-02-25 11:45:47 +00:00
|
|
|
}
|
2006-05-27 16:12:16 +00:00
|
|
|
|
2010-02-25 11:51:38 +00:00
|
|
|
/**
|
|
|
|
* Remove and return the smallest (and also first) item
|
|
|
|
* from the priority queue.
|
|
|
|
*
|
|
|
|
* @return The pointer to the removed item
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline T *Shift()
|
2010-02-25 11:45:47 +00:00
|
|
|
{
|
2010-02-25 11:50:58 +00:00
|
|
|
assert(!this->IsEmpty());
|
2006-05-27 16:12:16 +00:00
|
|
|
|
2010-02-25 11:50:58 +00:00
|
|
|
T *first = this->Begin();
|
2010-02-25 11:48:50 +00:00
|
|
|
|
2010-02-25 11:50:58 +00:00
|
|
|
this->items--;
|
2010-02-25 11:45:47 +00:00
|
|
|
/* at index 1 we have a gap now */
|
2010-02-25 11:50:58 +00:00
|
|
|
T *last = this->End();
|
|
|
|
uint gap = this->HeapifyDown(1, last);
|
2010-02-25 11:45:47 +00:00
|
|
|
/* move last item to the proper place */
|
2010-02-25 11:50:58 +00:00
|
|
|
if (!this->IsEmpty()) this->data[gap] = last;
|
2010-02-25 11:48:50 +00:00
|
|
|
|
2010-02-25 11:50:30 +00:00
|
|
|
CHECK_CONSISTY();
|
2010-02-25 11:48:50 +00:00
|
|
|
return first;
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 11:51:38 +00:00
|
|
|
/**
|
|
|
|
* Remove item at given index from the priority queue.
|
|
|
|
*
|
|
|
|
* @param index The position of the item in the heap
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline void Remove(uint index)
|
2010-02-25 11:45:47 +00:00
|
|
|
{
|
2010-02-25 11:50:58 +00:00
|
|
|
if (index < this->items) {
|
2010-02-25 11:49:17 +00:00
|
|
|
assert(index != 0);
|
2010-02-25 11:50:58 +00:00
|
|
|
this->items--;
|
2010-02-25 11:49:17 +00:00
|
|
|
/* at position index we have a gap now */
|
2010-02-25 11:45:47 +00:00
|
|
|
|
2010-02-25 11:50:58 +00:00
|
|
|
T *last = this->End();
|
2010-02-25 11:48:09 +00:00
|
|
|
/* Fix binary tree up and downwards */
|
2010-02-25 11:50:58 +00:00
|
|
|
uint gap = this->HeapifyUp(index, last);
|
|
|
|
gap = this->HeapifyDown(gap, last);
|
2010-02-25 11:47:18 +00:00
|
|
|
/* move last item to the proper place */
|
2010-02-25 11:50:58 +00:00
|
|
|
if (!this->IsEmpty()) this->data[gap] = last;
|
2010-02-25 11:45:47 +00:00
|
|
|
} else {
|
2010-02-25 11:50:58 +00:00
|
|
|
assert(index == this->items);
|
|
|
|
this->items--;
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
2010-02-25 11:50:30 +00:00
|
|
|
CHECK_CONSISTY();
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 11:51:38 +00:00
|
|
|
/**
|
|
|
|
* Search for an item in the priority queue.
|
2013-01-08 22:46:42 +00:00
|
|
|
* Matching is done by comparing address of the
|
2010-02-25 11:51:38 +00:00
|
|
|
* item.
|
|
|
|
*
|
|
|
|
* @param item The reference to the item
|
|
|
|
* @return The index of the item or zero if not found
|
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
inline uint FindIndex(const T &item) const
|
2010-02-25 11:45:47 +00:00
|
|
|
{
|
2010-02-25 11:50:58 +00:00
|
|
|
if (this->IsEmpty()) return 0;
|
|
|
|
for (T **ppI = this->data + 1, **ppLast = ppI + this->items; ppI <= ppLast; ppI++) {
|
2010-02-25 11:45:47 +00:00
|
|
|
if (*ppI == &item) {
|
2010-02-25 11:50:58 +00:00
|
|
|
return ppI - this->data;
|
2010-02-25 11:45:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2006-05-27 16:12:16 +00:00
|
|
|
}
|
|
|
|
|
2010-02-25 11:51:38 +00:00
|
|
|
/**
|
|
|
|
* Make the priority queue empty.
|
|
|
|
* All remaining items will remain untouched.
|
|
|
|
*/
|
2015-08-08 13:19:38 +00:00
|
|
|
inline void Clear()
|
|
|
|
{
|
|
|
|
this->items = 0;
|
|
|
|
}
|
2010-02-25 11:45:47 +00:00
|
|
|
};
|
2006-05-27 16:12:16 +00:00
|
|
|
|
|
|
|
#endif /* BINARYHEAP_HPP */
|