1
0
Fork 0

Codechange: replace uint with size_t in binary heap

pull/12647/merge
Rubidium 2024-05-12 09:45:39 +02:00 committed by rubidium42
parent 8349203038
commit 6cea49c117
2 changed files with 16 additions and 16 deletions

View File

@ -50,8 +50,8 @@
template <class T> template <class T>
class CBinaryHeapT { class CBinaryHeapT {
private: private:
uint items; ///< Number of items in the heap size_t items; ///< Number of items in the heap
uint capacity; ///< Maximum number of items the heap can hold size_t capacity; ///< Maximum number of items the heap can hold
T **data; ///< The pointer to the heap item pointers T **data; ///< The pointer to the heap item pointers
public: public:
@ -59,7 +59,7 @@ public:
* Create a binary heap. * Create a binary heap.
* @param max_items The limit of the heap * @param max_items The limit of the heap
*/ */
explicit CBinaryHeapT(uint max_items) explicit CBinaryHeapT(size_t max_items)
: items(0) : items(0)
, capacity(max_items) , capacity(max_items)
{ {
@ -83,12 +83,12 @@ protected:
* @param item The proposed item for filling the gap * @param item The proposed item for filling the gap
* @return The (gap)position where the item fits * @return The (gap)position where the item fits
*/ */
inline uint HeapifyDown(uint gap, T *item) inline size_t HeapifyDown(size_t gap, T *item)
{ {
assert(gap != 0); assert(gap != 0);
/* The first child of the gap is at [parent * 2] */ /* The first child of the gap is at [parent * 2] */
uint child = gap * 2; size_t child = gap * 2;
/* while children are valid */ /* while children are valid */
while (child <= this->items) { while (child <= this->items) {
@ -119,11 +119,11 @@ protected:
* @param item The proposed item for filling the gap * @param item The proposed item for filling the gap
* @return The (gap)position where the item fits * @return The (gap)position where the item fits
*/ */
inline uint HeapifyUp(uint gap, T *item) inline size_t HeapifyUp(size_t gap, T *item)
{ {
assert(gap != 0); assert(gap != 0);
uint parent; size_t parent;
while (gap > 1) { while (gap > 1) {
/* compare [gap] with its parent */ /* compare [gap] with its parent */
@ -142,8 +142,8 @@ protected:
/** Verify the heap consistency */ /** Verify the heap consistency */
inline void CheckConsistency() inline void CheckConsistency()
{ {
for (uint child = 2; child <= this->items; child++) { for (size_t child = 2; child <= this->items; child++) {
uint parent = child / 2; size_t parent = child / 2;
assert(!(*this->data[child] < *this->data[parent])); assert(!(*this->data[child] < *this->data[parent]));
} }
} }
@ -155,7 +155,7 @@ public:
* *
* @return The number of items in the queue * @return The number of items in the queue
*/ */
inline uint Length() const inline size_t Length() const
{ {
return this->items; return this->items;
} }
@ -218,7 +218,7 @@ public:
} }
/* Make place for new item. A gap is now at the end of the tree. */ /* Make place for new item. A gap is now at the end of the tree. */
uint gap = this->HeapifyUp(++items, new_item); size_t gap = this->HeapifyUp(++items, new_item);
this->data[gap] = new_item; this->data[gap] = new_item;
CHECK_CONSISTY(); CHECK_CONSISTY();
} }
@ -238,7 +238,7 @@ public:
this->items--; this->items--;
/* at index 1 we have a gap now */ /* at index 1 we have a gap now */
T *last = this->End(); T *last = this->End();
uint gap = this->HeapifyDown(1, last); size_t gap = this->HeapifyDown(1, last);
/* move last item to the proper place */ /* move last item to the proper place */
if (!this->IsEmpty()) this->data[gap] = last; if (!this->IsEmpty()) this->data[gap] = last;
@ -251,7 +251,7 @@ public:
* *
* @param index The position of the item in the heap * @param index The position of the item in the heap
*/ */
inline void Remove(uint index) inline void Remove(size_t index)
{ {
if (index < this->items) { if (index < this->items) {
assert(index != 0); assert(index != 0);
@ -260,7 +260,7 @@ public:
T *last = this->End(); T *last = this->End();
/* Fix binary tree up and downwards */ /* Fix binary tree up and downwards */
uint gap = this->HeapifyUp(index, last); size_t gap = this->HeapifyUp(index, last);
gap = this->HeapifyDown(gap, last); gap = this->HeapifyDown(gap, last);
/* move last item to the proper place */ /* move last item to the proper place */
if (!this->IsEmpty()) this->data[gap] = last; if (!this->IsEmpty()) this->data[gap] = last;
@ -279,7 +279,7 @@ public:
* @param item The reference to the item * @param item The reference to the item
* @return The index of the item or zero if not found * @return The index of the item or zero if not found
*/ */
inline uint FindIndex(const T &item) const inline size_t FindIndex(const T &item) const
{ {
if (this->IsEmpty()) return 0; if (this->IsEmpty()) return 0;
for (T **ppI = this->data + 1, **ppLast = ppI + this->items; ppI <= ppLast; ppI++) { for (T **ppI = this->data + 1, **ppLast = ppI + this->items; ppI <= ppLast; ppI++) {

View File

@ -118,7 +118,7 @@ public:
inline Titem_ &PopOpenNode(const Key &key) inline Titem_ &PopOpenNode(const Key &key)
{ {
Titem_ &item = m_open.Pop(key); Titem_ &item = m_open.Pop(key);
uint idxPop = m_open_queue.FindIndex(item); size_t idxPop = m_open_queue.FindIndex(item);
m_open_queue.Remove(idxPop); m_open_queue.Remove(idxPop);
return item; return item;
} }