F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
MaxHeap.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title MaxHeap.hpp
3 // \author dinkel
4 // \brief An implementation of a stable max heap data structure
5 //
6 // \copyright
7 // Copyright 2009-2015, by the California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 //
11 // ======================================================================
12 
13 #ifndef UTILS_TYPES_MAX_HEAP_HPP
14 #define UTILS_TYPES_MAX_HEAP_HPP
15 
16 #include <Fw/FPrimeBasicTypes.hpp>
17 #include <Fw/Types/ByteArray.hpp>
18 
19 namespace Types {
20 
29 class MaxHeap {
30  public:
35  MaxHeap();
40  ~MaxHeap();
47  void create(FwSizeType capacity, Fw::ByteArray heap_allocation);
49  void teardown();
59  bool push(FwQueuePriorityType value, FwSizeType id);
69  bool pop(FwQueuePriorityType& value, FwSizeType& id);
75  bool isFull();
81  bool isEmpty();
87  FwSizeType getSize() const;
88 
89  private:
90  // Private functions:
91  // Ensure the heap meets the heap property:
92  void heapify();
93  // Swap two elements on the heap:
94  void swap(FwSizeType a, FwSizeType b);
95  // Return the max between two elements on the heap:
97 
98  // The data structure for a node on the heap:
99  struct Node {
100  FwQueuePriorityType value; // the priority of the node
101  FwSizeType order; // order in which node was pushed
102  FwSizeType id; // unique id for this node
103  };
104 
105  // Private members:
106  Node* m_heap; // the heap itself
107  FwSizeType m_size; // the current size of the heap
108  FwSizeType m_order; // the current count of heap pushes
109  FwSizeType m_capacity; // the maximum capacity of the heap
110  public:
112  static constexpr FwSizeType ELEMENT_SIZE = sizeof(Node);
114  static constexpr FwSizeType ALIGNMENT = alignof(Node);
115 };
116 
117 } // namespace Types
118 
119 #endif // UTILS_TYPES_MAX_HEAP_HPP
static constexpr FwSizeType ALIGNMENT
Exposes the ALIGNMENT for pre-allocation.
Definition: MaxHeap.hpp:114
PlatformSizeType FwSizeType
bool pop(FwQueuePriorityType &value, FwSizeType &id)
Pop an item from the heap.
Definition: MaxHeap.cpp:113
~MaxHeap()
MaxHeap deconstructor.
Definition: MaxHeap.cpp:38
bool isEmpty()
Is the heap empty?
Definition: MaxHeap.cpp:147
bool push(FwQueuePriorityType value, FwSizeType id)
Push an item onto the heap.
Definition: MaxHeap.cpp:65
A variable-length byte array.
Definition: ByteArray.hpp:23
A stable max heap data structure.
Definition: MaxHeap.hpp:29
MaxHeap()
MaxHeap constructor.
Definition: MaxHeap.cpp:31
bool isFull()
Is the heap full?
Definition: MaxHeap.cpp:142
FwSizeType getSize() const
Get the current number of elements on the heap.
Definition: MaxHeap.cpp:152
PlatformQueuePriorityType FwQueuePriorityType
The type of queue priorities used.
static constexpr FwSizeType ELEMENT_SIZE
Exposes the ELEMENT_SIZE for pre-allocation.
Definition: MaxHeap.hpp:112
void create(FwSizeType capacity, Fw::ByteArray heap_allocation)
MaxHeap creation.
Definition: MaxHeap.cpp:42
void teardown()
MaxHeap teardown.
Definition: MaxHeap.cpp:53