MeVisLab Toolbox Reference
WEMFastVector.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
25template <class T>
27{
28public:
29
31 WEMFastVector(unsigned int init);
33 virtual ~WEMFastVector();
35 inline unsigned int num() const {return _length;}
37 inline T* at(unsigned int pos) const { return (pos < _length) ? _block[pos] : nullptr; }
39 inline T* first() {return at(0);}
41 inline const T* first() const {return at(0);}
43 inline T* last() {return at(_length - 1);}
45 inline const T* last() const {return at(_length - 1);}
47 virtual unsigned int append(T* elem);
49 virtual void swap(unsigned int p1, unsigned int p2);
52 virtual void clear();
54 virtual void destroy();
56 virtual void deleteAt(unsigned int pos);
58 virtual void deleteLast();
60 virtual int remove(T* elem);
62 virtual int lookup(T* elem) const;
65 virtual int removeUnSwapped(T* elem);
67 virtual void replace(T* elem, unsigned int pos);
68
69private:
70
72 unsigned int _length;
74 unsigned int _capacity;
76 T* *_block;
77};
78
81
82template <class T>
84{
85 _length = 0;
86 _capacity = init;
87 _block = new T*[init];
88
89 for (unsigned int i = 0; i < init; i ++)
90 {
91 _block[i] = nullptr;
92 }
93}
94
96
97template <class T>
99{
100 destroy();
101 delete [] _block;
102 _block = nullptr;
103}
104
106
107template <class T>
109{
110 _length = 0;
111}
112
114
115template <class T>
117{
118 // _capacity is used here to delete all blocks, also the hidden (deleted) ones
119 for (unsigned int i = 0; i < _capacity; i ++)
120 {
121 delete _block[i]; _block[i] = nullptr;
122 }
123 _length = 0;
124}
125
127
128template <class T>
130{
131 if (elem)
132 {
133 if (_length < _capacity)
134 {
135 _block[_length] = elem;
136 _length ++;
137 }
138 }
139 return _length - 1;
140}
141
143
144template <class T>
145void WEMFastVector<T>::replace(T* elem, unsigned int pos)
146{
147 _block[pos] = elem;
148}
149
151
152template <class T>
153void WEMFastVector<T>::swap(unsigned int p1, unsigned int p2)
154{
155 T* temp;
156 if (p1 != p2)
157 {
158 T*& pb1 = _block[p1];
159 T*& pb2 = _block[p2];
160 temp = pb1;
161 pb1 = pb2;
162 pb2 = temp;
163 }
164}
165
167
168template <class T>
169void WEMFastVector<T>::deleteAt(unsigned int pos)
170{
171 if (pos >= _length) { return; }
172
173 if (pos != _length - 1)
174 {
175 swap(pos, _length - 1);
176 }
177 deleteLast();
178}
179
181
182template <class T>
184{
185 if (_length > 0) { _length --; }
186}
187
189
190template <class T>
192{
193 int pos = -1;
194 for (unsigned int i = 0; i < _length; i ++)
195 {
196 if (at(i) == elem)
197 {
198 pos = i;
199 break;
200 }
201 }
202 return pos;
203}
204
206
207template <class T>
209{
210 int pos = lookup(elem);
211 if (pos != -1)
212 {
213 deleteAt(pos);
214 }
215 return pos;
216}
217
219
220template <class T>
222{
223 int pos = lookup(elem);
224 if (pos != -1)
225 {
226 for (unsigned int i = pos; i < _length - 1; i ++)
227 {
228 _block[i] = _block[i + 1];
229 }
230 deleteLast();
231 }
232 return pos;
233}
234
236
@ T
Dynamic templated vector.
unsigned int num() const
Returns the number of elements in this vector.
T * at(unsigned int pos) const
Returns the element at the given position, returns NULL if out of range.
T * last()
Returns the last element.
T * first()
Returns the first element.
const T * first() const
Returns the first element.
const T * last() const
Returns the last element.
Target mlrange_cast(Source arg)
Generic version of checked ML casts.