MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
CSOObjectVector.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#include <mlTypeDefs.h>
19#include "CSOHeapObject.h"
20
21
23
25
30template <class T> class CSOObjectVector
31{
32
33public:
35 CSOObjectVector(unsigned int init = 0, unsigned int bs = 65535);
37 virtual ~CSOObjectVector();
38
40 inline unsigned int num() const
41 {
42 return length;
43 }
45 inline T* atBoundsCheck(unsigned int pos) const
46 {
47 return (pos < length) ? block[pos] : nullptr;
48 }
50 inline T* at(unsigned int pos) const
51 {
52 return block[pos];
53 }
55 inline T* firstBoundsCheck() const
56 {
57 return atBoundsCheck(0);
58 }
60 inline T* first() const
61 {
62 return at(0);
63 }
65 inline T* lastBoundsCheck() const
66 {
67 return atBoundsCheck(length - 1);
68 }
70 inline T* last() const
71 {
72 return at(length - 1);
73 }
75 virtual unsigned int append(T* elem);
77 virtual void swap(unsigned int p1, unsigned int p2);
80 virtual void clear();
82 virtual void deleteAt(unsigned int pos);
84 virtual void deleteLast();
86 virtual int remove(T* elem);
88 virtual int lookup(T* elem) const;
90 virtual int removeUnSwapped(T* elem);
93 virtual void destroy();
95 virtual void replace(T* elem, unsigned int pos);
97 void reserve(unsigned int init);
98
99private:
101 unsigned int length;
103 unsigned int capacity;
104 unsigned int BLOCKSIZE;
106 T** block;
107
108protected:
110 virtual void expand();
113 virtual unsigned int appendUnsafe(T* elem);
114};
115
118
119template <class T> void deleteVector(CSOObjectVector<T>* vector, bool deleteEntries = true)
120{
121 if (!vector) {
122 return;
123 }
124 if (deleteEntries) {
125 vector->destroy();
126 }
127 delete vector;
128 vector = nullptr;
129}
130
133
134template <class T> CSOObjectVector<T>::CSOObjectVector(unsigned int init, unsigned int bs)
135{
136 BLOCKSIZE = bs;
137 length = 0;
138 capacity = init;
139 block = nullptr;
140 if (capacity) {
141 block = new T*[capacity];
142 }
143}
144
146
148{
149 clear();
150}
151
153
154template <class T> void CSOObjectVector<T>::clear()
155{
156 if (capacity > 0) {
157 delete[] block;
158 block = nullptr;
159 }
160 length = capacity = 0;
161}
162
164
165template <class T> void CSOObjectVector<T>::destroy()
166{
167 for (unsigned int i = 0; i < length; ++i) {
168 delete block[i];
169 block[i] = nullptr;
170 }
171 length = 0;
172}
173
175
176template <class T> void CSOObjectVector<T>::expand()
177{
178 T** newblock = nullptr;
179 newblock = new T*[capacity + BLOCKSIZE];
180
181 for (unsigned int i = 0; i < length; ++i) {
182 newblock[i] = block[i];
183 }
184 delete[] block;
185 block = nullptr;
186
187 block = newblock;
188 capacity += BLOCKSIZE;
189}
190
192
193template <class T> unsigned int CSOObjectVector<T>::append(T* elem)
194{
195 if (elem) {
196 if (length == capacity) {
197 expand();
198 }
199 block[length] = elem;
200 length++;
201 }
202 return length - 1;
203}
204
206
207template <class T> unsigned int CSOObjectVector<T>::appendUnsafe(T* elem)
208{
209 if (length == capacity) {
210 expand();
211 }
212 block[length] = elem;
213 length++;
214 return length - 1;
215}
216
218
219template <class T> void CSOObjectVector<T>::replace(T* elem, unsigned int pos)
220{
221 block[pos] = elem;
222}
223
225
226template <class T> void CSOObjectVector<T>::swap(unsigned int p1, unsigned int p2)
227{
228 T* temp;
229 if (p1 != p2) {
230 T*& pb1 = block[p1]; // use a temp reference to avoid redundant dereferencing
231 T*& pb2 = block[p2];
232 temp = pb1;
233 pb1 = pb2;
234 pb2 = temp;
235 }
236}
237
239
240template <class T> void CSOObjectVector<T>::deleteAt(unsigned int pos)
241{
242 if (pos != length - 1) {
243 swap(pos, length - 1);
244 }
245 deleteLast();
246}
247
249
250template <class T> void CSOObjectVector<T>::deleteLast()
251{
252 if (length > 0) {
253 length--;
254 block[length] = nullptr;
255 }
256}
257
259
260template <class T> int CSOObjectVector<T>::lookup(T* elem) const
261{
262 int pos = -1;
263 for (unsigned int i = 0; i < length; ++i) {
264 if (at(i) == elem) {
265 pos = static_cast<int>(i);
266 break;
267 }
268 }
269 return pos;
270}
271
273
274template <class T> int CSOObjectVector<T>::remove(T* elem)
275{
276 int pos = lookup(elem);
277 if (pos != -1) {
278 deleteAt(static_cast<unsigned int>(pos));
279 }
280 return pos;
281}
282
284
286{
287 const int pos = lookup(elem);
288 if (pos != -1) {
289 for (unsigned int i = static_cast<unsigned int>(pos); i < length - 1; ++i) {
290 block[i] = block[i + 1];
291 }
292 deleteLast();
293 }
294 return pos;
295}
296
298
299template <class T> void CSOObjectVector<T>::reserve(unsigned int init)
300{
301 if (init <= capacity) {
302 return;
303 }
304 T** newblock = nullptr;
305 newblock = new T*[init];
306 for (unsigned int i = 0; i < length; ++i) {
307 newblock[i] = block[i];
308 }
309 if (capacity > 0) {
310 delete[] block;
311 block = nullptr;
312 }
313 block = newblock;
314 capacity = init;
315}
316
318
@ T
Dynamic templated vector.
unsigned int num() const
Returns the number of elements in the vector.
T * lastBoundsCheck() const
Returns the last element, and returns nullptr if out of range.
virtual void destroy()
Deletes all elements in the vector.
T * atBoundsCheck(unsigned int pos) const
Returns the element at the specified position, and returns nullptr when out of range.
T * firstBoundsCheck() const
Returns the first element, and return nullptr if out of range.
T * at(unsigned int pos) const
Returns the element at the specified position.
T * last() const
Returns the last element.
T * first() const
Returns the first element.
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
void deleteVector(CSOObjectVector< T > *vector, bool deleteEntries=true)