LeviLamina
Loading...
Searching...
No Matches
BoolN.h
1#pragma once
2
3#include <cstddef>
4
5#include "ll/api/base/Concepts.h"
6#include "ll/api/base/Macro.h"
7
8#include "mc/math/vector/base/CommutativeGroup.h"
9#include "mc/math/vector/base/Field.h" // IWYU pragma: keep
10#include "mc/math/vector/base/VectorBase.h"
11
12namespace ll::math {
13template <typename T, typename... Components>
14 requires(traits::is_all_same_v<bool, Components...>)
15class LL_EBO BoolN : public CommutativeGroup<T, Components...>, public BoolNTag {
16public:
17 using first_type = bool;
18 [[nodiscard]] constexpr bool any() const noexcept {
19 bool res = false;
20 BoolN::forEachComponent([&]<typename axis_type, size_t iter> {
21 res = res || static_cast<T const*>(this)->template get<bool, iter>();
22 });
23 return res;
24 }
25 [[nodiscard]] constexpr bool all() const noexcept {
26 bool res = true;
27 BoolN::forEachComponent([&]<typename axis_type, size_t iter> {
28 res = res && static_cast<T const*>(this)->template get<bool, iter>();
29 });
30 return res;
31 }
32 [[nodiscard]] constexpr T operator!() const noexcept {
33 T tmp = *(static_cast<T const*>(this));
34 BoolN::forEachComponent([&]<typename axis_type, size_t iter> {
35 tmp.template get<bool, iter>() = !(tmp.template get<bool, iter>());
36 });
37 return tmp;
38 }
39 [[nodiscard]] constexpr T operator||(T const& x) const noexcept {
40 T tmp = *(static_cast<T const*>(this));
41 BoolN::forEachComponent([&]<typename axis_type, size_t iter> {
42 tmp.template get<bool, iter>() = tmp.template get<bool, iter>() || x.template get<bool, iter>();
43 });
44 return tmp;
45 }
46 [[nodiscard]] constexpr T operator&&(T const& x) const noexcept {
47 T tmp = *(static_cast<T const*>(this));
48 BoolN::forEachComponent([&]<typename axis_type, size_t iter> {
49 tmp.template get<bool, iter>() = tmp.template get<bool, iter>() && x.template get<bool, iter>();
50 });
51 return tmp;
52 }
53 [[nodiscard]] constexpr T operator xor(T const& x) const noexcept {
54 T tmp = *(static_cast<T const*>(this));
55 BoolN::forEachComponent([&]<typename axis_type, size_t iter> {
56 tmp.template get<bool, iter>() = tmp.template get<bool, iter>() xor x.template get<bool, iter>();
57 });
58 return tmp;
59 }
60 [[nodiscard]] constexpr T xnor(T const& x) const noexcept {
61 T tmp = *(static_cast<T const*>(this));
62 BoolN::forEachComponent([&]<typename axis_type, size_t iter> {
63 tmp.template get<bool, iter>() = !(tmp.template get<bool, iter>() xor x.template get<bool, iter>());
64 });
65 return tmp;
66 }
67 [[nodiscard]] constexpr T operator||(first_type const& x) const noexcept {
68 T tmp = *(static_cast<T const*>(this));
69 BoolN::forEachComponent([&]<typename axis_type, size_t iter> {
70 tmp.template get<bool, iter>() = tmp.template get<bool, iter>() || x;
71 });
72 return tmp;
73 }
74 [[nodiscard]] constexpr T operator&&(first_type const& x) const noexcept {
75 T tmp = *(static_cast<T const*>(this));
76 BoolN::forEachComponent([&]<typename axis_type, size_t iter> {
77 tmp.template get<bool, iter>() = tmp.template get<bool, iter>() && x;
78 });
79 return tmp;
80 }
81 [[nodiscard]] constexpr T operator xor(first_type const& x) const noexcept {
82 T tmp = *(static_cast<T const*>(this));
83 BoolN::forEachComponent([&]<typename axis_type, size_t iter> {
84 tmp.template get<bool, iter>() = tmp.template get<bool, iter>() xor x;
85 });
86 return tmp;
87 }
88 [[nodiscard]] constexpr T xnor(first_type const& x) const noexcept {
89 T tmp = *(static_cast<T const*>(this));
90 BoolN::forEachComponent([&]<typename axis_type, size_t iter> {
91 tmp.template get<bool, iter>() = !(tmp.template get<bool, iter>() xor x);
92 });
93 return tmp;
94 }
95 explicit(false) operator bool() const noexcept { return all(); }
96};
97
98template <IsBoolN T>
99[[nodiscard]] constexpr bool any(T const& x) noexcept {
100 return x.any();
101}
102
103template <IsBoolN T>
104[[nodiscard]] constexpr bool all(T const& x) noexcept {
105 return x.all();
106}
107
108template <IsBoolN T, concepts::IsVectorBase T2>
109[[nodiscard]] constexpr T2 select(T const& x, T2 const& a, T2 const& b) noexcept
110 requires(T::size() == T2::size())
111{
112 T2 tmp;
113 T2::forEachComponent([&]<typename axis_type, size_t iter> {
114 tmp.template get<axis_type, iter>() =
115 x.template get<bool, iter>() ? a.template get<axis_type, iter>() : b.template get<axis_type, iter>();
116 });
117 return tmp;
118}
119} // namespace ll::math
Definition BoolN.h:15
Definition VectorBase.h:32
Definition CommutativeGroup.h:17