-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathTSArray.h
More file actions
980 lines (823 loc) · 25.4 KB
/
Copy pathTSArray.h
File metadata and controls
980 lines (823 loc) · 25.4 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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
//
// This file is part of the Terathon Container Library, by Eric Lengyel.
// Copyright 1999-2025, Terathon Software LLC
//
// This software is distributed under the MIT License.
// Separate proprietary licenses are available from Terathon Software.
//
#ifndef TSArray_h
#define TSArray_h
/// \component Utility Library
/// \prefix Utilities/
#include "TSBasic.h"
#define TERATHON_ARRAY 1
namespace Terathon
{
/// \class Array A container class that holds an array of objects.
///
/// The $Array$ class represents a dynamically resizable array of objects
/// for which any entry can be accessed in constant time.
///
/// \def template <typename type, int32 baseCount = 0> class Array final : public ImmutableArray<type>
///
/// \tparam type The type of the class that can be stored in the array.
/// \tparam baseCount The minimum number of array elements for which storage is available inside the $Array$ object itself.
///
/// \ctor explicit Array(int32 count = 0);
///
/// \param count The number of array elements for which space is initially reserved in the array's storage.
///
/// \desc
/// The $Array$ class represents a homogeneous array of objects whose type is given by the
/// $type$ template parameter. Upon construction, the initial size of the array is zero, but
/// space is reserved for the number of objects given by the $count$ parameter. The array is
/// stored contiguously in memory, allowing constant-time random access to its elements.
///
/// As elements are added to the array (using the $@Array::AppendArrayElement@$ function), the storage
/// size is automatically increased to a size somewhat larger than that needed to store the new
/// element. The cost of adding an element is thus amortized linear time.
///
/// If the $baseCount$ template parameter is zero (the default), then storage space for the array
/// elements is always allocated on the heap separately from the $Array$ object. If the value of
/// $baseCount$ is greater than zero, then space for that number of array elements is built into the
/// structure of the $Array$ object so that no separate allocations need to be made until the size
/// of the array exceeds the value of $baseCount$.
///
/// The $count$ parameter can only be specified if the $baseCount$ template parameter is zero.
///
/// An $Array$ object can be implicitly converted to a pointer to its first element. This allows the
/// use of the $[]$ operator to access individual elements of the array.
///
/// It is possible to iterate over the elements of an array using a range-based for loop.
/// This is illustrated by the following code, where $array$ is a variable of type $Array<type>$.
///
/// \source
/// for (type& element : array)\n
/// {\n
/// \t...\n
/// }
///
/// \privbase ImmutableArray<type> Used internally.
///
/// \also $@List@$
/// \function Array::GetArrayElementCount Returns the current size of an array.
///
/// \proto int32 GetArrayElementCount(void) const;
///
/// \desc
/// The $GetArrayElementCount$ function returns the number of objects currently stored in an array.
/// When an array is constructed, its initial element count is zero.
///
/// \also $@Array::SetArrayElementCount@$
/// \also $@Array::AppendArrayElement@$
/// \also $@Array::InsertArrayElement@$
/// \also $@Array::RemoveArrayElement@$
/// \also $@Array::RemoveLastArrayElement@$
/// \function Array::SetArrayElementCount Sets the current size of an array.
///
/// \proto void SetArrayElementCount(int32 count);
/// \proto void SetArrayElementCount(int32 count, const type& init);
///
/// \param count The new size of the array.
/// \param init A reference to an object that is used to copy-construct new objects in the array.
///
/// \desc
/// The $SetArrayElementCount$ function sets the number of objects currently stored in an array.
/// If $count$ is greater than the current size of the array, then space is allocated for
/// $count$ objects and each new object is copy-constructed from the value of the $init$ parameter.
/// If $count$ is less than the current size of the array, then the logical size of the array
/// is reduced, and each object beyond the new size of the array is destroyed in reverse order.
///
/// If the $init$ parameter is omitted, then any new objects created are default-constructed if the
/// type of object stored in the array is a non-POD type. If the type of object stored in the array is
/// a POD type, then any new objects created are left uninitialized.
///
/// \also $@Array::GetArrayElementCount@$
/// \also $@Array::AppendArrayElement@$
/// \also $@Array::InsertArrayElement@$
/// \also $@Array::RemoveArrayElement@$
/// \also $@Array::RemoveLastArrayElement@$
/// \function Array::AppendArrayElement Adds an object to the end of an array.
///
/// \proto template <typename T> type *AppendArrayElement(T&& element);
/// \proto template <typename T> type *AppendArrayElement(void);
///
/// \param element The new element to add to the array.
///
/// \desc
/// The $AppendArrayElement$ function increases the size of an array by one and either copy-constructs
/// or move-constructs the new element using the object referenced by the $element$ parameter,
/// depending on whether an lvalue reference or rvalue reference is passed to the function.
/// If the parameter is omitted, then a default-constructed element is appended to the array.
/// The return value is a pointer to the newly appended element in the array.
///
/// \also $@Array::InsertArrayElement@$
/// \also $@Array::RemoveArrayElement@$
/// \also $@Array::RemoveLastArrayElement@$
/// \also $@Array::GetArrayElementCount@$
/// \also $@Array::SetArrayElementCount@$
/// \function Array::InsertArrayElement Inserts an object into an array.
///
/// \proto template <typename T> void InsertArrayElement(int32 index, T&& element);
///
/// \param index The location at which the object is to be inserted.
/// \param element The new element to insert into the array.
///
/// \desc
/// The $InsertArrayElement$ function increases the size of an array by one, moves all of the existing
/// elements at location $index$ or greater up by one, and either copy-constructs or move-constructs
/// the new element into the array using the object referenced by the $element$ parameter, depending on
/// whether an lvalue reference or rvalue reference is passed to the function. When the existing elements
/// are moved, they are move-constructed in their new locations, and the old objects are destroyed.
///
/// If the $index$ parameter is greater than or equal to the current size of the array, then the
/// array is enlarged to the size $index + 1$. In this case, elements between the old size and
/// new size are default-constructed if the type of object stored in the array is a non-POD type, and the
/// elements are left uninitialized if the type of object stored in the array is a POD type.
///
/// \also $@Array::RemoveArrayElement@$
/// \also $@Array::AppendArrayElement@$
/// \also $@Array::GetArrayElementCount@$
/// \also $@Array::SetArrayElementCount@$
/// \function Array::RemoveArrayElement Removes an object from an array.
///
/// \proto void RemoveArrayElement(int32 index);
///
/// \param index The location at which to remove an object.
///
/// \desc
/// The $RemoveArrayElement$ function decreases the size of an array by one, destroys the object at location
/// $index$, and moves all of the existing elements at location $index + 1$ or greater down by one.
/// When the existing elements are moved, they are move-constructed to their new locations, and the old
/// objects are destroyed.
///
/// If the $index$ parameter is greater than or equal to the current size of the array, then
/// calling the $RemoveArrayElement$ function has no effect.
///
/// \also $@Array::RemoveLastArrayElement@$
/// \also $@Array::InsertArrayElement@$
/// \also $@Array::AppendArrayElement@$
/// \also $@Array::GetArrayElementCount@$
/// \also $@Array::SetArrayElementCount@$
/// \function Array::RemoveLastArrayElement Removes the last object from an array.
///
/// \proto void RemoveLastArrayElement(void);
///
/// \desc
/// The $RemoveLastArrayElement$ function decreases the size of an array by one and destroys the object at the
/// original end of the array.
///
/// If the array is empty, then calling the $RemoveLastArrayElement$ function has no effect.
///
/// \also $@Array::RemoveArrayElement@$
/// \also $@Array::InsertArrayElement@$
/// \also $@Array::AppendArrayElement@$
/// \also $@Array::GetArrayElementCount@$
/// \also $@Array::SetArrayElementCount@$
/// \function Array::ClearArray Removes all objects from an array.
///
/// \proto void ClearArray(void);
///
/// \desc
/// The $ClearArray$ function destroys all objects in an array (in reverse order) and sets the size of
/// the array to zero. The storage for the array is not deallocated, so this function is best used
/// when the array is likely to be filled with a similar amount of data again. To both destroy all
/// objects in an array and deallocate the storage, call the $@Array::PurgeArray@$ function.
///
/// \also $@Array::PurgeArray@$
/// \also $@Array::RemoveArrayElement@$
/// \also $@Array::RemoveLastArrayElement@$
/// \also $@Array::SetArrayElementCount@$
/// \function Array::PurgeArray Removes all objects from an array and deallocates storage.
///
/// \proto void PurgeArray(void);
///
/// \desc
/// The $PurgeArray$ function destroys all objects in an array (in reverse order) and sets the size of
/// the array to zero. The storage for the array is also deallocated, returning the array to its
/// initial state. To destory all objects in an array without deallocating the storage, call the
/// $@Array::ClearArray@$ function.
///
/// \also $@Array::ClearArray@$
/// \also $@Array::RemoveArrayElement@$
/// \also $@Array::RemoveLastArrayElement@$
/// \also $@Array::SetArrayElementCount@$
/// \function Array::FindArrayElementIndex Finds a specific element in an array.
///
/// \proto int32 FindArrayElementIndex(const type& element) const;
///
/// \param element The value of the element to find.
///
/// \desc
/// The $FindArrayElementIndex$ function searches an array for the first element matching the value
/// passed into the $element$ parameter based on the $==$ operator. If a match is found, its index
/// is returned. If no match is found, then the return value is −1. The running time of this
/// function is <i>O</i>(<i>n</i>), where <i>n</i> is the number of elements in the array.
template <typename type>
class ImmutableArray
{
protected:
int32 elementCount;
int32 reservedCount;
type *arrayPointer;
inline ImmutableArray() = default;
inline ~ImmutableArray() = default;
ImmutableArray(const ImmutableArray&) {}
public:
operator type *(void)
{
return (arrayPointer);
}
operator type *(void) const
{
return (arrayPointer);
}
type *begin(void) const
{
return (arrayPointer);
}
type *end(void) const
{
return (arrayPointer + elementCount);
}
bool Empty(void) const
{
return (elementCount == 0);
}
int32 GetArrayElementCount(void) const
{
return (elementCount);
}
int32 FindArrayElementIndex(const type& element) const;
bool operator ==(const ImmutableArray& array) const;
};
template <typename type>
int32 ImmutableArray<type>::FindArrayElementIndex(const type& element) const
{
for (machine a = 0; a < elementCount; a++)
{
if (arrayPointer[a] == element)
{
return (int32(a));
}
}
return (-1);
}
template <typename type>
bool ImmutableArray<type>::operator ==(const ImmutableArray<type>& array) const
{
int32 count = elementCount;
if (count != array.elementCount)
{
return (false);
}
for (machine a = 0; a < count; a++)
{
if (arrayPointer[a] != array.arrayPointer[a])
{
return (false);
}
}
return (true);
}
template <typename type, int32 baseCount = 0>
class Array final : public ImmutableArray<type>
{
private:
using ImmutableArray<type>::elementCount;
using ImmutableArray<type>::reservedCount;
using ImmutableArray<type>::arrayPointer;
alignas(type) char arrayStorage[baseCount * sizeof(type)];
void SetReservedCount(int32 count);
public:
explicit Array();
Array(const Array& array);
Array(Array&& array);
~Array();
void ClearArray(void);
void PurgeArray(void);
void ReserveArrayElementCount(int32 count);
void SetArrayElementCount(int32 count);
void SetArrayElementCount(int32 count, const type& init);
type *AppendArrayElement(void);
template <typename T>
type *AppendArrayElement(T&& element);
template <typename T>
void InsertArrayElement(int32 index, T&& element);
void RemoveArrayElement(int32 index);
void RemoveLastArrayElement(void);
};
template <typename type, int32 baseCount>
Array<type, baseCount>::Array()
{
elementCount = 0;
reservedCount = baseCount;
arrayPointer = reinterpret_cast<type *>(arrayStorage);
}
template <typename type, int32 baseCount>
Array<type, baseCount>::Array(const Array& array)
{
elementCount = array.elementCount;
if (elementCount > baseCount)
{
reservedCount = array.reservedCount;
arrayPointer = reinterpret_cast<type *>(new char[sizeof(type) * reservedCount]);
}
else
{
reservedCount = baseCount;
arrayPointer = reinterpret_cast<type *>(arrayStorage);
}
for (machine a = 0; a < elementCount; a++)
{
new(&arrayPointer[a]) type(array.arrayPointer[a]);
}
}
template <typename type, int32 baseCount>
Array<type, baseCount>::Array(Array&& array)
{
elementCount = array.elementCount;
if (elementCount > baseCount)
{
reservedCount = array.reservedCount;
arrayPointer = array.arrayPointer;
}
else
{
reservedCount = baseCount;
arrayPointer = reinterpret_cast<type *>(arrayStorage);
type *pointer = array.arrayPointer;
for (machine a = 0; a < elementCount; a++)
{
new(&arrayPointer[a]) type(static_cast<type&&>(pointer[a]));
pointer[a].~type();
}
}
array.elementCount = 0;
array.reservedCount = baseCount;
array.arrayPointer = reinterpret_cast<type *>(array.arrayStorage);
}
template <typename type, int32 baseCount>
Array<type, baseCount>::~Array()
{
type *pointer = arrayPointer + elementCount;
for (machine a = elementCount - 1; a >= 0; a--)
{
(--pointer)->~type();
}
char *ptr = reinterpret_cast<char *>(arrayPointer);
if (ptr != arrayStorage)
{
delete[] ptr;
}
}
template <typename type, int32 baseCount>
void Array<type, baseCount>::ClearArray(void)
{
type *pointer = arrayPointer + elementCount;
for (machine a = elementCount - 1; a >= 0; a--)
{
(--pointer)->~type();
}
elementCount = 0;
}
template <typename type, int32 baseCount>
void Array<type, baseCount>::PurgeArray(void)
{
type *pointer = arrayPointer + elementCount;
for (machine a = elementCount - 1; a >= 0; a--)
{
(--pointer)->~type();
}
char *ptr = reinterpret_cast<char *>(arrayPointer);
if (ptr != arrayStorage)
{
delete[] ptr;
}
elementCount = 0;
reservedCount = baseCount;
arrayPointer = reinterpret_cast<type *>(arrayStorage);
}
template <typename type, int32 baseCount>
void Array<type, baseCount>::SetReservedCount(int32 count)
{
reservedCount = Max(Max(count, 4), reservedCount + Max((reservedCount / 2 + 3) & ~3, baseCount));
type *newPointer = reinterpret_cast<type *>(new char[sizeof(type) * reservedCount]);
type *pointer = arrayPointer;
for (machine a = 0; a < elementCount; a++)
{
new(&newPointer[a]) type(static_cast<type&&>(*pointer));
pointer->~type();
pointer++;
}
char *ptr = reinterpret_cast<char *>(arrayPointer);
if (ptr != arrayStorage)
{
delete[] ptr;
}
arrayPointer = newPointer;
}
template <typename type, int32 baseCount>
void Array<type, baseCount>::ReserveArrayElementCount(int32 count)
{
if (count > reservedCount)
{
SetReservedCount(count);
}
}
template <typename type, int32 baseCount>
void Array<type, baseCount>::SetArrayElementCount(int32 count)
{
if (count > reservedCount)
{
SetReservedCount(count);
}
if (count > elementCount)
{
type *pointer = arrayPointer + (elementCount - 1);
for (machine a = elementCount; a < count; a++)
{
new(++pointer) type;
}
}
else if (count < elementCount)
{
type *pointer = arrayPointer + elementCount;
for (machine a = elementCount - 1; a >= count; a--)
{
(--pointer)->~type();
}
}
elementCount = count;
}
template <typename type, int32 baseCount>
void Array<type, baseCount>::SetArrayElementCount(int32 count, const type& init)
{
if (count > reservedCount)
{
SetReservedCount(count);
}
if (count > elementCount)
{
type *pointer = arrayPointer + (elementCount - 1);
for (machine a = elementCount; a < count; a++)
{
new(++pointer) type(init);
}
}
else if (count < elementCount)
{
type *pointer = arrayPointer + elementCount;
for (machine a = elementCount - 1; a >= count; a--)
{
(--pointer)->~type();
}
}
elementCount = count;
}
template <typename type, int32 baseCount>
type *Array<type, baseCount>::AppendArrayElement(void)
{
if (elementCount >= reservedCount)
{
SetReservedCount(elementCount + 1);
}
type *pointer = arrayPointer + elementCount;
new(pointer) type;
elementCount++;
return (pointer);
}
template <typename type, int32 baseCount>
template <typename T>
type *Array<type, baseCount>::AppendArrayElement(T&& element)
{
if (elementCount >= reservedCount)
{
SetReservedCount(elementCount + 1);
}
type *pointer = arrayPointer + elementCount;
new(pointer) type(static_cast<T&&>(element));
elementCount++;
return (pointer);
}
template <typename type, int32 baseCount>
template <typename T>
void Array<type, baseCount>::InsertArrayElement(int32 index, T&& element)
{
if (index >= elementCount)
{
int32 count = index + 1;
if (count > reservedCount)
{
SetReservedCount(count);
}
type *pointer = &arrayPointer[elementCount - 1];
for (machine a = elementCount; a < index; a++)
{
new(++pointer) type;
}
new (++pointer) type(static_cast<T&&>(element));
elementCount = count;
}
else
{
int32 count = elementCount + 1;
if (count > reservedCount)
{
SetReservedCount(count);
}
type *pointer = &arrayPointer[elementCount];
for (machine a = elementCount; a > index; a--)
{
new(pointer) type(static_cast<type&&>(pointer[-1]));
(--pointer)->~type();
}
new (&arrayPointer[index]) type(static_cast<T&&>(element));
elementCount = count;
}
}
template <typename type, int32 baseCount>
void Array<type, baseCount>::RemoveArrayElement(int32 index)
{
if (index < elementCount)
{
type *pointer = &arrayPointer[index];
pointer->~type();
for (machine a = index + 1; a < elementCount; a++)
{
new(pointer) type(static_cast<type&&>(pointer[1]));
(++pointer)->~type();
}
elementCount--;
}
}
template <typename type, int32 baseCount>
void Array<type, baseCount>::RemoveLastArrayElement(void)
{
int32 index = elementCount - 1;
if (index >= 0)
{
type *pointer = &arrayPointer[index];
pointer->~type();
elementCount = index;
}
}
template <typename type>
class Array<type, 0> final : public ImmutableArray<type>
{
private:
using ImmutableArray<type>::elementCount;
using ImmutableArray<type>::reservedCount;
using ImmutableArray<type>::arrayPointer;
void SetReservedCount(int32 count);
public:
explicit Array(int32 count = 0);
Array(const Array& array);
Array(Array&& array);
~Array();
void ClearArray(void);
void PurgeArray(void);
void ReserveArrayElementCount(int32 count);
void SetArrayElementCount(int32 count);
void SetArrayElementCount(int32 count, const type& init);
type *AppendArrayElement(void);
template <typename T>
type *AppendArrayElement(T&& element);
template <typename T>
void InsertArrayElement(int32 index, T&& element);
void RemoveArrayElement(int32 index);
void RemoveLastArrayElement(void);
};
template <typename type>
Array<type, 0>::Array(int32 count)
{
elementCount = 0;
reservedCount = count;
arrayPointer = (count > 0) ? reinterpret_cast<type *>(new char[sizeof(type) * count]) : nullptr;
}
template <typename type>
Array<type, 0>::Array(const Array& array)
{
elementCount = array.elementCount;
reservedCount = array.reservedCount;
if (reservedCount > 0)
{
arrayPointer = reinterpret_cast<type *>(new char[sizeof(type) * reservedCount]);
for (machine a = 0; a < elementCount; a++)
{
new(&arrayPointer[a]) type(array.arrayPointer[a]);
}
}
else
{
arrayPointer = nullptr;
}
}
template <typename type>
Array<type, 0>::Array(Array&& array)
{
elementCount = array.elementCount;
reservedCount = array.reservedCount;
arrayPointer = array.arrayPointer;
array.elementCount = 0;
array.reservedCount = 0;
array.arrayPointer = nullptr;
}
template <typename type>
Array<type, 0>::~Array()
{
type *pointer = arrayPointer + elementCount;
for (machine a = elementCount - 1; a >= 0; a--)
{
(--pointer)->~type();
}
delete[] reinterpret_cast<char *>(arrayPointer);
}
template <typename type>
void Array<type, 0>::ClearArray(void)
{
type *pointer = arrayPointer + elementCount;
for (machine a = elementCount - 1; a >= 0; a--)
{
(--pointer)->~type();
}
elementCount = 0;
}
template <typename type>
void Array<type, 0>::PurgeArray(void)
{
type *pointer = arrayPointer + elementCount;
for (machine a = elementCount - 1; a >= 0; a--)
{
(--pointer)->~type();
}
delete[] reinterpret_cast<char *>(arrayPointer);
elementCount = 0;
reservedCount = 0;
arrayPointer = nullptr;
}
template <typename type>
void Array<type, 0>::SetReservedCount(int32 count)
{
reservedCount = Max(Max(count, 4), reservedCount + Max((reservedCount / 2 + 3) & ~3, 4));
type *newPointer = reinterpret_cast<type *>(new char[sizeof(type) * reservedCount]);
type *pointer = arrayPointer;
if (pointer)
{
for (machine a = 0; a < elementCount; a++)
{
new(&newPointer[a]) type(static_cast<type&&>(*pointer));
pointer->~type();
pointer++;
}
delete[] reinterpret_cast<char *>(arrayPointer);
}
arrayPointer = newPointer;
}
template <typename type>
void Array<type, 0>::ReserveArrayElementCount(int32 count)
{
if (count > reservedCount)
{
SetReservedCount(count);
}
}
template <typename type>
void Array<type, 0>::SetArrayElementCount(int32 count)
{
if (count > reservedCount)
{
SetReservedCount(count);
}
if (count > elementCount)
{
type *pointer = arrayPointer + (elementCount - 1);
for (machine a = elementCount; a < count; a++)
{
new(++pointer) type;
}
}
else if (count < elementCount)
{
type *pointer = arrayPointer + elementCount;
for (machine a = elementCount - 1; a >= count; a--)
{
(--pointer)->~type();
}
}
elementCount = count;
}
template <typename type>
void Array<type, 0>::SetArrayElementCount(int32 count, const type& init)
{
if (count > reservedCount)
{
SetReservedCount(count);
}
if (count > elementCount)
{
type *pointer = arrayPointer + (elementCount - 1);
for (machine a = elementCount; a < count; a++)
{
new(++pointer) type(init);
}
}
else if (count < elementCount)
{
type *pointer = arrayPointer + elementCount;
for (machine a = elementCount - 1; a >= count; a--)
{
(--pointer)->~type();
}
}
elementCount = count;
}
template <typename type>
type *Array<type, 0>::AppendArrayElement(void)
{
if (elementCount >= reservedCount)
{
SetReservedCount(elementCount + 1);
}
type *pointer = arrayPointer + elementCount;
new(pointer) type;
elementCount++;
return (pointer);
}
template <typename type>
template <typename T>
type *Array<type, 0>::AppendArrayElement(T&& element)
{
if (elementCount >= reservedCount)
{
SetReservedCount(elementCount + 1);
}
type *pointer = arrayPointer + elementCount;
new(pointer) type(static_cast<T&&>(element));
elementCount++;
return (pointer);
}
template <typename type>
template <typename T>
void Array<type, 0>::InsertArrayElement(int32 index, T&& element)
{
if (index >= elementCount)
{
int32 count = index + 1;
if (count > reservedCount)
{
SetReservedCount(count);
}
type *pointer = &arrayPointer[elementCount - 1];
for (machine a = elementCount; a < index; a++)
{
new(++pointer) type;
}
new (++pointer) type(static_cast<T&&>(element));
elementCount = count;
}
else
{
int32 count = elementCount + 1;
if (count > reservedCount)
{
SetReservedCount(count);
}
type *pointer = &arrayPointer[elementCount];
for (machine a = elementCount; a > index; a--)
{
new(pointer) type(static_cast<type&&>(pointer[-1]));
(--pointer)->~type();
}
new (&arrayPointer[index]) type(static_cast<T&&>(element));
elementCount = count;
}
}
template <typename type>
void Array<type, 0>::RemoveArrayElement(int32 index)
{
if (index < elementCount)
{
type *pointer = &arrayPointer[index];
pointer->~type();
for (machine a = index + 1; a < elementCount; a++)
{
new(pointer) type(static_cast<type&&>(pointer[1]));
(++pointer)->~type();
}
elementCount--;
}
}
template <typename type>
void Array<type, 0>::RemoveLastArrayElement(void)
{
int32 index = elementCount - 1;
if (index >= 0)
{
type *pointer = &arrayPointer[index];
pointer->~type();
elementCount = index;
}
}
}
#endif