File indexing completed on 2024-05-19 04:25:10

0001 /*
0002  *  SPDX-FileCopyrightText: 2022 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 #ifndef KISZUG_H
0007 #define KISZUG_H
0008 
0009 #include <QtGlobal>
0010 #include <type_traits>
0011 #include "KisMpl.h"
0012 #include <zug/transducer/map.hpp>
0013 #include <zug/reducing/last.hpp>
0014 
0015 /**
0016  * kiszug is a namespace extending the functionality of
0017  * zug library. It contains transducers, lenses and other
0018  * tools that are not present in zug itself.
0019  *
0020  * Naming convention exception:
0021  *
0022  * The namespace follows naming convention of zug library,
0023  * that is, all entities should be named in "snake_case"
0024  * manner.
0025  */
0026 
0027 namespace kiszug {
0028 
0029 template <typename T>
0030 constexpr auto map_static_cast = zug::map([](auto&& x) { return static_cast<T>(x); });
0031 
0032 template <typename T>
0033 constexpr auto map_mupliply = [] (T coeff) { return zug::map([coeff](auto&& x) { return x * coeff; }); };
0034 
0035 template <typename T>
0036 constexpr auto map_equal = [] (T value) { return zug::map([value](auto&& x) { return x == value; }); };
0037 
0038 template <typename T>
0039 constexpr auto map_greater = [] (T value) { return zug::map([value](auto&& x) { return x > value; }); };
0040 template <typename T>
0041 constexpr auto map_greater_equal = [] (T value) { return zug::map([value](auto&& x) { return x >= value; }); };
0042 
0043 template <typename T>
0044 constexpr auto map_less = [] (T value) { return zug::map([value](auto&& x) { return x < value; }); };
0045 
0046 template <typename T>
0047 constexpr auto map_less_equal = [] (T value) { return zug::map([value](auto&& x) { return x <= value; }); };
0048 
0049 template <>
0050 constexpr auto map_equal<qreal> =  [] (qreal value) { return zug::map([value](auto&& x) { return qFuzzyCompare(x, value); }); };
0051 
0052 template <>
0053 constexpr auto map_greater_equal<qreal> = [] (qreal value) { return zug::map([value](auto&& x) { return x >= value || qFuzzyCompare(x, value); }); };
0054 
0055 template <>
0056 constexpr auto map_less_equal<qreal> = [] (qreal value) { return zug::map([value](auto&& x) { return x <= value || qFuzzyCompare(x, value); }); };
0057 
0058 constexpr auto map_round = zug::map([](qreal x) -> int { return qRound(x); });
0059 
0060 struct empty_t
0061 {
0062 };
0063 
0064 constexpr auto to_functor = [] (auto f) {
0065     return
0066         [=] (auto &&x) {
0067             return f(zug::last)(empty_t{}, ZUG_FWD(x));
0068         };
0069 };
0070 
0071 constexpr auto foreach_tuple =
0072     [] (auto mapping) {
0073         return zug::map([=] (auto &&t) {
0074             return kismpl::apply_to_tuple(to_functor(mapping), ZUG_FWD(t));
0075         });
0076     };
0077 
0078 constexpr auto foreach_arg =
0079     [] (auto mapping) {
0080         return zug::map([=] (auto&&... t) {
0081             return std::make_tuple(zug::compat::invoke(to_functor(mapping), ZUG_FWD(t))...);
0082         });
0083     };
0084 
0085 } // namespace kiszug
0086 
0087 #endif // KISZUG_H