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