Skip to content

Commit

Permalink
refactor: tr_compare_3way() (transmission#5799)
Browse files Browse the repository at this point in the history
  • Loading branch information
ckerr committed Jul 16, 2023
1 parent 273f943 commit ea9fd64
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 138 deletions.
2 changes: 1 addition & 1 deletion gtk/Torrent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
78 changes: 31 additions & 47 deletions gtk/TorrentSorter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Utils.h"

#include <libtransmission/transmission.h>
#include <libtransmission/utils.h>

#include <algorithm>

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
19 changes: 0 additions & 19 deletions gtk/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/***
Expand Down
2 changes: 1 addition & 1 deletion libtransmission/announcer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1503,7 +1503,7 @@ int compareAnnounceTiers(tr_tier const* a, tr_tier const* b)

// the tiers are effectively equal priority, but add an arbitrary
// differentiation because ptrArray sorted mode hates equal items.
return a < b ? -1 : 1;
return tr_compare_3way(a, b);
}

void tierAnnounce(tr_announcer_impl* announcer, tr_tier* tier)
Expand Down
7 changes: 1 addition & 6 deletions libtransmission/peer-mgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -465,12 +465,7 @@ struct tr_pex
return i;
}

if (port != that.port)
{
return port < that.port ? -1 : 1;
}

return 0;
return tr_compare_3way(port, that.port);
}

[[nodiscard]] bool operator==(tr_pex const& that) const noexcept
Expand Down
5 changes: 0 additions & 5 deletions qt/FileTreeItem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,3 @@ QString FileTreeItem::path() const

return item_path;
}

bool FileTreeItem::isComplete() const
{
return have_size_ == totalSize();
}
6 changes: 5 additions & 1 deletion qt/FileTreeItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,12 @@ class FileTreeItem
return total_size_;
}

[[nodiscard]] constexpr auto isComplete() const noexcept
{
return have_size_ == totalSize();
}

QString path() const;
bool isComplete() const;
int priority() const;
int isSubtreeWanted() const;

Expand Down
25 changes: 2 additions & 23 deletions qt/Torrent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,7 @@ int Torrent::compareSeedProgress(Torrent const& that) const

double const a_progress = a_ratio / *a_ratio_limit;
double const b_progress = b_ratio / *b_ratio_limit;

if (a_progress < b_progress)
{
return -1;
}

if (a_progress > b_progress)
{
return 1;
}

return 0;
return tr_compare_3way(a_progress, b_progress);
}

int Torrent::compareRatio(Torrent const& that) const
Expand All @@ -128,17 +117,7 @@ int Torrent::compareRatio(Torrent const& that) const
return -1;
}

if (a < b)
{
return -1;
}

if (a > b)
{
return 1;
}

return 0;
return tr_compare_3way(a, b);
}

int Torrent::compareETA(Torrent const& that) const
Expand Down
Loading

0 comments on commit ea9fd64

Please sign in to comment.