diff options
Diffstat (limited to 'dev/lib/logic')
| -rw-r--r-- | dev/lib/logic/equiv.hpp | 102 | ||||
| -rw-r--r-- | dev/lib/logic/math.hpp | 35 | ||||
| -rw-r--r-- | dev/lib/logic/opt.hpp | 121 |
3 files changed, 258 insertions, 0 deletions
diff --git a/dev/lib/logic/equiv.hpp b/dev/lib/logic/equiv.hpp new file mode 100644 index 0000000..4bb5904 --- /dev/null +++ b/dev/lib/logic/equiv.hpp @@ -0,0 +1,102 @@ +/* + * File: equiv.hpp + * Purpose: Equivalence runtime c++ header. + * Author: Amlal El Mahrouss (founder@snu.systems) + * Copyright 2025, Amlal El Mahrouss and SNU Systems Corp all rights reserved. + */ + +#pragma once + +namespace snu::equiv +{ + template <typename T> + struct basic_hash_trait + { + /// @brief hash from T's result. + static typename T::result hash() + { + static T val; + return val.hash(); + } + }; + + template <typename T, typename U> + struct is_same + { + static constexpr bool value = false; + + /// @brief check if hash matches what we expect. + constexpr bool operator()() noexcept + { + return T::hash() == U::hash(); + } + }; + + template <typename T> + struct is_same<T, T> + { + static constexpr bool value = true; + }; + + template <typename T, typename U> + struct is_not_same + { + static constexpr bool value = true; + + constexpr bool operator()() noexcept + { + return T::hash() != U::hash(); + } + }; + + template <typename T> + struct is_not_same<T, T> + { + static constexpr bool value = false; + }; + + template <typename T> + struct equiv_is_int8 + { + private: + T left_ = 255, right_ = 255; + + public: + using result = T; + + constexpr result hash() + { + return (left_ + right_) < 1; + } + }; + + template <typename T> + struct equiv_not_int8 + { + private: + T left_ = 255, right_ = 255; + + public: + using result = T; + + constexpr result hash() + { + return (left_ + right_) > 0; + } + }; + + template <typename T> + struct equiv_is_real + { + private: + T left_ = 5, right_ = 3; + + public: + using result = T; + + constexpr result hash() + { + return left_ / right_; + } + }; +} // namespace snu::equiv
\ No newline at end of file diff --git a/dev/lib/logic/math.hpp b/dev/lib/logic/math.hpp new file mode 100644 index 0000000..2613799 --- /dev/null +++ b/dev/lib/logic/math.hpp @@ -0,0 +1,35 @@ +/* + * File: math.hpp + * Purpose: Mathematics c++ header. + * Author: Amlal El Mahrouss (founder@snu.systems) + * Copyright 2025, Amlal El Mahrouss all rights reserved. + */ + +#pragma once + +#include <cmath> + +namespace snu::math +{ + template <size_t T> + struct is_non_boolean_integer final + { + static constexpr const bool value = true; + }; + + template <> + struct is_non_boolean_integer<false> final + { + static constexpr const bool value = false; + }; + + template <> + struct is_non_boolean_integer<true> final + { + static constexpr const bool value = false; + }; + + constexpr inline auto not_a_number = NAN; + constexpr inline auto positive_infinity = INFINITY; + constexpr inline auto negative_infinity = -positive_infinity; +} // namespace snu::math
\ No newline at end of file diff --git a/dev/lib/logic/opt.hpp b/dev/lib/logic/opt.hpp new file mode 100644 index 0000000..facbf2c --- /dev/null +++ b/dev/lib/logic/opt.hpp @@ -0,0 +1,121 @@ +/* + * File: opt.hpp + * Author: Amlal El Mahrouss, + * Copyright 2023-2025, Amlal El Mahrouss/SNU Systems Corp all rights reserved. + */ + +#ifndef _SNU_OPT_HPP +#define _SNU_OPT_HPP + +#include <lib/detail/error.hpp> +#include <utility> + +namespace snu::opt +{ + enum class return_type + { + invalid = 0, + okay = 100, + err, + count = err - okay + 1, + }; + + struct opt final + { + explicit opt(const return_type& return_type) + : m_ret(return_type) + { + } + + opt& expect(const char* input) + { + if (m_ret == return_type::err) + { + throw std::runtime_error(input); + } + + return *this; + } + + private: + return_type m_ret; + }; + + template <typename Teller, typename... Lst> + inline return_type eval(Teller tell, Lst&&... arg) + { + return tell(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err; + } + + namespace traits + { + struct int_eq_teller + { + explicit int_eq_teller() + { + } + + bool operator()(int a, int b) + { + return (a == b); + } + }; + + struct int_greater_than_teller + { + explicit int_greater_than_teller() + { + } + + bool operator()(int a, int b) + { + return (a > b); + } + }; + + struct int_less_than_teller + { + explicit int_less_than_teller() + { + } + + bool operator()(int a, int b) + { + return (a < b); + } + }; + } // namespace traits + + template <typename... Lst> + inline return_type eval_less_than(Lst&&... arg) + { + static traits::int_less_than_teller eq; + return eq(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err; + } + + template <typename... Lst> + inline return_type eval_eq(Lst&&... arg) + { + static traits::int_eq_teller less_than; + return less_than(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err; + } + + template <typename... Lst> + inline return_type eval_greater_than(Lst&&... arg) + { + static traits::int_greater_than_teller greater_than; + return greater_than(std::forward<Lst>(arg)...) ? return_type::okay : return_type::err; + } + + inline return_type eval_true() + { + return return_type::okay; + } + + inline return_type eval_false() + { + return return_type::err; + } +} // namespace snu::opt + +#endif /* ifndef _SNU_OPT_HPP */ |
