Vector.h
Go to the documentation of this file.
1 #ifndef MiscLib__VECTOR_HEADER__
2 #define MiscLib__VECTOR_HEADER__
4 #include <algorithm>
5 #include <iterator>
6 #include <algorithm>
7 #ifdef min
8 #undef min
9 #endif
10 #ifdef max
11 #undef max
12 #endif
13 
14 namespace MiscLib
15 {
16  // This is a special implementation of std::vector. You should be able to use it whereever you could you use std::vector.
17  // For some reason it is faster than the actual std::vector (at least under windows and with the Intel Compiler)
18  template< class T, class AllocatorT = MiscLib::AlignedAllocator< T > >
19  class Vector
20  : protected AllocatorT
21  {
22  public:
23  typedef size_t size_type;
24  typedef T value_type;
25  typedef T* iterator;
26  typedef const T* const_iterator;
27  typedef T& reference;
28  typedef const T& const_reference;
29  typedef T* pointer;
30  typedef const T* const_pointer;
31  typedef size_t ptrdiff_t;
32  typedef std::reverse_iterator< T* > reverse_iterator;
33  typedef std::reverse_iterator< const T* > const_reverse_iterator;
34 
36  {
37  m_begin = NULL;
38  m_end = NULL;
39  m_capacity = NULL;
40  }
41 
43  {
44  m_begin = AllocatorT::allocate(s);
45  m_end = m_begin + s;
46  m_capacity = m_end;
48  for (size_type i = 0; i < s; ++i)
49  {
50  AllocatorT::construct(m_begin + i, v);
51  }
52  }
53 
54  Vector(size_type s, const T& v)
55  {
56  m_begin = AllocatorT::allocate(s);
57  m_end = m_begin + s;
58  m_capacity = m_end;
59  for (size_type i = 0; i < s; ++i)
60  {
61  AllocatorT::construct(m_begin + i, v);
62  }
63  }
64 
66  AllocatorT(v)
67  {
68  size_type s = v.size();
69  if (!s)
70  {
71  m_begin = NULL;
72  m_end = NULL;
73  m_capacity = NULL;
74  return;
75  }
76  m_begin = AllocatorT::allocate(s);
77  m_end = m_begin + s;
78  m_capacity = m_end;
79  for (size_type i = 0; i < s; ++i)
80  {
81  AllocatorT::construct(m_begin + i, v.m_begin[i]);
82  }
83  }
84 
85  template< class OtherAllocatorT >
87  {
88  size_type s = v.size();
89  if (!s)
90  {
91  m_begin = NULL;
92  m_end = NULL;
93  m_capacity = NULL;
94  return;
95  }
96  m_begin = AllocatorT::allocate(s);
97  m_end = m_begin + s;
98  m_capacity = m_end;
99  for (size_type i = 0; i < s; ++i)
100  {
101  AllocatorT::construct(m_begin + i, v.m_begin[i]);
102  }
103  }
104 
106  {
107  if (m_begin)
108  {
109  for (size_type i = 0; i < size(); ++i)
110  {
111  AllocatorT::destroy(m_begin + i);
112  }
113  AllocatorT::deallocate(m_begin, capacity());
114  }
115  }
116 
118  {
119  if (&v == this)
120  {
121  return *this;
122  }
123  size_type s = v.size();
124  if (!s)
125  {
126  clear();
127  return *this;
128  }
129  if (m_begin)
130  {
131  for (size_type i = 0; i < size(); ++i)
132  {
133  AllocatorT::destroy(m_begin + i);
134  }
135  AllocatorT::deallocate(m_begin, capacity());
136  }
137  m_begin = AllocatorT::allocate(s);
138  m_end = m_begin + s;
139  m_capacity = m_end;
140  for (size_type i = 0; i < s; ++i)
141  {
142  AllocatorT::construct(m_begin + i, v.m_begin[i]);
143  }
144  return *this;
145  }
146 
147  template< class OtherAllocatorT >
149  {
150  size_type s = v.size();
151  if (!s)
152  {
153  clear();
154  return *this;
155  }
156  if (m_begin)
157  {
158  for (size_type i = 0; i < size(); ++i)
159  {
160  AllocatorT::destroy(m_begin + i);
161  }
162  AllocatorT::deallocate(m_begin, capacity());
163  }
164  m_begin = AllocatorT::allocate(s);
165  m_end = m_begin + s;
166  m_capacity = m_end;
167  for (size_type i = 0; i < s; ++i)
168  {
169  AllocatorT::construct(m_begin + i, v.m_begin[i]);
170  }
171  return *this;
172  }
173 
174  void clear()
175  {
176  if (m_begin)
177  {
178  for (size_type i = 0; i < size(); ++i)
179  {
180  AllocatorT::destroy(m_begin + i);
181  }
182  AllocatorT::deallocate(m_begin, capacity());
183  }
184  m_end = m_begin = m_capacity = NULL;
185  }
186 
188  {
189  if (!s)
190  {
191  return;
192  }
193  if ((size_type)(m_capacity - m_begin) < s)
194  {
195  size_type olds = size();
196  T* newBegin = AllocatorT::allocate(s);
197  if (m_begin)
198  {
199  for (size_type i = 0; i < olds; ++i)
200  {
201  AllocatorT::construct(newBegin + i, m_begin[i]);
202  AllocatorT::destroy(m_begin + i);
203  }
204  AllocatorT::deallocate(m_begin, capacity());
205  }
206  m_end = newBegin + olds;
207  m_begin = newBegin;
208  m_capacity = m_begin + s;
209  }
210  }
211 
212  size_type size() const
213  {
214  return m_end - m_begin;
215  }
216 
218  {
219  return m_capacity - m_begin;
220  }
221 
222  void resize(size_type s, const value_type& v)
223  {
224  if (!s)
225  {
226  clear();
227  return;
228  }
229  if ((size_type)(m_capacity - m_begin) >= s)
230  {
231  if (2 * s <= capacity())
232  {
233  T* newBegin = AllocatorT::allocate(s);
234  size_type copyRange = std::min(s, size());
235  for (size_type i = 0; i < copyRange; ++i)
236  {
237  AllocatorT::construct(newBegin + i, m_begin[i]);
238  AllocatorT::destroy(m_begin + i);
239  }
240  for (size_type i = s; i < size(); ++i)
241  {
242  AllocatorT::destroy(m_begin + i);
243  }
244  for (size_type i = size(); i < s; ++i)
245  {
246  AllocatorT::construct(newBegin + i, v);
247  }
248  AllocatorT::deallocate(m_begin, capacity());
249  m_end = newBegin + s;
250  m_begin = newBegin;
251  m_capacity = m_begin + s;
252  return;
253  }
254  for (size_type i = size(); i < s; ++i)
255  {
256  AllocatorT::construct(m_begin + i, v);
257  }
258  for (size_type i = s; i < size(); ++i)
259  {
260  AllocatorT::destroy(m_begin + i);
261  }
262  m_end = m_begin + s;
263  return;
264  }
265  size_type newCapacity = std::max(s,
266  capacity() + capacity() / 2);
267  T* newBegin = AllocatorT::allocate(newCapacity);
268  if (m_begin)
269  {
270  for (size_type i = 0; i < size(); ++i)
271  {
272  AllocatorT::construct(newBegin + i, m_begin[i]);
273  AllocatorT::destroy(m_begin + i);
274  }
275  AllocatorT::deallocate(m_begin, capacity());
276  for (size_type i = size(); i < s; ++i)
277  {
278  AllocatorT::construct(newBegin + i, v);
279  }
280  }
281  else
282  {
283  for (size_type i = 0; i < s; ++i)
284  {
285  AllocatorT::construct(newBegin + i, v);
286  }
287  }
288  m_end = newBegin + s;
289  m_begin = newBegin;
290  m_capacity = m_begin + newCapacity;
291  }
292 
294  {
295  resize(s, value_type());
296  }
297 
298  operator T* ()
299  {
300  return m_begin;
301  }
302 
303  operator const T* () const
304  {
305  return m_begin;
306  }
307 
309  {
310  return m_begin[i];
311  }
312 
313  const T& at(size_type i) const
314  {
315  return m_begin[i];
316  }
317 
318  //T &operator[](size_type i)
319  //{
320  // if(i >= size())
321  // throw int(1);
322  // return m_begin[i];
323  //}
324 
325  //const T &operator[](size_type i) const
326  //{
327  // if(i >= size())
328  // throw int(1);
329  // return m_begin[i];
330  //}
331 
332  //T &at(size_type i)
333  //{
334  // if(i >= size())
335  // throw int(1);
336  // return m_begin[i];
337  //}
338 
339  //const T &at(size_type i) const
340  //{
341  // if(i >= size())
342  // throw int(1);
343  // return m_begin[i];
344  //}
345 
346  void push_back(const T& v)
347  {
348  if (m_end >= m_capacity)
349  {
350  size_type olds = size();
351  size_type s = olds * 2;
352  if (!s)
353  {
354  s = 1;
355  }
356  T* newBegin = AllocatorT::allocate(s);
357  if (m_begin)
358  {
359  for (size_type i = 0; i < olds; ++i)
360  {
361  AllocatorT::construct(newBegin + i, m_begin[i]);
362  AllocatorT::destroy(m_begin + i);
363  }
364  AllocatorT::deallocate(m_begin, capacity());
365  }
366  m_end = newBegin + olds;
367  m_begin = newBegin;
368  m_capacity = m_begin + s;
369  }
370  AllocatorT::construct(m_end, v);
371  ++m_end;
372  }
373 
374  void insert(T* where, const T& v)
375  {
376  size_type whereIdx = where - m_begin;
377  if (m_end >= m_capacity)
378  {
379  size_type olds = size();
380  size_type s = olds * 2;
381  if (!s)
382  {
383  s = 1;
384  }
385  T* newBegin = AllocatorT::allocate(s);
386  if (m_begin)
387  {
388  for (size_type i = 0; i < olds; ++i)
389  {
390  AllocatorT::construct(newBegin + i, m_begin[i]);
391  AllocatorT::destroy(m_begin + i);
392  }
393  AllocatorT::deallocate(m_begin, capacity());
394  }
395  m_end = newBegin + olds;
396  m_begin = newBegin;
397  m_capacity = m_begin + s;
398  where = m_begin + whereIdx;
399  }
400  if (size() > whereIdx)
401  {
402  AllocatorT::construct(m_end, m_begin[size() - 1]);
403  for (size_type i = size() - 1; i > whereIdx; --i)
404  {
405  m_begin[i] = m_begin[i - 1];
406  }
407  *where = v;
408  }
409  else
410  {
411  AllocatorT::construct(where, v);
412  }
413  ++m_end;
414  }
415 
416  void erase(T* where)
417  {
418  for (size_type i = where - m_begin; i < size() - 1; ++i)
419  {
420  m_begin[i] = m_begin[i + 1];
421  }
422  --m_end;
423  AllocatorT::destroy(m_end);
424  size_type s = size();
425  if (s && 2 * s <= capacity())
426  {
427  T* newBegin = AllocatorT::allocate(size());
428  for (size_type i = 0; i < s; ++i)
429  {
430  AllocatorT::construct(newBegin + i, m_begin[i]);
431  AllocatorT::destroy(m_begin + i);
432  }
433  AllocatorT::deallocate(m_begin, capacity());
434  m_end = newBegin + s;
435  m_begin = newBegin;
436  m_capacity = m_begin + s;
437  }
438  }
439 
440  void pop_back()
441  {
442  --m_end;
443  AllocatorT::destroy(m_end);
444  size_type s = size();
445  if (s && 2 * s <= capacity())
446  {
447  T* newBegin = AllocatorT::allocate(size());
448  for (size_type i = 0; i < s; ++i)
449  {
450  AllocatorT::construct(newBegin + i, m_begin[i]);
451  AllocatorT::destroy(m_begin + i);
452  }
453  AllocatorT::deallocate(m_begin, capacity());
454  m_end = newBegin + s;
455  m_begin = newBegin;
456  m_capacity = m_begin + s;
457  }
458  }
459 
460  T* begin()
461  {
462  return m_begin;
463  }
464 
465  const T* begin() const
466  {
467  return m_begin;
468  }
469 
470  T* end()
471  {
472  return m_end;
473  }
474 
475  const T* end() const
476  {
477  return m_end;
478  }
479 
481  {
482  return std::reverse_iterator< T* >(m_end);
483  }
484 
486  {
487  return std::reverse_iterator< const T* >(m_end);
488  }
489 
491  {
492  return std::reverse_iterator< T* >(m_begin);
493  }
494 
496  {
497  return std::reverse_iterator< const T* >(m_begin);
498  }
499 
500  T& back()
501  {
502  return *(m_end - 1);
503  }
504 
505  const T& back() const
506  {
507  return *(m_end - 1);
508  }
509 
510  T& front()
511  {
512  return *m_begin;
513  }
514 
515  const T& front() const
516  {
517  return *m_begin;
518  }
519 
520  private:
521  T* m_begin;
522  T* m_end;
523  T* m_capacity;
524  };
525 };
526 
527 #endif
MiscLib::Vector::front
T & front()
Definition: Vector.h:510
MiscLib::Vector::end
const T * end() const
Definition: Vector.h:475
MiscLib::Vector::begin
T * begin()
Definition: Vector.h:460
MiscLib::Vector::push_back
void push_back(const T &v)
Definition: Vector.h:346
MiscLib::Vector::reference
T & reference
Definition: Vector.h:27
MiscLib::Vector::resize
void resize(size_type s, const value_type &v)
Definition: Vector.h:222
MiscLib::Vector::~Vector
~Vector()
Definition: Vector.h:105
MiscLib::Vector::const_reverse_iterator
std::reverse_iterator< const T * > const_reverse_iterator
Definition: Vector.h:33
MiscLib::Vector::erase
void erase(T *where)
Definition: Vector.h:416
MiscLib::Vector::iterator
T * iterator
Definition: Vector.h:25
MiscLib::Vector::ptrdiff_t
size_t ptrdiff_t
Definition: Vector.h:31
MiscLib::Vector
Definition: Vector.h:19
MiscLib::Vector::size_type
size_t size_type
Definition: Vector.h:23
MiscLib::Vector::Vector
Vector(size_type s, const T &v)
Definition: Vector.h:54
MiscLib::Vector::const_iterator
const typedef T * const_iterator
Definition: Vector.h:26
MiscLib::Vector::back
T & back()
Definition: Vector.h:500
MiscLib::Vector::size
size_type size() const
Definition: Vector.h:212
MiscLib::Vector::pop_back
void pop_back()
Definition: Vector.h:440
MiscLib::Vector::reverse_iterator
std::reverse_iterator< T * > reverse_iterator
Definition: Vector.h:32
Point
Definition: PointCloud.h:21
MiscLib
Definition: AlignedAllocator.h:11
MiscLib::Vector::const_pointer
const typedef T * const_pointer
Definition: Vector.h:30
MiscLib::Vector::Vector
Vector(const Vector< T, OtherAllocatorT > &v)
Definition: Vector.h:86
MiscLib::Vector::end
T * end()
Definition: Vector.h:470
max
T max(T t1, T t2)
Definition: gdiam.h:48
MiscLib::Vector::rbegin
const_reverse_iterator rbegin() const
Definition: Vector.h:485
MiscLib::Vector::operator=
Vector< T, AllocatorT > & operator=(const Vector< T, OtherAllocatorT > &v)
Definition: Vector.h:148
MiscLib::Vector::operator=
Vector< T, AllocatorT > & operator=(const Vector< T, AllocatorT > &v)
Definition: Vector.h:117
MiscLib::Vector::at
T & at(size_type i)
Definition: Vector.h:308
MiscLib::Vector::value_type
T value_type
Definition: Vector.h:24
MiscLib::Vector::reserve
void reserve(size_type s)
Definition: Vector.h:187
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
MiscLib::Vector::at
const T & at(size_type i) const
Definition: Vector.h:313
MiscLib::Vector::rbegin
reverse_iterator rbegin()
Definition: Vector.h:480
MiscLib::Vector::rend
reverse_iterator rend()
Definition: Vector.h:490
MiscLib::Vector::front
const T & front() const
Definition: Vector.h:515
MiscLib::Vector::Vector
Vector()
Definition: Vector.h:35
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
min
T min(T t1, T t2)
Definition: gdiam.h:42
MiscLib::AlignedAllocator< Point >::size_type
size_t size_type
Definition: AlignedAllocator.h:20
MiscLib::Vector::const_reference
const typedef T & const_reference
Definition: Vector.h:28
MiscLib::Vector::Vector
Vector(size_type s)
Definition: Vector.h:42
MiscLib::Vector::rend
const_reverse_iterator rend() const
Definition: Vector.h:495
MiscLib::Vector::Vector
Vector(const Vector< T, AllocatorT > &v)
Definition: Vector.h:65
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
MiscLib::Vector::insert
void insert(T *where, const T &v)
Definition: Vector.h:374
AlignedAllocator.h
MiscLib::Vector::capacity
size_type capacity() const
Definition: Vector.h:217
MiscLib::Vector::clear
void clear()
Definition: Vector.h:174
MiscLib::Vector::pointer
T * pointer
Definition: Vector.h:29
MiscLib::Vector::resize
void resize(size_type s)
Definition: Vector.h:293
MiscLib::Vector::back
const T & back() const
Definition: Vector.h:505
MiscLib::Vector::begin
const T * begin() const
Definition: Vector.h:465