refactor: tr_compare_3way() (#5799)

This commit is contained in:
Charles Kerr
2023-07-15 21:55:44 -05:00
committed by GitHub
parent 273f943a3b
commit ea9fd64830
9 changed files with 57 additions and 138 deletions

View File

@@ -763,7 +763,7 @@ void Torrent::get_item_value(Glib::RefPtr<Glib::ObjectBase const> const& item, i
int Torrent::compare_by_id(Glib::RefPtr<Torrent const> const& lhs, Glib::RefPtr<Torrent const> const& rhs)
{
return gtr_compare_generic(lhs->get_id(), rhs->get_id());
return tr_compare_3way(lhs->get_id(), rhs->get_id());
}
bool Torrent::less_by_id(Glib::RefPtr<Torrent const> const& lhs, Glib::RefPtr<Torrent const> const& rhs)

View File

@@ -10,6 +10,7 @@
#include "Utils.h"
#include <libtransmission/transmission.h>
#include <libtransmission/utils.h>
#include <algorithm>
@@ -44,7 +45,7 @@ constexpr int compare_eta(time_t lhs, time_t rhs)
return 1;
}
return -gtr_compare_generic(lhs, rhs);
return -tr_compare_3way(lhs, rhs);
}
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
@@ -65,124 +66,107 @@ constexpr int compare_ratio(double lhs, double rhs)
return -1;
}
return gtr_compare_generic(lhs, rhs);
return tr_compare_3way(lhs, rhs);
}
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
int compare_by_name(Torrent const& lhs, Torrent const& rhs)
{
return lhs.get_name_collated().compare(rhs.get_name_collated());
return tr_compare_3way(lhs.get_name_collated(), rhs.get_name_collated());
}
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
int compare_by_queue(Torrent const& lhs, Torrent const& rhs)
{
return gtr_compare_generic(lhs.get_queue_position(), rhs.get_queue_position());
return tr_compare_3way(lhs.get_queue_position(), rhs.get_queue_position());
}
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
int compare_by_ratio(Torrent const& lhs, Torrent const& rhs)
{
auto result = -compare_ratio(lhs.get_ratio(), rhs.get_ratio()); // default descending
if (result == 0)
if (auto result = -compare_ratio(lhs.get_ratio(), rhs.get_ratio()); result != 0)
{
result = compare_by_queue(lhs, rhs);
return result;
}
return result;
return compare_by_queue(lhs, rhs);
}
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
int compare_by_activity(Torrent const& lhs, Torrent const& rhs)
{
auto result = -gtr_compare_generic(
lhs.get_speed_up() + lhs.get_speed_down(),
rhs.get_speed_up() + rhs.get_speed_down()); // default descending
if (result == 0)
if (auto val = -tr_compare_3way(lhs.get_speed_up() + lhs.get_speed_down(), rhs.get_speed_up() + rhs.get_speed_down());
val != 0)
{
result = -gtr_compare_generic(lhs.get_active_peer_count(), rhs.get_active_peer_count()); // default descending
return val;
}
if (result == 0)
if (auto val = -tr_compare_3way(lhs.get_active_peer_count(), rhs.get_active_peer_count()); val != 0)
{
result = compare_by_queue(lhs, rhs);
return val;
}
return result;
return compare_by_queue(lhs, rhs);
}
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
int compare_by_age(Torrent const& lhs, Torrent const& rhs)
{
auto result = -gtr_compare_generic(lhs.get_added_date(), rhs.get_added_date()); // default descending
if (result == 0)
if (auto val = -tr_compare_3way(lhs.get_added_date(), rhs.get_added_date()); val != 0)
{
result = compare_by_name(lhs, rhs);
return val;
}
return result;
return compare_by_name(lhs, rhs);
}
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
int compare_by_size(Torrent const& lhs, Torrent const& rhs)
{
auto result = -gtr_compare_generic(lhs.get_total_size(), rhs.get_total_size()); // default descending
if (result == 0)
if (auto val = -tr_compare_3way(lhs.get_total_size(), rhs.get_total_size()); val != 0)
{
result = compare_by_name(lhs, rhs);
return val;
}
return result;
return compare_by_name(lhs, rhs);
}
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
int compare_by_progress(Torrent const& lhs, Torrent const& rhs)
{
auto result = -gtr_compare_generic(lhs.get_percent_complete(), rhs.get_percent_complete()); // default descending
if (result == 0)
if (auto val = -tr_compare_3way(lhs.get_percent_complete(), rhs.get_percent_complete()); val != 0)
{
result = -gtr_compare_generic(
lhs.get_seed_ratio_percent_done(),
rhs.get_seed_ratio_percent_done()); // default descending
return val;
}
if (result == 0)
if (auto val = -tr_compare_3way(lhs.get_seed_ratio_percent_done(), rhs.get_seed_ratio_percent_done()); val != 0)
{
result = compare_by_ratio(lhs, rhs);
return val;
}
return result;
return compare_by_ratio(lhs, rhs);
}
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
int compare_by_eta(Torrent const& lhs, Torrent const& rhs)
{
auto result = compare_eta(lhs.get_eta(), rhs.get_eta());
if (result == 0)
if (auto val = compare_eta(lhs.get_eta(), rhs.get_eta()); val != 0)
{
result = compare_by_name(lhs, rhs);
return val;
}
return result;
return compare_by_name(lhs, rhs);
}
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
int compare_by_state(Torrent const& lhs, Torrent const& rhs)
{
auto result = -gtr_compare_generic(lhs.get_activity(), rhs.get_activity());
if (result == 0)
if (auto val = -tr_compare_3way(lhs.get_activity(), rhs.get_activity()); val != 0)
{
result = compare_by_queue(lhs, rhs);
return val;
}
return result;
return compare_by_queue(lhs, rhs);
}
} // namespace

View File

@@ -172,25 +172,6 @@ inline T gtr_str_strip(T const& text)
return new_begin == T::npos ? T() : text.substr(new_begin, new_end == T::npos ? new_end : new_end - new_begin + 1);
}
template<typename T>
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
constexpr int gtr_compare_generic(T const& lhs, T const& rhs)
{
using std::rel_ops::operator>;
if (lhs < rhs)
{
return -1;
}
if (lhs > rhs)
{
return 1;
}
return 0;
}
std::string gtr_get_full_resource_path(std::string const& rel_path);
/***