27#ifndef EASY3D_CORE_HEAP_H
28#define EASY3D_CORE_HEAP_H
65 template<
class HeapEntry,
class HeapInterface>
66 class Heap :
private std::vector<HeapEntry> {
79 explicit Heap(
const HeapInterface &i) : HeapVector(), interface_(i) {}
89 void clear() { HeapVector::clear(); }
95 bool empty() {
return HeapVector::empty(); }
101 unsigned int size() {
return static_cast<unsigned int>(HeapVector::size()); }
107 void reserve(
unsigned int n) { HeapVector::reserve(n); }
114 interface_.set_heap_position(h, -1);
123 return interface_.get_heap_position(h) != -1;
149 interface_.set_heap_position(entry(0), -1);
151 entry(0, entry(
size() - 1));
152 HeapVector::resize(
size() - 1);
155 HeapVector::resize(
size() - 1);
163 const int pos = interface_.get_heap_position(h);
164 interface_.set_heap_position(h, -1);
167 assert(
static_cast<unsigned int>(pos) <
size());
170 if (
static_cast<unsigned int>(pos) ==
size() - 1)
171 HeapVector::resize(
size() - 1);
174 entry(pos, entry(
size() - 1));
175 HeapVector::resize(
size() - 1);
187 const int pos = interface_.get_heap_position(h);
189 assert(
static_cast<unsigned int>(pos) <
size());
201 for (
unsigned int i = 0; i <
size(); ++i) {
202 if (((j = left(i)) <
size()) &&
203 interface_.greater(entry(i), entry(j))) {
204 std::cerr <<
"Heap condition violated\n";
207 if (((j = right(i)) <
size()) &&
208 interface_.greater(entry(i), entry(j))) {
209 std::cerr <<
"Heap condition violated\n";
218 typedef std::vector<HeapEntry> HeapVector;
224 void upheap(
unsigned int idx) {
225 HeapEntry h = entry(idx);
226 unsigned int parentIdx;
228 while ((idx > 0) && interface_.less(h, entry(parentIdx = parent(idx)))) {
229 entry(idx, entry(parentIdx));
240 void downheap(
unsigned int idx) {
241 HeapEntry h = entry(idx);
242 const unsigned int s =
size();
244 unsigned int childIdx = left(idx);
248 if ((childIdx + 1 < s) &&
249 (interface_.less(entry(childIdx + 1), entry(childIdx))))
252 if (interface_.less(h, entry(childIdx)))
255 entry(idx, entry(childIdx));
267 HeapEntry entry(
unsigned int idx) {
268 assert(idx <
size());
269 return (This::operator[](idx));
277 void entry(
unsigned int idx, HeapEntry h) {
278 assert(idx <
size());
279 This::operator[](idx) = h;
280 interface_.set_heap_position(h, idx);
288 unsigned int parent(
unsigned int i) {
return (i - 1) >> 1; }
295 unsigned int left(
unsigned int i) {
return (i << 1) + 1; }
302 unsigned int right(
unsigned int i) {
return (i << 1) + 2; }
305 HeapInterface interface_;
void remove(HeapEntry h)
Removes an entry from the heap.
Definition heap.h:162
bool empty()
Checks if the heap is empty.
Definition heap.h:95
HeapEntry front()
Retrieves the first entry in the heap.
Definition heap.h:139
void pop_front()
Deletes the first entry in the heap.
Definition heap.h:147
Heap()
Default constructor.
Definition heap.h:73
unsigned int size()
Returns the size of the heap.
Definition heap.h:101
void update(HeapEntry h)
Updates an entry in the heap.
Definition heap.h:186
void insert(HeapEntry h)
Inserts an entry into the heap.
Definition heap.h:130
~Heap()=default
Destructor.
void reset_heap_position(HeapEntry h)
Resets the heap position of an entry to -1 (not in heap).
Definition heap.h:113
void clear()
Clears the heap.
Definition heap.h:89
bool is_stored(HeapEntry h)
Checks if an entry is stored in the heap.
Definition heap.h:122
bool check()
Checks the heap condition.
Definition heap.h:198
void reserve(unsigned int n)
Reserves space for n entries.
Definition heap.h:107
Heap(const HeapInterface &i)
Constructs a heap with a given HeapInterface.
Definition heap.h:79
Heap< SurfaceMesh::Vertex, HeapInterface > This
Definition heap.h:68
Definition collider.cpp:182