Back to home page

EIC code displayed by LXR

 
 

    


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 // Copyright (C) 2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
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 //  W A R N I N G
0013 //  -------------
0014 //
0015 // This file is not part of the Qt API. Types and functions defined in this
0016 // file can reliably be replaced by their std counterparts, once available.
0017 // You may use these definitions in your own code, but be aware that we
0018 // will remove them once Qt depends on the C++ version that supports
0019 // them in namespace std. There will be NO deprecation warning, the
0020 // definitions will JUST go away.
0021 //
0022 // If you can't agree to these terms, don't use these definitions!
0023 //
0024 // We mean it.
0025 //
0026 
0027 QT_BEGIN_NAMESPACE
0028 
0029 namespace q20 {
0030 // like std::<algorithm> (ie. not ranged, but constexpr)
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 // binary transform missing on purpose (no users)
0136 
0137 #endif
0138 }
0139 
0140 namespace q20::ranges {
0141 // like std::ranges::{any,all,none}_of, just unconstrained, so no range-overload
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 { // Niebloid
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 { // Niebloid
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 { // Niebloid
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 // __cpp_lib_ranges
0187 } // namespace q20::ranges
0188 
0189 QT_END_NAMESPACE
0190 
0191 #endif /* Q20ALGORITHM_H */