MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
WEMHeap.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2007, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#pragma once
14
15#include "WEMVector.h"
17
18
20
22
27template <class T> class WEMHeap : public WEMVector<WEMPrimitive>
28{
29public:
31 WEMHeap();
37 T* root();
39 const T* root() const;
41 void swap(unsigned int i, unsigned int j) override;
43 void insert(WEMPrimitive* wp);
45 void insert(WEMPrimitive* wp, double v);
47 void update(WEMPrimitive* wp, double nv);
49 int remove(WEMPrimitive* wp) override;
51 void sort();
53 void clear() override;
54
57 inline int parent(int i) const
58 {
59 return (i - 1) / 2;
60 }
62 inline int left(int i) const
63 {
64 return 2 * i + 1;
65 }
67 inline int right(int i) const
68 {
69 return 2 * i + 2;
70 }
72 int upHeap(int i);
74 int downHeap(int i);
75};
76
79
80template <class T> WEMHeap<T>::WEMHeap()
82{}
83
85
86template <class T> WEMHeap<T>::~WEMHeap()
87{
88 clear();
89}
90
92
93template <class T> T* WEMHeap<T>::root()
94{
96 if (wp) {
97 return reinterpret_cast<T*>(wp);
98 }
99 else {
100 return nullptr;
101 }
102}
103
105
106template <class T> const T* WEMHeap<T>::root() const
107{
108 return root();
109}
110
112
113template <class T> void WEMHeap<T>::swap(unsigned int i, unsigned int j)
114{
116 at(i)->setHeapPosition(i);
117 at(j)->setHeapPosition(j);
118}
119
121
122template <class T> void WEMHeap<T>::insert(WEMPrimitive* wp)
123{
124 if (wp == nullptr) {
125 return;
126 }
127 if (wp->getHeapPosition() != -1) {
128 return;
129 }
130
132 const unsigned int n = num() - 1;
133 last()->setHeapPosition(n);
134 upHeap(n);
135}
136
138
139template <class T> void WEMHeap<T>::insert(WEMPrimitive* wp, double v)
140{
141 if (wp == nullptr) {
142 return;
143 }
144
145 if (wp->getHeapPosition() == -1) {
146 wp->setHeapValue(v);
148 const unsigned int n = num() - 1;
149 last()->setHeapPosition(n);
150 upHeap(n);
151 }
152 else {
153 update(wp, v);
154 }
155}
156
158
159template <class T> int WEMHeap<T>::remove(WEMPrimitive* wp)
160{
161 if (wp == nullptr) {
162 return -1;
163 }
164
165 const int i = wp->getHeapPosition();
166 if (i == -1) {
167 return -1;
168 }
169
170 const int n = num();
171 if (i >= n) {
172 return -1;
173 }
174
175 if (i != n - 1) {
176 swap(i, n - 1);
177 wp->setHeapPosition(-1);
178 deleteLast();
179 if (WEMMinHeapCompare::less(*at(i), *wp)) {
180 return upHeap(i);
181 }
182 else {
183 return downHeap(i);
184 }
185 }
186 else {
187 wp->setHeapPosition(-1);
188 deleteLast();
189 }
190 return -1;
191}
192
194
195template <class T> void WEMHeap<T>::update(WEMPrimitive* wp, double nv)
196{
197 const int i = wp->getHeapPosition();
198 if (i == -1) {
199 return;
200 }
201
202 const int n = num();
203 if (i >= n) {
204 return;
205 }
206
207 const double ov = wp->getHeapValue();
208
209 wp->setHeapValue(nv);
210
211 if (nv < ov) {
212 upHeap(i);
213 }
214 else {
215 downHeap(i);
216 }
217}
218
220
221template <class T> int WEMHeap<T>::upHeap(int i)
222{
223 if (i == 0) {
224 return 0;
225 }
226 const int pi = parent(i);
227 if (WEMMinHeapCompare::less(*at(i), *at(pi))) {
228 swap(i, pi);
229 return upHeap(pi);
230 }
231 return i;
232}
233
235
236template <class T> int WEMHeap<T>::downHeap(int i)
237{
238 int largest = i, l = left(i), r = right(i);
239 const int n = num();
240
241 if (i >= n) {
242 return -1;
243 }
244 if ((l < n) && WEMMinHeapCompare::less(*at(l), *at(largest))) {
245 largest = l;
246 }
247 if ((r < n) && WEMMinHeapCompare::less(*at(r), *at(largest))) {
248 largest = r;
249 }
250
251 if (largest != i) {
252 swap(i, largest);
253 return downHeap(largest);
254 }
255 return i;
256}
257
259
260template <class T> void WEMHeap<T>::sort()
261{
264
265 unsigned int i = 0;
266 const unsigned int n = num();
267 for (i = 0; i < n; i++) {
268 newheap->append(at(i));
269 }
270 clear();
271 const unsigned int nn = newheap->num();
272 for (i = 0; i < nn; i++) {
273 insert(newheap->at(i));
274 }
275 delete newheap;
276 newheap = nullptr;
277}
278
280
281template <class T> void WEMHeap<T>::clear()
282{
283 const unsigned int n = num();
284 for (unsigned int i = 0; i < n; i++) {
285 at(i)->setHeapPosition(-1);
286 }
288}
289
@ T
Min-Heap structure with property v[i]<v[2*i+1] and v[i]<v[2*i+2] Parent at index i has children at in...
Definition WEMHeap.h:28
void update(WEMPrimitive *wp, double nv)
Updates given heap element with the given value and resorts this heap.
Definition WEMHeap.h:195
void clear() override
Remove all elements from heap.
Definition WEMHeap.h:281
void sort()
Sorts this heap.
Definition WEMHeap.h:260
void swap(unsigned int i, unsigned int j) override
Swaps two heap elements given by their indices and resorts this heap.
Definition WEMHeap.h:113
T * root()
Returns the root (first) element of this heap, typecast from WEMPrimitive to T.
Definition WEMHeap.h:93
~WEMHeap() override
Standard destructor.
Definition WEMHeap.h:86
int remove(WEMPrimitive *wp) override
Removes the given heap element and resorts this heap.
Definition WEMHeap.h:159
WEMHeap(WEMHeap &&) noexcept=default
void insert(WEMPrimitive *wp)
Inserts the given heap element and resorts this heap.
Definition WEMHeap.h:122
This is the base class for the WEM elements nodes, edges, and faces.
Dynamic templated vector.
Definition WEMVector.h:27
virtual void clear()
Clears all internal pointers.
Definition WEMVector.h:167
virtual void swap(unsigned int p1, unsigned int p2)
Swaps the two elements given by their indices in this vector.
Definition WEMVector.h:240
virtual unsigned int appendUnsafe(T *elem)
Append element to back of vector, don't check on element being non-NULL.
Definition WEMVector.h:220
T * first()
Returns the first element.
Definition WEMVector.h:47
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
IteratorTraits::value_type & at(const ml_iterator_map< IteratorTraits, IDMap > &i, typename property_traits< IDMap >::key_type key)
static bool less(const WEMPrimitive &x, const WEMPrimitive &y)