PhysicsBasedAnimationToolkit 0.0.10
Cross-platform C++20 library of algorithms and data structures commonly used in computer graphics research on physically-based simulation.
Loading...
Searching...
No Matches
Heap.h
Go to the documentation of this file.
1
9
10#ifndef PBAT_COMMON_HEAP_H
11#define PBAT_COMMON_HEAP_H
12
13#include "pbat/HostDevice.h"
14
15#include <algorithm>
16#include <cassert>
17#include <functional>
18
19namespace pbat::common {
20
28template <class T, class Less = std::less<T>, auto kCapacity = 64>
29class Heap
30{
31 public:
37 PBAT_HOST_DEVICE Heap(Less _less = Less{}) : heap{}, less(_less), size{0} {}
43 PBAT_HOST_DEVICE void Push(T value)
44 {
45 assert(size < kCapacity);
46 heap[size++] = value;
47 std::push_heap(heap, heap + size, less);
48 }
49
54 PBAT_HOST_DEVICE T Pop()
55 {
56 assert(not IsEmpty());
57 std::pop_heap(heap, heap + size--, less);
58 return heap[size];
59 }
60
65 PBAT_HOST_DEVICE T const& Top() const { return heap[0]; }
71 PBAT_HOST_DEVICE auto Size() const { return size; }
77 PBAT_HOST_DEVICE bool IsEmpty() const { return size == 0; }
83 PBAT_HOST_DEVICE bool IsFull() const { return size == kCapacity; }
84
85 private:
86 T heap[kCapacity];
87 Less less;
88 int size;
89};
90
91} // namespace pbat::common
92
93#endif // PBAT_COMMON_HEAP_H
PBAT_HOST_DEVICE auto Size() const
Get the size of the heap.
Definition Heap.h:71
PBAT_HOST_DEVICE bool IsEmpty() const
Check if the heap is empty.
Definition Heap.h:77
PBAT_HOST_DEVICE T const & Top() const
Get the top element of the heap.
Definition Heap.h:65
PBAT_HOST_DEVICE bool IsFull() const
Check if the heap is full.
Definition Heap.h:83
PBAT_HOST_DEVICE Heap(Less _less=Less{})
Construct a new Heap object.
Definition Heap.h:37
PBAT_HOST_DEVICE T Pop()
Pop the top element from the heap.
Definition Heap.h:54
PBAT_HOST_DEVICE void Push(T value)
Push an element to the heap.
Definition Heap.h:43
Common functionality.
Definition ArgSort.h:20