-
Notifications
You must be signed in to change notification settings - Fork 0
/
lvt_impl.hpp
1280 lines (1104 loc) · 36 KB
/
lvt_impl.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
#ifndef LVT_IMPL_HPP
#define LVT_IMPL_HPP
template <typename T>
concept StringConvertible = std::is_convertible_v<T, std::string_view> ||
std::is_convertible_v<T, const char *> || std::is_convertible_v<T, char>;
template <typename T>
concept Comparable = requires(T a, T b) {
{
a < b
} -> std::convertible_to<bool>;
{
a == b
} -> std::convertible_to<bool>;
{
a != b
} -> std::convertible_to<bool>;
{
a > b
} -> std::convertible_to<bool>;
{
a <= b
} -> std::convertible_to<bool>;
{
a >= b
} -> std::convertible_to<bool>;
};
template <typename T>
concept Printable = requires(T a, std::ostream &os) {
{
os << a
} -> std::same_as<std::ostream &>;
};
template <typename R, typename T>
concept Matrix = std::convertible_to<std::ranges::range_reference_t<std::ranges::range_reference_t<R>>, T>;
template <typename T>
void printVec(std::vector<T> const &vec)
{
for (const auto &el : vec)
std::cout << el << ' ';
endl(std::cout);
}
template <typename T>
void print_range(std::span<const T> __range)
{
for (const auto &el : __range)
std::cout << el << ' ';
endl(std::cout);
}
template <typename Iter>
void print_range(Iter begin, Iter end)
{
for (auto iter{begin}; iter != end; ++iter)
std::cout << *iter << ' ';
endl(std::cout);
}
template <typename T>
void printMatrix(std::vector<std::vector<T>> const &matrix)
{
for (size_t row{}; row < matrix.size(); row++)
{
for (size_t col{}; col < matrix.at(row).size(); col++)
std::cout << matrix.at(row).at(col) << '\t';
std::endl(std::cout);
}
}
/**
* @brief Prints matrix
* @tparam matrix matrix to print
*/
template <Printable T>
void printMatrix(Matrix<T> auto const &matrix)
{
for (size_t i{}; i < matrix.size(); i++)
{
for (size_t j{}; j < matrix[i].size(); j++)
std::cout << matrix[i][j] << ' ';
std::endl(std::cout);
}
}
template <typename T1, typename T2>
void print_pair_vec(const std::vector<std::pair<T1, T2>> &__vec)
{
for (size_t index{0UL}; index < __vec.size(); index++)
{
std::cout << "array[" << __vec.at(index).first << "] = " << __vec.at(index).second << std::endl;
}
}
template <typename T1, typename T2>
void print_dictionary(const std::map<T1, T2> &__dictionary)
{
for (const auto &pair : __dictionary)
{
std::cout << pair.first << " - " << pair.second << '\n';
}
}
template <typename TupleType, size_t TupleSize>
void printTuple(const TupleType &t)
{
if constexpr (TupleSize > 1)
printTuple<TupleType, TupleSize - 1>(t);
std::cout << std::get<TupleSize - 1>(t) << std::endl;
}
template <typename T>
bool is_equal(const T &__num1, const T &__num2)
{
return (__num1 == __num2) ? true : false;
}
template <typename T>
bool is_lower(const T &__num1, const T &__num2)
{
return (__num1 < __num2) ? true : false;
}
template <typename T>
bool is_bigger(const T &__num1, const T &__num2)
{
return (__num1 > __num2) ? true : false;
}
bool is_uint_number(const std::string &__str)
{
// Iterator for '__str' variable
std::string::const_iterator it = __str.begin();
// Using 'while' loop to check if all symbols in string are digits
while (it != __str.end() && std::isdigit(*it))
{
// Incrementing iterator
++it;
}
return ((!__str.empty()) && (it == __str.end()));
}
bool is_int_number(const std::string &__str)
{
// Iterator for '__str' variable
std::string::const_iterator it = __str.begin();
unsigned int minus_count{0U};
// Iterating by symbols in string
// If symbol is minus -> incrementing counter of minuses
while (it != __str.end() && (*it == '-'))
{
minus_count++;
// Moving to next symbol
++it;
}
// Move iterator in the beginning of string
it = __str.begin();
if (minus_count <= 1U)
{
// Using 'while' loop to check if all symbols in string are digits
while (it != __str.end() && (std::isdigit(*it) || (*it == '-')))
{
// Incrementing iterator
++it;
}
}
return ((!__str.empty()) && (it == __str.end()));
}
bool is_floating_number(const std::string &__str)
{
// Iterator for '__str' variable
std::string::const_iterator it = __str.begin();
// Count of points in string (must be 1 point in number)
// Same thing with minus sign
unsigned int count_point{0U}, count_minus{0U};
// Iterating by symbols in string
while (it != __str.end())
{
// Counting all minuses in string
if ((*it == '-'))
count_minus++;
// Counting all points in string
if ((*it == '.'))
count_point++;
// Incrementing iterator
++it;
}
// Exception case when point stay in the beginning of string
if (__str.at(0UL) == '.')
return false;
// Moving iterator to the begin of string
it = __str.begin();
// If number contains less or 1 point or minus -> continue
if (count_point <= 1U && count_minus <= 1U)
{
// Using 'while' loop to check if all symbols in string are digits
while (it != __str.end() && (std::isdigit(*it) || (*it) == '.' || (*it) == '-'))
{
// Incrementing iterator
++it;
}
}
return ((!__str.empty()) && (it == __str.end()));
}
template <typename T>
consteval bool isArithmeticType([[maybe_unused]] const T &val) noexcept
{
if constexpr (std::is_integral_v<T> || std::is_signed_v<T> ||
std::is_unsigned_v<T> || std::is_floating_point_v<T>)
return true;
return false;
}
template <typename T>
T str_to_T(const std::string &__str)
{
T value;
std::istringstream iss(__str);
iss >> value;
return value;
}
template <typename T>
std::vector<T> matrixToArr(std::vector<std::vector<T>> const &matrix)
{
std::vector<T> arr(matrix.size() * matrix.at(0).size());
size_t idx{};
for (size_t row{}; row < matrix.size(); row++)
{
for (size_t col{}; col < matrix.at(row).size(); col++)
{
arr.at(idx) = matrix.at(row).at(col);
idx++;
}
}
return arr;
}
template <typename T>
std::string T_to_str(const T &__value)
{
std::ostringstream oss;
oss << __value;
return oss.str();
}
template <typename T>
std::vector<std::vector<T>> arrToMatrix(std::vector<T> const &arr, size_t rows, size_t cols)
{
if (arr.size() != rows * cols)
{
std::cout << "The size of array and matrix don't match. Returning empty matrix..." << std::endl;
return std::vector<std::vector<T>>{{}};
}
std::vector<std::vector<T>> matrix(rows, std::vector<T>(cols));
size_t idx{};
for (size_t row{}; row < rows && (idx < arr.size()); row++)
{
for (size_t col{}; col < cols; col++)
{
matrix.at(row).at(col) = arr.at(idx);
idx++;
}
}
return matrix;
}
template <typename T>
T input_to_uint(const char *__msg)
{
// User's input string
std::string users_input{};
// Using 'while' loop and string input to avoid errors
while (true)
{
std::cout << __msg;
std::cin >> users_input;
// If 'input' is contains only of digits -> break infinity loop
if (is_uint_number(users_input))
break;
// Else: print message and back to the begininng of 'while' loop
else
std::cout << "You entered not an unsigned integer number. Try again.\n";
}
// Converting user's string to numeric type to return it as a result
T num{str_to_T<T>(users_input)};
// Returning converted user's inputted value to 'T' type
return num;
}
template <typename T>
T input_to_int(const char *__msg)
{
// User's input string
std::string users_input{};
// Using 'while' loop and string input to avoid errors
while (true)
{
std::cout << __msg;
std::cin >> users_input;
// If 'input' is contains only of digits -> break infinity loop
if (is_int_number(users_input))
break;
// Else: print message and back to the begininng of 'while' loop
else
std::cout << "You entered not an signed integer number. Try again.\n";
}
// Converting user's string to numeric type to return it as a result
T num{str_to_T<T>(users_input)};
// Returning converted user's inputted value to 'T' type
return num;
}
template <typename T>
T input_to_floating(const char *__msg)
{
// User's input string
std::string users_input{};
// Using 'while' loop and string input to avoid errors
while (true)
{
std::cout << __msg;
std::cin >> users_input;
// If 'input' is contains only of digits -> break infinity loop
if (is_floating_number(users_input))
break;
// Else: print message and back to the begininng of 'while' loop
else
std::cout << "You entered not an floating number. Try again.\n";
}
// Converting user's string to numeric type to return it as a result
T num{str_to_T<T>(users_input)};
// Returning converted user's inputted value to 'T' type
return num;
}
template <typename T>
constexpr void bubbleSortAscending(std::vector<T> &arr)
{
for (size_t i{}; i < arr.size(); i++)
for (size_t j{}; j < arr.size(); j++)
if (arr.at(i) < arr.at(j))
std::swap(arr.at(i), arr.at(j));
}
template <typename T>
constexpr void bubbleSort2DAscending(std::vector<std::vector<T>> &matrix)
{
for (size_t row_1{}; row_1 < matrix.size(); row_1++)
for (size_t row_2{}; row_2 < matrix.size(); row_2++)
for (size_t col_1{}; col_1 < matrix.at(row_1).size(); col_1++)
for (size_t col_2{}; col_2 < matrix.at(row_2).size(); col_2++)
if (matrix.at(row_1).at(col_1) < matrix.at(row_2).at(col_2))
std::swap(matrix.at(row_1).at(col_1), matrix.at(row_2).at(col_2));
}
template <typename T>
constexpr void insertionSortAscending(std::vector<T> &vecToSort)
{
// Iterating by vector from 2nd element to end: [begin + 1; end]
for (size_t i{1UL}; i < vecToSort.size(); i++)
{
// Initializing position of previous element from 'i'
size_t j{i - 1UL};
// Initializing current value of vector
T value{vecToSort[i]};
// While position of prev element is lower than size of vector
// and element in this position is bigger than current value ->
// assigning it to next element (j + 1) of vector
while (j < vecToSort.size() && vecToSort[j] > value)
{
vecToSort[j + 1] = vecToSort[j];
j--;
}
// Assigning current element to next from previous
vecToSort[j + 1] = value;
}
}
template <typename T>
constexpr void insertionSort2DAscending(std::vector<std::vector<T>> &matrix)
{
std::vector<T> arr(matrixToArr(matrix));
for (size_t i{1}; i < arr.size(); i++)
{
T val{arr.at(i)};
size_t pos{i - 1};
while (pos < arr.size() && arr.at(pos) > val)
{
arr.at(pos + 1) = arr.at(pos);
pos--;
}
arr.at(pos + 1) = val;
}
matrix = arrToMatrix(arr, matrix.size(), matrix.at(0).size());
}
template <typename T>
constexpr void selectionSortAscending(std::vector<T> &vecToSort)
{
// Iterating over the range
for (size_t i{}; i < vecToSort.size(); i++)
{
// For example, minimal element is begin element of the vector
size_t minPos{i};
// Iterating over the unsorted range
for (size_t j{i + 1UL}; j < vecToSort.size(); j++)
{
// If element from the unsorted range is lower than the current ->
// assigning new position to 'minPos' variable
if (vecToSort.at(j) < vecToSort.at(minPos))
minPos = j;
}
// Swap minimal element with current
std::swap(vecToSort.at(i), vecToSort.at(minPos));
}
}
template <typename T>
constexpr void selectionSort2DAscending(std::vector<std::vector<T>> &matrix)
{
for (size_t row{}; row < matrix.size(); row++)
{
for (size_t col{}; col < matrix.at(row).size(); col++)
{
size_t minRow{row}, minCol{col};
T min{matrix.at(row).at(col)};
for (size_t j{col + 1}; j < matrix.at(row).size(); j++)
{
if (matrix.at(row).at(j) < min)
{
minRow = row;
minCol = j;
min = matrix.at(row).at(j);
}
}
for (size_t i{row + 1}; i < matrix.size(); i++)
{
for (size_t j{}; j < matrix.at(row).size(); j++)
{
if (matrix.at(i).at(j) < min)
{
minRow = i;
minCol = j;
min = matrix.at(i).at(j);
}
}
}
matrix.at(minRow).at(minCol) = matrix.at(row).at(col);
std::swap(matrix.at(row).at(col), min);
}
}
}
template <typename T>
constexpr void ShellSortAscending(std::vector<T> &arr)
{
for (size_t interval{arr.size() / 2}; interval > 0; interval /= 2)
{
for (size_t i{}; i < arr.size(); i++)
{
T val{arr.at(i)};
size_t j{};
for (j = i; (j >= interval) && (arr.at(j - interval) > val); j -= interval)
{
arr.at(j) = arr.at(j - interval);
}
arr.at(j) = val;
}
}
}
template <typename T>
constexpr void ShellSort2DAscending(std::vector<std::vector<T>> &matrix)
{
std::vector<int> arr(matrixToArr(matrix));
ShellSortAscending(arr);
matrix = arrToMatrix(arr, matrix.size(), matrix.at(0).size());
}
template <typename T>
constexpr void qSortAscending(std::vector<T> &arr, size_t low, size_t high)
{
size_t i{low}, j{high};
// Select pivot value
T pivot{arr.at((i + j) / 2)}, tmp{};
while (i <= j && i < arr.size() && j < arr.size())
{
while (arr.at(i) < pivot && i < arr.size())
i++;
while (arr.at(j) > pivot && j < arr.size())
j--;
if (i <= j && i < arr.size() && j < arr.size())
{
tmp = arr.at(i);
arr.at(i) = arr.at(j);
arr.at(j) = tmp;
i++;
j--;
}
}
// Recursive call sorting to left side from pivot
if (j > low && j < arr.size())
qSortAscending(arr, low, j);
// Recursive call sorting to right side from pivot
if (i < high && i < arr.size())
qSortAscending(arr, i, high);
}
template <typename T>
constexpr void quickSortAscending(std::vector<T> &arr)
{
qSortAscending(arr, 0, arr.size() - 1);
}
template <typename T>
constexpr void quickSort2DAscending(std::vector<std::vector<T>> &matrix)
{
std::vector<T> arr(matrixToArr(matrix));
qSortAscending(arr, 0, arr.size() - 1);
matrix = arrToMatrix(arr, matrix.size(), matrix.at(0).size());
}
template <typename T>
constexpr void bubbleSortDescending(std::vector<T> &arr)
{
for (size_t i{}; i < arr.size(); i++)
for (size_t j{}; j < arr.size(); j++)
if (arr.at(i) > arr.at(j))
std::swap(arr.at(i), arr.at(j));
}
template <typename T>
constexpr void bubbleSort2DDescending(std::vector<std::vector<T>> &matrix)
{
for (size_t row_1{}; row_1 < matrix.size(); row_1++)
for (size_t row_2{}; row_2 < matrix.size(); row_2++)
for (size_t col_1{}; col_1 < matrix.at(row_1).size(); col_1++)
for (size_t col_2{}; col_2 < matrix.at(row_2).size(); col_2++)
if (matrix.at(row_1).at(col_1) > matrix.at(row_2).at(col_2))
std::swap(matrix.at(row_1).at(col_1), matrix.at(row_2).at(col_2));
}
template <typename T>
constexpr void insertionSortDescending(std::vector<T> &arr)
{
// Iterating by vector from 2nd element to end: [begin + 1; end]
for (size_t i{1}; i < arr.size(); i++)
{
// Initializing current value of vector
T val{arr.at(i)};
// Initializing position of previous element from 'i'
size_t pos{i - 1};
// While position of prev element is lower than size of vector
// and element in this position is bigger than current value ->
// assigning it to next element (j + 1) of vector
while (pos < arr.size() && arr.at(pos) < val)
{
arr.at(pos + 1) = arr.at(pos);
pos--;
}
// Assigning current element to next from previous
arr.at(pos + 1) = val;
}
}
template <typename T>
constexpr void insertionSort2DDescending(std::vector<std::vector<T>> &matrix)
{
std::vector<T> arr(matrixToArr(matrix));
insertionSortDescending(arr);
matrix = arrToMatrix(arr, matrix.size(), matrix.at(0).size());
}
template <typename T>
constexpr void selectionSortDescending(std::vector<T> &vecToSort)
{
// Iterating over the range
for (size_t i{}; i < vecToSort.size(); i++)
{
// For example, minimal element is begin element of the vector
size_t maxPos{i};
// Iterating over the unsorted range
for (size_t j{i + 1UL}; j < vecToSort.size(); j++)
{
// If element from the unsorted range is lower than the current ->
// assigning new position to 'maxPos' variable
if (vecToSort.at(j) > vecToSort.at(maxPos))
maxPos = j;
}
// Swap minimal element with current
std::swap(vecToSort.at(i), vecToSort.at(maxPos));
}
}
template <typename T>
constexpr void selectionSort2DDescending(std::vector<std::vector<T>> &matrix)
{
for (size_t row{}; row < matrix.size(); row++)
{
for (size_t col{}; col < matrix.at(row).size(); col++)
{
size_t maxRow{row}, maxCol{col};
T max{matrix.at(row).at(col)};
for (size_t j{col + 1}; j < matrix.at(row).size(); j++)
{
if (matrix.at(row).at(j) > max)
{
maxRow = row;
maxCol = j;
max = matrix.at(row).at(j);
}
}
for (size_t i{row + 1}; i < matrix.size(); i++)
{
for (size_t j{}; j < matrix.at(row).size(); j++)
{
if (matrix.at(i).at(j) > max)
{
maxRow = i;
maxCol = j;
max = matrix.at(i).at(j);
}
}
}
matrix.at(maxRow).at(maxCol) = matrix.at(row).at(col);
std::swap(matrix.at(row).at(col), max);
}
}
}
template <typename T>
constexpr void ShellSortDescending(std::vector<T> &arr)
{
for (size_t interval{arr.size() / 2}; interval > 0; interval /= 2)
{
for (size_t i{}; i < arr.size(); i++)
{
T val{arr.at(i)};
size_t j{};
for (j = i; (j >= interval) && (arr.at(j - interval) < val); j -= interval)
{
arr.at(j) = arr.at(j - interval);
}
arr.at(j) = val;
}
}
}
template <typename T>
constexpr void ShellSort2DDescending(std::vector<std::vector<T>> &matrix)
{
std::vector<int> arr(matrixToArr(matrix));
ShellSortDescending(arr);
matrix = arrToMatrix(arr, matrix.size(), matrix.at(0).size());
}
template <typename T>
constexpr void qSortDescending(std::vector<T> &arr, size_t low, size_t high)
{
size_t i{low}, j{high};
// Select pivot value
T pivot{arr.at((i + j) / 2)}, tmp{};
while (i <= j && i < arr.size() && j < arr.size())
{
while (arr.at(i) > pivot && i < arr.size())
i++;
while (arr.at(j) < pivot && j < arr.size())
j--;
if (i <= j && i < arr.size() && j < arr.size())
{
tmp = arr.at(i);
arr.at(i) = arr.at(j);
arr.at(j) = tmp;
i++;
j--;
}
}
// Recursive call sorting to left side from pivot
if (j > low && j < arr.size())
qSortDescending(arr, low, j);
// Recursive call sorting to right side from pivot
if (i < high && i < arr.size())
qSortDescending(arr, i, high);
}
template <typename T>
constexpr void quickSortDescending(std::vector<T> &arr)
{
qSortDescending(arr, 0, arr.size() - 1);
}
template <typename T>
constexpr void quickSort2DDescending(std::vector<std::vector<T>> &matrix)
{
std::vector<T> arr(matrixToArr(matrix));
qSortDescending(arr, 0, arr.size() - 1);
matrix = arrToMatrix(arr, matrix.size(), matrix.at(0).size());
}
template <Comparable T>
constexpr void mergeSortAscendingHelper(std::vector<T> &arr, std::vector<T> &result, size_t start, size_t end)
{
// Base case: if array consist of 1 element - do nothing
if (end - start <= 1ul)
return;
// If array consist of 2 elements and it's need to swap them - swap them
if (end - start == 2ul)
{
if (result.at(start) > result.at(start + 1))
std::swap(result.at(start), result.at(start + 1));
return;
}
size_t middle{(start + end) / 2};
mergeSortHelper(result, arr, start, middle);
mergeSortHelper(result, arr, middle, end);
// Merging left and right subarrays
// 'k' - index for result array
size_t i{start}, j{middle}, k{start};
// Filling result subarray with sorted values
while (k < end)
{
if (j >= end || (i < middle && arr.at(i) < arr.at(j)))
{
// Filling with elements of left subarray
result.at(k) = arr.at(i);
i++;
}
else
{
// Filling with elements with right subarray
result.at(k) = arr.at(j);
j++;
}
k++;
}
}
template <Comparable T>
constexpr void mergeSortAscending(std::vector<T> &vec)
{
std::vector<T> copy(vec);
mergeSortAscendingHelper(copy, vec, 0, vec.size());
}
template <Comparable T>
constexpr void mergeSortDescendingHelper(std::vector<T> &arr, std::vector<T> &result, size_t start, size_t end)
{
// Base case: if array consist of 1 element - do nothing
if (end - start <= 1ul)
return;
// If array consist of 2 elements and it's need to swap them - swap them
if (end - start == 2ul)
{
if (result.at(start) < result.at(start + 1))
std::swap(result.at(start), result.at(start + 1));
return;
}
size_t middle{(start + end) / 2};
mergeSortHelper(result, arr, start, middle);
mergeSortHelper(result, arr, middle, end);
// Merging left and right subarrays
// 'k' - index for result array
size_t i{start}, j{middle}, k{start};
// Filling result subarray with sorted values
while (k < end)
{
if (j >= end || (i < middle && arr.at(i) > arr.at(j)))
{
// Filling with elements of left subarray
result.at(k) = arr.at(i);
i++;
}
else
{
// Filling with elements with right subarray
result.at(k) = arr.at(j);
j++;
}
k++;
}
}
template <Comparable T>
constexpr void mergeSortDescending(std::vector<T> &vec)
{
std::vector<T> copy(vec);
mergeSortDescendingHelper(copy, vec, 0, vec.size());
}
template <typename unumeric_t>
std::vector<int> split_number_on_digits(const unumeric_t &__number)
{
std::vector<int> digits;
unumeric_t number_copy{__number};
while (true)
{
digits.push_back(number_copy % 10);
number_copy /= 10;
if (number_copy == 0)
return digits;
}
}
template <typename T, typename retType>
retType composeNumberWithDigits(const std::vector<T> &vec)
{
std::vector<T> vecCopy{vec};
std::reverse(std::begin(vecCopy), std::end(vecCopy));
retType num{}, decimal{1};
for (const auto &digit : vecCopy)
{
num += digit * decimal;
decimal *= 10;
}
return num;
}
template <typename Iter, typename StartValue, typename Operation>
auto accumulateData(Iter begin, Iter end, StartValue start_value, Operation op)
{
auto accumulated{start_value};
for (Iter iter{begin}; iter != end; ++iter)
{
accumulated = op(accumulated, *iter);
}
return accumulated;
}
template <typename T>
void remove_same_elems(std::vector<T> &__range)
{
for (auto it{std::cbegin(__range)}; it != std::cend(__range); ++it)
{
while (((it + 1) != std::cend(__range)) && (*it == *(it + 1)))
__range.erase(it);
}
}
template <typename T1, typename T2>
std::vector<std::pair<T1, T2>> make_vector_of_pairs_by_two_vectors(const std::vector<T1> &__vec1,
const std::vector<T2> &__vec2)
{
std::vector<std::pair<T1, T2>> result_vec;
std::vector<T1> vec1(__vec1);
std::vector<T2> vec2(__vec2);
result_vec.reserve(__vec1.size());
std::transform(std::begin(vec1), std::end(vec1), std::begin(vec2), std::back_inserter(result_vec),
[](auto value1, auto value2)
{
return std::make_pair(value1, value2);
});
return result_vec;
}
template <typename InputIter, typename OutputIter, typename Predicate>
OutputIter find_all(InputIter first, InputIter last,
OutputIter dest, Predicate pred)
{
while (first not_eq last)
{
if (pred(*first))
{
*dest = first;
++dest;
}
++first;
}
return dest;
// Example:
// vector vec{3, 4, 5, 4, 5, 6, 5, 8};
// vector<vector<int>::iterator> matches;
// find_all(begin(vec), end(vec), back_inserter(matches),
// [](int i)
// { return i == 5; });
// cout << format("Found {} matching elements: ", matches.size()) << endl;
// for (const auto &it : matches)
// {
// cout << *it << " at position " << distance(begin(vec), it) << endl;
// }
}
template <typename T>
std::vector<T> sliceVector(std::span<const T> vec, const std::integral auto &first, const std::integral auto &last)
{
std::vector<T> res(std::cbegin(vec) + first, std::cbegin(vec) + last + 1);
return res;
}
template <typename T>
void sliceVector(std::vector<T> &vec, const std::integral auto &first, const std::integral auto &last)
{
vec = std::vector(std::cbegin(vec) + first, std::cbegin(vec) + last + 1);
vec.shrink_to_fit();
}
template <typename T>
T maxSubarraySum(std::span<const T> arr)
{
T max{0}, local{0};
if constexpr (std::is_integral_v<T> || std::is_signed_v<T> ||
std::is_unsigned_v<T> || std::is_floating_point_v<T>)
{
for (const auto &el : arr)
{
local += el;
if (local > max)
max = local;
if (local < 0)
local = 0;
}
return max;
}
}
template <typename T>
constexpr T mostFreqElem(std::span<T const> range)
{
int maxcount{};
T el_max_freq{};
for (auto it{range.begin()}; it != range.end(); ++it)
{
int count{};
for (auto jt{range.begin()}; jt != range.end(); ++jt)
if (*it == *jt)
count++;
if (count > maxcount)
{
maxcount = count;
el_max_freq = *it;
}
}
return el_max_freq;
}
template <typename T, std::integral U>