26 #define LCHILD(x) (2 * x + 1)
27 #define RCHILD(x) (2 * x + 2)
28 #define PARENT(x) ((x - 1) / 2)
35 this->m_heap =
nullptr;
41 delete [] this->m_heap;
42 this->m_heap =
nullptr;
49 if(
nullptr != this->m_heap ) {
50 delete [] this->m_heap;
51 this->m_heap =
nullptr;
54 this->m_heap =
new(std::nothrow) Node[capacity];
55 if(
nullptr == this->m_heap ) {
58 this->m_capacity = capacity;
79 while(index && maxCount < maxIter) {
88 if(value <= this->m_heap[parent].value) {
92 this->m_heap[index] = this->m_heap[parent];
98 FW_ASSERT(maxCount < maxIter, maxCount, maxIter);
102 this->m_heap[index].value = value;
103 this->m_heap[index].order = m_order;
104 this->m_heap[index].id = id;
120 value = this->m_heap[0].value;
121 id = this->m_heap[0].id;
129 this->m_heap[0]= this->m_heap[index];
141 return (this->m_size == this->m_capacity);
146 return (this->m_size == 0);
158 void MaxHeap::heapify() {
168 while(index <= this->m_size && maxCount < maxIter) {
178 if (left >= this->m_size) {
188 largest = this->max(left, largest);
191 if (right < this->m_size) {
194 largest = this->max(right, largest);
199 if (largest == index)
208 this->swap(index, largest);
215 FW_ASSERT(maxCount < maxIter, maxCount, maxIter);
222 FW_ASSERT(a < this->m_size, a, this->m_size);
223 FW_ASSERT(b < this->m_size, b, this->m_size);
235 if(aValue == bValue) {
245 if( aValue > bValue ) {
254 FW_ASSERT(a < this->m_size, a, this->m_size);
255 FW_ASSERT(b < this->m_size, b, this->m_size);
256 Node temp = this->m_heap[a];
257 this->m_heap[a] = this->m_heap[b];
258 this->m_heap[b] = temp;
267 while(index < this->m_size) {
271 if( left >= m_size && index == 0) {
273 index, this->m_heap[index].value, this->m_heap[index].
id);
275 else if( right >= m_size && left < m_size ) {
277 index, this->m_heap[index].value, this->m_heap[index].
id,
278 left, this->m_heap[left].value, this->m_heap[left].
id);
280 else if( right < m_size && left < m_size ) {
282 index, this->m_heap[index].value, this->m_heap[index].
id,
283 left, this->m_heap[left].value,this->m_heap[left].
id,
284 right, this->m_heap[right].value, this->m_heap[right].
id);
PlatformIntType NATIVE_INT_TYPE
PlatformUIntType NATIVE_UINT_TYPE
C++-compatible configuration header for fprime configuration.
static void logMsg(const char *fmt, POINTER_CAST a0=0, POINTER_CAST a1=0, POINTER_CAST a2=0, POINTER_CAST a3=0, POINTER_CAST a4=0, POINTER_CAST a5=0, POINTER_CAST a6=0, POINTER_CAST a7=0, POINTER_CAST a8=0, POINTER_CAST a9=0)
bool isFull()
Is the heap full?
~MaxHeap()
MaxHeap deconstructor.
bool push(NATIVE_INT_TYPE value, NATIVE_UINT_TYPE id)
Push an item onto the heap.
MaxHeap()
MaxHeap constructor.
bool create(NATIVE_UINT_TYPE capacity)
MaxHeap creation.
void print()
Print the contents of the heap to stdout.
bool pop(NATIVE_INT_TYPE &value, NATIVE_UINT_TYPE &id)
Pop an item from the heap.
bool isEmpty()
Is the heap empty?
NATIVE_UINT_TYPE getSize()
Get the current number of elements on the heap.