66 #include "../Config/ACGDefines.hh" 86 template <
class HeapEntry>
90 bool less(
const HeapEntry& _e1,
const HeapEntry& _e2);
93 bool greater(
const HeapEntry& _e1,
const HeapEntry& _e2);
120 template <
class HeapEntry,
class HeapInterface=HeapEntry>
121 class ACGDLLEXPORT
HeapT :
private std::vector<HeapEntry>
125 typedef std::vector<HeapEntry> Base;
132 HeapT(
const HeapInterface& _interface)
133 : HeapVector(), interface_(_interface)
141 void clear() { HeapVector::clear(); }
144 bool empty() {
return HeapVector::empty(); }
147 unsigned int size() {
return HeapVector::size(); }
150 void reserve(
unsigned int _n) { HeapVector::reserve(_n); }
154 { interface_.set_heap_position(_h, -1); }
158 {
return interface_.get_heap_position(_h) != -1; }
161 void insert(HeapEntry _h) { this->push_back(_h); upheap(size()-1); }
164 HeapEntry
front() { assert(!empty());
return entry(0); }
170 interface_.set_heap_position(entry(0), -1);
173 entry(0, entry(size()-1));
174 this->resize(size()-1);
177 else this->resize(size()-1);
181 void remove(HeapEntry _h)
183 int pos = interface_.get_heap_position(_h);
184 interface_.set_heap_position(_h, -1);
187 assert((
unsigned int) pos < size());
190 if ((
unsigned int) pos == size()-1)
194 entry(pos, entry(size()-1));
206 int pos = interface_.get_heap_position(_h);
208 assert((
unsigned int)pos < size());
218 for (i=0; i<size(); ++i)
220 if (((j=left(i))<size()) && interface_.greater(entry(i), entry(j))) {
221 std::cerr <<
"Heap condition violated\n";
224 if (((j=right(i))<size()) && interface_.greater(entry(i), entry(j))){
225 std::cerr <<
"Heap condition violated\n";
236 typedef std::vector<HeapEntry> HeapVector;
240 void upheap(
unsigned int _idx);
244 void downheap(
unsigned int _idx);
248 inline HeapEntry
entry(
unsigned int _idx) {
249 assert(_idx < size());
250 return (Base::operator[](_idx));
255 inline void entry(
unsigned int _idx, HeapEntry _h) {
256 assert(_idx < size());
257 Base::operator[](_idx) = _h;
258 interface_.set_heap_position(_h, _idx);
263 inline unsigned int parent(
unsigned int _i) {
return (_i-1)>>1; }
265 inline unsigned int left(
unsigned int _i) {
return (_i<<1)+1; }
267 inline unsigned int right(
unsigned int _i) {
return (_i<<1)+2; }
280 template <
class HeapEntry,
class HeapInterface>
285 HeapEntry h = entry(_idx);
286 unsigned int parentIdx;
289 interface_.less(h, entry(parentIdx=parent(_idx))))
291 entry(_idx, entry(parentIdx));
302 template <
class HeapEntry,
class HeapInterface>
307 HeapEntry h = entry(_idx);
308 unsigned int childIdx;
309 unsigned int s = size();
313 childIdx = left(_idx);
314 if (childIdx >= s)
break;
316 if ((childIdx+1 < s) &&
317 (interface_.less(entry(childIdx+1), entry(childIdx))))
320 if (interface_.less(h, entry(childIdx)))
break;
322 entry(_idx, entry(childIdx));
333 #endif // ACG_HEAP_HH defined bool check()
check heap condition
void update(HeapEntry _h)
bool is_stored(HeapEntry _h)
is an entry in the heap?
int get_heap_position(const HeapEntry &_e)
Get the heap position of HeapEntry _e.
unsigned int left(unsigned int _i)
Get left child's index.
void reset_heap_position(HeapEntry _h)
reset heap position to -1 (not in heap)
HeapInterface interface_
Instance of HeapInterface.
bool less(const HeapEntry &_e1, const HeapEntry &_e2)
Comparison of two HeapEntry's: strict less.
HeapEntry front()
get the first entry
void insert(HeapEntry _h)
insert the entry _h
void downheap(unsigned int _idx)
Downheap. Establish heap property.
HeapEntry entry(unsigned int _idx)
Get the entry at index _idx.
void upheap(unsigned int _idx)
Upheap. Establish heap property.
unsigned int right(unsigned int _i)
Get right child's index.
void reserve(unsigned int _n)
reserve space for _n entries
HeapT(const HeapInterface &_interface)
Construct with a given HeapIterface.
bool greater(const HeapEntry &_e1, const HeapEntry &_e2)
Comparison of two HeapEntry's: strict greater.
void clear()
clear the heap
Namespace providing different geometric functions concerning angles.
void pop_front()
delete the first entry
unsigned int size()
returns the size of heap
void set_heap_position(HeapEntry &_e, int _i)
Set the heap position of HeapEntry _e.
unsigned int parent(unsigned int _i)
Get parent's index.
bool empty()
is heap empty?
void entry(unsigned int _idx, HeapEntry _h)
Set entry _h to index _idx and update _h's heap position.