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 empty() const
is heap empty?
Definition: HeapT.hh:180
HeapEntry front() const
get the first entry
Definition: HeapT.hh:204
This class demonstrates the HeapInterface's interface.
Definition: HeapT.hh:103
bool is_stored(HeapEntry _h)
is an entry in the heap?
Definition: HeapT.hh:193
~HeapT()
Destructor.
Definition: HeapT.hh:166
void clear()
clear the heap
Definition: HeapT.hh:177
void set_heap_position(HeapEntry &_e, int _i)
Set the heap position of HeapEntry _e.
void insert(HeapEntry _h)
insert the entry _h
Definition: HeapT.hh:197
This file provides the streams omlog, omout, and omerr.
HeapInterface interface_
Instance of HeapInterface.
Definition: HeapT.hh:285
int get_heap_position(const HeapEntry &_e)
Get the heap position of HeapEntry _e.
void pop_front()
delete the first entry
Definition: HeapT.hh:211
bool greater(const HeapEntry &_e1, const HeapEntry &_e2)
Comparison of two HeapEntry's: strict greater.
void reset_heap_position(HeapEntry _h)
reset heap position to -1 (not in heap)
Definition: HeapT.hh:189
An efficient, highly customizable heap.
Definition: HeapT.hh:143
size_t size() const
returns the size of heap
Definition: HeapT.hh:183
bool less(const HeapEntry &_e1, const HeapEntry &_e2)
Comparison of two HeapEntry's: strict less.
HeapT()
Constructor.
Definition: HeapT.hh:151
void update(HeapEntry _h)
update an entry: change the key and update the position to reestablish the heap property.
Definition: HeapT.hh:253
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:64
void reserve(size_t _n)
reserve space for _n entries
Definition: HeapT.hh:186
bool check()
check heap condition
Definition: HeapT.hh:263