66 #ifndef OPENMESH_UTILS_HEAPT_HH
67 #define OPENMESH_UTILS_HEAPT_HH
92 template <
class HeapEntry>
96 bool less(
const HeapEntry& _e1,
const HeapEntry& _e2);
99 bool greater(
const HeapEntry& _e1,
const HeapEntry& _e2);
132 template <
class HeapEntry,
class HeapInterface=HeapEntry>
133 class HeapT :
private std::vector<HeapEntry>
136 typedef std::vector<HeapEntry> Base;
144 HeapT(
const HeapInterface& _interface)
153 void clear() { HeapVector::clear(); }
156 bool empty()
const {
return HeapVector::empty(); }
159 size_t size()
const {
return HeapVector::size(); }
162 void reserve(
size_t _n) { HeapVector::reserve(_n); }
170 {
return interface_.get_heap_position(_h) != -1; }
193 entry(0, entry(
size()-1));
204 void remove(HeapEntry _h)
210 assert((
unsigned int) pos <
size());
213 if ((
unsigned int) pos ==
size()-1)
219 entry(pos, entry(
size()-1));
233 assert((
unsigned int)pos <
size());
243 for (i=0; i<
size(); ++i)
245 if (((j=left(i))<
size()) &&
interface_.greater(entry(i), entry(j)))
247 omerr() <<
"Heap condition violated\n";
250 if (((j=right(i))<
size()) &&
interface_.greater(entry(i), entry(j)))
252 omerr() <<
"Heap condition violated\n";
265 typedef std::vector<HeapEntry> HeapVector;
269 void upheap(
size_t _idx);
273 void downheap(
size_t _idx);
277 inline HeapEntry entry(
size_t _idx)
const
279 assert(_idx <
size());
280 return (Base::operator[](_idx));
285 inline void entry(
size_t _idx, HeapEntry _h)
287 assert(_idx <
size());
288 Base::operator[](_idx) = _h;
294 inline size_t parent(
size_t _i) {
return (_i-1)>>1; }
296 inline size_t left(
size_t _i) {
return (_i<<1)+1; }
298 inline size_t right(
size_t _i) {
return (_i<<1)+2; }
308 template <
class HeapEntry,
class HeapInterface>
310 HeapT<HeapEntry, HeapInterface>::
313 HeapEntry h = entry(_idx);
316 while ((_idx>0) && interface_.less(h, entry(parentIdx=parent(_idx))))
318 entry(_idx, entry(parentIdx));
329 template <
class HeapEntry,
class HeapInterface>
331 HeapT<HeapEntry, HeapInterface>::
332 downheap(
size_t _idx)
334 HeapEntry h = entry(_idx);
340 childIdx = left(_idx);
341 if (childIdx >= s)
break;
343 if ((childIdx + 1 < s) && (interface_.less(entry(childIdx + 1), entry(childIdx))))
346 if (interface_.less(h, entry(childIdx)))
break;
348 entry(_idx, entry(childIdx));
360 #endif // OSG_HEAP_HH defined
HeapEntry front() const
get the first entry
Definition: HeapT.hh:180
~HeapT()
Destructor.
Definition: HeapT.hh:149
bool less(const HeapEntry &_e1, const HeapEntry &_e2)
Comparison of two HeapEntry's: strict less.
void reserve(size_t _n)
reserve space for _n entries
Definition: HeapT.hh:162
size_t size() const
returns the size of heap
Definition: HeapT.hh:159
HeapT()
Constructor.
Definition: HeapT.hh:141
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.
void clear()
clear the heap
Definition: HeapT.hh:153
void update(HeapEntry _h)
update an entry: change the key and update the position to reestablish the heap property.
Definition: HeapT.hh:229
HeapInterface interface_
Instance of HeapInterface.
Definition: HeapT.hh:261
bool is_stored(HeapEntry _h)
is an entry in the heap?
Definition: HeapT.hh:169
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:173
This class demonstrates the HeapInterface's interface.
Definition: HeapT.hh:93
bool empty() const
is heap empty?
Definition: HeapT.hh:156
HeapT(const HeapInterface &_interface)
Construct with a given HeapIterface.
Definition: HeapT.hh:144
This file provides the streams omlog, omout, and omerr.
void pop_front()
delete the first entry
Definition: HeapT.hh:187
void reset_heap_position(HeapEntry _h)
reset heap position to -1 (not in heap)
Definition: HeapT.hh:165
bool check()
check heap condition
Definition: HeapT.hh:239
An efficient, highly customizable heap.
Definition: HeapT.hh:133