MeVisLab Toolbox Reference
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>
28class WEMHeap : public WEMVector<WEMPrimitive>
29{
30public:
31
33 WEMHeap();
39 T* root();
41 const T* root() const;
43 void swap(unsigned int i,unsigned int j) override;
45 void insert(WEMPrimitive *wp);
47 void insert(WEMPrimitive *wp, double v);
49 void update(WEMPrimitive *wp, double nv);
51 int remove(WEMPrimitive *wp) override;
53 void sort();
55 void clear() override;
56
58
60 inline int parent(int i) const {return (i-1)/2;}
62 inline int left(int i) const {return 2*i+1;}
64 inline int right(int i) const {return 2*i+2;}
66 int upHeap(int i);
68 int downHeap(int i);
69};
70
73
74template <class T>
78
80
81template <class T>
83{
84 clear();
85}
86
88
89template <class T>
91{
93 if (wp)
94 {
95 return reinterpret_cast<T*>(wp);
96 }
97 else
98 {
99 return nullptr;
100 }
101}
102
104
105template <class T>
106const T* WEMHeap<T>::root() const
107{
108 return root();
109}
110
112
113template <class T>
114void WEMHeap<T>::swap(unsigned int i,unsigned int j)
115{
117 at(i)->setHeapPosition(i);
118 at(j)->setHeapPosition(j);
119}
120
122
123template <class T>
125{
126 if (wp == nullptr) { return; }
127 if (wp->getHeapPosition() != -1) { return; }
128
130 const unsigned int n = num()-1;
131 last()->setHeapPosition(n);
132 upHeap(n);
133}
134
136
137template <class T>
139{
140 if (wp == nullptr) { return; }
141
142 if (wp->getHeapPosition() == -1)
143 {
144 wp->setHeapValue(v);
146 const unsigned int n = num()-1;
147 last()->setHeapPosition(n);
148 upHeap(n);
149 }
150 else
151 {
152 update(wp,v);
153 }
154}
155
157
158template <class T>
160{
161 if (wp == nullptr) { return -1; }
162
163 const int i = wp->getHeapPosition();
164 if (i == -1) { return -1; }
165
166 const int n = num();
167 if (i >= n) { return -1; }
168
169 if (i != n - 1)
170 {
171 swap(i,n - 1);
172 wp->setHeapPosition(-1);
173 deleteLast();
174 if (WEMMinHeapCompare::less(*at(i), *wp))
175 {
176 return upHeap(i);
177 }
178 else
179 {
180 return downHeap(i);
181 }
182 }
183 else
184 {
185 wp->setHeapPosition(-1);
186 deleteLast();
187 }
188 return -1;
189}
190
192
193template <class T>
195{
196 const int i = wp->getHeapPosition();
197 if (i == -1) { return; }
198
199 const int n = num();
200 if (i >= n) { return; }
201
202 const double ov = wp->getHeapValue();
203
204 wp->setHeapValue(nv);
205
206 if (nv < ov)
207 {
208 upHeap(i);
209 }
210 else
211 {
212 downHeap(i);
213 }
214}
215
217
218template <class T>
219int WEMHeap<T>::upHeap(int i)
220{
221 if (i == 0) { return 0; }
222 const int pi = parent(i);
223 if (WEMMinHeapCompare::less(*at(i), *at(pi)))
224 {
225 swap(i,pi);
226 return upHeap(pi);
227 }
228 return i;
229}
230
232
233template <class T>
234int WEMHeap<T>::downHeap(int i)
235{
236 int largest=i,l=left(i),r=right(i);
237 const int n = num();
238
239 if (i >= n) { return -1; }
240 if ((l < n) && WEMMinHeapCompare::less(*at(l), *at(largest)))
241 {
242 largest = l;
243 }
244 if ((r < n) && WEMMinHeapCompare::less(*at(r), *at(largest)))
245 {
246 largest = r;
247 }
248
249 if (largest !=i )
250 {
251 swap(i,largest);
252 return downHeap(largest);
253 }
254 return i;
255}
256
258
259template <class T>
261{
264
265 unsigned int i = 0;
266 const unsigned int n = num();
267 for (i = 0; i < n; i ++) { newheap->append(at(i)); }
268 clear();
269 const unsigned int nn = newheap->num();
270 for (i = 0; i < nn; i ++) { insert(newheap->at(i)); }
271 delete newheap;
272 newheap = nullptr;
273}
274
276
277template <class T>
279{
280 const unsigned int n = num();
281 for (unsigned int i = 0; i < n; i++) {
282 at(i)->setHeapPosition(-1);
283 }
285}
286
@ 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:29
void update(WEMPrimitive *wp, double nv)
Updates given heap element with the given value and resorts this heap.
Definition WEMHeap.h:194
void clear() override
Remove all elements from heap.
Definition WEMHeap.h:278
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:114
T * root()
Returns the root (first) element of this heap, typecast from WEMPrimitive to T.
Definition WEMHeap.h:90
~WEMHeap() override
Standard destructor.
Definition WEMHeap.h:82
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:124
This is the base class for the WEM elements nodes, edges, and faces.
Dynamic templated vector.
Definition WEMVector.h:28
virtual void clear()
Clears all internal pointers.
Definition WEMVector.h:152
virtual void swap(unsigned int p1, unsigned int p2)
Swaps the two elements given by their indices in this vector.
Definition WEMVector.h:227
virtual unsigned int appendUnsafe(T *elem)
Append element to back of vector, don't check on element being non-NULL.
Definition WEMVector.h:205
T * first()
Returns the first element.
Definition WEMVector.h:43
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)