68 #ifndef OPENMESH_UTILS_HEAPT_HH 69 #define OPENMESH_UTILS_HEAPT_HH 77 #if (defined(_MSC_VER) && (_MSC_VER >= 1800)) || __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__) 97 template <
class HeapEntry>
101 bool less(
const HeapEntry& _e1,
const HeapEntry& _e2);
104 bool greater(
const HeapEntry& _e1,
const HeapEntry& _e2);
137 template <
class HeapEntry,
class HeapInterface=HeapEntry>
138 class HeapT :
private std::vector<HeapEntry>
141 typedef std::vector<HeapEntry> Base;
148 #if (defined(_MSC_VER) && (_MSC_VER >= 1800)) || __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__) 149 HeapT(HeapInterface _interface)
151 : HeapVector(), interface_(std::move(_interface))
154 HeapT(
const HeapInterface &_interface)
156 : HeapVector(), interface_(_interface)
163 HeapInterface &getInterface() {
167 const HeapInterface &getInterface()
const {
172 void clear() { HeapVector::clear(); }
175 bool empty()
const {
return HeapVector::empty(); }
178 size_t size()
const {
return HeapVector::size(); }
181 void reserve(
size_t _n) { HeapVector::reserve(_n); }
185 { interface_.set_heap_position(_h, -1); }
189 {
return interface_.get_heap_position(_h) != -1; }
202 return HeapVector::front();
209 reset_heap_position(HeapVector::front());
212 entry(0, entry(size()-1));
223 void remove(HeapEntry _h)
225 int pos = interface_.get_heap_position(_h);
226 reset_heap_position(_h);
229 assert((
unsigned int) pos < size());
232 if ((
unsigned int) pos == size()-1)
238 entry(pos, entry(size()-1));
250 int pos = interface_.get_heap_position(_h);
252 assert((
unsigned int)pos < size());
262 for (i=0; i<size(); ++i)
264 if (((j=left(i))<size()) && interface_.greater(entry(i), entry(j)))
266 omerr() <<
"Heap condition violated\n";
269 if (((j=right(i))<size()) && interface_.greater(entry(i), entry(j)))
271 omerr() <<
"Heap condition violated\n";
284 typedef std::vector<HeapEntry> HeapVector;
288 void upheap(
size_t _idx);
292 void downheap(
size_t _idx);
296 inline HeapEntry entry(
size_t _idx)
const 298 assert(_idx < size());
299 return (Base::operator[](_idx));
304 inline void entry(
size_t _idx, HeapEntry _h)
306 assert(_idx < size());
307 Base::operator[](_idx) = _h;
308 interface_.set_heap_position(_h,
int(_idx));
313 inline size_t parent(
size_t _i) {
return (_i-1)>>1; }
315 inline size_t left(
size_t _i) {
return (_i<<1)+1; }
317 inline size_t right(
size_t _i) {
return (_i<<1)+2; }
327 template <
class HeapEntry,
class HeapInterface>
332 HeapEntry h = entry(_idx);
335 while ((_idx>0) && interface_.less(h, entry(parentIdx=parent(_idx))))
337 entry(_idx, entry(parentIdx));
348 template <
class HeapEntry,
class HeapInterface>
353 const HeapEntry h = entry(_idx);
355 const size_t s = size();
359 childIdx = left(_idx);
360 if (childIdx >= s)
break;
362 if ((childIdx + 1 < s) && (interface_.less(entry(childIdx + 1), entry(childIdx))))
365 if (interface_.less(h, entry(childIdx)))
break;
367 entry(_idx, entry(childIdx));
379 #endif // OSG_HEAP_HH defined HeapT()
Constructor.
Definition: HeapT.hh:146
An efficient, highly customizable heap.
Definition: HeapT.hh:138
size_t size() const
returns the size of heap
Definition: HeapT.hh:178
int get_heap_position(const HeapEntry &_e)
Get the heap position of HeapEntry _e.
void set_heap_position(HeapEntry &_e, int _i)
Set the heap position of HeapEntry _e.
void reset_heap_position(HeapEntry _h)
reset heap position to -1 (not in heap)
Definition: HeapT.hh:184
HeapInterface interface_
Instance of HeapInterface.
Definition: HeapT.hh:280
~HeapT()
Destructor.
Definition: HeapT.hh:161
bool check()
check heap condition
Definition: HeapT.hh:258
bool greater(const HeapEntry &_e1, const HeapEntry &_e2)
Comparison of two HeapEntry's: strict greater.
HeapEntry front() const
get the first entry
Definition: HeapT.hh:199
bool is_stored(HeapEntry _h)
is an entry in the heap?
Definition: HeapT.hh:188
This class demonstrates the HeapInterface's interface.
Definition: HeapT.hh:98
void reserve(size_t _n)
reserve space for _n entries
Definition: HeapT.hh:181
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:59
void clear()
clear the heap
Definition: HeapT.hh:172
bool less(const HeapEntry &_e1, const HeapEntry &_e2)
Comparison of two HeapEntry's: strict less.
void pop_front()
delete the first entry
Definition: HeapT.hh:206
bool empty() const
is heap empty?
Definition: HeapT.hh:175
void update(HeapEntry _h)
update an entry: change the key and update the position to reestablish the heap property.
Definition: HeapT.hh:248
void insert(HeapEntry _h)
insert the entry _h
Definition: HeapT.hh:192
This file provides the streams omlog, omout, and omerr.