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
22ML_START_NAMESPACE
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);
52 int remove(T *we) override;
54 void sort();
55
56private:
57
59 inline int parent(int i) const {return (i-1)/2;}
61 inline int left(int i) const {return 2*i+1;}
63 inline int right(int i) const {return 2*i+2;}
65 int upHeap(int i);
67 int downHeap(int i);
68};
69
72
73template <class T>
77
79
80template <class T>
85
87
88template <class T>
90{
91 return static_cast<T*>(CSOObjectVector<T>::firstBoundsCheck());
92}
93
95
96template <class T>
97void CSOObjectHeap<T>::swap(unsigned int i,unsigned int j)
98{
100 CSOObjectVector<T>::at(i)->heapPosition = i;
101 CSOObjectVector<T>::at(j)->heapPosition = j;
102}
103
105
106template <class T>
108{
109 if (we==nullptr) { return; }
110 if (we->heapPosition != -1) { return; }
111
113 // No range cast, explicitly allow heapPosition to be < 0 if num() is 0.
114 const int n = static_cast<int>(CSOObjectVector<T>::num())-1;
115 CSOObjectVector<T>::last()->heapPosition = n;
116 upHeap(n);
117}
118
120
121template <class T>
123{
124 if (we==nullptr) { return; }
125
126 if (we->heapPosition == -1)
127 {
128 we->value = v;
131 upHeap(CSOObjectVector<T>::num()-1);
132 }
133 else
134 {
135 update(we,v);
136 }
137}
138
140
141template <class T>
143{
144 if (we == nullptr) { return -1; }
145
146 const int i = we->heapPosition;
147 if (i == -1) { return -1; }
148
149 const int n = CSOObjectVector<T>::num();
150 if (i >= n) { return -1; }
151
152 if (i != n - 1)
153 {
154 swap(i,n - 1);
155 CSOObjectVector<T>::at(n - 1)->heapPosition = -1;
157 if (CSOObjectVector<T>::at(i)->value < we->value)
158 {
159 return upHeap(i);
160 }
161 else
162 {
163 return downHeap(i);
164 }
165 }
166 else
167 {
168 CSOObjectVector<T>::at(n - 1)->heapPosition = -1;
170 }
171 return -1;
172}
173
175
176template <class T>
177void CSOObjectHeap<T>::update(T *we,float nv)
178{
179 const int i = we->heapPosition;
180 if (i == -1) { return; }
181
182 const int n = CSOObjectVector<T>::num();
183 if (i >= n) { return; }
184
185 const float ov = static_cast<float>(we->value);
186
187 we->value = nv;
188
189 if (nv < ov)
190 {
191 upHeap(i);
192 }
193 else
194 {
195 downHeap(i);
196 }
197}
198
200
201template <class T>
203{
204 if (i==0) { return 0; }
205 const int pi = parent(i);
206 if (CSOObjectVector<T>::at(i)->value < CSOObjectVector<T>::at(pi)->value)
207 {
208 swap(i,pi);
209 return upHeap(pi);
210 }
211 return i;
212}
213
215
216template <class T>
217int CSOObjectHeap<T>::downHeap(int i)
218{
219 int largest=i,l=left(i),r=right(i);
220 const int n = CSOObjectVector<T>::num();
221
222 if (i >= n) { return -1; }
223 if ((l < n) && (CSOObjectVector<T>::at(l)->value < CSOObjectVector<T>::at(largest)->value)) { largest=l; }
224 if ((r < n) && (CSOObjectVector<T>::at(r)->value < CSOObjectVector<T>::at(largest)->value)) { largest=r; }
225
226 if (largest!=i)
227 {
228 swap(i,largest);
229 return downHeap(largest);
230 }
231 return i;
232}
233
235
236template <class T>
238{
239 CSOObjectVector<T> *newheap = nullptr;
240 newheap = new CSOObjectVector<T>();
241
242 unsigned int i=0;
243 unsigned int n = CSOObjectVector<T>::num();
244 for (i = 0; i < n; ++i)
245 {
246 newheap->append(CSOObjectVector<T>::at(i));
247 }
249 n = newheap->CSOObjectVector<T>::num();
250 for ( i = 0; i < n; ++i)
251 {
252 insert(newheap->CSOObjectVector<T>::at(i));
253 }
254 delete newheap;
255 newheap = nullptr;
256}
257
259
260ML_END_NAMESPACE
@ 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.
virtual unsigned int append(T *elem)
Appends the element to the back of the vector and returns its position.
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.