MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
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> class WEMFastVector
26{
27public:
29 WEMFastVector(unsigned int init);
31 virtual ~WEMFastVector();
33 inline unsigned int num() const
34 {
35 return _length;
36 }
38 inline T* at(unsigned int pos) const
39 {
40 return (pos < _length) ? _block[pos] : nullptr;
41 }
43 inline T* first()
44 {
45 return at(0);
46 }
48 inline const T* first() const
49 {
50 return at(0);
51 }
53 inline T* last()
54 {
55 return at(_length - 1);
56 }
58 inline const T* last() const
59 {
60 return at(_length - 1);
61 }
63 virtual unsigned int append(T* elem);
65 virtual void swap(unsigned int p1, unsigned int p2);
68 virtual void clear();
70 virtual void destroy();
72 virtual void deleteAt(unsigned int pos);
74 virtual void deleteLast();
76 virtual int remove(T* elem);
78 virtual int lookup(T* elem) const;
81 virtual int removeUnSwapped(T* elem);
83 virtual void replace(T* elem, unsigned int pos);
84
85private:
87 unsigned int _length;
89 unsigned int _capacity;
91 T** _block;
92};
93
96
97template <class T> WEMFastVector<T>::WEMFastVector(unsigned int init)
98{
99 _length = 0;
100 _capacity = init;
101 _block = new T*[init];
102
103 for (unsigned int i = 0; i < init; i++) {
104 _block[i] = nullptr;
105 }
106}
107
109
111{
112 destroy();
113 delete[] _block;
114 _block = nullptr;
115}
116
118
119template <class T> void WEMFastVector<T>::clear()
120{
121 _length = 0;
122}
123
125
126template <class T> void WEMFastVector<T>::destroy()
127{
128 // _capacity is used here to delete all blocks, also the hidden (deleted) ones
129 for (unsigned int i = 0; i < _capacity; i++) {
130 delete _block[i];
131 _block[i] = nullptr;
132 }
133 _length = 0;
134}
135
137
138template <class T> unsigned int WEMFastVector<T>::append(T* elem)
139{
140 if (elem) {
141 if (_length < _capacity) {
142 _block[_length] = elem;
143 _length++;
144 }
145 }
146 return _length - 1;
147}
148
150
151template <class T> void WEMFastVector<T>::replace(T* elem, unsigned int pos)
152{
153 _block[pos] = elem;
154}
155
157
158template <class T> void WEMFastVector<T>::swap(unsigned int p1, unsigned int p2)
159{
160 T* temp;
161 if (p1 != p2) {
162 T*& pb1 = _block[p1];
163 T*& pb2 = _block[p2];
164 temp = pb1;
165 pb1 = pb2;
166 pb2 = temp;
167 }
168}
169
171
172template <class T> void WEMFastVector<T>::deleteAt(unsigned int pos)
173{
174 if (pos >= _length) {
175 return;
176 }
177
178 if (pos != _length - 1) {
179 swap(pos, _length - 1);
180 }
181 deleteLast();
182}
183
185
186template <class T> void WEMFastVector<T>::deleteLast()
187{
188 if (_length > 0) {
189 _length--;
190 }
191}
192
194
195template <class T> int WEMFastVector<T>::lookup(T* elem) const
196{
197 int pos = -1;
198 for (unsigned int i = 0; i < _length; i++) {
199 if (at(i) == elem) {
200 pos = static_cast<int>(i);
201 break;
202 }
203 }
204 return pos;
205}
206
208
209template <class T> int WEMFastVector<T>::remove(T* elem)
210{
211 int pos = lookup(elem);
212 if (pos != -1) {
213 deleteAt(static_cast<unsigned int>(pos));
214 }
215 return pos;
216}
217
219
221{
222 int pos = lookup(elem);
223 if (pos != -1) {
224 for (unsigned int i = static_cast<unsigned int>(pos); i < _length - 1; i++) {
225 _block[i] = _block[i + 1];
226 }
227 deleteLast();
228 }
229 return pos;
230}
231
233
@ 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.