-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynarray.hpp
1758 lines (1511 loc) · 66.2 KB
/
dynarray.hpp
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
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @copyright
BSD 3-Clause License
Copyright (c) 2020, cnbatch
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*!***************************************************************//*!
* @file dynarray.hpp
* @brief VLA for C++
*
* @author cnbatch
* @date January 2021
*********************************************************************/
/*! \mainpage VLA for C++: dynarray
*
* \section sec1 Depencencies
*
* C++ 17
*
* C++ Standard Library
*
* \section sec2 Project on Github:
*
* https://github.com/cnbatch/dynarray
*/
#pragma once
#ifndef DYNARRAY_HPP
#define DYNARRAY_HPP
#include <algorithm>
#include <cstdlib>
#include <initializer_list>
#include <iterator>
#include <limits>
#include <memory>
#include <numeric>
#include <stdexcept>
#include <type_traits>
#include <utility>
#if __cplusplus >= 202002L || (defined(_MSVC_LANG) && (_MSVC_LANG >= 202002L))
#define DYNARRAY_USING_CPP20
#endif
#ifdef DYNARRAY_USING_CPP20
#define CPP20_DYNARRAY_CONSTEXPR constexpr
#define CPP20_DYNARRAY_NODISCARD [[nodiscard]]
#else
#define CPP20_DYNARRAY_CONSTEXPR
#define CPP20_DYNARRAY_NODISCARD
#endif
namespace vla
{
template<typename T, template<typename U> typename _Allocator>
class dynarray;
namespace internal_impl
{
template <typename T, template<typename U> typename _Allocator>
struct inner_type
{
using value_type = T;
enum { nested_level = 0 };
};
template <typename T, template<typename U> typename _Allocator>
struct inner_type<dynarray<T, _Allocator>, _Allocator>
{
using value_type = typename inner_type<T, _Allocator>::value_type;
enum { nested_level = inner_type<T, _Allocator>::nested_level + 1 };
};
template<typename Skip> CPP20_DYNARRAY_CONSTEXPR
std::size_t expand_parameters(std::size_t count, const Skip &skip) { return count; }
template<typename Skip, typename ... Args> CPP20_DYNARRAY_CONSTEXPR
std::size_t expand_parameters(std::size_t count, const Skip &skip, Args&& ... args) { return count * expand_parameters(std::forward<Args>(args)...); }
} // internal namespace
template<typename T>
class vla_iterator
{
using self_value_type = vla_iterator<T>;
using self_reference = vla_iterator<T> &;
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = T*;
using reference = T&;
CPP20_DYNARRAY_CONSTEXPR explicit vla_iterator(pointer ptr = nullptr) : dynarray_ptr(ptr) {}
CPP20_DYNARRAY_CONSTEXPR vla_iterator(const vla_iterator<T> &other_iterator) : dynarray_ptr(other_iterator.dynarray_ptr) {}
// operators
CPP20_DYNARRAY_CONSTEXPR reference operator*() const noexcept { return *dynarray_ptr; }
CPP20_DYNARRAY_CONSTEXPR pointer operator->() const noexcept { return dynarray_ptr; }
CPP20_DYNARRAY_CONSTEXPR reference operator[](difference_type offset) const noexcept { return dynarray_ptr[offset]; }
CPP20_DYNARRAY_CONSTEXPR self_reference operator=(const self_value_type & right_iterator) noexcept { dynarray_ptr = right_iterator.dynarray_ptr; return *this; }
CPP20_DYNARRAY_CONSTEXPR self_reference operator=(pointer ptr) noexcept { dynarray_ptr = ptr; return *this; }
CPP20_DYNARRAY_CONSTEXPR self_reference operator++() noexcept { ++dynarray_ptr; return *this; }
CPP20_DYNARRAY_CONSTEXPR self_value_type operator++(int) noexcept { return self_value_type(dynarray_ptr++); }
CPP20_DYNARRAY_CONSTEXPR self_reference operator--() noexcept { --dynarray_ptr; return *this; }
CPP20_DYNARRAY_CONSTEXPR self_value_type operator--(int) noexcept { return self_value_type(dynarray_ptr--); }
CPP20_DYNARRAY_CONSTEXPR self_reference operator+=(difference_type offset) noexcept { dynarray_ptr += offset; return *this; }
CPP20_DYNARRAY_CONSTEXPR self_reference operator-=(difference_type offset) noexcept { dynarray_ptr -= offset; return *this; }
CPP20_DYNARRAY_CONSTEXPR self_value_type operator+(difference_type offset) const noexcept { return self_value_type(dynarray_ptr) += offset; }
CPP20_DYNARRAY_CONSTEXPR self_value_type operator-(difference_type offset) const noexcept { return self_value_type(dynarray_ptr - offset); }
CPP20_DYNARRAY_CONSTEXPR difference_type operator-(const self_value_type & right_iterator) const noexcept { return dynarray_ptr - right_iterator.dynarray_ptr; }
CPP20_DYNARRAY_CONSTEXPR bool operator==(const self_value_type &right_iterator) const noexcept { return dynarray_ptr == right_iterator.dynarray_ptr; }
#ifdef DYNARRAY_USING_CPP20
CPP20_DYNARRAY_CONSTEXPR auto operator<=>(const self_value_type &right_iterator) const noexcept = default;
#else
CPP20_DYNARRAY_CONSTEXPR bool operator!=(const self_value_type &right_iterator) const noexcept { return dynarray_ptr != right_iterator.dynarray_ptr; }
CPP20_DYNARRAY_CONSTEXPR bool operator<(const self_value_type &right_iterator) const noexcept { return dynarray_ptr < right_iterator.dynarray_ptr; }
CPP20_DYNARRAY_CONSTEXPR bool operator>(const self_value_type &right_iterator) const noexcept { return dynarray_ptr > right_iterator.dynarray_ptr; }
CPP20_DYNARRAY_CONSTEXPR bool operator<=(const self_value_type &right_iterator) const noexcept { return dynarray_ptr <= right_iterator.dynarray_ptr; }
CPP20_DYNARRAY_CONSTEXPR bool operator>=(const self_value_type &right_iterator) const noexcept { return dynarray_ptr >= right_iterator.dynarray_ptr; }
#endif
friend CPP20_DYNARRAY_CONSTEXPR self_value_type operator+(typename vla_iterator::difference_type offset, const self_value_type &other) noexcept { return self_value_type(other) += offset; }
private:
pointer dynarray_ptr;
};
template<typename T, template<typename U> typename _Allocator = std::allocator>
class dynarray
{
friend class dynarray<dynarray<T, _Allocator>, _Allocator>;
public:
// Member types
using value_type = T;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using reference = value_type &;
using const_reference = const value_type &;
using pointer = value_type *;
using const_pointer = const value_type *;
using iterator = vla_iterator<T>;
using const_iterator = vla_iterator<const T>;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using allocator_type = _Allocator<T>;
private:
using internal_value_type = typename internal_impl::inner_type<T, _Allocator>::value_type;
using internal_pointer_type = internal_value_type *;
public:
using contiguous_allocator_type = _Allocator<internal_value_type>;
// Member functions
/*!
* @brief Default Constructor.
* Create a zero-size array.
*/
CPP20_DYNARRAY_CONSTEXPR dynarray() noexcept
{
initialise();
}
/*!
* @brief Construct by count.
* Create a one-dimensional array.
*
* @param count The size (length) of array
*/
CPP20_DYNARRAY_CONSTEXPR dynarray(size_type count)
{
initialise();
allocate_array(count);
}
/*!
* @brief Construct by count and use your custom allocator.
* Create a one-dimensional array.
*
* @param count The size (length) of array
* @param other_allocator Your custom allocator
*/
template<typename _Alloc_t, typename = std::enable_if_t<std::is_same_v<std::decay_t<_Alloc_t>, allocator_type>>>
CPP20_DYNARRAY_CONSTEXPR dynarray(size_type count, _Alloc_t &&other_allocator)
{
initialise(other_allocator);
allocate_array(count);
}
/*!
* @brief Usage 1: Construct by multiple 'count'.\n
* Create a multi-dimensional array.\n
* Example A: dynarray<dynarray<int>> my_array(10, 10); // creates a 2D array (10 × 10), initialise with default value (zero)\n
* Example B: dynarray<dynarray<int>> my_array(10, 10, 20); // creates a 2D array (10 × 10), initialise with value (20)
*
* Usage 2: Construct by a 'count' and initialise the elements with 'args'\n
* Create a one-dimensional array, use 'args' to initialise the array's elements.\n
* Example A: dynarray<int> my_array(100); // creates an array (100 elements), initialise with default value (zero)\n
* Example B: dynarray<int> my_array(100, 20); // creates an array (100 elements), initialise with value (20)
*
* @param count The first dimention
* @param ...args If 'sizeof...(args)' is greater than the level of nested array, the rest of arg(s) will be used for initial array's elements.
*/
template<typename ... Args>
CPP20_DYNARRAY_CONSTEXPR dynarray(size_type count, Args&& ... args)
{
initialise();
allocate_array(count, std::forward<Args>(args)...);
}
/*!
* @brief Construct by multiple 'count' and use your custom allocator.
*
* @param count The first dimension
* @param other_allocator Your custom allocator
* @param ...args If 'sizeof...(args)' is greater than the level of nested array, the rest of arg(s) will be used for initial array's elements.
*/
template<typename _Alloc_t, typename = std::enable_if_t<std::is_same_v<std::decay_t<_Alloc_t>, allocator_type>>, typename ... Args>
CPP20_DYNARRAY_CONSTEXPR dynarray(size_type count, _Alloc_t &&other_allocator, Args&& ... args)
{
initialise(other_allocator);
allocate_array(count, other_allocator, std::forward<Args>(args)...);
}
/*!
* @brief Duplicate an existing dynarray.
*
* @param other Another array to be copied
*/
CPP20_DYNARRAY_CONSTEXPR dynarray(const dynarray &other)
{
initialise();
copy_array(other);
}
/*!
* @brief Initialise with rvalue.
*
* @param other Another array
*/
CPP20_DYNARRAY_CONSTEXPR dynarray(dynarray &&other) noexcept
{
move_array(other);
}
/*!
* @brief Duplicate an existing dynarray and use your custom allocator.
*
* @param other Another array to be copied
* @param other_allocator Allocator for the first level
* @param ...args Other dimention's allocator(s)
*/
template<typename ... Args>
CPP20_DYNARRAY_CONSTEXPR dynarray(const dynarray &other, const allocator_type &other_allocator, Args&& ... args)
{
initialise(other_allocator);
copy_array(other, other_allocator, std::forward<Args>(args)...);
}
/*!
* @brief Duplicate an existing dynarray by using iterator.
*
* @param other_begin begin(), cbegin() of iterator; or rbegin(), crbegin() of reverse iterator
* @param other_end end(), cend() of iterator; or rend(), crend() of reverse iterator
*/
template<typename InputIterator, typename = decltype(*std::declval<InputIterator&>(), ++std::declval<InputIterator&>(), void())>
CPP20_DYNARRAY_CONSTEXPR dynarray(InputIterator other_begin, InputIterator other_end)
{
initialise();
copy_array(other_begin, other_end);
}
/*!
* @brief Create a one-dimensional array with initializer_list and use your allocator (if any)
*
* @param input_list Your initializer_list
* @param other_allocator Your allocator (if any)
*/
CPP20_DYNARRAY_CONSTEXPR dynarray(std::initializer_list<T> input_list, const allocator_type &other_allocator = allocator_type())
{
initialise(other_allocator);
allocate_array(input_list);
}
/*!
* @brief Create a multiple-dimensional array with initializer_list.
*
* @param input_listYour initializer_list
*/
template<typename Ty>
CPP20_DYNARRAY_CONSTEXPR dynarray(std::initializer_list<std::initializer_list<Ty>> input_list)
{
initialise();
allocate_array(input_list);
}
/*!
* @brief Create a multiple-dimensional array with initializer_list.
*
* @param input_listYour initializer_list
*/
template<typename Ty, typename ... Args>
CPP20_DYNARRAY_CONSTEXPR dynarray(std::initializer_list<std::initializer_list<Ty>> input_list, const allocator_type &other_allocator, Args&& ...args)
{
initialise(other_allocator);
allocate_array(input_list, std::forward<Args>(args)...);
}
/*!
* @brief Copy an existing dynarray.
*
* If using operator= in nested dynarray, the original structure will not change. Replace original values only.
*
* @param other The right side of '='
* @return A copied dynarray
*/
CPP20_DYNARRAY_CONSTEXPR dynarray& operator=(const dynarray &other) noexcept
{
loop_copy(other);
return *this;
}
/*!
* @brief Save an temporary created dynarray.
*
* If using operator= in nested dynarray, the original structure will not change. Replace original values only.
*
* @param other The right side of '='
* @return A new dynarray
*/
CPP20_DYNARRAY_CONSTEXPR dynarray& operator=(dynarray &&other) noexcept
{
if (entire_array_data == nullptr && this_level_array_head != nullptr)
move_values(other);
else
{
deallocate_array();
move_array(other);
}
return *this;
}
/*!
* @brief Erase existing data and construct a new dynarray with initializer_list.
*
* @param input_list Your initializer_list
* @return A new dynarray
*/
CPP20_DYNARRAY_CONSTEXPR dynarray& operator=(std::initializer_list<internal_value_type> input_list) noexcept
{
loop_copy(input_list);
return *this;
}
/*!
* @brief Erase existing data and construct a new dynarray with initializer_list.
*
* @param input_list Your initializer_list
* @return A new dynarray
*/
template<typename Ty>
CPP20_DYNARRAY_CONSTEXPR dynarray& operator=(std::initializer_list<std::initializer_list<Ty>> input_list) noexcept
{
loop_copy(input_list);
return *this;
}
/*!
* @brief Deconstruct.
*
*/
CPP20_DYNARRAY_CONSTEXPR ~dynarray()
{
deallocate_array();
}
// Element access
/*!
* @brief Returns a reference to the element at specified location pos, with bounds checking.
*
* If pos is not within the range of the container, an exception of type std::out_of_range is thrown.
*
* @param pos Position of the element to return
* @return Reference to the requested element
*/
CPP20_DYNARRAY_CONSTEXPR reference at(size_type pos);
/*!
* @brief Returns a const reference to the element at specified location pos, with bounds checking.
*
* If pos is not within the range of the container, an exception of type std::out_of_range is thrown.
*
* @param pos Position of the element to return
* @return Reference to the requested element
*/
CPP20_DYNARRAY_CONSTEXPR const_reference at(size_type pos) const;
/*!
* Returns a reference to the element at specified location pos. No bounds checking is performed.
*
* @param pos Position of the element to return
* @return Reference to the requested element
*/
CPP20_DYNARRAY_CONSTEXPR reference operator[](size_type pos);
/*!
* Returns a const reference to the element at specified location pos. No bounds checking is performed.
*
* @param pos Position of the element to return
* @return Reference to the requested element
*/
CPP20_DYNARRAY_CONSTEXPR const_reference operator[](size_type pos) const;
/*!
* @brief Returns a reference to the first element in the container.
*
* Calling front() on an empty container should be undefined. But for everyone's convenience, nullptr is returned here.
*
* @return Reference to the first element
*/
CPP20_DYNARRAY_CONSTEXPR reference front() { return (*this)[0]; }
/*!
* @brief Returns a const reference to the first element in the container.
*
* Calling front() on an empty container should be undefined. But for everyone's convenience, nullptr is returned here.
*
* @return Const reference to the first element
*/
CPP20_DYNARRAY_CONSTEXPR const_reference front() const { return (*this)[0]; }
/*!
* @brief Returns a reference to the last element in the container.
*
* Calling back() on an empty container should be undefined. But for everyone's convenience, nullptr is returned here.
*
* @return Reference to the last element
*/
CPP20_DYNARRAY_CONSTEXPR reference back();
/*!
* @brief Returns a const reference to the first element in the container.
*
* Calling back() on an empty container should be undefined. But for everyone's convenience, nullptr is returned here.
*
* @return Const reference to the first element
*/
CPP20_DYNARRAY_CONSTEXPR const_reference back() const;
/*!
* @brief Returns pointer to the innermost underlying array serving as element storage.
*
* For everyone's convenience, calling data() on an empty container will return nullptr.
*
* @return Pointer to the innermost underlying element storage.
* For non-empty containers, the returned pointer compares equal to the address of the first element.
*
*/
CPP20_DYNARRAY_CONSTEXPR internal_pointer_type data() noexcept { return this_level_array_head; }
/*!
* @brief Returns const pointer to the innermost underlying array serving as element storage.
*
* For everyone's convenience, calling data() on an empty container will return nullptr.
*
* @return Const pointer to the innermost underlying element storage.
* For non-empty containers, the returned pointer compares equal to the address of the first element.
*/
CPP20_DYNARRAY_CONSTEXPR const internal_pointer_type data() const noexcept { return this_level_array_head; }
/*!
* @brief Returns pointer to the underlying array serving as element storage.
*
* For everyone's convenience, calling data() on an empty container will return nullptr.
*
* @return Pointer to the underlying element storage.
* For non-empty containers, the returned pointer compares equal to the address of the first element.
*/
CPP20_DYNARRAY_CONSTEXPR pointer get() noexcept
{
if constexpr (std::is_same_v<T, internal_value_type>)
return this_level_array_head;
else
return current_dimension_array_data;
}
/*!
* @brief Returns const pointer to the underlying array serving as element storage.
*
* For everyone's convenience, calling data() on an empty container will return nullptr.
*
* @return Const pointer to the underlying element storage.
* For non-empty containers, the returned pointer compares equal to the address of the first element.
*/
CPP20_DYNARRAY_CONSTEXPR const pointer get() const noexcept
{
if constexpr (std::is_same_v<T, internal_value_type>)
return this_level_array_head;
else
return current_dimension_array_data;
}
/*!
* @brief Checks if the container has no elements.
* @return true if the container is empty, false otherwise
*/
CPP20_DYNARRAY_CONSTEXPR CPP20_DYNARRAY_NODISCARD bool empty() const noexcept { return !static_cast<bool>(size()); }
/*!
* @brief Returns the number of elements in the container.
* @return The number of elements in the container.
*/
CPP20_DYNARRAY_CONSTEXPR size_type size() const noexcept { return current_dimension_array_size; }
/*!
* @brief Returns the maximum number of elements the container is able to hold due to system or library implementation limitations.
*
* This value typically reflects the theoretical limit on the size of the container, at most std::numeric_limits<difference_type>::max().
* At runtime, the size of the container may be limited to a value smaller than max_size() by the amount of RAM available.
*
* @return Maximum number of elements.
*/
CPP20_DYNARRAY_CONSTEXPR size_type max_size() const noexcept { return std::numeric_limits<difference_type>::max(); }
/*!
* @brief Exchanges the contents of the container with those of other.
*
* Does not invoke any move, copy, or swap operations on individual elements.
*
* All iterators and references remain valid. The past-the-end iterator is invalidated.
*
* @param other dynarray to exchange the contents with
*/
CPP20_DYNARRAY_CONSTEXPR void swap(dynarray &other) noexcept;
/*!
* @brief Assigns the given value value to all elements in the container.
* @param value The value to assign to the elements
*/
CPP20_DYNARRAY_CONSTEXPR void fill(const internal_value_type& value);
// Iterators
/*!
* @brief Returns an iterator to the first element of the vector.
*
* If the vector is empty, the returned iterator will be equal to end().
*
* @return Iterator to the first element.
*/
CPP20_DYNARRAY_CONSTEXPR iterator begin() noexcept
{
if constexpr (std::is_same_v<T, internal_value_type>)
return iterator(this_level_array_head);
else
return iterator(current_dimension_array_data);
}
/*!
* @brief Returns an iterator to the first element of the vector.
*
* If the vector is empty, the returned iterator will be equal to end().
*
* @return Iterator to the first element.
*/
CPP20_DYNARRAY_CONSTEXPR const_iterator begin() const noexcept
{
if constexpr (std::is_same_v<T, internal_value_type>)
return const_iterator(this_level_array_head);
else
return const_iterator(current_dimension_array_data);
}
/*!
* @brief Returns an iterator to the first element of the vector.
*
* If the vector is empty, the returned iterator will be equal to end().
*
* @return Iterator to the first element.
*/
CPP20_DYNARRAY_CONSTEXPR const_iterator cbegin() const noexcept { return begin(); }
/*!
* @brief Returns an iterator to the element following the last element of the vector.
*
* This element acts as a placeholder; attempting to access it results in undefined behavior.
*
* @return Iterator to the element following the last element.
*/
CPP20_DYNARRAY_CONSTEXPR iterator end() noexcept
{
if constexpr (std::is_same_v<T, internal_value_type>)
return iterator(this_level_array_head + current_dimension_array_size);
else
return iterator(current_dimension_array_data + current_dimension_array_size);
}
/*!
* @brief Returns an iterator to the element following the last element of the vector.
*
* This element acts as a placeholder; attempting to access it results in undefined behavior.
*
* @return Iterator to the element following the last element.
*/
CPP20_DYNARRAY_CONSTEXPR const_iterator end() const noexcept
{
if constexpr (std::is_same_v<T, internal_value_type>)
return const_iterator(this_level_array_head + current_dimension_array_size);
else
return const_iterator(current_dimension_array_data + current_dimension_array_size);
}
/*!
* @brief Returns an iterator to the element following the last element of the vector.
*
* This element acts as a placeholder; attempting to access it results in undefined behavior.
*
* @return Iterator to the element following the last element.
*/
CPP20_DYNARRAY_CONSTEXPR const_iterator cend() const noexcept { return end(); }
/*!
* @brief Returns a reverse iterator to the first element of the reversed vector.
*
* It corresponds to the last element of the non-reversed vector. If the vector is empty, the returned iterator is equal to rend().
*
* @return Reverse iterator to the first element.
*/
CPP20_DYNARRAY_CONSTEXPR reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
/*!
* @brief Returns a reverse iterator to the first element of the reversed vector.
*
* It corresponds to the last element of the non-reversed vector. If the vector is empty, the returned iterator is equal to rend().
*
* @return Reverse iterator to the first element.
*/
CPP20_DYNARRAY_CONSTEXPR const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
/*!
* @brief Returns a reverse iterator to the first element of the reversed vector.
*
* It corresponds to the last element of the non-reversed vector. If the vector is empty, the returned iterator is equal to rend().
*
* @return Reverse iterator to the first element.
*/
CPP20_DYNARRAY_CONSTEXPR const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); }
/*!
* @brief Returns a reverse iterator to the element following the last element of the reversed vector.
*
* It corresponds to the element preceding the first element of the non-reversed vector.
* This element acts as a placeholder, attempting to access it results in undefined behavior.
*
* @return Reverse iterator to the element following the last element.
*/
CPP20_DYNARRAY_CONSTEXPR reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
/*!
* @brief Returns a reverse iterator to the element following the last element of the reversed vector.
*
* It corresponds to the element preceding the first element of the non-reversed vector.
* This element acts as a placeholder, attempting to access it results in undefined behavior.
*
* @return Reverse iterator to the element following the last element.
*/
CPP20_DYNARRAY_CONSTEXPR const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
/*!
* @brief Returns a reverse iterator to the element following the last element of the reversed vector.
*
* It corresponds to the element preceding the first element of the non-reversed vector.
* This element acts as a placeholder, attempting to access it results in undefined behavior.
*
* @return Reverse iterator to the element following the last element.
*/
CPP20_DYNARRAY_CONSTEXPR const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); }
private:
internal_pointer_type entire_array_data; // always nullptr in nested-dynarray
size_type current_dimension_array_size;
pointer current_dimension_array_data; // as node managers if not innermost layer; set as nullptr in innermost layer
internal_pointer_type this_level_array_head;
internal_pointer_type this_level_array_tail;
allocator_type array_allocator;
contiguous_allocator_type contiguous_allocator;
CPP20_DYNARRAY_CONSTEXPR void initialise(const allocator_type &other_allocator = allocator_type());
CPP20_DYNARRAY_CONSTEXPR void reset();
CPP20_DYNARRAY_CONSTEXPR size_type get_block_size() const
{
if (this_level_array_tail == this_level_array_head)
return true;
return static_cast<size_type>(this_level_array_tail - this_level_array_head + 1);
}
template<typename Ty>
static CPP20_DYNARRAY_CONSTEXPR size_type expand_list(std::initializer_list<Ty> init);
template<typename ... Args>
static CPP20_DYNARRAY_CONSTEXPR size_type expand_counts(size_type count, Args&& ... args);
template<typename ... Args>
static CPP20_DYNARRAY_CONSTEXPR contiguous_allocator_type expand_allocator(const allocator_type &_allocator, Args&& ... args);
template<typename Skip, typename ... Args>
static CPP20_DYNARRAY_CONSTEXPR contiguous_allocator_type expand_allocators(const Skip &ignore, const allocator_type &_allocator, Args&& ... args);
CPP20_DYNARRAY_CONSTEXPR void verify_size(size_type count);
CPP20_DYNARRAY_CONSTEXPR void allocate_array(size_type count);
CPP20_DYNARRAY_CONSTEXPR void allocate_array(internal_pointer_type starting_address, size_type count);
template<typename ...Args>
CPP20_DYNARRAY_CONSTEXPR void allocate_array(size_type count, Args&& ... args);
template<typename ...Args>
CPP20_DYNARRAY_CONSTEXPR void allocate_array(internal_pointer_type starting_address, size_type count, Args&& ... args);
template<typename _Alloc_t, typename = std::enable_if_t<std::is_same_v<std::decay_t<_Alloc_t>, allocator_type>>, typename ...Args>
CPP20_DYNARRAY_CONSTEXPR void allocate_array(size_type count, _Alloc_t &&other_allocator, Args&& ... args);
template<typename _Alloc_t, typename = std::enable_if_t<std::is_same_v<std::decay_t<_Alloc_t>, allocator_type>>, typename ...Args>
CPP20_DYNARRAY_CONSTEXPR void allocate_array(internal_pointer_type starting_address, size_type count, _Alloc_t &&other_allocator, Args&& ... args);
CPP20_DYNARRAY_CONSTEXPR void allocate_array(std::initializer_list<T> input_list);
template<typename Ty, typename ... Args>
CPP20_DYNARRAY_CONSTEXPR void allocate_array(std::initializer_list<std::initializer_list<Ty>> input_list, Args&& ...args);
template<typename Ty, typename ... Args>
CPP20_DYNARRAY_CONSTEXPR void allocate_array(internal_pointer_type starting_address, std::initializer_list<Ty> input_list,
const allocator_type &other_allocator = allocator_type(), Args&& ...args);
CPP20_DYNARRAY_CONSTEXPR void deallocate_array();
CPP20_DYNARRAY_CONSTEXPR void copy_array(const dynarray &other);
template<typename ...Args>
CPP20_DYNARRAY_CONSTEXPR void copy_array(const dynarray &other, const allocator_type &other_allocator, Args&& ... args);
CPP20_DYNARRAY_CONSTEXPR void copy_array(internal_pointer_type starting_address, const dynarray &other);
template<typename ...Args>
CPP20_DYNARRAY_CONSTEXPR void copy_array(internal_pointer_type starting_address, const dynarray &other,
const allocator_type &other_allocator, Args&& ... args);
template<typename InputIterator>
CPP20_DYNARRAY_CONSTEXPR void copy_array(InputIterator other_begin, InputIterator other_end);
CPP20_DYNARRAY_CONSTEXPR void loop_copy(const dynarray &other) noexcept;
CPP20_DYNARRAY_CONSTEXPR void loop_copy(std::initializer_list<internal_value_type> input_list) noexcept;
template<typename Ty>
CPP20_DYNARRAY_CONSTEXPR void loop_copy(std::initializer_list<std::initializer_list<Ty>> input_list) noexcept;
CPP20_DYNARRAY_CONSTEXPR void move_array(dynarray &other) noexcept;
CPP20_DYNARRAY_CONSTEXPR void move_values(dynarray &other) noexcept;
CPP20_DYNARRAY_CONSTEXPR void swap_all(dynarray &other) noexcept;
CPP20_DYNARRAY_CONSTEXPR void swap_outside(dynarray &other) noexcept;
/**** Non-member functions ***/
/*!
* @brief Exchanges the contents of the container with those of other.
*
* If both of the containers are outter-most layer, they will be swapped completely, including sizes.
*
* Otherwise swap the contents only.
*
* @param lhs A dynarray
* @param rhs Another dynarray
* @return
*/
friend CPP20_DYNARRAY_CONSTEXPR void swap(dynarray &lhs, dynarray &rhs) noexcept
{
lhs.swap_outside(rhs);
}
/*!
* @brief Replaces old_array with new_array and returns the old value of old_array.
*
* If old_array is an inner layer, exchange() will replaces the contents of the container only, the size of old_array will keeps unchanged.
*
* @param old_array Old array to be replaced
* @param new_array New array replace with
* @return The value of old_array
*/
friend CPP20_DYNARRAY_CONSTEXPR dynarray exchange(dynarray & old_array, dynarray && new_array) noexcept
{
dynarray current_array = std::move(old_array);
if (old_array.entire_array_data == nullptr && old_array.current_dimension_array_size > 0)
{
old_array.swap(new_array);
}
else
{
if (new_array.entire_array_data == nullptr && new_array.current_dimension_array_size > 0)
{
dynarray temp_array = std::move(new_array);
old_array.swap_all(temp_array);
}
else
{
old_array.swap_all(new_array);
}
}
return current_array;
}
friend CPP20_DYNARRAY_CONSTEXPR bool operator==(const dynarray &lhs, const dynarray &rhs)
{
return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
#ifdef DYNARRAY_USING_CPP20
friend CPP20_DYNARRAY_CONSTEXPR auto operator<=>(const dynarray &lhs, const dynarray &rhs)
{
return std::lexicographical_compare_three_way(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
#else
friend bool operator!=(const dynarray &lhs, const dynarray &rhs)
{
return !(lhs == rhs);
}
friend bool operator<(const dynarray &lhs, const dynarray &rhs)
{
return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
friend bool operator>(const dynarray &lhs, const dynarray &rhs)
{
return rhs < lhs;
}
friend bool operator<=(const dynarray &lhs, const dynarray &rhs)
{
return !(rhs < lhs);
}
friend bool operator>=(const dynarray &lhs, const dynarray &rhs)
{
return !(lhs < rhs);
}
#endif
};
template<typename T, template<typename U> typename _Allocator>
template<typename Ty>
inline CPP20_DYNARRAY_CONSTEXPR typename dynarray<T, _Allocator>::size_type
dynarray<T, _Allocator>::expand_list(std::initializer_list<Ty> init)
{
if constexpr (std::is_same_v<T, internal_value_type>)
{
return init.size();
}
else
{
size_type count = 0;
for (Ty sub_list : init)
count += T::expand_list(sub_list);
return count;
}
}
template<typename T, template<typename U> typename _Allocator>
template<typename ...Args>
inline CPP20_DYNARRAY_CONSTEXPR typename dynarray<T, _Allocator>::size_type
dynarray<T, _Allocator>::expand_counts(size_type count, Args && ...args)
{
if constexpr (std::is_same_v<T, internal_value_type> || sizeof...(args) == 0)
return count;
else
return T::expand_counts(std::forward<Args>(args)...) * count;
}
template<typename T, template<typename U> typename _Allocator>
template<typename ...Args>
inline CPP20_DYNARRAY_CONSTEXPR typename dynarray<T, _Allocator>::contiguous_allocator_type
dynarray<T, _Allocator>::expand_allocator(const allocator_type &_allocator, Args && ...args)
{
if constexpr (std::is_same_v<T, internal_value_type>)
return _allocator;
else if constexpr (sizeof...(args) == 0)
return contiguous_allocator_type();
else
return T::expand_allocator(std::forward<Args>(args)...);
}
template<typename T, template<typename U> typename _Allocator>
template<typename Skip, typename ...Args>
inline CPP20_DYNARRAY_CONSTEXPR typename dynarray<T, _Allocator>::contiguous_allocator_type
dynarray<T, _Allocator>::expand_allocators(const Skip &skip, const allocator_type &_allocator, Args && ...args)
{
if constexpr (std::is_same_v<T, internal_value_type>)
return _allocator;
else if constexpr (sizeof...(args) == 0)
return contiguous_allocator_type();
else
return T::expand_allocators(std::forward<Args>(args)...);
}
template<typename T, template<typename U> typename _Allocator>
inline CPP20_DYNARRAY_CONSTEXPR void
dynarray<T, _Allocator>::initialise(const allocator_type &other_allocator)
{
if constexpr (std::is_same_v<T, internal_value_type>)
contiguous_allocator = other_allocator;
else
contiguous_allocator = contiguous_allocator_type();
array_allocator = other_allocator;
reset();
}
template<typename T, template<typename U> typename _Allocator>
inline CPP20_DYNARRAY_CONSTEXPR void
dynarray<T, _Allocator>::reset()
{
entire_array_data = nullptr;
current_dimension_array_size = 0;
current_dimension_array_data = nullptr;
this_level_array_head = nullptr;
this_level_array_tail = nullptr;
}
template<typename T, template<typename U> typename _Allocator>
inline CPP20_DYNARRAY_CONSTEXPR void