MeVisLab Toolbox Reference
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>
32{
33
34public:
35
39 ~CSOObjectHeap() override;
41 T* root() const;
43 void swap(unsigned int i,unsigned int j) override;
45 void insert(T *we);
47 void insert(T *we,float v);
49 void update(T *we,float nv);
51 int remove(T *we) override;
53 void sort();
54
55private:
56
58 inline int parent(int i) const {return (i-1)/2;}
60 inline int left(int i) const {return 2*i+1;}
62 inline int right(int i) const {return 2*i+2;}
64 int upHeap(int i);
66 int downHeap(int i);
67};
68
71
72template <class T>
76
78
79template <class T>
84
86
87template <class T>
89{
90 return static_cast<T*>(CSOObjectVector<T>::firstBoundsCheck());
91}
92
94
95template <class T>
96void CSOObjectHeap<T>::swap(unsigned int i,unsigned int j)
97{
99 CSOObjectVector<T>::at(i)->heapPosition = i;
100 CSOObjectVector<T>::at(j)->heapPosition = j;
101}
102
104
105template <class T>
107{
108 if (we==nullptr) { return; }
109 if (we->heapPosition != -1) { return; }
110
112 // No range cast, explicitly allow heapPosition to be < 0 if num() is 0.
113 const int n = static_cast<int>(CSOObjectVector<T>::num())-1;
114 CSOObjectVector<T>::last()->heapPosition = n;
115 upHeap(n);
116}
117
119
120template <class T>
122{
123 if (we==nullptr) { return; }
124
125 if (we->heapPosition == -1)
126 {
127 we->value = v;
130 upHeap(CSOObjectVector<T>::num()-1);
131 }
132 else
133 {
134 update(we,v);
135 }
136}
137
139
140template <class T>
142{
143 if (we == nullptr) { return -1; }
144
145 const int i = we->heapPosition;
146 if (i == -1) { return -1; }
147
148 const int n = CSOObjectVector<T>::num();
149 if (i >= n) { return -1; }
150
151 if (i != n - 1)
152 {
153 swap(i,n - 1);
154 CSOObjectVector<T>::at(n - 1)->heapPosition = -1;
157 {
158 return upHeap(i);
159 }
160 else
161 {
162 return downHeap(i);
163 }
164 }
165 else
166 {
167 CSOObjectVector<T>::at(n - 1)->heapPosition = -1;
169 }
170 return -1;
171}
172
174
175template <class T>
177{
178 const int i = we->heapPosition;
179 if (i == -1) { return; }
180
181 const int n = CSOObjectVector<T>::num();
182 if (i >= n) { return; }
183
184 const float ov = static_cast<float>(we->value);
185
186 we->value = nv;
187
188 if (nv < ov)
189 {
190 upHeap(i);
191 }
192 else
193 {
194 downHeap(i);
195 }
196}
197
199
200template <class T>
202{
203 if (i==0) { return 0; }
204 const int pi = parent(i);
206 {
207 swap(i,pi);
208 return upHeap(pi);
209 }
210 return i;
211}
212
214
215template <class T>
216int CSOObjectHeap<T>::downHeap(int i)
217{
218 int largest=i,l=left(i),r=right(i);
219 const int n = CSOObjectVector<T>::num();
220
221 if (i >= n) { return -1; }
222 if ((l < n) && (CSOObjectVector<T>::at(l)->value < CSOObjectVector<T>::at(largest)->value)) { largest=l; }
223 if ((r < n) && (CSOObjectVector<T>::at(r)->value < CSOObjectVector<T>::at(largest)->value)) { largest=r; }
224
225 if (largest!=i)
226 {
227 swap(i,largest);
228 return downHeap(largest);
229 }
230 return i;
231}
232
234
235template <class T>
237{
238 CSOObjectVector<T> *newheap = nullptr;
240
241 unsigned int i=0;
242 unsigned int n = CSOObjectVector<T>::num();
243 for (i = 0; i < n; ++i)
244 {
246 }
248 n = newheap->CSOObjectVector<T>::num();
249 for ( i = 0; i < n; ++i)
250 {
251 insert(newheap->CSOObjectVector<T>::at(i));
252 }
253 delete newheap;
254 newheap = nullptr;
255}
256
258
@ T
Heap structure with property i>2*i+1 and i>2*i+2 Parent i has children 2*i+1 and 2*i+2 Smallest value...
T * root() const
Get root (first) element of heap, typecast from CSOLiveWireNode to T.
void insert(T *we)
Insert heap element, resort heap.
int remove(T *we) override
Remove heap element, resort heap.
void swap(unsigned int i, unsigned int j) override
Swap two heap elements and resort heap.
void sort()
Sort heap.
~CSOObjectHeap() override
Standard destructor.
void update(T *we, float nv)
Update given heap element with new value, resort heap.
Dynamic templated vector For speed and better memory handling, the vector is an array within an array...
unsigned int num() const
Returns number of elements in the vector.
virtual void swap(unsigned int p1, unsigned int p2)
Swaps two elements in vector.
T * firstBoundsCheck() const
Returns first element, return NULL when out of range.
T * at(unsigned int pos) const
Returns element at given position.
virtual void deleteLast()
Deletes last element of vector.
virtual unsigned int appendUnsafe(T *elem)
Appends element to back of vector, don't check on element being non-NULL Don't use this function dire...
virtual void clear()
Clears all internal pointers This does not delete the elements in the vector!!
T * last() const
Returns last element.
Target mlrange_cast(Source arg)
Generic version of checked ML casts.