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
8namespace 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
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*
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
const_reverse_iterator rend() const
const T * end() const
std::reverse_iterator< const T * > const_reverse_iterator
const T & front() const
void push_back(const T &nn)
FlatCopyVector(const FlatCopyVector< T > &v)
T & at(size_type i)
std::reverse_iterator< T * > reverse_iterator
reverse_iterator rend()
void reserve(size_t s)
size_t capacity() const
const T & back() const
void insert(T *where, const T &nn)
FlatCopyVector< T > & operator=(const FlatCopyVector< T > &v)
void resize(size_t s, const value_type &v)
reverse_iterator rbegin()
const T & at(size_type i) const
const_reverse_iterator rbegin() const
const T * begin() const
Definition AABox.h:10