MeVisLab Toolbox Reference
WEMVector.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 "MLWEMIncludes.h"
16
18
20
26template <class T, int fixedBufferSize = 32>
28{
29public:
30
32 WEMVector(unsigned int init = 0);
35
37 virtual ~WEMVector();
39 inline unsigned int num() const {return _length;}
41 inline T* at(unsigned int pos) const { return (pos<_length) ? _block[pos] : nullptr; }
43 inline T* first() {return at(0);}
45 inline const T* first() const {return first();}
47 inline T* last() {return at(_length-1);}
49 inline const T* last() const {return last();}
51 virtual unsigned int append(T* elem);
53 virtual void swap(unsigned int p1,unsigned int p2);
56 virtual void clear();
58 virtual void deleteAt(unsigned int pos);
60 virtual void deleteLast();
62 virtual int remove(T* elem);
64 virtual int lookup(T* elem) const;
66 virtual int removeUnSwapped(T* elem);
69 virtual void destroy();
71 virtual void replace(T* elem,unsigned int pos);
73 void reserve(unsigned int init);
74
75private:
76
78 unsigned int _length;
80 unsigned int _capacity;
82 T* *_block;
83
84 T* _fixedBuffer[fixedBufferSize];
85
86protected:
87
89 virtual void expand();
92 virtual unsigned int appendUnsafe(T* elem);
93};
94
96
97template <class T, int fixedBufferSize>
99{
100 _length = 0;
101 _capacity = fixedBufferSize;
102 _block = _fixedBuffer;
103
104 if (init > _capacity) {
105 _capacity = init;
106 _block = new T*[_capacity];
107 }
108}
109
110template <class T, int fixedBufferSize>
112{
113 *this = std::move(other);
114}
115
116template <class T, int fixedBufferSize>
118{
119 _length = other._length;
120 other._length = 0;
121 _capacity = other._capacity;
122 other._capacity = fixedBufferSize;
123
124 if (other._block == other._fixedBuffer)
125 {
126 _block = _fixedBuffer;
127 for (auto i = 0u; i < _length; ++i)
128 {
129 _fixedBuffer[i] = other._fixedBuffer[i];
130 }
131 }
132 else
133 {
134 _block = other._block;
135 }
136 other._block = other._fixedBuffer;
137
138 return *this;
139}
140
142
143template <class T, int fixedBufferSize>
148
150
151template <class T, int fixedBufferSize>
153{
154 if (_block != _fixedBuffer)
155 {
156 delete [] _block;
157 _block = nullptr;
158 }
159 _length = 0;
160
161 _capacity = fixedBufferSize;
162 _block = _fixedBuffer;
163}
164
166
167template <class T, int fixedBufferSize>
169{
170 for (unsigned int i = 0; i < _length; i++)
171 {
172 delete _block[i]; _block[i] = nullptr;
173 }
174 _length = 0;
175}
176
178
179template <class T, int fixedBufferSize>
181{
182 reserve(_capacity * 2);
183}
184
186
187template <class T, int fixedBufferSize>
189{
190 if (elem)
191 {
192 if (_length == _capacity)
193 {
194 expand();
195 }
196 _block[_length] = elem;
197 _length++;
198 }
199 return _length-1;
200}
201
203
204template <class T, int fixedBufferSize>
206{
207 if (_length == _capacity)
208 {
209 expand();
210 }
211 _block[_length] = elem;
212 _length++;
213 return _length-1;
214}
215
217
218template <class T, int fixedBufferSize>
220{
221 _block[pos] = elem;
222}
223
225
226template <class T, int fixedBufferSize>
227void WEMVector<T, fixedBufferSize>::swap(unsigned int p1,unsigned int p2)
228{
229 T* temp = nullptr;
230 if (p1 != p2)
231 {
232 T*& pb1 = _block[p1];
233 T*& pb2 = _block[p2];
234 temp = pb1;
235 pb1 = pb2;
236 pb2 = temp;
237 }
238}
239
241
242template <class T, int fixedBufferSize>
244{
245 if (pos != _length-1)
246 {
247 swap(pos,_length-1);
248 }
249 deleteLast();
250}
251
253
254template <class T, int fixedBufferSize>
256{
257 if (_length > 0)
258 {
259 _length--;
260 }
261}
262
264
265template <class T, int fixedBufferSize>
267{
268 int pos = -1;
269 for (unsigned int i = 0; i < _length; i++)
270 {
271 if (_block[i] == elem)
272 {
273 pos = i;
274 break;
275 }
276 }
277 return pos;
278}
279
281
282template <class T, int fixedBufferSize>
284{
285 const int pos = lookup(elem);
286 if (pos != -1)
287 {
288 deleteAt(pos);
289 }
290 return pos;
291}
292
294
295template <class T, int fixedBufferSize>
297{
298 const int pos = lookup(elem);
299 if (pos != -1)
300 {
301 for (unsigned int i = pos; i < _length-1; i++)
302 {
303 _block[i] = _block[i+1];
304 }
305 deleteLast();
306 }
307 return pos;
308}
309
311
312template <class T, int fixedBufferSize>
314{
315 if (init <= _capacity) { return; }
316
317 T* *newblock = nullptr;
318 newblock = new T*[init];
319 for (unsigned int i = 0; i < _length; i++)
320 {
321 newblock[i] = _block[i];
322 }
323 if (_block != _fixedBuffer)
324 {
325 delete [] _block;
326 _block = nullptr;
327 }
328 _block = newblock;
329 _capacity = init;
330}
331
333
@ T
Dynamic templated vector.
Definition WEMVector.h:28
virtual void clear()
Clears all internal pointers.
Definition WEMVector.h:152
T * last()
Returns the last element.
Definition WEMVector.h:47
WEMVector(unsigned int init=0)
Standard constructor.
Definition WEMVector.h:98
WEMVector(WEMVector &&other) noexcept
Definition WEMVector.h:111
virtual void replace(T *elem, unsigned int pos)
Replaces the given position with the given element.
Definition WEMVector.h:219
const T * first() const
Returns the first element.
Definition WEMVector.h:45
WEMVector & operator=(WEMVector &&other) noexcept
Definition WEMVector.h:117
virtual void destroy()
Deletes all elements in this vector.
Definition WEMVector.h:168
virtual unsigned int append(T *elem)
Appends the given element to back of this vector.
Definition WEMVector.h:188
virtual void expand()
Grow vector, add extra block of size BLOCKSIZE.
Definition WEMVector.h:180
T * at(unsigned int pos) const
Returns the element at the given position or returns NULL if out of range.
Definition WEMVector.h:41
unsigned int num() const
Returns the number of elements in this vector.
Definition WEMVector.h:39
virtual int removeUnSwapped(T *elem)
Deletes the element given by its pointer. Keeps the order of the elements!
Definition WEMVector.h:296
virtual int lookup(T *elem) const
Searches for the given element in this vector and returns its position.
Definition WEMVector.h:266
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 void deleteAt(unsigned int pos)
Deletes the element at the given position.
Definition WEMVector.h:243
const T * last() const
Returns the last element.
Definition WEMVector.h:49
virtual int remove(T *elem)
Deletes the element given by its pointer.
Definition WEMVector.h:283
virtual unsigned int appendUnsafe(T *elem)
Append element to back of vector, don't check on element being non-NULL.
Definition WEMVector.h:205
virtual void deleteLast()
Deletes the last element of this vector.
Definition WEMVector.h:255
virtual ~WEMVector()
Standard destructor.
Definition WEMVector.h:144
T * first()
Returns the first element.
Definition WEMVector.h:43
void reserve(unsigned int init)
Reserves init elements, copies old ones if existing.
Definition WEMVector.h:313
Target mlrange_cast(Source arg)
Generic version of checked ML casts.