60 #include "../Config/ACGDefines.hh" 80 template <
class HeapEntry>
84 bool less(
const HeapEntry& _e1,
const HeapEntry& _e2);
87 bool greater(
const HeapEntry& _e1,
const HeapEntry& _e2);
114 template <
class HeapEntry,
class HeapInterface=HeapEntry>
115 class ACGDLLEXPORT
HeapT :
private std::vector<HeapEntry>
119 typedef std::vector<HeapEntry> Base;
126 HeapT(
const HeapInterface& _interface)
127 : HeapVector(), interface_(_interface)
135 void clear() { HeapVector::clear(); }
138 bool empty() {
return HeapVector::empty(); }
141 unsigned int size() {
return HeapVector::size(); }
144 void reserve(
unsigned int _n) { HeapVector::reserve(_n); }
148 { interface_.set_heap_position(_h, -1); }
152 {
return interface_.get_heap_position(_h) != -1; }
155 void insert(HeapEntry _h) { this->push_back(_h); upheap(size()-1); }
158 HeapEntry
front() { assert(!empty());
return entry(0); }
164 interface_.set_heap_position(entry(0), -1);
167 entry(0, entry(size()-1));
168 this->resize(size()-1);
171 else this->resize(size()-1);
175 void remove(HeapEntry _h)
177 int pos = interface_.get_heap_position(_h);
178 interface_.set_heap_position(_h, -1);
181 assert((
unsigned int) pos < size());
184 if ((
unsigned int) pos == size()-1)
188 entry(pos, entry(size()-1));
200 int pos = interface_.get_heap_position(_h);
202 assert((
unsigned int)pos < size());
212 for (i=0; i<size(); ++i)
214 if (((j=left(i))<size()) && interface_.greater(entry(i), entry(j))) {
215 std::cerr <<
"Heap condition violated\n";
218 if (((j=right(i))<size()) && interface_.greater(entry(i), entry(j))){
219 std::cerr <<
"Heap condition violated\n";
230 typedef std::vector<HeapEntry> HeapVector;
234 void upheap(
unsigned int _idx);
238 void downheap(
unsigned int _idx);
242 inline HeapEntry
entry(
unsigned int _idx) {
243 assert(_idx < size());
244 return (Base::operator[](_idx));
249 inline void entry(
unsigned int _idx, HeapEntry _h) {
250 assert(_idx < size());
251 Base::operator[](_idx) = _h;
252 interface_.set_heap_position(_h, _idx);
257 inline unsigned int parent(
unsigned int _i) {
return (_i-1)>>1; }
259 inline unsigned int left(
unsigned int _i) {
return (_i<<1)+1; }
261 inline unsigned int right(
unsigned int _i) {
return (_i<<1)+2; }
274 template <
class HeapEntry,
class HeapInterface>
279 HeapEntry h = entry(_idx);
280 unsigned int parentIdx;
283 interface_.less(h, entry(parentIdx=parent(_idx))))
285 entry(_idx, entry(parentIdx));
296 template <
class HeapEntry,
class HeapInterface>
301 HeapEntry h = entry(_idx);
302 unsigned int childIdx;
303 unsigned int s = size();
307 childIdx = left(_idx);
308 if (childIdx >= s)
break;
310 if ((childIdx+1 < s) &&
311 (interface_.less(entry(childIdx+1), entry(childIdx))))
314 if (interface_.less(h, entry(childIdx)))
break;
316 entry(_idx, entry(childIdx));
327 #endif // ACG_HEAP_HH defined void reset_heap_position(HeapEntry _h)
reset heap position to -1 (not in heap)
void insert(HeapEntry _h)
insert the entry _h
Namespace providing different geometric functions concerning angles.
unsigned int parent(unsigned int _i)
Get parent's index.
bool empty()
is heap empty?
HeapT(const HeapInterface &_interface)
Construct with a given HeapIterface.
void reserve(unsigned int _n)
reserve space for _n entries
void upheap(unsigned int _idx)
Upheap. Establish heap property.
unsigned int left(unsigned int _i)
Get left child's index.
void set_heap_position(HeapEntry &_e, int _i)
Set the heap position of HeapEntry _e.
void clear()
clear the heap
int get_heap_position(const HeapEntry &_e)
Get the heap position of HeapEntry _e.
bool greater(const HeapEntry &_e1, const HeapEntry &_e2)
Comparison of two HeapEntry's: strict greater.
unsigned int size()
returns the size of heap
bool check()
check heap condition
void update(HeapEntry _h)
HeapEntry entry(unsigned int _idx)
Get the entry at index _idx.
HeapInterface interface_
Instance of HeapInterface.
bool is_stored(HeapEntry _h)
is an entry in the heap?
void pop_front()
delete the first entry
bool less(const HeapEntry &_e1, const HeapEntry &_e2)
Comparison of two HeapEntry's: strict less.
void downheap(unsigned int _idx)
Downheap. Establish heap property.
HeapEntry front()
get the first entry
unsigned int right(unsigned int _i)
Get right child's index.
void entry(unsigned int _idx, HeapEntry _h)
Set entry _h to index _idx and update _h's heap position.