IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
algorithm.hxx
Go to the documentation of this file.
1
3
4#pragma once
6
7namespace ice::math
8{
9
10 namespace concepts
11 {
12
13 template<typename... Args>
14 concept is_arithmetic = requires {
15 (... && std::is_arithmetic_v<Args>);
16 };
17
18 } // namespace concepts
19
20 template<typename T = void, typename U, typename... Args>
21 requires concepts::is_arithmetic<U, Args...>
22 constexpr auto sum(U first, Args... args) noexcept
23 {
24 using Target = std::conditional_t<std::is_same_v<T, void>, U, T>;
25 return (static_cast<Target>(first) + ... + static_cast<Target>(args));
26 }
27
28 template<typename T = void, typename U, typename... Args>
29 requires concepts::is_arithmetic<U, Args...>
30 constexpr auto sub(U first, Args... args) noexcept
31 {
32 using Target = std::conditional_t<std::is_same_v<T, void>, U, T>;
33 return (static_cast<Target>(first) - ... - static_cast<Target>(args));
34 }
35
36 template<typename T = void, typename U, typename... Args>
37 requires concepts::is_arithmetic<U, Args...>
38 constexpr auto mul(U first, Args... args) noexcept
39 {
40 using Target = std::conditional_t<std::is_same_v<T, void>, U, T>;
41 return (static_cast<Target>(first) * ... * static_cast<Target>(args));
42 }
43
44 template<typename T = void, typename U, typename... Args>
45 requires concepts::is_arithmetic<U, Args...>
46 constexpr auto div(U first, Args... args) noexcept
47 {
48 using Target = std::conditional_t<std::is_same_v<T, void>, U, T>;
49 return (static_cast<Target>(first) / ... / static_cast<Target>(args));
50 }
51
52 template<typename T = void, typename U, typename... Args>
53 requires concepts::is_arithmetic<U, Args...>
54 constexpr auto max_of(U first, Args... args) noexcept
55 {
56 using Target = std::conditional_t<std::is_same_v<T, void>, U, T>;
57 if constexpr (sizeof...(Args) == 0)
58 {
59 return static_cast<Target>(first);
60 }
61 else if constexpr (sizeof...(Args) == 1)
62 {
63 return std::max<Target>(first, args...);
64 }
65 else
66 {
67 return std::max<Target>(first, ice::math::max_of<Target>(args...));
68 }
69 }
70
71 template<typename T = void, typename U, typename... Args>
72 requires concepts::is_arithmetic<U, Args...>
73 constexpr auto min_of(U first, Args... args) noexcept
74 {
75 using Target = std::conditional_t<std::is_same_v<T, void>, U, T>;
76 if constexpr (sizeof...(Args) == 0)
77 {
78 return static_cast<Target>(first);
79 }
80 else if constexpr (sizeof...(Args) == 1)
81 {
82 return std::min<Target>(first, args...);
83 }
84 else
85 {
86 return std::min<Target>(first, ice::math::min_of<Target>(args...));
87 }
88 }
89
90} // namespace ice::math
Definition algorithm.hxx:14
Definition algorithm.hxx:11
Definition algorithm.hxx:8
constexpr auto mul(U first, Args... args) noexcept
Definition algorithm.hxx:38
constexpr auto div(U first, Args... args) noexcept
Definition algorithm.hxx:46
constexpr auto max_of(U first, Args... args) noexcept
Definition algorithm.hxx:54
constexpr auto min_of(U first, Args... args) noexcept
Definition algorithm.hxx:73
constexpr auto sum(U first, Args... args) noexcept
Definition algorithm.hxx:22
constexpr auto sub(U first, Args... args) noexcept
Definition algorithm.hxx:30