Overload price comp fns to accept doubles

This commit is contained in:
John Colvin committed 2021-11-19 16:20:41 -05:00
1 parent 77dfe79d95
commit 6573985bea
2 files changed
+49

No files matched your search

+44
View File
@@ -54,14 +54,22 @@ class Price {
[[nodiscard]] constexpr bool is_nan() const noexcept;
[[nodiscard]] constexpr bool is_better(Side s, Price p2) const noexcept;
[[nodiscard]] static constexpr bool is_better(
Side s, double p1, double p2) noexcept;
[[nodiscard]] constexpr bool is_worse(Side s, Price p2) const noexcept;
[[nodiscard]] static constexpr bool is_worse(
Side s, double p1, double p2) noexcept;
[[nodiscard]] constexpr bool is_better_or_equal(
Side s, Price p2) const noexcept;
[[nodiscard]] static constexpr bool is_better_or_equal(
Side s, double p1, double p2) noexcept;
[[nodiscard]] constexpr bool is_worse_or_equal(
Side s, Price p2) const noexcept;
[[nodiscard]] static constexpr bool is_worse_or_equal(
Side s, double p1, double p2) noexcept;
[[nodiscard]] constexpr Price repl_nan(Price repl) const noexcept;
@@ -233,10 +241,27 @@ constexpr bool Price::is_better(Side s, Price p2) const noexcept {
return *this < p2;
}
constexpr bool Price::is_better(Side s, double p1, double p2) noexcept {
if (std::isnan(p1)) {
return false;
}
if (std::isnan(p2)) {
return true;
}
if (s == Side::Buy) {
return p1 > p2;
}
return p1 < p2;
}
constexpr bool Price::is_worse(Side s, Price p2) const noexcept {
return p2.is_better(s, *this);
}
constexpr bool Price::is_worse(Side s, double p1, double p2) noexcept {
return is_better(s, p2, p1);
}
constexpr bool Price::is_better_or_equal(Side s, Price p2) const noexcept {
if (is_nan()) {
// This case is always false (even if p2 is nan), because nan != nan.
@@ -251,10 +276,29 @@ constexpr bool Price::is_better_or_equal(Side s, Price p2) const noexcept {
return *this <= p2;
}
constexpr bool Price::is_better_or_equal(
Side s, double p1, double p2) noexcept {
if (std::isnan(p1)) {
// This case is always false (even if p2 is nan), because nan != nan.
return false;
}
if (std::isnan(p2)) {
return true;
}
if (s == Side::Buy) {
return p1 >= p2;
}
return p1 <= p2;
}
constexpr bool Price::is_worse_or_equal(Side s, Price p2) const noexcept {
return p2.is_better_or_equal(s, *this);
}
constexpr bool Price::is_worse_or_equal(Side s, double p1, double p2) noexcept {
return is_better_or_equal(s, p2, p1);
}
constexpr Price Price::repl_nan(Price repl) const noexcept {
if (is_nan()) {
return repl;
+5
View File
@@ -10,6 +10,7 @@ struct PriceComp {
constexpr PriceComp(const PriceComp &other) = default;
constexpr bool operator()(const Price &lhs, const Price &rhs);
constexpr bool operator()(double lhs, double rhs);
private:
Side _side = Side::Buy;
@@ -25,4 +26,8 @@ constexpr bool PriceComp::operator()(const Price &lhs, const Price &rhs) {
return lhs.is_better(_side, rhs);
}
constexpr bool PriceComp::operator()(double lhs, double rhs) {
return Price::is_better(_side, lhs, rhs);
}
} // namespace bts::domain