FlatCopyVector.h
Go to the documentation of this file.
1 #ifndef GfxTL__FLATCOPYVECTOR_HEADER__
2 #define GfxTL__FLATCOPYVECTOR_HEADER__
3 #include <iterator>
4 
5 #include <malloc.h>
6 #include <memory.h>
7 
8 namespace GfxTL
9 {
10  template <class T>
12  {
13  public:
14  typedef size_t size_type;
15  typedef T value_type;
16  typedef T* iterator;
17  typedef const T* const_iterator;
18  typedef T& reference;
19  typedef const T& const_reference;
20  typedef T* pointer;
21  typedef const T* const_pointer;
22  typedef size_t difference_type;
23  typedef std::reverse_iterator<T*> reverse_iterator;
24  typedef std::reverse_iterator<const T*> const_reverse_iterator;
25 
27  {
28  m_begin = NULL;
29  m_end = NULL;
30  m_capacity = NULL;
31  }
32 
33  FlatCopyVector(size_t s)
34  {
35  m_begin = (T*)_mm_malloc(s * sizeof(T), 16); //new T[s];
36  m_end = m_begin + s;
37  m_capacity = m_end;
38  }
39 
41  {
42  size_t s = v.size();
43  if (!s)
44  {
45  m_begin = NULL;
46  m_end = NULL;
47  m_capacity = NULL;
48  return;
49  }
50  m_begin = (T*)_mm_malloc(s * sizeof(T), 16); //new T[s];
51  m_end = m_begin + s;
52  m_capacity = m_end;
53  memcpy(m_begin, v.m_begin, s * sizeof(T));
54  }
55 
57  {
58  if (m_begin)
59  {
60  _mm_free(m_begin); //delete[] m_begin;
61  }
62  }
63 
66  {
67  if (&v == this)
68  {
69  return *this;
70  }
71  size_t s = v.size();
72  if (!s)
73  {
74  clear();
75  return *this;
76  }
77  if (m_begin)
78  {
79  _mm_free(m_begin); //delete[] m_begin;
80  }
81  m_begin = (T*)_mm_malloc(s * sizeof(T), 16); //new T[s];
82  m_end = m_begin + s;
83  m_capacity = m_end;
84  memcpy(m_begin, v.m_begin, s * sizeof(T));
85  return *this;
86  }
87 
88  void
90  {
91  m_end = m_begin;
92  }
93 
94  void
95  reserve(size_t s)
96  {
97  if (!s)
98  {
99  return;
100  }
101  if ((size_t)(m_capacity - m_begin) < s)
102  {
103  size_t olds = size();
104  T* newBegin = (T*)_mm_malloc(s * sizeof(T), 16); //new T[s];
105  if (m_begin)
106  {
107  memcpy(newBegin, m_begin, olds * sizeof(T));
108  _mm_free(m_begin); //delete[] m_begin;
109  }
110  m_end = newBegin + olds;
111  m_begin = newBegin;
112  m_capacity = m_begin + s;
113  }
114  }
115 
116  size_t
117  size() const
118  {
119  return m_end - m_begin;
120  }
121 
122  size_t
123  capacity() const
124  {
125  return m_capacity - m_begin;
126  }
127 
128  void
129  resize(size_t s)
130  {
131  if (!s)
132  {
133  clear();
134  return;
135  }
136  if ((size_t)(m_capacity - m_begin) >= s)
137  {
138  m_end = m_begin + s;
139  return;
140  }
141  T* newBegin = (T*)_mm_malloc(s * sizeof(T), 16); //new T[s];
142  if (m_begin)
143  {
144  memcpy(newBegin, m_begin, size() * sizeof(T));
145  _mm_free(m_begin); //delete[] m_begin;
146  }
147  m_end = newBegin + s;
148  m_begin = newBegin;
149  m_capacity = m_end;
150  }
151 
152  void
153  resize(size_t s, const value_type& v)
154  {
155  size_t oldsize = size();
156  resize(s);
157  if (s > oldsize)
158  {
159  for (size_t i = oldsize; i < s; ++i)
160  {
161  m_begin[i] = v;
162  }
163  }
164  }
165 
166  operator T*()
167  {
168  return m_begin;
169  }
170 
171  operator const T*() const
172  {
173  return m_begin;
174  }
175 
176  T&
178  {
179  return m_begin[i];
180  }
181 
182  const T&
183  at(size_type i) const
184  {
185  return m_begin[i];
186  }
187 
188  void
189  push_back(const T& nn)
190  {
191  if (m_end >= m_capacity)
192  {
193  size_t olds = size();
194  size_t s = olds * 2;
195  if (!s)
196  {
197  s = 1;
198  }
199  T* newBegin = (T*)_mm_malloc(s * sizeof(T), 16); //new T[s];
200  if (m_begin)
201  {
202  memcpy(newBegin, m_begin, olds * sizeof(T));
203  _mm_free(m_begin); //delete[] m_begin;
204  }
205  m_end = newBegin + olds;
206  m_begin = newBegin;
207  m_capacity = m_begin + s;
208  }
209  *m_end = nn;
210  ++m_end;
211  }
212 
213  void
214  insert(T* where, const T& nn)
215  {
216  if (m_end >= m_capacity)
217  {
218  size_t whereIdx = where - m_begin;
219  size_t olds = size();
220  size_t s = olds * 2;
221  if (!s)
222  {
223  s = 1;
224  }
225  T* newBegin = (T*)_mm_malloc(s * sizeof(T), 16); //new T[s];
226  if (m_begin)
227  {
228  memcpy(newBegin, m_begin, olds * sizeof(T));
229  _mm_free(m_begin); //delete[] m_begin;
230  }
231  m_end = newBegin + olds;
232  m_begin = newBegin;
233  m_capacity = m_begin + s;
234  where = m_begin + whereIdx;
235  }
236  memmove(where + 1, where, (m_end - where) * sizeof(T));
237  *where = nn;
238  ++m_end;
239  }
240 
241  void
242  erase(T* where)
243  {
244  memmove(where, where + 1, (m_end - where - 1) * sizeof(T));
245  --m_end;
246  }
247 
248  void
250  {
251  if (m_end > m_begin)
252  {
253  --m_end;
254  }
255  }
256 
257  T*
259  {
260  return m_begin;
261  }
262 
263  const T*
264  begin() const
265  {
266  return m_begin;
267  }
268 
269  T*
270  end()
271  {
272  return m_end;
273  }
274 
275  const T*
276  end() const
277  {
278  return m_end;
279  }
280 
283  {
284  return std::reverse_iterator<T*>(m_end);
285  }
286 
288  rbegin() const
289  {
290  return std::reverse_iterator<const T*>(m_end);
291  }
292 
295  {
296  return std::reverse_iterator<T*>(m_begin);
297  }
298 
300  rend() const
301  {
302  return std::reverse_iterator<const T*>(m_begin);
303  }
304 
305  T&
307  {
308  return *(m_end - 1);
309  }
310 
311  const T&
312  back() const
313  {
314  return *(m_end - 1);
315  }
316 
317  T&
319  {
320  return *m_begin;
321  }
322 
323  const T&
324  front() const
325  {
326  return *m_begin;
327  }
328 
329  private:
330  T* m_begin;
331  T* m_end;
332  T* m_capacity;
333  };
334 }; // namespace GfxTL
335 
336 #endif
GfxTL::FlatCopyVector::iterator
T * iterator
Definition: FlatCopyVector.h:16
GfxTL::FlatCopyVector::~FlatCopyVector
~FlatCopyVector()
Definition: FlatCopyVector.h:56
GfxTL::FlatCopyVector::begin
const T * begin() const
Definition: FlatCopyVector.h:264
GfxTL::FlatCopyVector::at
const T & at(size_type i) const
Definition: FlatCopyVector.h:183
GfxTL::FlatCopyVector::const_pointer
const typedef T * const_pointer
Definition: FlatCopyVector.h:21
GfxTL::FlatCopyVector::reserve
void reserve(size_t s)
Definition: FlatCopyVector.h:95
GfxTL::FlatCopyVector::pointer
T * pointer
Definition: FlatCopyVector.h:20
GfxTL::FlatCopyVector::rend
reverse_iterator rend()
Definition: FlatCopyVector.h:294
GfxTL::FlatCopyVector::value_type
T value_type
Definition: FlatCopyVector.h:15
GfxTL::FlatCopyVector::push_back
void push_back(const T &nn)
Definition: FlatCopyVector.h:189
GfxTL::FlatCopyVector::clear
void clear()
Definition: FlatCopyVector.h:89
GfxTL::FlatCopyVector::const_iterator
const typedef T * const_iterator
Definition: FlatCopyVector.h:17
GfxTL::FlatCopyVector::insert
void insert(T *where, const T &nn)
Definition: FlatCopyVector.h:214
GfxTL::FlatCopyVector
Definition: FlatCopyVector.h:11
GfxTL::FlatCopyVector::FlatCopyVector
FlatCopyVector(size_t s)
Definition: FlatCopyVector.h:33
GfxTL::FlatCopyVector::size
size_t size() const
Definition: FlatCopyVector.h:117
GfxTL::FlatCopyVector::rbegin
reverse_iterator rbegin()
Definition: FlatCopyVector.h:282
GfxTL::FlatCopyVector::rbegin
const_reverse_iterator rbegin() const
Definition: FlatCopyVector.h:288
GfxTL::FlatCopyVector::end
const T * end() const
Definition: FlatCopyVector.h:276
GfxTL::FlatCopyVector::FlatCopyVector
FlatCopyVector(const FlatCopyVector< T > &v)
Definition: FlatCopyVector.h:40
GfxTL::FlatCopyVector::const_reference
const typedef T & const_reference
Definition: FlatCopyVector.h:19
GfxTL::FlatCopyVector::back
const T & back() const
Definition: FlatCopyVector.h:312
GfxTL::FlatCopyVector::reference
T & reference
Definition: FlatCopyVector.h:18
GfxTL::FlatCopyVector::FlatCopyVector
FlatCopyVector()
Definition: FlatCopyVector.h:26
GfxTL::FlatCopyVector::const_reverse_iterator
std::reverse_iterator< const T * > const_reverse_iterator
Definition: FlatCopyVector.h:24
GfxTL::FlatCopyVector::at
T & at(size_type i)
Definition: FlatCopyVector.h:177
GfxTL::FlatCopyVector::end
T * end()
Definition: FlatCopyVector.h:270
GfxTL
Definition: AABox.h:9
GfxTL::FlatCopyVector::pop_back
void pop_back()
Definition: FlatCopyVector.h:249
GfxTL::FlatCopyVector::size_type
size_t size_type
Definition: FlatCopyVector.h:14
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
GfxTL::FlatCopyVector::front
const T & front() const
Definition: FlatCopyVector.h:324
GfxTL::FlatCopyVector::rend
const_reverse_iterator rend() const
Definition: FlatCopyVector.h:300
GfxTL::FlatCopyVector::reverse_iterator
std::reverse_iterator< T * > reverse_iterator
Definition: FlatCopyVector.h:23
GfxTL::FlatCopyVector::capacity
size_t capacity() const
Definition: FlatCopyVector.h:123
GfxTL::FlatCopyVector::front
T & front()
Definition: FlatCopyVector.h:318
T
float T
Definition: UnscentedKalmanFilterTest.cpp:38
GfxTL::FlatCopyVector::operator=
FlatCopyVector< T > & operator=(const FlatCopyVector< T > &v)
Definition: FlatCopyVector.h:65
GfxTL::FlatCopyVector::difference_type
size_t difference_type
Definition: FlatCopyVector.h:22
GfxTL::FlatCopyVector::resize
void resize(size_t s, const value_type &v)
Definition: FlatCopyVector.h:153
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
GfxTL::FlatCopyVector::back
T & back()
Definition: FlatCopyVector.h:306
memory.h
GfxTL::FlatCopyVector::resize
void resize(size_t s)
Definition: FlatCopyVector.h:129
GfxTL::FlatCopyVector::begin
T * begin()
Definition: FlatCopyVector.h:258
GfxTL::FlatCopyVector::erase
void erase(T *where)
Definition: FlatCopyVector.h:242