25 #define LCHILD(x) (2 * x + 1) 26 #define RCHILD(x) (2 * x + 2) 27 #define PARENT(x) ((x - 1) / 2) 33 this->m_heap =
nullptr;
39 this->m_heap =
nullptr;
44 FW_ASSERT(heap_allocation.
size >= (capacity *
sizeof(Node)), static_cast<FwAssertArgType>(capacity),
45 static_cast<FwAssertArgType>(heap_allocation.
size));
48 FW_ASSERT(capacity < std::numeric_limits<FwSizeType>::max());
49 this->m_heap = Fw::arrayPlacementNew<Node>(heap_allocation, capacity);
50 this->m_capacity = capacity;
55 if (this->m_heap !=
nullptr) {
56 Fw::arrayPlacementDestruct<Node>(this->m_heap, this->m_capacity);
60 this->m_heap =
nullptr;
82 for (i = 0; (i < maxIter) && (index != 0); i++) {
87 FW_ASSERT(parent < index, static_cast<FwAssertArgType>(parent), static_cast<FwAssertArgType>(index));
91 if (value <= this->m_heap[parent].value) {
95 this->m_heap[index] = this->m_heap[parent];
100 FW_ASSERT(i < maxIter, static_cast<FwAssertArgType>(i), static_cast<FwAssertArgType>(maxIter));
101 FW_ASSERT(index <= this->m_size, static_cast<FwAssertArgType>(index));
104 this->m_heap[index].value = value;
105 this->m_heap[index].order = m_order;
106 this->m_heap[index].id = id;
122 value = this->m_heap[0].value;
123 id = this->m_heap[0].id;
131 this->m_heap[0] = this->m_heap[index];
143 return (this->m_size == this->m_capacity);
148 return (this->m_size == 0);
160 void MaxHeap::heapify() {
170 for (i = 0; (i < maxIter) && (index <= this->m_size); i++) {
174 FW_ASSERT(left > index, static_cast<FwAssertArgType>(left), static_cast<FwAssertArgType>(index));
175 FW_ASSERT(right > left, static_cast<FwAssertArgType>(right), static_cast<FwAssertArgType>(left));
180 if (left >= this->m_size) {
190 largest = this->max(left, largest);
193 if (right < this->m_size) {
196 largest = this->max(right, largest);
201 if (largest == index) {
206 this->swap(index, largest);
213 FW_ASSERT(i < maxIter, static_cast<FwAssertArgType>(i), static_cast<FwAssertArgType>(maxIter));
214 FW_ASSERT(index <= this->m_size, static_cast<FwAssertArgType>(index));
220 static_assert(not std::numeric_limits<FwSizeType>::is_signed,
"FwSizeType must be unsigned");
221 FW_ASSERT(a < this->m_size, static_cast<FwAssertArgType>(a), static_cast<FwAssertArgType>(this->m_size));
222 FW_ASSERT(b < this->m_size, static_cast<FwAssertArgType>(b), static_cast<FwAssertArgType>(this->m_size));
234 if (aValue == bValue) {
235 FwSizeType aAge = this->m_order - this->m_heap[a].order;
236 FwSizeType bAge = this->m_order - this->m_heap[b].order;
244 if (aValue > bValue) {
253 FW_ASSERT(a < this->m_size, static_cast<FwAssertArgType>(a), static_cast<FwAssertArgType>(this->m_size));
254 FW_ASSERT(b < this->m_size, static_cast<FwAssertArgType>(b), static_cast<FwAssertArgType>(this->m_size));
255 Node temp = this->m_heap[a];
256 this->m_heap[a] = this->m_heap[b];
257 this->m_heap[b] = temp;
PlatformSizeType FwSizeType
bool pop(FwQueuePriorityType &value, FwSizeType &id)
Pop an item from the heap.
~MaxHeap()
MaxHeap deconstructor.
bool isEmpty()
Is the heap empty?
bool push(FwQueuePriorityType value, FwSizeType id)
Push an item onto the heap.
A variable-length byte array.
MaxHeap()
MaxHeap constructor.
bool isFull()
Is the heap full?
FwSizeType getSize() const
Get the current number of elements on the heap.
PlatformQueuePriorityType FwQueuePriorityType
The type of queue priorities used.
U8 *const bytes
The bytes.
void create(FwSizeType capacity, Fw::ByteArray heap_allocation)
MaxHeap creation.
void teardown()
MaxHeap teardown.
const FwSizeType size
The size.