ORIGINAL
Loading...
Searching...
No Matches
algorithms.h
Go to the documentation of this file.
1#ifndef ALGORITHMS_H
2#define ALGORITHMS_H
3
4#include <functional>
5#include <cmath>
6#include "vector.h"
7#include "filter.h"
8#include "iterator.h"
9#include "types.h"
10#include "refCntPtr.h"
11
12
19
20namespace original
21{
22
34 class algorithms final
35 {
36 public:
37
45 template<typename TYPE>
46 static integer distance(const iterator<TYPE>& end, const iterator<TYPE>& begin);
47
55 template<typename TYPE>
57
65 template<typename TYPE>
67
78 template<typename TYPE, typename Callback>
80 static bool allOf(const iterator<TYPE>& begin, const iterator<TYPE>& end, const Callback& condition);
81
92 template<typename TYPE, typename Callback>
94 static bool anyOf(const iterator<TYPE>& begin, const iterator<TYPE>& end, const Callback& condition);
95
106 template<typename TYPE, typename Callback>
108 static bool noneOf(const iterator<TYPE>& begin, const iterator<TYPE>& end, const Callback& condition);
109
118 template<typename TYPE>
119 static strongPtr<iterator<TYPE>> find(const iterator<TYPE> &begin, const iterator<TYPE> &end, const TYPE &target);
120
130 template<typename TYPE>
131 static strongPtr<iterator<TYPE>> find(const iterator<TYPE> &begin, u_integer n, const TYPE &target);
132
143 template<typename TYPE, typename Callback>
146 const iterator<TYPE> &end, const Callback& condition);
147
157 template<typename TYPE, typename Callback>
159 static strongPtr<iterator<TYPE>> find(const iterator<TYPE> &begin, u_integer n, const Callback& condition);
160
169 template<typename TYPE>
170 static u_integer count(const iterator<TYPE>& begin, const iterator<TYPE>& end, const TYPE& target);
171
182 template<typename TYPE, typename Callback>
184 static u_integer count(const iterator<TYPE>& begin, const iterator<TYPE>& end, const Callback& condition);
185
195 template<typename TYPE>
196 static bool equal(const iterator<TYPE>& begin1, const iterator<TYPE>& end1,
197 const iterator<TYPE>& begin2, const iterator<TYPE>& end2);
198
209 template<typename TYPE, typename Callback>
211 static void forEach(const iterator<TYPE>& begin,
212 const iterator<TYPE>& end, Callback operation);
213
224 template<typename TYPE, typename Callback>
226 static strongPtr<iterator<TYPE>> forEach(const iterator<TYPE> &begin, u_integer n, Callback operation);
227
239 template<typename TYPE, typename Callback_O, typename Callback_C>
241 static void forEach(const iterator<TYPE>& begin, const iterator<TYPE>& end, Callback_O operation, const Callback_C& condition);
242
255 template<typename TYPE, typename Callback_O, typename Callback_C>
257 static strongPtr<iterator<TYPE>> forEach(const iterator<TYPE> &begin, u_integer n, Callback_O operation, const Callback_C& condition);
258
267 template<typename TYPE>
268 static void fill(const iterator<TYPE>& begin,
269 const iterator<TYPE>& end, const TYPE& value = TYPE{});
270
279 template<typename TYPE>
280 static strongPtr<iterator<TYPE>> fill(const iterator<TYPE> &begin, u_integer n, const TYPE &value = TYPE{});
281
289 template<typename TYPE>
290 static void swap(const iterator<TYPE>& it1, const iterator<TYPE>& it2) noexcept;
291
300 template<typename TYPE>
301 static strongPtr<iterator<TYPE>> copy(const iterator<TYPE> &begin_src, const iterator<TYPE> &end_src,
302 const iterator<TYPE> &begin_tar);
303
315 template<typename TYPE, typename Callback>
317 static strongPtr<iterator<TYPE>> copy(const iterator<TYPE> &begin_src, const iterator<TYPE> &end_src,
318 const iterator<TYPE> &begin_tar, Callback condition = filter<TYPE>{});
319
328 template<typename TYPE>
330
340 template<typename TYPE, typename Callback>
342 static bool compare(const iterator<TYPE>& it1, const iterator<TYPE>& it2, const Callback& compares);
343
353 template<typename TYPE, typename Callback>
355 static void heapAdjustDown(const iterator<TYPE>& begin, const iterator<TYPE>& range,
356 const iterator<TYPE>& current, const Callback& compares);
357
366 template<typename TYPE, typename Callback>
368 static void heapAdjustUp(const iterator<TYPE>& begin, const iterator<TYPE>& current,
369 const Callback& compares);
370
380 template<typename TYPE, typename Callback>
382 static void heapInit(const iterator<TYPE> &begin, const iterator<TYPE> &end,
383 const Callback& compares);
384
403 template<typename TYPE, typename Callback>
405 static void sort(const iterator<TYPE> &begin, const iterator<TYPE> &end,
406 const Callback& compares, bool isStable = false);
407
425 template<typename TYPE, typename Callback>
427 static void introSort(const iterator<TYPE> &begin, const iterator<TYPE> &end,
428 const Callback& compares);
429
443 template<typename TYPE, typename Callback>
445 static void stableSort(const iterator<TYPE>& begin, const iterator<TYPE>& end,
446 const Callback& compares);
447
461 template<typename TYPE, typename Callback>
463 static void heapSort(const iterator<TYPE> &begin, const iterator<TYPE> &end,
464 const Callback& compares);
465
478 template<typename TYPE, typename Callback>
480 static void insertionSort(const iterator<TYPE> &begin, const iterator<TYPE> &end,
481 const Callback& compares);
482 protected:
495 template<typename TYPE, typename Callback>
498 const iterator<TYPE>& parent, const Callback& compares);
499
511 template<typename TYPE, typename Callback>
513 static strongPtr<iterator<TYPE>> _introSortGetPivot(const iterator<TYPE>& begin, const iterator<TYPE>& end,
514 const Callback& compares);
515
527 template<typename TYPE, typename Callback>
529 static strongPtr<iterator<TYPE>> _introSortPartition(const iterator<TYPE>& begin, const iterator<TYPE>& end,
530 const Callback& compares);
531
543 template<typename TYPE, typename Callback>
545 static void _introSort(const iterator<TYPE> &begin, const iterator<TYPE> &end,
546 const Callback& compares, u_integer depth_limit);
547
562 template<typename TYPE, typename Callback>
564 static void _stableSortMerge(const iterator<TYPE>& begin, const iterator<TYPE>& mid,
565 const iterator<TYPE>& end, const Callback& compares);
566
581 template<typename TYPE, typename Callback>
583 static void _stableSort(const iterator<TYPE>& begin, const iterator<TYPE>& end,
584 const Callback& compares);
585
586 // ---- Implementation of pointer overload version ----
587
588 public:
592 template <typename TYPE>
593 static auto distance(const strongPtr<iterator<TYPE>> end, const strongPtr<iterator<TYPE>> begin) -> integer
594 {
595 return distance(*end, *begin);
596 }
597
601 template<typename TYPE>
603 return frontOf(*it, steps);
604 }
605
609 template<typename TYPE>
611 return backOf(*it, steps);
612 }
613
617 template<typename TYPE, typename Callback>
619 static auto allOf(const strongPtr<iterator<TYPE>> begin, const strongPtr<iterator<TYPE>> end,
620 const Callback &condition) -> bool {
621 return allOf(*begin, *end, condition);
622 }
623
627 template<typename TYPE, typename Callback>
629 static auto anyOf(const strongPtr<iterator<TYPE>> begin, const strongPtr<iterator<TYPE>> end,
630 const Callback &condition) -> bool {
631 return anyOf(*begin, *end, condition);
632 }
633
637 template<typename TYPE, typename Callback>
639 static auto noneOf(const strongPtr<iterator<TYPE>> begin, const strongPtr<iterator<TYPE>> end,
640 const Callback &condition) -> bool {
641 return noneOf(*begin, *end, condition);
642 }
643
647 template <typename TYPE>
648 static auto find(const strongPtr<iterator<TYPE>> begin, const strongPtr<iterator<TYPE>> end,
649 const TYPE& target) -> strongPtr<iterator<TYPE>> {
650 return find(*begin, *end, target);
651 }
652
656 template <typename TYPE>
657 static auto find(const strongPtr<iterator<TYPE>> begin, u_integer n,
658 const TYPE& target) -> strongPtr<iterator<TYPE>> {
659 return find(*begin, n, target);
660 }
661
665 template<typename TYPE, typename Callback>
667 static auto find(const strongPtr<iterator<TYPE>> begin, const strongPtr<iterator<TYPE>> end,
668 const Callback& condition) -> strongPtr<iterator<TYPE>> {
669 return find(*begin, *end, condition);
670 }
671
675 template <typename TYPE, typename Callback>
677 static auto find(const strongPtr<iterator<TYPE>> begin, u_integer n, const Callback& condition) -> strongPtr<iterator<TYPE>> {
678 return find(*begin, n, condition);
679 }
680
684 template <typename TYPE>
685 static auto count(const strongPtr<iterator<TYPE>> begin, const strongPtr<iterator<TYPE>> end,
686 const TYPE& target) -> u_integer {
687 return count(*begin, *end, target);
688 }
689
693 template <typename TYPE, typename Callback>
695 static auto count(const strongPtr<iterator<TYPE>> begin, const strongPtr<iterator<TYPE>> end,
696 const Callback& condition) -> u_integer {
697 return count(*begin, *end, condition);
698 }
699
703 template <typename TYPE>
704 static auto equal(const strongPtr<iterator<TYPE>> begin1, const strongPtr<iterator<TYPE>> end1,
705 const strongPtr<iterator<TYPE>> begin2, const strongPtr<iterator<TYPE>> end2) -> bool {
706 return equal(*begin1, *end1, *begin2, *end2);
707 }
708
712 template <typename TYPE, typename Callback>
714 static auto forEach(const strongPtr<iterator<TYPE>> begin, const strongPtr<iterator<TYPE>> end,
715 Callback operation) -> void {
716 forEach(*begin, *end, operation);
717 }
718
722 template <typename TYPE, typename Callback>
724 static auto forEach(const strongPtr<iterator<TYPE>> begin, u_integer n, Callback operation) -> strongPtr<iterator<TYPE>> {
725 return forEach(*begin, n, operation);
726 }
727
731 template<typename TYPE>
732 static auto fill(const strongPtr<iterator<TYPE>> begin, const strongPtr<iterator<TYPE>> end,
733 const TYPE& value) -> void {
734 fill(*begin, *end, value);
735 }
736
740 template<typename TYPE>
741 static auto fill(const strongPtr<iterator<TYPE>> begin, u_integer n, const TYPE& value) -> strongPtr<iterator<TYPE>> {
742 return fill(*begin, n, value);
743 }
744
748 template <typename TYPE>
749 static auto swap(const strongPtr<iterator<TYPE>> it1, const strongPtr<iterator<TYPE>> it2) noexcept -> void {
750 swap(*it1, *it2);
751 }
752
756 template <typename TYPE>
757 static auto copy(const strongPtr<iterator<TYPE>> begin_src, const strongPtr<iterator<TYPE>> end_src,
759 return copy(*begin_src, *end_src, *begin_tar);
760 }
761
765 template<typename TYPE, typename Callback>
767 static auto copy(const strongPtr<iterator<TYPE>> begin_src, const strongPtr<iterator<TYPE>> end_src,
768 const strongPtr<iterator<TYPE>> begin_tar, Callback condition) -> strongPtr<iterator<TYPE>> {
769 return copy(*begin_src, *end_src, *begin_tar, condition);
770 }
771
775 template<typename TYPE>
777 return reverse(*begin, *end);
778 }
779
783 template <typename TYPE, typename Callback>
785 static auto compare(const strongPtr<iterator<TYPE>> it1, const strongPtr<iterator<TYPE>> it2,
786 const Callback& compares) -> bool {
787 return compare(*it1, *it2, compares);
788 }
789
793 template<typename TYPE, typename Callback>
795 static auto heapAdjustDown(const strongPtr<iterator<TYPE>> begin, const strongPtr<iterator<TYPE>> range,
796 const strongPtr<iterator<TYPE>> current, const Callback& compares) -> void {
797 heapAdjustDown(*begin, *range, *current, compares);
798 }
799
803 template<typename TYPE, typename Callback>
805 static auto heapAdjustUp(const strongPtr<iterator<TYPE>> begin, const strongPtr<iterator<TYPE>> current,
806 const Callback& compares) -> void {
807 heapAdjustUp(*begin, *current, compares);
808 }
809
813 template<typename TYPE, typename Callback>
815 static auto heapInit(const strongPtr<iterator<TYPE>> begin, const strongPtr<iterator<TYPE>> end,
816 const Callback& compares) -> void {
817 heapInit(*begin, *end, compares);
818 }
819
823 template<typename TYPE, typename Callback>
825 static auto heapSort(const strongPtr<iterator<TYPE>> begin, const strongPtr<iterator<TYPE>> end,
826 const Callback& compares) -> void{
827 heapSort(*begin, *end, compares);
828 }
829
833 template<typename TYPE, typename Callback>
835 static auto insertionSort(const strongPtr<iterator<TYPE>> begin, const strongPtr<iterator<TYPE>> end,
836 const Callback& compares) -> void{
837 insertionSort(*begin, *end, compares);
838 }
839
840 protected:
844 template<typename TYPE, typename Callback>
846 static auto _heapGetPrior(const strongPtr<iterator<TYPE>> begin, const strongPtr<iterator<TYPE>> range,
847 const strongPtr<iterator<TYPE>> parent, const Callback& compares) -> strongPtr<iterator<TYPE>> {
848 return _heapGetPrior(*begin, *range, *parent, compares);
849 }
850 };
851}
852
853 template <typename TYPE>
854 auto original::algorithms::distance(const iterator<TYPE>& end, const iterator<TYPE>& begin) -> integer
855 {
856 auto it1 = strongPtr(end.clone());
857 auto it2 = strongPtr(begin.clone());
858 return *it1 - *it2;
859 }
860
861 template<typename TYPE>
862 original::strongPtr<original::iterator<TYPE>> original::algorithms::frontOf(const iterator<TYPE> &it, integer steps) {
863 return strongPtr(it + steps);
864 }
865
866 template<typename TYPE>
867 original::strongPtr<original::iterator<TYPE>> original::algorithms::backOf(const iterator<TYPE> &it, integer steps) {
868 return strongPtr(it - steps);
869 }
870
871 template<typename TYPE, typename Callback>
872 requires original::Condition<Callback, TYPE>
874 const Callback& condition) {
875 auto it = strongPtr(begin.clone());
876 for (; distance(*it, end) <= 0; it->next()){
877 if (!condition(it->get())){
878 return false;
879 }
880 }
881 return true;
882 }
883
884 template<typename TYPE, typename Callback>
887 const Callback& condition) {
888 auto it = strongPtr(begin.clone());
889 for (; distance(*it, end) <= 0; it->next()){
890 if (condition(it->get())){
891 return true;
892 }
893 }
894 return false;
895 }
896
897 template<typename TYPE, typename Callback>
900 const Callback& condition) {
901 auto it = strongPtr(begin.clone());
902 for (; distance(*it, end) <= 0; it->next()){
903 if (condition(it->get())){
904 return false;
905 }
906 }
907 return true;
908 }
909
910 template <typename TYPE>
911 auto original::algorithms::find(const iterator<TYPE>& begin, const iterator<TYPE>& end,
912 const TYPE& target) -> strongPtr<iterator<TYPE>> {
913 auto it = strongPtr(begin.clone());
914 while (it->isValid() && !it->equal(end)) {
915 if (it->get() == target) {
916 return it;
917 }
918 it->next();
919 }
920 return strongPtr(end.clone());
921 }
922
923 template <typename TYPE>
924 auto original::algorithms::find(const iterator<TYPE>& begin, const u_integer n, const TYPE& target) -> strongPtr<iterator<TYPE>> {
925 auto it = strongPtr(begin.clone());
926 for (u_integer i = 0; i < n; i += 1, it->next())
927 {
928 if (it->get() == target) return it;
929 }
930 return it;
931 }
932
933 template<typename TYPE, typename Callback>
934 requires original::Condition<Callback, TYPE>
935 auto original::algorithms::find(const iterator<TYPE> &begin, const iterator<TYPE> &end,
936 const Callback& condition) -> strongPtr<iterator<TYPE>> {
937 auto it = strongPtr(begin.clone());
938 while (it->isValid() && !it->equal(end)) {
939 if (condition(it->get())) {
940 return it;
941 }
942 it->next();
943 }
944 return strongPtr(end.clone());
945 }
946
947 template <typename TYPE, typename Callback>
948 requires original::Condition<Callback, TYPE>
949 auto original::algorithms::find(const iterator<TYPE>& begin, const u_integer n, const Callback& condition) -> strongPtr<iterator<TYPE>> {
950 auto it = strongPtr(begin.clone());
951 for (u_integer i = 0; i < n; i += 1, it->next())
952 {
953 if (condition(it->get())) return it;
954 }
955 return it;
956 }
957
958 template <typename TYPE>
959 auto original::algorithms::count(const iterator<TYPE>& begin, const iterator<TYPE>& end,
960 const TYPE& target) -> u_integer
961 {
962 u_integer cnt = 0;
963 auto it = strongPtr(begin.clone());
964 while (it->isValid() && distance(end, *it) != -1) {
965 if (it->get() == target) {
966 cnt += 1;
967 }
968 it->next();
969 }
970 return cnt;
971 }
972
973 template <typename TYPE, typename Callback>
974 requires original::Condition<Callback, TYPE>
975 auto original::algorithms::count(const iterator<TYPE>& begin, const iterator<TYPE>& end,
976 const Callback& condition) -> u_integer
977 {
978 u_integer cnt = 0;
979 auto it = strongPtr(begin.clone());
980 while (it->isValid() && distance(end, *it) != -1) {
981 if (condition(it->get())) {
982 cnt += 1;
983 }
984 it->next();
985 }
986 return cnt;
987 }
988
989 template <typename TYPE>
990 auto original::algorithms::equal(const iterator<TYPE>& begin1, const iterator<TYPE>& end1,
991 const iterator<TYPE>& begin2, const iterator<TYPE>& end2) -> bool
992 {
993 auto it1 = strongPtr(begin1.clone());
994 auto it2 = strongPtr(begin2.clone());
995
996 while (it1->isValid() && it2->isValid() && !it1->equal(end1) && !it2->equal(end2)) {
997 it1->next();
998 it2->next();
999 }
1000 const bool res = it1->equal(end1) && it2->equal(end2) && it1->get() == it2->get();
1001 return res;
1002 }
1003
1004 template <typename TYPE, typename Callback>
1005 requires original::Operation<Callback, TYPE>
1006 auto original::algorithms::forEach(const iterator<TYPE>& begin, const iterator<TYPE>& end,
1007 Callback operation) -> void
1008 {
1009 auto it = strongPtr(begin.clone());
1010 for (; !it->equal(end); it->next()) {
1011 operation(it->get());
1012 }
1013 operation(it->get());
1014 }
1015
1016 template <typename TYPE, typename Callback>
1017 requires original::Operation<Callback, TYPE>
1018 auto original::algorithms::forEach(const iterator<TYPE>& begin, const u_integer n,
1019 Callback operation) -> strongPtr<iterator<TYPE>> {
1020 auto it = strongPtr(begin.clone());
1021 for (u_integer i = 0; i < n; i += 1, it->next())
1022 {
1023 operation(it->get());
1024 }
1025 return it;
1026 }
1027
1028 template<typename TYPE, typename Callback_O, typename Callback_C>
1029 requires original::Operation<Callback_O, TYPE> && original::Condition<Callback_C, TYPE>
1030 auto original::algorithms::forEach(const iterator<TYPE> &begin, const iterator<TYPE> &end, Callback_O operation,
1031 const Callback_C &condition) -> void {
1032 auto it = strongPtr(begin.clone());
1033 for (; !it->equal(end); it->next()) {
1034 if (condition(it->get()))
1035 operation(it->get());
1036 }
1037
1038 if (condition(it->get()))
1039 operation(it->get());
1040 }
1041
1042 template<typename TYPE, typename Callback_O, typename Callback_C>
1043 requires original::Operation<Callback_O, TYPE> && original::Condition<Callback_C, TYPE>
1044 auto original::algorithms::forEach(const iterator<TYPE> &begin, const u_integer n, Callback_O operation,
1045 const Callback_C &condition) -> strongPtr<iterator<TYPE>> {
1046 auto it = strongPtr(begin.clone());
1047 for (u_integer i = 0; i < n; i += 1, it->next())
1048 {
1049 if (condition(it->get()))
1050 operation(it->get());
1051 }
1052 return it;
1053 }
1054
1055 template<typename TYPE>
1057 const iterator<TYPE>& end, const TYPE& value) -> void
1058 {
1059 auto it = strongPtr(begin.clone());
1060 while (!it->equal(end)){
1061 it->set(value);
1062 it->next();
1063 }
1064 it->set(value);
1065 }
1066
1067 template<typename TYPE>
1069 const u_integer n, const TYPE& value) -> strongPtr<iterator<TYPE>> {
1070 auto it = strongPtr(begin.clone());
1071 for (u_integer i = 0; i < n; ++i) {
1072 it->set(value);
1073 it->next();
1074 }
1075 return it;
1076 }
1077
1078 template <typename TYPE>
1079 auto original::algorithms::swap(const iterator<TYPE>& it1, const iterator<TYPE>& it2) noexcept -> void
1080 {
1081 auto it_1 = strongPtr(it1.clone());
1082 auto it_2 = strongPtr(it2.clone());
1083 TYPE tmp = it_2->get();
1084 it_2->set(it_1->get());
1085 it_1->set(tmp);
1086 }
1087
1088 template <typename TYPE>
1089 auto original::algorithms::copy(const iterator<TYPE>& begin_src, const iterator<TYPE>& end_src,
1090 const iterator<TYPE>& begin_tar) -> strongPtr<iterator<TYPE>> {
1091 auto it_src = strongPtr(begin_src.clone());
1092 auto it_tar = strongPtr(begin_tar.clone());
1093 while (!it_src->equal(end_src)){
1094 it_tar->set(it_src->get());
1095 it_src->next();
1096 it_tar->next();
1097 }
1098 it_tar->set(it_src->get());
1099 it_tar->next();
1100 return it_tar;
1101 }
1102
1103 template<typename TYPE, typename Callback>
1104 requires original::Condition<Callback, TYPE>
1105 auto original::algorithms::copy(const iterator<TYPE>& begin_src, const iterator<TYPE>& end_src,
1106 const iterator<TYPE>& begin_tar, Callback condition) -> strongPtr<iterator<TYPE>> {
1107 auto it_src = strongPtr(begin_src.clone());
1108 auto it_tar = strongPtr(begin_tar.clone());
1109 while (!it_src->equal(end_src)){
1110 if (condition(it_src->get())){
1111 it_tar->set(it_src->get());
1112 }
1113 it_src->next();
1114 it_tar->next();
1115 }
1116 if (condition(it_src->get())){
1117 it_tar->set(it_src->get());
1118 }
1119 it_tar->next();
1120 return it_tar;
1121 }
1122
1123 template<typename TYPE>
1126 auto left = strongPtr(begin.clone());
1127 auto right = strongPtr(end.clone());
1128 while (distance(right, left) > 0){
1129 swap(left, right);
1130 left->next();
1131 right->prev();
1132 }
1133 return left;
1134 }
1135
1136 template <typename TYPE, typename Callback>
1137 requires original::Compare<Callback, TYPE>
1139 const Callback& compares) -> bool
1140 {
1141 return compares(it1.get(), it2.get());
1142 }
1143
1144 template <typename TYPE, typename Callback>
1145 requires original::Compare<Callback, TYPE>
1147 const iterator<TYPE>& current, const Callback& compares) -> void
1148 {
1149 if (distance(current, begin) < 0)
1150 return;
1151
1152 auto it = strongPtr(current.clone());
1153 while ((distance(*it, begin) + 1) * 2 - 1 <= distance(range, begin))
1154 {
1155 auto child = _heapGetPrior(begin, range, *it, compares);
1156 if (compare(it, child, compares))
1157 {
1158 break;
1159 }
1160 swap(it, child);
1161 it = strongPtr(child->clone());
1162 }
1163 }
1164
1165 template <typename TYPE, typename Callback>
1166 requires original::Compare<Callback, TYPE>
1167 auto original::algorithms::heapAdjustUp(const iterator<TYPE>& begin, const iterator<TYPE>& current,
1168 const Callback& compares) -> void
1169 {
1170 auto it = strongPtr(current.clone());
1171 while (distance(*it, begin) > 0)
1172 {
1173 auto parent = frontOf(begin, (distance(*it, begin) + 1) / 2 - 1);
1174 if (compare(it, parent, compares))
1175 {
1176 swap(it, parent);
1177 it = strongPtr(parent->clone());
1178 }else
1179 {
1180 break;
1181 }
1182 }
1183 }
1184
1185 template <typename TYPE, typename Callback>
1186 requires original::Compare<Callback, TYPE>
1187 auto original::algorithms::heapInit(const iterator<TYPE>& begin, const iterator<TYPE>& end,
1188 const Callback& compares) -> void
1189 {
1190 auto it = frontOf(begin, (distance(end, begin) + 1) / 2 - 1);
1191 for (; distance(*it, begin) >= 0; it->prev())
1192 {
1193 heapAdjustDown(begin, end, *it, compares);
1194 }
1195 }
1196
1197 template<typename TYPE, typename Callback>
1198 requires original::Compare<Callback, TYPE>
1200 const Callback &compares, const bool isStable) {
1201 isStable ? stableSort(begin, end, compares) : introSort(begin, end, compares);
1202 }
1203
1204 template<typename TYPE, typename Callback>
1207 const Callback &compares) {
1208 if (const integer dis = distance(end, begin); dis <= 0)
1209 return;
1210
1211 u_integer depth_limit = 2 * std::log2(distance(end, begin));
1212 _introSort(begin, end, compares, depth_limit);
1213 }
1214
1215 template<typename TYPE, typename Callback>
1218 const Callback &compares) {
1219 _stableSort(begin, end, compares);
1220 }
1221
1222 template<typename TYPE, typename Callback>
1225 const Callback& compares) {
1226 if (distance(end, begin) <= 0)
1227 return;
1228
1229 heapInit(begin, end, std::not_fn(compares));
1230 auto right = strongPtr(end.clone());
1231 while (distance(*right, begin) > 0){
1232 swap(begin, *right);
1233 right->prev();
1234 heapAdjustDown(begin, *right, begin, std::not_fn(compares));
1235 }
1236 }
1237
1238 template <typename TYPE, typename Callback>
1241 const iterator<TYPE>& parent, const Callback& compares) -> strongPtr<iterator<TYPE>>
1242 {
1243 if ((distance(parent, begin) + 1) * 2 <= distance(range, begin))
1244 {
1245 auto left = frontOf(begin, (distance(parent, begin) + 1) * 2 - 1);
1246 auto right = frontOf(begin, (distance(parent, begin) + 1) * 2);
1247 if (compare(left, right, compares))
1248 {
1249 return left;
1250 }
1251 return right;
1252 }
1253 return frontOf(begin, (distance(parent, begin) + 1) * 2 - 1);
1254 }
1255
1256 template<typename TYPE, typename Callback>
1257 requires original::Compare<Callback, TYPE>
1258 original::strongPtr<original::iterator<TYPE>>
1259 original::algorithms::_introSortGetPivot(const iterator<TYPE> &begin, const iterator<TYPE> &end,
1260 const Callback &compares) {
1261 auto mid = frontOf(begin, distance(end, begin) / 2);
1262 if ((!compare(begin, *mid, compares) && !compare(end, begin, compares))
1263 || (!compare(*mid, begin, compares) && !compare(begin, end, compares))){
1264 return strongPtr(begin.clone());
1265 }
1266 if ((!compare(*mid, begin, compares) && !compare(end, *mid, compares))
1267 || (!compare(begin, *mid, compares) && !compare(*mid, end, compares))){
1268 return mid;
1269 }
1270 return strongPtr(end.clone());
1271 }
1272
1273 template<typename TYPE, typename Callback>
1274 requires original::Compare<Callback, TYPE>
1275 original::strongPtr<original::iterator<TYPE>>
1276 original::algorithms::_introSortPartition(const iterator<TYPE> &begin, const iterator<TYPE> &end,
1277 const Callback &compares) {
1278 auto left = strongPtr(begin.clone());
1279 auto right = strongPtr(end.clone());
1280 auto pivot = _introSortGetPivot(begin, end, compares);
1281 TYPE tmp = left->getElem();
1282 left->set(pivot->getElem());
1283 bool move_right = true;
1284 while (distance(right, left) > 0) {
1285 if (move_right){
1286 if (compare(right, left, std::not_fn(compares))){
1287 right->prev();
1288 } else{
1289 swap(left, right);
1290 move_right = false;
1291 }
1292 } else{
1293 if (compare(right, left, std::not_fn(compares))){
1294 left->next();
1295 } else{
1296 swap(left, right);
1297 move_right = true;
1298 }
1299 }
1300 }
1301 left->set(tmp);
1302 return left;
1303 }
1304
1305 template<typename TYPE, typename Callback>
1306 requires original::Compare<Callback, TYPE>
1307 void
1308 original::algorithms::_introSort(const iterator<TYPE> &begin, const iterator<TYPE> &end,
1309 const Callback &compares, u_integer depth_limit) {
1310 if (distance(end, begin) <= 16) {
1311 insertionSort(begin, end, compares);
1312 return;
1313 }
1314
1315 if (depth_limit == 0) {
1316 heapSort(begin, end, compares);
1317 return;
1318 }
1319
1320 auto pivot = _introSortPartition(begin, end, compares);
1321 _introSort(begin, *pivot, compares, depth_limit - 1);
1322 _introSort(*pivot, end, compares, depth_limit - 1);
1323 }
1324
1325 template<typename TYPE, typename Callback>
1326 requires original::Compare<Callback, TYPE>
1328 const Callback &compares) {
1329 if (distance(end, begin) <= 0)
1330 return;
1331
1332 auto left = frontOf(begin, 1);
1333 while (distance(end, *left) >= 0) {
1334 auto current = strongPtr(left->clone());
1335 auto prev = backOf(current, 1);
1336 while (distance(*current, begin) > 0 && compare(current, prev, compares)){
1337 swap(current, prev);
1338 current->prev();
1339 prev->prev();
1340 }
1341 left->next();
1342 }
1343 }
1344
1345 template<typename TYPE, typename Callback>
1348 const iterator<TYPE> &end, const Callback &compares) {
1349 vector<TYPE> tmp;
1350 auto left = strongPtr(begin.clone());
1351 auto right = strongPtr(mid.clone());
1352
1353 while (distance(mid, *left) > 0 && distance(end, *right) > 0){
1354 if (compare(left, right, compares)){
1355 tmp.pushEnd(left->get());
1356 left->next();
1357 } else{
1358 tmp.pushEnd(right->get());
1359 right->next();
1360 }
1361 }
1362
1363 while (distance(mid, *left) > 0){
1364 tmp.pushEnd(left->get());
1365 left->next();
1366 }
1367
1368 while (distance(end, *right) > 0){
1369 tmp.pushEnd(right->get());
1370 right->next();
1371 }
1372
1373 copy(tmp.first(), tmp.last(), begin);
1374 }
1375
1376 template<typename TYPE, typename Callback>
1379 const Callback &compares) {
1380 const integer dis = distance(end, begin);
1381
1382 if (dis <= 1)
1383 return;
1384
1385 if (dis <= 16){
1386 insertionSort(begin, end, compares);
1387 return;
1388 }
1389
1390 auto mid = frontOf(begin, dis / 2);
1391 _stableSort(begin, *mid, compares);
1392 _stableSort(*mid, end, compares);
1393 _stableSortMerge(begin, *mid, end, compares);
1394 }
1395
1396#endif // ALGORITHMS_H
Utility class containing generic container algorithms.
Definition algorithms.h:35
static auto frontOf(const strongPtr< iterator< TYPE > > it, integer steps) -> strongPtr< iterator< TYPE > >
Pointer overload version of frontOf()
Definition algorithms.h:602
static auto copy(const strongPtr< iterator< TYPE > > begin_src, const strongPtr< iterator< TYPE > > end_src, const strongPtr< iterator< TYPE > > begin_tar) -> strongPtr< iterator< TYPE > >
Pointer overload version of copy()
Definition algorithms.h:757
static auto _heapGetPrior(const strongPtr< iterator< TYPE > > begin, const strongPtr< iterator< TYPE > > range, const strongPtr< iterator< TYPE > > parent, const Callback &compares) -> strongPtr< iterator< TYPE > >
Pointer overload version of _heapGetPrior()
Definition algorithms.h:846
static void fill(const iterator< TYPE > &begin, const iterator< TYPE > &end, const TYPE &value=TYPE{})
Fills a range with a specific value.
static bool anyOf(const iterator< TYPE > &begin, const iterator< TYPE > &end, const Callback &condition)
Checks if any elements satisfy condition.
Definition algorithms.h:886
static auto backOf(const strongPtr< iterator< TYPE > > it, integer steps) -> strongPtr< iterator< TYPE > >
Pointer overload version of backOf()
Definition algorithms.h:610
static auto find(const strongPtr< iterator< TYPE > > begin, const strongPtr< iterator< TYPE > > end, const TYPE &target) -> strongPtr< iterator< TYPE > >
Pointer overload version of find()
Definition algorithms.h:648
static integer distance(const iterator< TYPE > &end, const iterator< TYPE > &begin)
Calculates distance between two iterators.
static void heapInit(const iterator< TYPE > &begin, const iterator< TYPE > &end, const Callback &compares)
Initializes a heap structure from a range of iterators.
static strongPtr< iterator< TYPE > > _heapGetPrior(const iterator< TYPE > &begin, const iterator< TYPE > &range, const iterator< TYPE > &parent, const Callback &compares)
Get parent node's priority child in heap structure.
static auto heapInit(const strongPtr< iterator< TYPE > > begin, const strongPtr< iterator< TYPE > > end, const Callback &compares) -> void
Pointer overload version of heapInit()
Definition algorithms.h:815
static void forEach(const iterator< TYPE > &begin, const iterator< TYPE > &end, Callback_O operation, const Callback_C &condition)
Apply operation to elements with condition check.
static auto heapAdjustUp(const strongPtr< iterator< TYPE > > begin, const strongPtr< iterator< TYPE > > current, const Callback &compares) -> void
Pointer overload version of heapAdjustUp()
Definition algorithms.h:805
static auto find(const strongPtr< iterator< TYPE > > begin, u_integer n, const Callback &condition) -> strongPtr< iterator< TYPE > >
Pointer overload version of find()
Definition algorithms.h:677
static auto allOf(const strongPtr< iterator< TYPE > > begin, const strongPtr< iterator< TYPE > > end, const Callback &condition) -> bool
Pointer overload version of allOf()
Definition algorithms.h:619
static bool equal(const iterator< TYPE > &begin1, const iterator< TYPE > &end1, const iterator< TYPE > &begin2, const iterator< TYPE > &end2)
Checks if two iterator ranges are equal.
static auto forEach(const strongPtr< iterator< TYPE > > begin, const strongPtr< iterator< TYPE > > end, Callback operation) -> void
Pointer overload version of forEach()
Definition algorithms.h:714
static auto swap(const strongPtr< iterator< TYPE > > it1, const strongPtr< iterator< TYPE > > it2) noexcept -> void
Pointer overload version of swap()
Definition algorithms.h:749
static strongPtr< iterator< TYPE > > backOf(const iterator< TYPE > &it, integer steps)
Gets iterator n steps backward.
static auto insertionSort(const strongPtr< iterator< TYPE > > begin, const strongPtr< iterator< TYPE > > end, const Callback &compares) -> void
Pointer overload version of insertionSort()
Definition algorithms.h:835
static void _stableSortMerge(const iterator< TYPE > &begin, const iterator< TYPE > &mid, const iterator< TYPE > &end, const Callback &compares)
Merges two sorted sub-ranges during merge sort.
Definition algorithms.h:1347
static auto equal(const strongPtr< iterator< TYPE > > begin1, const strongPtr< iterator< TYPE > > end1, const strongPtr< iterator< TYPE > > begin2, const strongPtr< iterator< TYPE > > end2) -> bool
Pointer overload version of equal()
Definition algorithms.h:704
static void sort(const iterator< TYPE > &begin, const iterator< TYPE > &end, const Callback &compares, bool isStable=false)
Sorts a range of elements using either stable or unstable sorting algorithm.
Definition algorithms.h:1199
static auto forEach(const strongPtr< iterator< TYPE > > begin, u_integer n, Callback operation) -> strongPtr< iterator< TYPE > >
Pointer overload version of forEach()
Definition algorithms.h:724
static auto fill(const strongPtr< iterator< TYPE > > begin, u_integer n, const TYPE &value) -> strongPtr< iterator< TYPE > >
Pointer overload version of fill()
Definition algorithms.h:741
static auto count(const strongPtr< iterator< TYPE > > begin, const strongPtr< iterator< TYPE > > end, const TYPE &target) -> u_integer
Pointer overload version of count()
Definition algorithms.h:685
static strongPtr< iterator< TYPE > > fill(const iterator< TYPE > &begin, u_integer n, const TYPE &value=TYPE{})
Fill range with value (fixed number of elements)
static auto reverse(const strongPtr< iterator< TYPE > > begin, const strongPtr< iterator< TYPE > > end) -> strongPtr< iterator< TYPE > >
Pointer overload version of reverse()
Definition algorithms.h:776
static auto heapAdjustDown(const strongPtr< iterator< TYPE > > begin, const strongPtr< iterator< TYPE > > range, const strongPtr< iterator< TYPE > > current, const Callback &compares) -> void
Pointer overload version of heapAdjustDown()
Definition algorithms.h:795
static strongPtr< iterator< TYPE > > copy(const iterator< TYPE > &begin_src, const iterator< TYPE > &end_src, const iterator< TYPE > &begin_tar, Callback condition=filter< TYPE >{})
Conditional copy with filtering.
static void swap(const iterator< TYPE > &it1, const iterator< TYPE > &it2) noexcept
Swaps the values of two elements.
static void heapAdjustUp(const iterator< TYPE > &begin, const iterator< TYPE > &current, const Callback &compares)
Adjusts a heap structure upwards from a given iterator.
static strongPtr< iterator< TYPE > > find(const iterator< TYPE > &begin, u_integer n, const TYPE &target)
Find first element satisfying condition in iterator range.
static strongPtr< iterator< TYPE > > find(const iterator< TYPE > &begin, u_integer n, const Callback &condition)
Fill range with value (fixed number of elements)
static void introSort(const iterator< TYPE > &begin, const iterator< TYPE > &end, const Callback &compares)
Sorts a range of elements using introspective sort.
Definition algorithms.h:1206
static auto distance(const strongPtr< iterator< TYPE > > end, const strongPtr< iterator< TYPE > > begin) -> integer
Pointer overload version of distance()
Definition algorithms.h:593
static auto anyOf(const strongPtr< iterator< TYPE > > begin, const strongPtr< iterator< TYPE > > end, const Callback &condition) -> bool
Pointer overload version of anyOf()
Definition algorithms.h:629
static auto compare(const strongPtr< iterator< TYPE > > it1, const strongPtr< iterator< TYPE > > it2, const Callback &compares) -> bool
Pointer overload version of compare()
Definition algorithms.h:785
static void heapSort(const iterator< TYPE > &begin, const iterator< TYPE > &end, const Callback &compares)
Sorts a range of elements using heap sort.
Definition algorithms.h:1224
static void forEach(const iterator< TYPE > &begin, const iterator< TYPE > &end, Callback operation)
Applies an operation to each element in the range.
static strongPtr< iterator< TYPE > > copy(const iterator< TYPE > &begin_src, const iterator< TYPE > &end_src, const iterator< TYPE > &begin_tar)
Copies a range of elements from one iterator to another.
static auto find(const strongPtr< iterator< TYPE > > begin, u_integer n, const TYPE &target) -> strongPtr< iterator< TYPE > >
Pointer overload version of find()
Definition algorithms.h:657
static strongPtr< iterator< TYPE > > reverse(const iterator< TYPE > &begin, const iterator< TYPE > &end)
Range-based reverse operation.
static strongPtr< iterator< TYPE > > forEach(const iterator< TYPE > &begin, u_integer n, Callback operation)
Applies an operation to the first n elements in the range.
static strongPtr< iterator< TYPE > > find(const iterator< TYPE > &begin, const iterator< TYPE > &end, const TYPE &target)
Find first occurrence of target in iterator range.
static void insertionSort(const iterator< TYPE > &begin, const iterator< TYPE > &end, const Callback &compares)
Sorts a range of elements using insertion sort.
Definition algorithms.h:1327
static auto heapSort(const strongPtr< iterator< TYPE > > begin, const strongPtr< iterator< TYPE > > end, const Callback &compares) -> void
Pointer overload version of heapSort()
Definition algorithms.h:825
static auto fill(const strongPtr< iterator< TYPE > > begin, const strongPtr< iterator< TYPE > > end, const TYPE &value) -> void
Pointer overload version of fill()
Definition algorithms.h:732
static void stableSort(const iterator< TYPE > &begin, const iterator< TYPE > &end, const Callback &compares)
Performs stable hybrid sort on element range.
Definition algorithms.h:1217
static auto count(const strongPtr< iterator< TYPE > > begin, const strongPtr< iterator< TYPE > > end, const Callback &condition) -> u_integer
Pointer overload version of count()
Definition algorithms.h:695
static bool compare(const iterator< TYPE > &it1, const iterator< TYPE > &it2, const Callback &compares)
Compares two elements using a comparison callback.
static strongPtr< iterator< TYPE > > frontOf(const iterator< TYPE > &it, integer steps)
Gets iterator n steps forward.
static auto find(const strongPtr< iterator< TYPE > > begin, const strongPtr< iterator< TYPE > > end, const Callback &condition) -> strongPtr< iterator< TYPE > >
Pointer overload version of find()
Definition algorithms.h:667
static strongPtr< iterator< TYPE > > forEach(const iterator< TYPE > &begin, u_integer n, Callback_O operation, const Callback_C &condition)
Conditional forEach with element count limit.
static bool allOf(const iterator< TYPE > &begin, const iterator< TYPE > &end, const Callback &condition)
Checks if all elements satisfy condition.
Definition algorithms.h:873
static void heapAdjustDown(const iterator< TYPE > &begin, const iterator< TYPE > &range, const iterator< TYPE > &current, const Callback &compares)
Adjusts a heap structure downwards starting from a given iterator.
static u_integer count(const iterator< TYPE > &begin, const iterator< TYPE > &end, const TYPE &target)
Counts occurrences of a target element in the range.
static void _stableSort(const iterator< TYPE > &begin, const iterator< TYPE > &end, const Callback &compares)
Recursive implementation of stable sort.
Definition algorithms.h:1378
static strongPtr< iterator< TYPE > > find(const iterator< TYPE > &begin, const iterator< TYPE > &end, const Callback &condition)
Find first element satisfying condition in iterator range.
static auto noneOf(const strongPtr< iterator< TYPE > > begin, const strongPtr< iterator< TYPE > > end, const Callback &condition) -> bool
Pointer overload version of noneOf()
Definition algorithms.h:639
static bool noneOf(const iterator< TYPE > &begin, const iterator< TYPE > &end, const Callback &condition)
Checks if no elements satisfy condition.
Definition algorithms.h:899
static auto copy(const strongPtr< iterator< TYPE > > begin_src, const strongPtr< iterator< TYPE > > end_src, const strongPtr< iterator< TYPE > > begin_tar, Callback condition) -> strongPtr< iterator< TYPE > >
Pointer overload version of copy()
Definition algorithms.h:767
static u_integer count(const iterator< TYPE > &begin, const iterator< TYPE > &end, const Callback &condition)
Counts occurrences of elements satisfying condition in the range.
Base class for filter operations.
Definition filter.h:31
iterAdaptor first()
Returns an iterator pointing to the first element.
Definition iterable.h:472
iterAdaptor last()
Returns an iterator pointing to the last element.
Definition iterable.h:478
Base iterator interface that supports common operations for iteration.
Definition iterator.h:35
iterator * clone() const override=0
Creates a clone of the iterator.
Shared ownership smart pointer with strong references.
Definition refCntPtr.h:100
Dynamic array container with amortized constant time operations.
Definition vector.h:42
void pushEnd(const TYPE &e) override
Inserts an element at the end of the vector.
Definition vector.h:656
Combines Comparable and CallbackOf for comparison callbacks.
Definition types.h:76
Constraint for predicate callbacks.
Definition types.h:97
Constraint for mutating operations.
Definition types.h:117
Filter base class and derived filter classes for various matching operations.
Defines the iterator class for traversing and manipulating container elements.
Main namespace for the project Original.
Definition algorithms.h:21
std::uint32_t u_integer
32-bit unsigned integer type for sizes/indexes
Definition config.h:17
std::int64_t integer
64-bit signed integer type for arithmetic operations
Definition config.h:15
Reference-counted smart pointer hierarchy.
Type system foundations and concept definitions.
Dynamic array container with automatic resizing.