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__)
97template <
class HeapEntry>
101 bool less(
const HeapEntry& _e1,
const HeapEntry& _e2);
104 bool greater(
const HeapEntry& _e1,
const HeapEntry& _e2);
137template <
class HeapEntry,
class HeapInterface=HeapEntry>
138class 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__)
150 explicit HeapT(HeapInterface _interface)
151 : HeapVector(),
interface_(std::move(_interface))
155 explicit HeapT(
const HeapInterface &_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); }
189 {
return interface_.get_heap_position(_h) != -1; }
202 return HeapVector::front();
212 entry(0, entry(
size()-1));
229 assert((
unsigned int) pos <
size());
232 if ((
unsigned int) pos ==
size()-1)
238 entry(pos, entry(
size()-1));
252 assert((
unsigned int)pos <
size());
261 for (
unsigned int 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;
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; }
327template <
class HeapEntry,
class HeapInterface>
329HeapT<HeapEntry, HeapInterface>::
332 HeapEntry h = entry(_idx);
335 while ((_idx>0) && interface_.less(h, entry(parentIdx=parent(_idx))))
337 entry(_idx, entry(parentIdx));
348template <
class HeapEntry,
class HeapInterface>
350HeapT<HeapEntry, HeapInterface>::
353 const HeapEntry h = entry(_idx);
354 const size_t s = size();
358 size_t childIdx = left(_idx);
359 if (childIdx >= s)
break;
361 if ((childIdx + 1 < s) && (interface_.less(entry(childIdx + 1), entry(childIdx))))
364 if (interface_.less(h, entry(childIdx)))
break;
366 entry(_idx, entry(childIdx));
This file provides the streams omlog, omout, and omerr.
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:59
This class demonstrates the HeapInterface's interface.
Definition: HeapT.hh:99
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 set_heap_position(HeapEntry &_e, int _i)
Set the heap position of HeapEntry _e.
bool less(const HeapEntry &_e1, const HeapEntry &_e2)
Comparison of two HeapEntry's: strict less.
An efficient, highly customizable heap.
Definition: HeapT.hh:139
HeapT(const HeapInterface &_interface)
Construct with a given HeapIterface.
Definition: HeapT.hh:155
HeapT()
Constructor.
Definition: HeapT.hh:146
size_t size() const
returns the size of heap
Definition: HeapT.hh:178
void reset_heap_position(HeapEntry _h)
reset heap position to -1 (not in heap)
Definition: HeapT.hh:184
bool check()
check heap condition
Definition: HeapT.hh:258
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
~HeapT()
Destructor.
Definition: HeapT.hh:161
void insert(HeapEntry _h)
insert the entry _h
Definition: HeapT.hh:192
HeapEntry front() const
get the first entry
Definition: HeapT.hh:199
void pop_front()
delete the first entry
Definition: HeapT.hh:206
void reserve(size_t _n)
reserve space for _n entries
Definition: HeapT.hh:181
void remove(HeapEntry _h)
remove an entry
Definition: HeapT.hh:223
bool is_stored(HeapEntry _h)
is an entry in the heap?
Definition: HeapT.hh:188
HeapInterface interface_
Instance of HeapInterface.
Definition: HeapT.hh:280
void clear()
clear the heap
Definition: HeapT.hh:172