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)
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