73 #ifndef OPENMESH_UTILS_HEAPT_HH 74 #define OPENMESH_UTILS_HEAPT_HH 82 #if (defined(_MSC_VER) && (_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 (defined(_MSC_VER) && (_MSC_VER >= 1900)) || __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__) 154 HeapT(HeapInterface _interface)
156 : HeapVector(), interface_(std::move(_interface))
159 HeapT(
const HeapInterface &_interface)
161 : HeapVector(), interface_(_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); }
190 { interface_.set_heap_position(_h, -1); }
194 {
return interface_.get_heap_position(_h) != -1; }
207 return HeapVector::front();
214 reset_heap_position(HeapVector::front());
217 entry(0, entry(size()-1));
228 void remove(HeapEntry _h)
230 int pos = interface_.get_heap_position(_h);
231 reset_heap_position(_h);
234 assert((
unsigned int) pos < size());
237 if ((
unsigned int) pos == size()-1)
243 entry(pos, entry(size()-1));
255 int pos = interface_.get_heap_position(_h);
257 assert((
unsigned int)pos < size());
267 for (i=0; i<size(); ++i)
269 if (((j=left(i))<size()) && interface_.greater(entry(i), entry(j)))
271 omerr() <<
"Heap condition violated\n";
274 if (((j=right(i))<size()) && interface_.greater(entry(i), entry(j)))
276 omerr() <<
"Heap condition violated\n";
289 typedef std::vector<HeapEntry> HeapVector;
293 void upheap(
size_t _idx);
297 void downheap(
size_t _idx);
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 bool less(const HeapEntry &_e1, const HeapEntry &_e2)
Comparison of two HeapEntry's: strict less.
void reset_heap_position(HeapEntry _h)
reset heap position to -1 (not in heap)
bool check()
check heap condition
HeapInterface interface_
Instance of HeapInterface.
bool is_stored(HeapEntry _h)
is an entry in the heap?
void entry(size_t _idx, HeapEntry _h)
Set entry _h to index _idx and update _h's heap position.
size_t size() const
returns the size of heap
HeapEntry entry(size_t _idx) const
Get the entry at index _idx.
void pop_front()
delete the first entry
void insert(HeapEntry _h)
insert the entry _h
int get_heap_position(const HeapEntry &_e)
Get the heap position of HeapEntry _e.
void reserve(size_t _n)
reserve space for _n entries
void set_heap_position(HeapEntry &_e, int _i)
Set the heap position of HeapEntry _e.
bool greater(const HeapEntry &_e1, const HeapEntry &_e2)
Comparison of two HeapEntry's: strict greater.
void upheap(size_t _idx)
Upheap. Establish heap property.
size_t parent(size_t _i)
Get parent's index.
HeapEntry front() const
get the first entry
bool empty() const
is heap empty?
void clear()
clear the heap
void downheap(size_t _idx)
Downheap. Establish heap property.
size_t right(size_t _i)
Get right child's index.
size_t left(size_t _i)
Get left child's index.
void update(HeapEntry _h)