73 #ifndef OPENMESH_UTILS_HEAPT_HH
74 #define OPENMESH_UTILS_HEAPT_HH
82 #if _MSC_VER >= 1900 || __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__)
102 template <
class HeapEntry>
106 bool less(
const HeapEntry& _e1,
const HeapEntry& _e2);
109 bool greater(
const HeapEntry& _e1,
const HeapEntry& _e2);
142 template <
class HeapEntry,
class HeapInterface=HeapEntry>
143 class HeapT :
private std::vector<HeapEntry>
146 typedef std::vector<HeapEntry> Base;
153 #if _MSC_VER >= 1900 || __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__)
154 HeapT(HeapInterface _interface)
159 HeapT(
const HeapInterface &_interface)
168 HeapInterface &getInterface() {
172 const HeapInterface &getInterface()
const {
177 void clear() { HeapVector::clear(); }
180 bool empty()
const {
return HeapVector::empty(); }
183 size_t size()
const {
return HeapVector::size(); }
186 void reserve(
size_t _n) { HeapVector::reserve(_n); }
194 {
return interface_.get_heap_position(_h) != -1; }
207 return HeapVector::front();
228 void remove(HeapEntry _h)
234 assert((
unsigned int) pos <
size());
237 if ((
unsigned int) pos ==
size()-1)
257 assert((
unsigned int)pos <
size());
267 for (i=0; i<
size(); ++i)
271 omerr() <<
"Heap condition violated\n";
276 omerr() <<
"Heap condition violated\n";
289 typedef std::vector<HeapEntry> HeapVector;
301 inline HeapEntry
entry(
size_t _idx)
const
303 assert(_idx <
size());
304 return (Base::operator[](_idx));
309 inline void entry(
size_t _idx, HeapEntry _h)
311 assert(_idx <
size());
312 Base::operator[](_idx) = _h;
313 interface_.set_heap_position(_h,
int(_idx));
318 inline size_t parent(
size_t _i) {
return (_i-1)>>1; }
320 inline size_t left(
size_t _i) {
return (_i<<1)+1; }
322 inline size_t right(
size_t _i) {
return (_i<<1)+2; }
332 template <
class HeapEntry,
class HeapInterface>
337 HeapEntry h = entry(_idx);
340 while ((_idx>0) && interface_.less(h, entry(parentIdx=parent(_idx))))
342 entry(_idx, entry(parentIdx));
353 template <
class HeapEntry,
class HeapInterface>
358 const HeapEntry h = entry(_idx);
360 const size_t s = size();
364 childIdx = left(_idx);
365 if (childIdx >= s)
break;
367 if ((childIdx + 1 < s) && (interface_.less(entry(childIdx + 1), entry(childIdx))))
370 if (interface_.less(h, entry(childIdx)))
break;
372 entry(_idx, entry(childIdx));
384 #endif // OSG_HEAP_HH defined
size_t left(size_t _i)
Get left child's index.
void clear()
clear the heap
bool greater(const HeapEntry &_e1, const HeapEntry &_e2)
Comparison of two HeapEntry's: strict greater.
HeapInterface interface_
Instance of HeapInterface.
void entry(size_t _idx, HeapEntry _h)
Set entry _h to index _idx and update _h's heap position.
void downheap(size_t _idx)
Downheap. Establish heap property.
bool empty() const
is heap empty?
size_t parent(size_t _i)
Get parent's index.
size_t size() const
returns the size of heap
bool is_stored(HeapEntry _h)
is an entry in the heap?
bool less(const HeapEntry &_e1, const HeapEntry &_e2)
Comparison of two HeapEntry's: strict less.
HeapEntry front() const
get the first entry
void upheap(size_t _idx)
Upheap. Establish heap property.
void set_heap_position(HeapEntry &_e, int _i)
Set the heap position of HeapEntry _e.
void reserve(size_t _n)
reserve space for _n entries
int get_heap_position(const HeapEntry &_e)
Get the heap position of HeapEntry _e.
void reset_heap_position(HeapEntry _h)
reset heap position to -1 (not in heap)
void update(HeapEntry _h)
bool check()
check heap condition
void insert(HeapEntry _h)
insert the entry _h
size_t right(size_t _i)
Get right child's index.
void pop_front()
delete the first entry
HeapEntry entry(size_t _idx) const
Get the entry at index _idx.