27#ifndef EASY3D_CORE_HEAP_H
28#define EASY3D_CORE_HEAP_H
58 template<
class HeapEntry,
class HeapInterface>
59 class Heap :
private std::vector<HeapEntry> {
67 explicit Heap(
const HeapInterface &i) : HeapVector(), interface_(i) {}
73 void clear() { HeapVector::clear(); }
76 bool empty() {
return HeapVector::empty(); }
79 unsigned int size() {
return (
unsigned int) HeapVector::size(); }
82 void reserve(
unsigned int n) { HeapVector::reserve(n); }
86 interface_.set_heap_position(h, -1);
91 return interface_.get_heap_position(h) != -1;
109 interface_.set_heap_position(entry(0), -1);
111 entry(0, entry(
size() - 1));
112 HeapVector::resize(
size() - 1);
115 HeapVector::resize(
size() - 1);
120 int pos = interface_.get_heap_position(h);
121 interface_.set_heap_position(h, -1);
124 assert((
unsigned int) pos <
size());
127 if ((
unsigned int) pos ==
size() - 1)
128 HeapVector::resize(
size() - 1);
131 entry(pos, entry(
size() - 1));
132 HeapVector::resize(
size() - 1);
141 int pos = interface_.get_heap_position(h);
143 assert((
unsigned int) pos <
size());
152 for (i = 0; i <
size(); ++i) {
153 if (((j = left(i)) <
size()) &&
154 interface_.greater(entry(i), entry(j))) {
155 std::cerr <<
"Heap condition violated\n";
158 if (((j = right(i)) <
size()) &&
159 interface_.greater(entry(i), entry(j))) {
160 std::cerr <<
"Heap condition violated\n";
169 typedef std::vector<HeapEntry> HeapVector;
172 void upheap(
unsigned int idx) {
173 HeapEntry h = entry(idx);
174 unsigned int parentIdx;
176 while ((idx > 0) && interface_.less(h, entry(parentIdx = parent(idx)))) {
177 entry(idx, entry(parentIdx));
185 void downheap(
unsigned int idx) {
186 HeapEntry h = entry(idx);
187 unsigned int childIdx;
188 unsigned int s =
size();
191 childIdx = left(idx);
195 if ((childIdx + 1 < s) &&
196 (interface_.less(entry(childIdx + 1), entry(childIdx))))
199 if (interface_.less(h, entry(childIdx)))
202 entry(idx, entry(childIdx));
210 inline HeapEntry entry(
unsigned int idx) {
211 assert(idx <
size());
212 return (This::operator[](idx));
216 inline void entry(
unsigned int idx, HeapEntry h) {
217 assert(idx <
size());
218 This::operator[](idx) = h;
219 interface_.set_heap_position(h, idx);
223 inline unsigned int parent(
unsigned int i) {
return (i - 1) >> 1; }
226 inline unsigned int left(
unsigned int i) {
return (i << 1) + 1; }
229 inline unsigned int right(
unsigned int i) {
return (i << 1) + 2; }
232 HeapInterface interface_;
A class implementing a heap.
Definition: heap.h:59
void remove(HeapEntry h)
remove an entry
Definition: heap.h:119
bool empty()
is heap empty?
Definition: heap.h:76
HeapEntry front()
get the first entry
Definition: heap.h:101
void pop_front()
delete the first entry
Definition: heap.h:107
Heap()
Constructor.
Definition: heap.h:64
unsigned int size()
returns the size of heap
Definition: heap.h:79
void update(HeapEntry h)
update an entry: change the key and update the position to reestablish the heap property.
Definition: heap.h:140
void insert(HeapEntry h)
insert the entry h
Definition: heap.h:95
~Heap()=default
Destructor.
void reset_heap_position(HeapEntry h)
reset heap position to -1 (not in heap)
Definition: heap.h:85
void clear()
clear the heap
Definition: heap.h:73
bool is_stored(HeapEntry h)
is an entry in the heap?
Definition: heap.h:90
bool check()
check heap condition
Definition: heap.h:149
void reserve(unsigned int n)
reserve space for N entries
Definition: heap.h:82
Heap(const HeapInterface &i)
Construct with a given HeapInterface.
Definition: heap.h:67
Definition: collider.cpp:182