-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patharray.h
executable file
·356 lines (312 loc) · 6.77 KB
/
array.h
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// The core library - copyright GarageGames. The core library is released under the MIT Open Source software license. See /license.txt in this distribution for specific license terms.
// Maintain a reserve of half the public size
struct array_half_reserve_policy
{
inline static uint32 grow_to(uint32 public_size, uint32 /*array_size*/) {
return public_size + public_size / 2;
}
inline static uint32 shrink_to(uint32 public_size, uint32 array_size) {
return (public_size < array_size / 2)?
public_size + public_size / 2: array_size;
};
};
template<class type, class policy = array_half_reserve_policy> class array
{
public:
typedef type value_type;
typedef type &reference;
typedef const type &const_reference;
typedef type *iterator;
typedef const type *const_iterator;
array()
{
_public_size = 0;
_array_size = 0;
_array = 0;
}
array(const uint32 initial_reserve)
{
_public_size = 0;
_array_size = initial_reserve;
_array = initial_reserve ? (type *) memory_allocate(initial_reserve * sizeof(type)) : 0;
}
array(const array &p)
{
_public_size = 0;
_array_size = 0;
_array = 0;
*this = p;
}
~array()
{
destroy(begin(), end());
memory_deallocate(_array);
}
uint32 size() const
{
return _public_size;
}
uint32 capacity() const
{
return _array_size;
}
void reserve(uint32 size)
{
if(size > _array_size)
_resize(size);
}
void resize(uint32 size)
{
if(size > _public_size)
{
if(size > _array_size)
_resize(policy::grow_to(size, _array_size));
construct(end(), end() + (size - _public_size));
_public_size = size;
}
else if(size < _public_size)
{
destroy(end() - (_public_size - size), end());
_public_size = size;
uint32 new_size = policy::shrink_to(size, _array_size);
if(new_size != _array_size)
_resize(new_size);
}
}
void compact()
{
if(_public_size < _array_size)
_resize(_public_size);
}
void clear()
{
destroy(begin(), end());
memory_deallocate(_array);
_public_size = 0;
_array_size = 0;
_array = 0;
}
bool is_empty()
{
return _public_size == 0;
}
iterator insert(uint32 index, const type &x)
{
if(_public_size + 1 > _array_size)
{
uint32 new_size = policy::grow_to(_public_size + 1, _array_size);
assert(new_size > _public_size);
type *new_array = (type *) memory_allocate(new_size * sizeof(type));
uninitialized_copy(_array + index, end(), new_array + index + 1);
destroy(begin(), end());
memory_deallocate(_array);
_array = new_array;
_array_size = new_size;
} else {
construct(end());
copy_backwards(_array + index, end(), end() + 1);
_array[index] = x;
}
_public_size++;
return _array + index;
}
iterator insert(iterator itr, const type &x)
{
return insert(uint32(itr - _array), x);
}
iterator insert(uint32 index)
{
return insert(index, type());
}
iterator insert(iterator itr)
{
return insert(uint32(itr - _array));
}
iterator push_front(const type &x)
{
return insert(uint32(0), x);
}
iterator push_back(const type &x)
{
if(_public_size + 1 > _array_size)
_resize(policy::grow_to(_public_size + 1, _array_size));
return construct(_array + _public_size++, x);
}
iterator push_front()
{
return insert(uint32(0));
}
iterator push_back()
{
return push_back(type());
}
void erase(uint32 index)
{
if(has_trivial_copy<type>::is_true)
{
copy(_array + index + 1, end(), _array + index);
destroy(&last());
uint32 new_size = policy::shrink_to(--_public_size, _array_size);
assert(new_size >= _public_size);
if(new_size != _array_size)
{
_array = (type *) memory_reallocate(_array, new_size * sizeof(type));
_array_size = new_size;
}
}
else
{
uint32 new_size = policy::shrink_to(_public_size - 1, _array_size);
if(new_size != _array_size)
{
assert(new_size >= _public_size - 1);
type *new_array = (type *) memory_allocate(new_size * sizeof(type));
uninitialized_copy(begin(), _array + index, new_array);
uninitialized_copy(_array + index + 1, end(), new_array + index);
destroy(begin(), end());
memory_deallocate(_array);
_array = new_array;
_array_size = new_size;
}
else
{
copy(_array + index + 1, end(), _array + index);
destroy(&last());
}
_public_size--;
}
}
void erase(iterator itr)
{
erase(uint32(itr - _array));
}
void erase_unstable(iterator itr)
{
if(itr != end() - 1)
swap(*itr, last());
pop_back();
}
void erase_unstable(uint32 index)
{
erase_unstable(&_array[index]);
}
void pop_front()
{
erase(uint32(0));
}
void pop_back()
{
destroy(&last());
uint32 size = policy::shrink_to(--_public_size, _array_size);
if(size != _array_size)
_resize(size);
}
iterator begin()
{
return _array;
}
const_iterator begin() const
{
return _array;
}
iterator end()
{
return _array + _public_size;
}
const_iterator end() const
{
return _array + _public_size;
}
iterator rbegin()
{
return end() - 1;
}
const_iterator rbegin() const
{
return end() - 1;
}
iterator rend()
{
return begin() - 1;
}
const_iterator rend() const
{
return begin() - 1;
}
type &first()
{
return _array[0];
}
const type &first() const
{
return _array[0];
}
type &last()
{
return _array[_public_size - 1];
}
const type &last() const
{
return _array[_public_size - 1];
}
type &operator[](uint32 index)
{
return _array[index];
}
const type &operator[](uint32 index) const
{
return _array[index];
}
void operator=(const array &p)
{
if(_array_size != p._array_size)
{
destroy(begin(), end());
memory_deallocate(_array);
_array_size = p._array_size;
_array = (type *) memory_allocate(p._array_size * sizeof(type));
uninitialized_copy(p.begin(), p.end(), _array);
}
else if(_public_size > p._public_size)
{
destroy(&_array[p._public_size], end());
copy(p.begin(), p.end(), begin());
}
else
{
copy(p.begin(), &p[_public_size], begin());
uninitialized_copy(&p[_public_size], p.end(), end());
}
_public_size = p._public_size;
}
protected:
uint32 _public_size;
uint32 _array_size;
type *_array;
void _resize(uint32 size)
{
assert(size >= _public_size);
if(has_trivial_copy<type>::is_true)
_array = (type *) memory_reallocate(_array, size * sizeof(type));
else
{
type *new_array = (type *) memory_allocate(size * sizeof(type));
uninitialized_copy(begin(), end(), new_array);
destroy(begin(), end());
memory_deallocate(_array);
_array = new_array;
}
_array_size = size;
}
};
/*template<class type, class policy> struct container_manipulator<array<type, policy> >
{
static type_record *get_key_type()
{
return get_global_type_record<uint32>();
}
static type_record *get_value_type()
{
return get_global_type_record<type>();
}
};*/