Skip to content

Commit 593010c

Browse files
committed
Value: Optimize comparison operators
1 parent 21d2c4e commit 593010c

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

src/scratch/value.h

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -247,28 +247,42 @@ class LIBSCRATCHCPP_EXPORT Value : public ValueVariant
247247

248248
friend inline bool operator>(const Value &v1, const Value &v2)
249249
{
250-
if (v1.m_type == Type::Infinity) {
251-
return v2.m_type != Type::Infinity;
252-
} else if (v1.m_type == Type::NegativeInfinity)
253-
return false;
254-
else if (v2.m_type == Type::Infinity)
255-
return false;
256-
else if (v2.m_type == Type::NegativeInfinity)
257-
return true;
258-
return v1.toDouble() > v2.toDouble();
250+
if ((static_cast<int>(v1.m_type) < 0) || (static_cast<int>(v2.m_type) < 0)) {
251+
if (v1.m_type == Type::Infinity) {
252+
return v2.m_type != Type::Infinity;
253+
} else if (v1.m_type == Type::NegativeInfinity)
254+
return false;
255+
else if (v2.m_type == Type::Infinity)
256+
return false;
257+
else if (v2.m_type == Type::NegativeInfinity)
258+
return true;
259+
}
260+
auto p1 = std::get_if<long>(&v1);
261+
auto p2 = std::get_if<long>(&v2);
262+
if (p1 && p2)
263+
return *p1 > *p2;
264+
else
265+
return v1.toDouble() > v2.toDouble();
259266
}
260267

261268
friend inline bool operator<(const Value &v1, const Value &v2)
262269
{
263-
if (v1.m_type == Type::Infinity) {
264-
return false;
265-
} else if (v1.m_type == Type::NegativeInfinity)
266-
return v2.m_type != Type::NegativeInfinity;
267-
else if (v2.m_type == Type::Infinity)
268-
return v1.m_type != Type::Infinity;
269-
else if (v2.m_type == Type::NegativeInfinity)
270-
return false;
271-
return v1.toDouble() < v2.toDouble();
270+
if ((static_cast<int>(v1.m_type) < 0) || (static_cast<int>(v2.m_type) < 0)) {
271+
if (v1.m_type == Type::Infinity) {
272+
return false;
273+
} else if (v1.m_type == Type::NegativeInfinity)
274+
return v2.m_type != Type::NegativeInfinity;
275+
else if (v2.m_type == Type::Infinity)
276+
return v1.m_type != Type::Infinity;
277+
else if (v2.m_type == Type::NegativeInfinity)
278+
return false;
279+
}
280+
auto p1 = std::get_if<long>(&v1);
281+
auto p2 = std::get_if<long>(&v2);
282+
if (p1 && p2)
283+
return *p1 < *p2;
284+
else
285+
return v1.toDouble() < v2.toDouble();
272286
}
273287

274288
friend inline bool operator>=(const Value &v1, const Value &v2) { return v1 > v2 || v1 == v2; }

0 commit comments

Comments
 (0)