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