26#define LCHILD(x) (2 * x + 1)
27#define RCHILD(x) (2 * x + 2)
28#define PARENT(x) ((x - 1) / 2)
49 if(
nullptr != this->heap ) {
54 this->heap =
new(std::nothrow) Node[capacity];
55 if(
nullptr == this->heap ) {
58 this->capacity = capacity;
79 while(index && maxCount < maxIter) {
88 if(value <= this->heap[parent].value) {
92 this->heap[index] = this->heap[parent];
98 FW_ASSERT(maxCount < maxIter, maxCount, maxIter);
102 this->heap[index].value = value;
103 this->heap[index].order = order;
104 this->heap[index].id = id;
120 value = this->heap[0].value;
121 id = this->heap[0].id;
129 this->heap[0]= this->heap[index];
141 return (this->size == this->capacity);
146 return (this->size == 0);
158 void MaxHeap::heapify() {
168 while(index <= this->size && maxCount < maxIter) {
178 if (left >= this->size) {
188 largest = this->max(left, largest);
191 if (right < this->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->size, a, this->size);
223 FW_ASSERT(b < this->size, b, this->size);
235 if(aValue == bValue) {
245 if( aValue > bValue ) {
254 FW_ASSERT(a < this->size, a, this->size);
255 FW_ASSERT(b < this->size, b, this->size);
256 Node temp = this->heap[a];
257 this->heap[a] = this->heap[b];
258 this->heap[b] = temp;
267 while(index < this->size) {
271 if( left >= size && index == 0) {
273 index, this->heap[index].value, this->heap[index].
id);
275 else if( right >= size && left < size ) {
277 index, this->heap[index].value, this->heap[index].
id,
278 left, this->heap[left].value, this->heap[left].
id);
280 else if( right < size && left < size ) {
282 index, this->heap[index].value, this->heap[index].
id,
283 left, this->heap[left].value,this->heap[left].
id,
284 right, this->heap[right].value, this->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.