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