From 3fa86910339b76b4a5c030c727502192bb5be538 Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Tue, 26 Nov 2019 18:08:41 +0000 Subject: [PATCH] [util] Implement simplest ratio helper --- src/util/util_ratio.h | 81 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/util/util_ratio.h diff --git a/src/util/util_ratio.h b/src/util/util_ratio.h new file mode 100644 index 000000000..1e56d33ed --- /dev/null +++ b/src/util/util_ratio.h @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include + +namespace dxvk { + + /** + * \brief Simplest ratio helper + */ + template + class Ratio { + + public: + + Ratio(T num, T denom) { + set(num, denom); + } + + Ratio(std::string_view view) { + set(0, 0); + + size_t colon = view.find(":"); + + if (colon == std::string_view::npos) + return; + + std::string_view numStr = view.substr(0, colon); + std::string_view denomStr = view.substr(colon + 1); + + T num = 0, denom = 0; + std::from_chars(numStr.data(), numStr.data() + numStr.size(), num); + std::from_chars(denomStr.data(), denomStr.data() + denomStr.size(), denom); + + set(num, denom); + } + + inline T num() const { return m_num; } + inline T denom() const { return m_denom; } + + inline bool undefined() const { return m_denom == 0; } + + inline void set(T num, T denom) { + const T gcd = std::gcd(num, denom); + + m_num = num / gcd; + m_denom = denom / gcd; + } + + inline bool operator == (const Ratio& other) const { + return num() == other.num() && denom() == other.denom(); + } + + inline bool operator != (const Ratio& other) const { + return !(*this == other); + } + + inline bool operator >= (const Ratio& other) const { + return num() * other.denom() >= other.num() * denom(); + } + + inline bool operator > (const Ratio& other) const { + return num() * other.denom() > other.num() * denom(); + } + + inline bool operator < (const Ratio& other) const { + return !(*this >= other); + } + + inline bool operator <= (const Ratio& other) const { + return !(*this > other); + } + + private: + + T m_num, m_denom; + + }; + +} \ No newline at end of file