diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-11-23 20:16:02 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-23 20:16:02 -0500 |
| commit | 85f89ee4bb137100cbeffcbc10168eb8ea52e6cc (patch) | |
| tree | f6e2063319ceaaa02f523fb5c289e4f37411a2df /include/ocl/logic | |
| parent | 9a70f32ddaec0eef99efbf7ff5597c2adf08f45a (diff) | |
| parent | 65a8349aa5526d071b18cd4d42586c46faaa3823 (diff) | |
Merge pull request #18 from amlel-el-mahrouss/developv1.0.48
OCL v1.0.48
Diffstat (limited to 'include/ocl/logic')
| -rw-r--r-- | include/ocl/logic/equiv.hpp | 104 | ||||
| -rw-r--r-- | include/ocl/logic/math.hpp | 33 | ||||
| -rw-r--r-- | include/ocl/logic/opt.hpp | 134 |
3 files changed, 271 insertions, 0 deletions
diff --git a/include/ocl/logic/equiv.hpp b/include/ocl/logic/equiv.hpp new file mode 100644 index 0000000..1bdb6d9 --- /dev/null +++ b/include/ocl/logic/equiv.hpp @@ -0,0 +1,104 @@ +/* + * File: equiv.hpp + * Purpose: Equivalence header. + * Author: Amlal El Mahrouss (amlal@nekernel.org) + * Copyright 2025, Amlal El Mahrouss, licensed under the Boost Software License. + */ + +#pragma once + +/// @brief OCL equivalence namespace. +namespace ocl::equiv +{ + template <typename T> + struct basic_hash_trait + { + /// @brief hash from T's result_type. + static typename T::result_type 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_ = 127, right_ = 127; + + public: + using result_type = T; + + constexpr result_type hash() + { + return (left_ + right_) < 1; + } + }; + + template <typename T> + struct equiv_not_int8 + { + private: + // these shall overflow if not int8. + T left_ = 127, right_ = 127; + + public: + using result_type = T; + + constexpr result_type hash() + { + return (left_ + right_) > 0; + } + }; + + template <typename T> + struct equiv_is_real + { + private: + T left_ = 5, right_ = 3; + + public: + using result_type = T; + + constexpr result_type hash() + { + return left_ / right_ == 1; + } + }; +} // namespace ocl::equiv diff --git a/include/ocl/logic/math.hpp b/include/ocl/logic/math.hpp new file mode 100644 index 0000000..d131ef9 --- /dev/null +++ b/include/ocl/logic/math.hpp @@ -0,0 +1,33 @@ +/* + * File: math.hpp + * Purpose: Mathematics c++ header. + * Author: Amlal El Mahrouss (amlal@nekernel.org) + * Copyright 2025, Amlal El Mahrouss, Licensed under the Boost Software License. + */ + +#pragma once + +namespace ocl +{ + template <__SIZE_TYPE__ 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 = __builtin_nanf(""); + constexpr inline auto positive_infinity = __builtin_inff(); + constexpr inline auto negative_infinity = -positive_infinity; +} // namespace ocl
\ No newline at end of file diff --git a/include/ocl/logic/opt.hpp b/include/ocl/logic/opt.hpp new file mode 100644 index 0000000..ba76885 --- /dev/null +++ b/include/ocl/logic/opt.hpp @@ -0,0 +1,134 @@ +/* + * File: opt.hpp + * Author: Amlal El Mahrouss, + * Copyright 2023-2025, Amlal El Mahrouss, Licensed under the Boost Software License + */ + +#ifndef _OCL_OPT_HPP +#define _OCL_OPT_HPP + +#include <except/error.hpp> +#include <utility> + +namespace ocl +{ + enum class return_type + { + invalid = 0, + okay = 100, + err, + count = err - okay + 1, + }; + + template <typename char_type = char> + struct opt final + { + explicit opt(const return_type& return_type) + : m_ret(return_type) + { + } + + opt& expect(const char_type* input) + { + if (m_ret == return_type::err) + { + throw std::runtime_error(input ? input : "opt::error"); + } + + return *this; + } + + template <typename ErrorHandler> + opt& expect_or_handle(const char_type* input) + { + if (m_ret == return_type::err) + { + ErrorHandler err_handler; + err_handler(input ? input : "opt::error"); + } + + return *this; + } + + private: + return_type m_ret{return_type::invalid}; + }; + + 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() noexcept + { + return return_type::okay; + } + + inline return_type eval_false() noexcept + { + return return_type::err; + } +} // namespace ocl + +#endif /* ifndef _OCL_OPT_HPP */
\ No newline at end of file |
