-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfast_array.cpp
More file actions
551 lines (357 loc) · 14.9 KB
/
fast_array.cpp
File metadata and controls
551 lines (357 loc) · 14.9 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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/*
Fast Array 2.0 (fast_array) is a fast, simple and easy to use similar to C++'s std::vector or Java's ArrayList, with some small differences.
Author: Edward Alexander
Copyright: Edward Alexander ©2016
Date: August 17, 2016
Version: 2.0
Revision: 519
Code: E.A.2.519.1
Known bugs: - Throws std::length_error when using an std::string
Details available:
- size : The current position of the last element.
- total_size : The capacity of the list.
Actions available:
- void grow_array(size_t new_size);
Grows array according to the new_size, throws if the new_size is smaller than the current size.
- void shrink_array();
Shrinks the array to half its size, losing every entry after its half.
- void add(T entry);
Adds an entry after the last position and increasing the array's size the one and a half its original size.
- void add_at(size_t position, T entry);
Adds an entry at the specified position, throws if the position is smaller than 0 or bigger than the array's size.
- void remove(size_t position);
Removes the entry at the specified position, throws if the position is smaller than 0 or bigger than the position of the last entry.
- T get(size_t position);
Gets the entry at the specified position, throws if the position is smaller than 0 or bigger than the position of the last entry.
- void release();
Releases the used resources.
Guide:
- An array can be made either with the default size or with a custom one.
- add_at(size_t position, T entry) only works if there are at least 2 elements in the array, if so, it will add a new element after the specified position.
- As such, add(T entry) is recommend whenever adding an element at a random position.
- When going through elements, the size detail is the one that should be used.
- The only time total_size should be used is to check the list's capacity.
- Whenever growing the array, the new size has to always be bigger than the old one.
- Whenever shrinking the array, only shrink_array() has to be used.
- An alternative to add(T entry), add_at(size_t position, T entry) and remove(size_t position) is the [] Subscript operator.
Log:
Version 2 : Rewrites the whole logic..
- Revision 519 (October 7th 2016) :
- Adds: - [] Subscript operator overload
- Rewrites : - Changed all size_t parameters to int
- Revision 517:
- Adds : - Constructor defaults to 100 if the entered size is 0
- Revision 513:
- Adds : - Logs
- Guide
- Details
- Revision 317:
- Adds : - void copy_array(T * _new_entries, T * _entries); (Internal)
- Revision 315:
- Rewrites : - void grow_array(size_t new_size);
- void shrink_array();
- Revision 313:
- Adds : - void grow_array(size_t new_size);
- void shrink_array();
- Revisions
- Rewrites : - void remove(size_t position);
- void add(T entry);
- void add_at(size_t position, T entry);
Version 1 : Shows the idea of the fast_array logic, formerly named advanced_array.
Current status: Working, no bugs found.
*/
#include "stdafx.h"
template <typename T>
class fast_array
{
private:
/// Current position in the list.
size_t _index = 0;
/// Current capacity of the list.
size_t _capacity = 0;
/// The list.
T * _entries;
/// Copies from _entries to _new_entries.
void copy_array(T * _new_entries, T * _entries);
/// Copies from _entries to _new_entries, based on the specified _size.
void copy_array(T * _new_entries, T * _entries, size_t _size);
/// Copies from _entries to _new_entries, leaving an empty place at _position where the new _entry will go.
void copy_array_add(T * _new_entries, T * _entries, size_t _position);
/// Copies from _entries to _new_entries, skipping at _position, deleting the _entry.
void copy_array_remove(T * _new_entries, T * _entries, size_t _position);
/// Makes a new list that's the same size as the original, afterwards it copies it and adds the new _entry.
void add_entry(T _entry, size_t _position);
/// Makes a new list that's one and a half the size of the original, afterwards it copies it and adds the new _entry.
void add_entry_increase_array(T _entry, size_t _position);
/// Makes a new list that's the same size as the original, afterwards it copies it and removes the specified _entry.
void remove_entry(size_t _position);
/// Sets the _entry at _position.
void set_entry(T _entry, size_t _position);
public:
/// Public size of the list, that gets updated whenever an _entry is added.
size_t size = 0;
/// Public total size of the list, that always gets updated.
size_t total_size = 0;
/// Default constructor, that initializes a list with 100 elements.
fast_array();
/// Constructor based on capacity, that initializes a list with the specified capacity.
fast_array(size_t capacity);
/// Default destructor which calls release().
~fast_array();
/// Default destructor which calls release().
T& operator[](size_t position);
/// Adds an _entry at the last position which is _index.
void add(T entry);
/// Adds an _entry at the specified position, throws if position < 0 || position > _index.
void add_at(size_t position, T entry);
/// Removes the _entry at the specified position, throws if position < 0 || position > _index.
void remove(size_t position);
/// Gets the _entry at the specified position, throws if position < 0 || position > _capacity.
T get(size_t position);
/// Makes a new list with the specified size, afterwards it copies it.
void grow_array(size_t new_size);
/// Makes a new list that's half the size of the original, afterwards it copies it.
void shrink_array();
/// Releases all used resources.
void release();
};
/// Copies from _entries to _new_entries.
template <typename T>
void fast_array <T>::copy_array(T * _new_entries, T * _entries)
{
/// _count is where it currently is.
size_t _count = 0;
/// Copies from the beginning till the end.
while (_count < _capacity)
{
*(_new_entries + _count) = *(_entries + _count);
_count++;
}
}
/// Copies from _entries to _new_entries, based on the specified _size.
template <typename T>
void fast_array <T>::copy_array(T * _new_entries, T * _entries, size_t _size)
{
/// _count is where it currently is.
size_t _count = 0;
/// Copies from the beginning till the end.
while (_count < _size)
{
*(_new_entries + _count) = *(_entries + _count);
_count++;
}
}
/// Copies from _entries to _new_entries, leaving an empty place at _position where the new _entry will go.
template <typename T>
void fast_array <T>::copy_array_add(T * _new_entries, T * _entries, size_t _position)
{
/// _before is the specific _postition, _after is next to it, _count is where it currently is.
size_t _before = _position, _after = _position + 1, _count = 0;
/// Copies from the beginning till the specified _position (inclusive).
while (_count <= _before)
{
*(_new_entries + _count) = *(_entries + _count);
_count++;
}
/// Skips at _position.
_count = _after;
/// Copies from _position to _position + 1 until the _capacity is full.
while (_count < _capacity)
{
*(_new_entries + _count) = *(_entries + _count - 1);
_count++;
}
}
/// Copies from _entries to _new_entries, skipping at _position, deleting the _entry.
template <typename T>
void fast_array <T>::copy_array_remove(T * _new_entries, T * _entries, size_t _position)
{
/// _before is before the specific _postition, _after is next to the _position, _count is where it currently is.
size_t _before = _position - 1, _after = _position + 1, _count = 0;
/// Copies from the beginning till the specified _position (inclusive).
while (_count <= _before)
{
*(_new_entries + _count) = *(_entries + _count);
_count++;
}
/// Skips after the _position.
_count = _after;
/// Copies from _position + 1 to _position until the _capacity is full.
while (_count < _capacity)
{
*(_new_entries + _count - 1) = *(_entries + _count);
_count++;
}
}
/// Makes a new list that's the same size as the original, afterwards it copies it and adds the new _entry.
template <typename T>
void fast_array <T>::add_entry(T _entry, size_t _position)
{
/// Makes a new list that's the same size as the original.
T *_new_entries = new T[sizeof(T) * _capacity];
( /// Copies from _entries to _new_entries, leaving an empty place at _position where the new _entry will go.
copy_array_add(_new_entries, _entries, _position),
/// Sets the _entry at _position.
*(_new_entries + _position) = _entry,
/// Deletes the old list.
delete[] _entries,
/// Updates the list.
_entries = _new_entries );
}
/// Makes a new list that's one and a half the size of the original, afterwards it copies it and adds the new _entry.
template <typename T>
void fast_array <T>::add_entry_increase_array(T _entry, size_t _position)
{
/// Makes a new list that's one and a half the size of the original.
T *_new_entries = new T[3 * _capacity / 2];
_capacity = ( /// Copies from _entries to _new_entries, leaving an empty place at _position where the new _entry will go.
copy_array_add(_new_entries, _entries, _position),
/// Sets the _entry at _position.
*(_new_entries + _position) = _entry,
/// Deletes the old list.
delete[] _entries,
/// Updates the list.
_entries = _new_entries,
/// Increases the _capacity of the list.
3 * _capacity / 2 );
}
/// Makes a new list with the specified size, afterwards it copies it.
template <typename T>
void fast_array <T>::grow_array(size_t new_size)
{
/// Makes a new list that's half the original.
T *_new_entries = new T[new_size];
_capacity = ( /// Copies from _entries to _new_entries.
copy_array(_new_entries, _entries),
/// Deletes the old list.
delete[] _entries,
/// Updates the list.
_entries = _new_entries,
/// Sets the _capacity of the list.
new_size );
total_size = _capacity;
}
/// Makes a new list that's the same size as the original, afterwards it copies it and removes the specified _entry.
template <typename T>
void fast_array <T>::remove_entry(size_t _position)
{
/// Makes a new list that's the same size as the original.
T *_new_entries = new T[sizeof(T) * _capacity];
( /// Copies from _entries to _new_entries, skipping at _position, deleting the _entry.
copy_array_remove(_new_entries, _entries, _position),
/// Deletes the old list.
delete[] _entries,
/// Updates the list.
_entries = _new_entries );
}
/// Makes a new list that's half the original, afterwards it copies it.
template <typename T>
void fast_array <T>::shrink_array()
{
/// Makes a new list that's half the original.
T *_new_entries = new T[2 * _capacity / 3];
_capacity = ( /// Copies from _entries to _new_entries.
copy_array(_new_entries, _entries, 2 * _capacity / 3),
/// Deletes the old list.
delete[] _entries,
/// Updates the list.
_entries = _new_entries,
/// Decreases the _capacity of the list.
2 * _capacity / 3);
total_size = _capacity;
}
/// Sets the _entry at _position.
template <typename T>
void fast_array <T>::set_entry(T _entry, size_t _position)
{
/// Sets the _entry at _position.
*(_entries + _position) = _entry;
}
/// Default constructor, that initializes a list with 100 elements.
template <typename T>
fast_array <T>::fast_array()
{
/// Defaults the _capacity to 100.
_capacity = total_size = 100;
/// Makes a list with the default _capacity.
_entries = new T[sizeof(T) * _capacity];
}
/// Constructor based on capacity, that initializes a list with the specified capacity.
template <typename T>
fast_array <T>::fast_array(size_t capacity)
{
/// Defaults to 100, in case _capacity is 0.
_capacity == 0 ? _capacity = 100 : false;
/// Defaults the _capacity to the specified capacity.
_capacity = total_size = capacity;
/// Makes a list with the specified capacity.
_entries = new T[sizeof(T) * _capacity];
}
/// Default destructor which calls release().
template <typename T>
fast_array <T>::~fast_array()
{
/// Releases all used resources.
release();
}
/// Overwrites an _entry at position.
template<typename T>
T& fast_array<T>::operator[](size_t position)
{
/// Throws if the position is before 0.
(int) position < 0 ? throw : false;
/// Grows the array if the position is after the total size.
position >= total_size ? (total_size = _capacity = 3 * _capacity / 2 + 1, grow_array(total_size)) : false;
/// Increases the size if the position is after the last element.
position >= size ? size = ++_index : false;
/// Returns the element at the specified position.
return _entries[position];
}
/// Adds an _entry at the last position which is _index.
template <typename T>
void fast_array <T>::add(T entry)
{
/// Sets the entry at the latest position which is _index or increases capacity.
_index < _capacity ? set_entry(entry, _index) : add_entry_increase_array(entry, _index);
/// Increases the size.
size = ++_index;
}
///Adds an _entry at the specified position, throws if position < 0 || position > _index.
template <typename T>
void fast_array <T>::add_at(size_t position, T entry)
{
/// Throws if the position is after the latest _entry or before 0.
position < _index && position > 0 ? true : throw;
/// Increases the size.
size = ++_index;
/// Sets the entry at the specified position if there is enough space, otherwise it increases the array.
_index < _capacity ? add_entry(entry, position) : add_entry_increase_array(entry, position);
}
///Removes the _entry at the specified position, throws if position < 0 || position > _index.
template <typename T>
void fast_array <T>::remove(size_t position)
{
/// Throws if the position is after the latest _entry or before 0.
position > _index || position < 0 ? throw : false;
/// Decreases the size.
size = --_index;
/// Decreases the array and removes the _entry at the specified position if it's before capacity, otherwise it throws.
position < _capacity ? remove_entry(position) : throw;
_index <= _capacity / 2 ? shrink_array() : false;
}
///Gets the _entry at the specified position, throws if position >= 0 && position < _index.
template <typename T>
T fast_array <T>::get(size_t position)
{
///Gets the _entry at the specified position, throws if position >= 0 && position < _index.
return position >= 0 || position < _index ? *(_entries + position) : throw;
}
///Releases all used resources.
template <typename T>
void fast_array <T>::release()
{
///Deletes the list.
delete[] _entries;
/// Deletes the pointer to the list.
_entries = nullptr;
}