-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathObjectArray.h
More file actions
247 lines (219 loc) · 4.1 KB
/
Copy pathObjectArray.h
File metadata and controls
247 lines (219 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#ifndef __OBJECTARRAY_H__
#define __OBJECTARRAY_H__
#include <memory.h>
#include "Common.h"
namespace Utilities
{
template <class T, int growBy = 100, bool linearGrow = true>
class ObjectArray
{
private:
unsigned int m_size;
unsigned int m_capacity;
T* m_data;
protected:
FORCE_INLINE void init()
{
m_size = 0u;
m_capacity = 0u;
m_data = 0;
}
public:
FORCE_INLINE ObjectArray()
{
init();
}
FORCE_INLINE ObjectArray(const ObjectArray& other)
{
init();
*this = other;
}
~ObjectArray()
{
clear();
}
/** Return the pointer of the data.
*/
FORCE_INLINE T* arrayPointer()
{
return m_data;
}
/** Return the pointer of the data.
*/
FORCE_INLINE const T* arrayPointer() const
{
return m_data;
}
/** Set size to zero but do not change capacity.
*/
FORCE_INLINE void reset()
{
m_size = 0u;
}
FORCE_INLINE void clear()
{
delete[] m_data;
init();
}
/** Return the number of elements
*/
FORCE_INLINE unsigned int size() const
{
return m_size;
}
/** Return the capacity
*/
FORCE_INLINE unsigned int capacity() const
{
return m_capacity;
}
FORCE_INLINE T* getData()
{
return m_data;
}
FORCE_INLINE const T* getData() const
{
return m_data;
}
T& create()
{
if (m_size >= m_capacity)
grow();
return m_data[m_size++];
}
/** Insert element at the given index.
*/
FORCE_INLINE void insert(const unsigned int index, const T & data)
{
if (m_size >= m_capacity)
grow();
for (unsigned int i = m_size; i > index; i--)
{
m_data[i] = m_data[i - 1];
}
m_size++;
m_data[index] = data;
}
/** Remove the element at the given index.
*/
FORCE_INLINE void removeAt(const unsigned int index)
{
m_data[index].~T();
for (unsigned int i = index + 1u; i < m_size; i++)
{
m_data[i - 1] = m_data[i];
}
m_size--;
}
/** Remove the given element and return true, if the element was found.
*/
FORCE_INLINE bool remove(const T & element)
{
for (unsigned int i = 0u; i < m_size; i++)
{
if (m_data[i] == element)
{
removeAt(i);
return true;
}
}
return false;
}
FORCE_INLINE void pop_back()
{
m_size--;
m_data[m_size].~T();
}
FORCE_INLINE void push_back(const T & data)
{
if (m_size >= m_capacity)
grow();
m_data[m_size] = data;
m_size++;
}
FORCE_INLINE void push_back(T & data)
{
if (m_size >= m_capacity)
grow();
m_data[m_size] = data;
m_size++;
}
FORCE_INLINE T& operator[](const unsigned int i)
{
return m_data[i];
}
FORCE_INLINE const T& operator[](const unsigned int i) const
{
return m_data[i];
}
FORCE_INLINE ObjectArray& operator=(const ObjectArray & val)
{
this->resize(val.m_size);
memcpy(this->m_data, val.m_data, sizeof(T) * val.m_size);
return *this;
}
FORCE_INLINE void reserve(const unsigned int count)
{
if (!m_data)
{
unsigned int newSize = count;
if (newSize < growBy)
newSize = growBy;
m_data = new T[newSize];
m_size = 0;
m_capacity = newSize;
}
else
{
if (count <= m_capacity)
return;
unsigned int newSize = m_capacity;
newSize = (linearGrow) ? (newSize + growBy) : (newSize * 2u);
if (count < newSize)
{
grow();
return;
}
T* tmp = new T[count];
memcpy(tmp, m_data, sizeof(T) * m_size);
delete[] m_data;
m_data = tmp;
m_capacity = count;
}
}
FORCE_INLINE void resize(unsigned int count)
{
reserve(count);
m_size = count;
}
private:
FORCE_INLINE void grow()
{
if (!m_data)
{
m_data = new T[growBy];
m_size = 0;
m_capacity = growBy;
}
else if (linearGrow)
{
const unsigned int newSize = m_capacity + growBy;
T* tmp = new T[newSize];
memcpy(tmp, m_data, sizeof(T) * m_size);
delete[] m_data;
m_data = tmp;
m_capacity = newSize;
}
else
{
const unsigned int newSize = m_capacity * 2u;
T* tmp = new T[newSize];
memcpy(tmp, m_data, sizeof(T) * m_size);
delete[] m_data;
m_data = tmp;
m_capacity = newSize;
}
}
};
}
#endif