File indexing completed on 2026-10-02 09:16:52
0001
0002
0003
0004 #ifndef Q20ALGORITHM_H
0005 #define Q20ALGORITHM_H
0006
0007 #include <QtCore/qglobal.h>
0008
0009 #include <algorithm>
0010 #include <QtCore/q20functional.h>
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 QT_BEGIN_NAMESPACE
0029
0030 namespace q20 {
0031
0032 #ifdef __cpp_lib_constexpr_algorithms
0033 using std::copy;
0034 using std::copy_if;
0035 using std::copy_n;
0036 using std::fill;
0037 using std::fill_n;
0038 using std::is_sorted_until;
0039 using std::is_sorted;
0040 using std::transform;
0041 #else
0042 template <typename InputIterator, typename OutputIterator>
0043 constexpr OutputIterator
0044 copy(InputIterator first, InputIterator last, OutputIterator dest)
0045 {
0046 while (first != last) {
0047 *dest = *first;
0048 ++first;
0049 ++dest;
0050 }
0051 return dest;
0052 }
0053
0054 template <typename InputIterator, typename OutputIterator, typename UnaryPredicate>
0055 constexpr OutputIterator
0056 copy_if(InputIterator first, InputIterator last, OutputIterator dest, UnaryPredicate pred)
0057 {
0058 while (first != last) {
0059 if (pred(*first)) {
0060 *dest = *first;
0061 ++dest;
0062 }
0063 ++first;
0064 }
0065 return dest;
0066 }
0067
0068 template <typename InputIterator, typename Size, typename OutputIterator>
0069 constexpr OutputIterator
0070 copy_n(InputIterator first, Size n, OutputIterator dest)
0071 {
0072 while (n > Size{0}) {
0073 *dest = *first;
0074 ++first;
0075 ++dest;
0076 --n;
0077 }
0078 return dest;
0079 }
0080
0081 template <typename ForwardIterator, typename Value>
0082 constexpr void
0083 fill(ForwardIterator first, ForwardIterator last, const Value &value)
0084 {
0085 while (first != last) {
0086 *first = value;
0087 ++first;
0088 }
0089 }
0090
0091 template <typename OutputIterator, typename Size, typename Value>
0092 constexpr OutputIterator
0093 fill_n(OutputIterator first, Size n, const Value &value)
0094 {
0095 while (n > Size{0}) {
0096 *first = value;
0097 ++first;
0098 --n;
0099 }
0100 return first;
0101 }
0102
0103 template <typename ForwardIterator, typename BinaryPredicate = std::less<>>
0104 constexpr ForwardIterator
0105 is_sorted_until(ForwardIterator first, ForwardIterator last, BinaryPredicate p = {})
0106 {
0107 if (first == last)
0108 return first;
0109 auto prev = first;
0110 while (++first != last) {
0111 if (p(*first, *prev))
0112 return first;
0113 prev = first;
0114 }
0115 return first;
0116 }
0117
0118 template <typename ForwardIterator, typename BinaryPredicate = std::less<>>
0119 constexpr bool is_sorted(ForwardIterator first, ForwardIterator last, BinaryPredicate p = {})
0120 {
0121 return q20::is_sorted_until(first, last, p) == last;
0122 }
0123
0124 template <typename InputIterator, typename OutputIterator, typename UnaryFunction>
0125 constexpr OutputIterator
0126 transform(InputIterator first, InputIterator last, OutputIterator dest, UnaryFunction op)
0127 {
0128 while (first != last) {
0129 *dest = op(*first);
0130 ++first;
0131 ++dest;
0132 }
0133 return dest;
0134 }
0135
0136
0137
0138 #endif
0139 }
0140
0141 namespace q20::ranges {
0142
0143 #ifdef __cpp_lib_ranges
0144 using std::ranges::any_of;
0145 using std::ranges::all_of;
0146 using std::ranges::none_of;
0147 #else
0148 [[maybe_unused]] inline constexpr struct {
0149 template <typename InputIterator, typename Sentinel,
0150 typename Predicate, typename Projection = q20::identity>
0151 [[maybe_unused]] constexpr bool operator()(InputIterator first, Sentinel last, Predicate pred, Projection proj = {}) const
0152 {
0153 while (first != last) {
0154 if (std::invoke(pred, std::invoke(proj, *first)))
0155 return true;
0156 ++first;
0157 }
0158 return false;
0159 }
0160 } any_of;
0161 [[maybe_unused]] inline constexpr struct {
0162 template <typename InputIterator, typename Sentinel,
0163 typename Predicate, typename Projection = q20::identity>
0164 [[maybe_unused]] constexpr bool operator()(InputIterator first, Sentinel last, Predicate pred, Projection proj = {}) const
0165 {
0166 while (first != last) {
0167 if (!std::invoke(pred, std::invoke(proj, *first)))
0168 return false;
0169 ++first;
0170 }
0171 return true;
0172 }
0173 } all_of;
0174 [[maybe_unused]] inline constexpr struct {
0175 template <typename InputIterator, typename Sentinel,
0176 typename Predicate, typename Projection = q20::identity>
0177 [[maybe_unused]] constexpr bool operator()(InputIterator first, Sentinel last, Predicate pred, Projection proj = {}) const
0178 {
0179 while (first != last) {
0180 if (std::invoke(pred, std::invoke(proj, *first)))
0181 return false;
0182 ++first;
0183 }
0184 return true;
0185 }
0186 } none_of;
0187 #endif
0188 }
0189
0190 QT_END_NAMESPACE
0191
0192 #endif