MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
CSOObjectHeap.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
15
16#pragma once
17
18
19#include "CSOObjectVector.h"
20
21
23
25
30template <class T> class CSOObjectHeap : public CSOObjectVector<T>
31{
32
33public:
37 ~CSOObjectHeap() override;
39 T* root() const;
41 void swap(unsigned int i, unsigned int j) override;
43 void insert(T* we);
45 void insert(T* we, float v);
47 void update(T* we, float nv);
50 int remove(T* we) override;
52 void sort();
53
54private:
56 inline int parent(int i) const
57 {
58 return (i - 1) / 2;
59 }
61 inline int left(int i) const
62 {
63 return 2 * i + 1;
64 }
66 inline int right(int i) const
67 {
68 return 2 * i + 2;
69 }
71 int upHeap(int i);
73 int downHeap(int i);
74};
75
78
81{}
82
84
89
91
92template <class T> T* CSOObjectHeap<T>::root() const
93{
94 return static_cast<T*>(CSOObjectVector<T>::firstBoundsCheck());
95}
96
98
99template <class T> void CSOObjectHeap<T>::swap(unsigned int i, unsigned int j)
100{
102 CSOObjectVector<T>::at(i)->heapPosition = i;
103 CSOObjectVector<T>::at(j)->heapPosition = j;
104}
105
107
108template <class T> void CSOObjectHeap<T>::insert(T* we)
109{
110 if (we == nullptr) {
111 return;
112 }
113 if (we->heapPosition != -1) {
114 return;
115 }
116
118 // No range cast, explicitly allow heapPosition to be < 0 if num() is 0.
119 const int n = static_cast<int>(CSOObjectVector<T>::num()) - 1;
120 CSOObjectVector<T>::last()->heapPosition = n;
121 upHeap(n);
122}
123
125
126template <class T> void CSOObjectHeap<T>::insert(T* we, float v)
127{
128 if (we == nullptr) {
129 return;
130 }
131
132 if (we->heapPosition == -1) {
133 we->value = v;
136 upHeap(CSOObjectVector<T>::num() - 1);
137 }
138 else {
139 update(we, v);
140 }
141}
142
144
145template <class T> int CSOObjectHeap<T>::remove(T* we)
146{
147 if (we == nullptr) {
148 return -1;
149 }
150
151 const int i = we->heapPosition;
152 if (i == -1) {
153 return -1;
154 }
155
156 const int n = CSOObjectVector<T>::num();
157 if (i >= n) {
158 return -1;
159 }
160
161 if (i != n - 1) {
162 swap(i, n - 1);
163 CSOObjectVector<T>::at(n - 1)->heapPosition = -1;
166 return upHeap(i);
167 }
168 else {
169 return downHeap(i);
170 }
171 }
172 else {
173 CSOObjectVector<T>::at(n - 1)->heapPosition = -1;
175 }
176 return -1;
177}
178
180
181template <class T> void CSOObjectHeap<T>::update(T* we, float nv)
182{
183 const int i = we->heapPosition;
184 if (i == -1) {
185 return;
186 }
187
188 const int n = CSOObjectVector<T>::num();
189 if (i >= n) {
190 return;
191 }
192
193 const float ov = static_cast<float>(we->value);
194
195 we->value = nv;
196
197 if (nv < ov) {
198 upHeap(i);
199 }
200 else {
201 downHeap(i);
202 }
203}
204
206
207template <class T> int CSOObjectHeap<T>::upHeap(int i)
208{
209 if (i == 0) {
210 return 0;
211 }
212 const int pi = parent(i);
213 if (CSOObjectVector<T>::at(i)->value < CSOObjectVector<T>::at(pi)->value) {
214 swap(i, pi);
215 return upHeap(pi);
216 }
217 return i;
218}
219
221
222template <class T> int CSOObjectHeap<T>::downHeap(int i)
223{
224 int largest = i, l = left(i), r = right(i);
225 const int n = CSOObjectVector<T>::num();
226
227 if (i >= n) {
228 return -1;
229 }
230 if ((l < n) && (CSOObjectVector<T>::at(l)->value < CSOObjectVector<T>::at(largest)->value)) {
231 largest = l;
232 }
233 if ((r < n) && (CSOObjectVector<T>::at(r)->value < CSOObjectVector<T>::at(largest)->value)) {
234 largest = r;
235 }
236
237 if (largest != i) {
238 swap(i, largest);
239 return downHeap(largest);
240 }
241 return i;
242}
243
245
246template <class T> void CSOObjectHeap<T>::sort()
247{
248 CSOObjectVector<T>* newheap = nullptr;
250
251 unsigned int i = 0;
252 unsigned int n = CSOObjectVector<T>::num();
253 for (i = 0; i < n; ++i) {
255 }
257 n = newheap->CSOObjectVector<T>::num();
258 for (i = 0; i < n; ++i) {
259 insert(newheap->CSOObjectVector<T>::at(i));
260 }
261 delete newheap;
262 newheap = nullptr;
263}
264
266
@ T
Heap structure with property i>2*i+1 and i>2*i+2.
T * root() const
Returns the root (first) element of heap that is typecast from CSOLiveWireNode to T.
void insert(T *we)
Inserts heap element and resorts the heap.
int remove(T *we) override
Removes the heap element, and resorts the heap.
void swap(unsigned int i, unsigned int j) override
Swaps two heap elements and resorts the heap.
void sort()
Sorts the heap.
~CSOObjectHeap() override
Standard destructor.
void update(T *we, float nv)
Updates the specified heap element with the new value, and resorts the heap.
Dynamic templated vector.
unsigned int num() const
Returns the number of elements in the vector.
virtual void swap(unsigned int p1, unsigned int p2)
Swaps two elements in the vector.
T * firstBoundsCheck() const
Returns the first element, and return nullptr if out of range.
T * at(unsigned int pos) const
Returns the element at the specified position.
virtual void deleteLast()
Deletes the last element of the vector.
virtual unsigned int appendUnsafe(T *elem)
Appends the element to back of vector.
virtual void clear()
Clears all internal pointers.
T * last() const
Returns the last element.
Target mlrange_cast(Source arg)
Generic version of checked ML casts.