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 
17 ML_START_NAMESPACE
18 
20 
25 template <class T>
27 {
28 public:
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 
69 private:
70 
72  unsigned int _length;
74  unsigned int _capacity;
76  T* *_block;
77 };
78 
81 
82 template <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 
97 template <class T>
99 {
100  destroy();
101  delete [] _block;
102  _block = nullptr;
103 }
104 
106 
107 template <class T>
109 {
110  _length = 0;
111 }
112 
114 
115 template <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 
128 template <class T>
129 unsigned int WEMFastVector<T>::append(T* elem)
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 
144 template <class T>
145 void WEMFastVector<T>::replace(T* elem, unsigned int pos)
146 {
147  _block[pos] = elem;
148 }
149 
151 
152 template <class T>
153 void 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 
168 template <class T>
169 void 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 
182 template <class T>
184 {
185  if (_length > 0) { _length --; }
186 }
187 
189 
190 template <class T>
191 int WEMFastVector<T>::lookup(T* elem) const
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 
207 template <class T>
209 {
210  int pos = lookup(elem);
211  if (pos != -1)
212  {
213  deleteAt(pos);
214  }
215  return pos;
216 }
217 
219 
220 template <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 
237 ML_END_NAMESPACE
@ T
Definition: SoKeyGrabber.h:71
Dynamic templated vector.
Definition: WEMFastVector.h:27
unsigned int num() const
Returns the number of elements in this vector.
Definition: WEMFastVector.h:35
T * first()
Returns the first element.
Definition: WEMFastVector.h:39
T * last()
Returns the last element.
Definition: WEMFastVector.h:43
const T * first() const
Returns the first element.
Definition: WEMFastVector.h:41
T * at(unsigned int pos) const
Returns the element at the given position, returns NULL if out of range.
Definition: WEMFastVector.h:37
const T * last() const
Returns the last element.
Definition: WEMFastVector.h:45
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().