F´ Flight Software - C/C++ Documentation  NASA-v1.6.0
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 OS_PTHREADS_MAX_HEAP_HPP
14 #define OS_PTHREADS_MAX_HEAP_HPP
15 
16 #include "Fw/Types/BasicTypes.hpp"
17 
18 namespace Os {
19 
27  class MaxHeap {
28 
29  public:
34  MaxHeap();
39  ~MaxHeap();
46  bool create(NATIVE_UINT_TYPE capacity);
56  bool push(NATIVE_INT_TYPE value, NATIVE_UINT_TYPE id);
66  bool pop(NATIVE_INT_TYPE& value, NATIVE_UINT_TYPE& id);
72  bool isFull();
78  bool isEmpty();
89  void print();
90 
91  private:
92  // Private functions:
93  // Ensure the heap meets the heap property:
94  void heapify();
95  // Swap two elements on the heap:
96  void swap(NATIVE_UINT_TYPE a, NATIVE_UINT_TYPE b);
97  // Return the max between two elements on the heap:
99 
100  // The data structure for a node on the heap:
101  struct Node {
102  NATIVE_INT_TYPE value; // the priority of the node
103  NATIVE_UINT_TYPE order; // order in which node was pushed
104  NATIVE_UINT_TYPE id; // unique id for this node
105  };
106 
107  // Private members:
108  Node* heap; // the heap itself
109  NATIVE_UINT_TYPE size; // the current size of the heap
110  NATIVE_UINT_TYPE order; // the current count of heap pushes
111  NATIVE_UINT_TYPE capacity; // the maximum capacity of the heap
112  };
113 
114 }
115 
116 #endif // OS_PTHREADS_MAX_HEAP_HPP
Os
Definition: File.cpp:7
Os::MaxHeap::push
bool push(NATIVE_INT_TYPE value, NATIVE_UINT_TYPE id)
Push an item onto the heap.
Definition: MaxHeap.cpp:62
Os::MaxHeap::create
bool create(NATIVE_UINT_TYPE capacity)
MaxHeap creation.
Definition: MaxHeap.cpp:45
Os::MaxHeap::isEmpty
bool isEmpty()
Is the heap empty?
Definition: MaxHeap.cpp:145
Os::MaxHeap::getSize
NATIVE_UINT_TYPE getSize()
Get the current number of elements on the heap.
Definition: MaxHeap.cpp:150
NATIVE_UINT_TYPE
unsigned int NATIVE_UINT_TYPE
native unsigned integer type declaration
Definition: BasicTypes.hpp:28
Os::MaxHeap::~MaxHeap
~MaxHeap()
MaxHeap deconstructor.
Definition: MaxHeap.cpp:40
Os::MaxHeap
A stable max heap data structure.
Definition: MaxHeap.hpp:27
NATIVE_INT_TYPE
int NATIVE_INT_TYPE
native integer type declaration
Definition: BasicTypes.hpp:27
Os::MaxHeap::print
void print()
Print the contents of the heap to stdout.
Definition: MaxHeap.cpp:262
Os::MaxHeap::isFull
bool isFull()
Is the heap full?
Definition: MaxHeap.cpp:140
Os::MaxHeap::pop
bool pop(NATIVE_INT_TYPE &value, NATIVE_UINT_TYPE &id)
Pop an item from the heap.
Definition: MaxHeap.cpp:111
Os::MaxHeap::MaxHeap
MaxHeap()
MaxHeap constructor.
Definition: MaxHeap.cpp:32