File indexing completed on 2024-04-28 05:46:50

0001 /*****************************************************************************
0002  *   Copyright 2013 - 2015 Yichao Yu <yyc1992@gmail.com>                     *
0003  *                                                                           *
0004  *   This program is free software; you can redistribute it and/or modify    *
0005  *   it under the terms of the GNU Lesser General Public License as          *
0006  *   published by the Free Software Foundation; either version 2.1 of the    *
0007  *   License, or (at your option) version 3, or any later version accepted   *
0008  *   by the membership of KDE e.V. (or its successor approved by the         *
0009  *   membership of KDE e.V.), which shall act as a proxy defined in          *
0010  *   Section 6 of version 3 of the license.                                  *
0011  *                                                                           *
0012  *   This program is distributed in the hope that it will be useful,         *
0013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
0014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       *
0015  *   Lesser General Public License for more details.                         *
0016  *                                                                           *
0017  *   You should have received a copy of the GNU Lesser General Public        *
0018  *   License along with this library. If not,                                *
0019  *   see <http://www.gnu.org/licenses/>.                                     *
0020  *****************************************************************************/
0021 #ifndef _QTC_UTILS_NUMBER_H_
0022 #define _QTC_UTILS_NUMBER_H_
0023 
0024 #include "utils.h"
0025 
0026 #include <cmath>
0027 #include <cstdlib>
0028 
0029 template <typename T1, typename T2>
0030 static inline auto
0031 qtcMax(const T1 &a, const T2 &b) -> decltype((a > b) ? a : b)
0032 {
0033     return (a > b) ? a : b;
0034 }
0035 template <typename T1, typename T2>
0036 static inline auto
0037 qtcMin(const T1 &a, const T2 &b) -> decltype((a < b) ? a : b)
0038 {
0039     return (a < b) ? a : b;
0040 }
0041 template <typename T>
0042 static inline T
0043 qtcSquare(const T &a)
0044 {
0045     return a * a;
0046 }
0047 
0048 #define qtcBound(a, b, c) qtcMax(a, qtcMin(b, c))
0049 #define qtcLimit(v, l) qtcBound(0, v, l)
0050 #define qtcEqual(v1, v2) (std::abs(v1 - v2) < 0.0001)
0051 
0052 namespace QtCurve {
0053 
0054 template<typename T1, typename T2>
0055 static inline auto
0056 getPadding(T1 len, T2 align) -> decltype(len + align)
0057 {
0058     if (auto left = len % align) {
0059         return align - left;
0060     }
0061     return 0;
0062 }
0063 
0064 template<typename T1, typename T2>
0065 static inline auto
0066 alignTo(T1 len, T2 align) -> decltype(len + align)
0067 {
0068     return len + getPadding(len, align);
0069 }
0070 
0071 }
0072 
0073 template<typename First>
0074 static inline First
0075 qtcSum(First &&first)
0076 {
0077     return first;
0078 }
0079 
0080 template<typename First, typename... Rest>
0081 static inline auto
0082 qtcSum(First &&first, Rest &&...rest)
0083     -> decltype(first + qtcSum(std::forward<Rest>(rest)...))
0084 {
0085     return first + qtcSum(std::forward<Rest>(rest)...);
0086 }
0087 
0088 #endif