Skip to content

Commit e60c1b0

Browse files
Nikolai Tillmannmeta-codesync[bot]
authored andcommitted
Replace MethodDelta::compare with operator< for idiomatic C++
Summary: Address code review feedback on D87519337. Replace the static compare method with operator< member function for MethodDelta struct, which is the more idiomatic C++ approach for defining custom comparison logic. This makes the code more intuitive and follows standard C++ conventions for sortable types. Reviewed By: jimmycFB Differential Revision: D87662185 fbshipit-source-id: 899b19359dd0a23896d5824218ba42e696cebab3
1 parent e40aea1 commit e60c1b0

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

libredex/SourceBlocks.cpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,34 +1982,34 @@ struct ViolationsHelper::ViolationsHelperImpl {
19821982
MethodDelta(DexMethod* p1, size_t p2, size_t p3)
19831983
: method(p1), violations_delta(p2), method_size(p3) {}
19841984

1985-
// Comparison operator for sorting by proportional violations
1986-
static bool compare(const MethodDelta& t1, const MethodDelta& t2) {
1987-
double t1_proportional_violations =
1988-
(double)t1.violations_delta / (double)t1.method_size;
1989-
double t2_proportional_violations =
1990-
(double)t2.violations_delta / (double)t2.method_size;
1991-
if (t1_proportional_violations > t2_proportional_violations) {
1985+
// Comparison operator for sorting by proportional violations (descending)
1986+
bool operator<(const MethodDelta& other) const {
1987+
double this_proportional_violations =
1988+
(double)violations_delta / (double)method_size;
1989+
double other_proportional_violations =
1990+
(double)other.violations_delta / (double)other.method_size;
1991+
if (this_proportional_violations > other_proportional_violations) {
19921992
return true;
19931993
}
1994-
if (t1_proportional_violations < t2_proportional_violations) {
1994+
if (this_proportional_violations < other_proportional_violations) {
19951995
return false;
19961996
}
19971997

1998-
if (t1.violations_delta > t2.violations_delta) {
1998+
if (violations_delta > other.violations_delta) {
19991999
return true;
20002000
}
2001-
if (t1.violations_delta < t2.violations_delta) {
2001+
if (violations_delta < other.violations_delta) {
20022002
return false;
20032003
}
20042004

2005-
if (t1.method_size < t2.method_size) {
2005+
if (method_size < other.method_size) {
20062006
return true;
20072007
}
2008-
if (t1.method_size > t2.method_size) {
2008+
if (method_size > other.method_size) {
20092009
return false;
20102010
}
20112011

2012-
return compare_dexmethods(t1.method, t2.method);
2012+
return compare_dexmethods(method, other.method);
20132013
}
20142014
};
20152015

@@ -2110,10 +2110,9 @@ struct ViolationsHelper::ViolationsHelperImpl {
21102110
return;
21112111
}
21122112
MethodDelta m_t{m, m_delta, s};
2113-
if (MethodDelta::compare(m_t, top_changes.back())) {
2113+
if (m_t < top_changes.back()) {
21142114
top_changes.back() = m_t;
2115-
std::sort(top_changes.begin(), top_changes.end(),
2116-
MethodDelta::compare);
2115+
std::sort(top_changes.begin(), top_changes.end());
21172116
}
21182117
},
21192118
violations_start);

0 commit comments

Comments
 (0)