MeVisLab Toolbox Reference
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 
19 #include "CSOHeapObject.h"
20 
21 
22 ML_START_NAMESPACE
23 
25 
30 template <class T>
32 {
33 
34 public:
35 
37  CSOObjectVector(unsigned int init = 0,unsigned int bs = 65535);
39  virtual ~CSOObjectVector();
40 
42  inline unsigned int num() const {return length;}
44  inline T* atBoundsCheck(unsigned int pos) const { return (pos<length) ? block[pos] : nullptr; }
46  inline T* at(unsigned int pos) const { return block[pos]; }
48  inline T* firstBoundsCheck() const {return atBoundsCheck(0);}
50  inline T* first() const {return at(0);}
52  inline T* lastBoundsCheck() const {return atBoundsCheck(length-1);}
54  inline T* last() const {return at(length-1);}
56  virtual unsigned int append(T* elem);
58  virtual void swap(unsigned int p1,unsigned int p2);
61  virtual void clear();
63  virtual void deleteAt(unsigned int pos);
65  virtual void deleteLast();
67  virtual int remove(T* elem);
69  virtual int lookup(T* elem) const;
71  virtual int removeUnSwapped(T* elem);
74  virtual void destroy();
76  virtual void replace(T* elem,unsigned int pos);
78  void reserve(unsigned int init);
79 
80 private:
81 
83  unsigned int length;
85  unsigned int capacity;
86  unsigned int BLOCKSIZE;
88  T* *block;
89 
90 protected:
91 
93  virtual void expand();
96  virtual unsigned int appendUnsafe(T* elem);
97 };
98 
101 
102 template <class T>
103 void deleteVector(CSOObjectVector<T> *vector,bool deleteEntries=true)
104 {
105  if (!vector)
106  {
107  return;
108  }
109  if (deleteEntries)
110  {
111  vector->destroy();
112  }
113  delete vector;
114  vector = nullptr;
115 }
116 
119 
120 template <class T>
121 CSOObjectVector<T>::CSOObjectVector(unsigned int init,unsigned int bs)
122 {
123  BLOCKSIZE = bs;
124  length = 0;
125  capacity = init;
126  block = nullptr;
127  if (capacity)
128  {
129  block = new T * [capacity];
130  }
131 }
132 
134 
135 template <class T>
137 {
138  clear();
139 }
140 
142 
143 template <class T>
145 {
146  if (capacity > 0)
147  {
148  delete [] block;
149  block = nullptr;
150  }
151  length = capacity = 0;
152 }
153 
155 
156 template <class T>
158 {
159  for (unsigned int i = 0; i < length; ++i)
160  {
161  delete block[i]; block[i] = nullptr;
162  }
163  length = 0;
164 }
165 
167 
168 template <class T>
170 {
171  T* *newblock = nullptr;
172  newblock = new T*[capacity+BLOCKSIZE];
173 
174  for (unsigned int i = 0; i < length; ++i)
175  {
176  newblock[i] = block[i];
177  }
178  delete [] block;
179  block = nullptr;
180 
181  block = newblock;
182  capacity += BLOCKSIZE;
183 }
184 
186 
187 template <class T>
188 unsigned int CSOObjectVector<T>::append(T* elem)
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 
204 template <class T>
206 {
207  if (length == capacity)
208  {
209  expand();
210  }
211  block[length] = elem;
212  length++;
213  return length-1;
214 }
215 
217 
218 template <class T>
219 void CSOObjectVector<T>::replace(T* elem,unsigned int pos)
220 {
221  block[pos] = elem;
222 }
223 
225 
226 template <class T>
227 void CSOObjectVector<T>::swap(unsigned int p1,unsigned int p2)
228 {
229  T* temp;
230  if (p1 != p2)
231  {
232  T*& pb1 = block[p1]; //use a temp reference to avoid redundant dereferencing
233  T*& pb2 = block[p2];
234  temp = pb1;
235  pb1 = pb2;
236  pb2 = temp;
237  }
238 }
239 
241 
242 template <class T>
243 void CSOObjectVector<T>::deleteAt(unsigned int pos)
244 {
245  if (pos != length-1)
246  {
247  swap(pos,length-1);
248  }
249  deleteLast();
250 }
251 
253 
254 template <class T>
256 {
257  if (length > 0)
258  {
259  length--;
260  block[length] = nullptr;
261  }
262 }
263 
265 
266 template <class T>
268 {
269  int pos = -1;
270  for (unsigned int i = 0; i < length; ++i)
271  {
272  if (at(i) == elem)
273  {
274  pos = static_cast<int>(i);
275  break;
276  }
277  }
278  return pos;
279 }
280 
282 
283 template <class T>
285 {
286  int pos = lookup(elem);
287  if (pos != -1)
288  {
289  deleteAt(static_cast<unsigned int>(pos));
290  }
291  return pos;
292 }
293 
295 
296 template <class T>
298 {
299  const int pos = lookup(elem);
300  if (pos != -1)
301  {
302  for (unsigned int i = static_cast<unsigned int>(pos); i < length-1; ++i)
303  {
304  block[i] = block[i+1];
305  }
306  deleteLast();
307  }
308  return pos;
309 }
310 
312 
313 template <class T>
315 {
316  if (init <= capacity)
317  {
318  return;
319  }
320  T* *newblock = nullptr;
321  newblock = new T*[init];
322  for (unsigned int i = 0; i < length; ++i)
323  {
324  newblock[i] = block[i];
325  }
326  if(capacity > 0)
327  {
328  delete [] block;
329  block = nullptr;
330  }
331  block = newblock;
332  capacity = init;
333 }
334 
336 
337 ML_END_NAMESPACE
@ T
Definition: SoKeyGrabber.h:71
Dynamic templated vector For speed and better memory handling, the vector is an array within an array...
unsigned int num() const
Returns number of elements in the vector.
virtual void destroy()
Deletes all elements in the vector This does not reset the number of elements!!
T * first() const
Returns first element.
T * atBoundsCheck(unsigned int pos) const
Returns element at given position, return NULL when out of range.
T * firstBoundsCheck() const
Returns first element, return NULL when out of range.
T * at(unsigned int pos) const
Returns element at given position.
T * last() const
Returns last element.
T * lastBoundsCheck() const
Returns last element, return NULL when out of range.
IteratorTraits::value_type & at(const ml_iterator_map< IteratorTraits, IDMap > &i, typename property_traits< IDMap >::key_type key)
void init()
Initializes the ML, the runtime type system, the memory manager, fields, static buffers,...
void destroy()
Delete dynamic data structures allocated by init().
void deleteVector(CSOObjectVector< T > *vector, bool deleteEntries=true)