mirror of
https://github.com/doitsujin/dxvk.git
synced 2025-01-18 20:52:10 +01:00
[util] Implement simplest ratio helper
This commit is contained in:
parent
16dd1249b7
commit
3fa8691033
81
src/util/util_ratio.h
Normal file
81
src/util/util_ratio.h
Normal file
@ -0,0 +1,81 @@
|
||||
#include <numeric>
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <charconv>
|
||||
|
||||
namespace dxvk {
|
||||
|
||||
/**
|
||||
* \brief Simplest ratio helper
|
||||
*/
|
||||
template <typename T>
|
||||
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;
|
||||
|
||||
};
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user