diff --git a/src/base/bittorrent/sessionimpl.cpp b/src/base/bittorrent/sessionimpl.cpp index 5baba9c08..21ee13d12 100644 --- a/src/base/bittorrent/sessionimpl.cpp +++ b/src/base/bittorrent/sessionimpl.cpp @@ -33,7 +33,7 @@ #include #include #include -#include +#include #include #ifdef Q_OS_WIN @@ -2569,52 +2569,24 @@ bool SessionImpl::cancelDownloadMetadata(const TorrentID &id) void SessionImpl::increaseTorrentsQueuePos(const QList &ids) { - using ElementType = std::pair; - std::priority_queue - , std::greater> torrentQueue; - - // Sort torrents by queue position - for (const TorrentID &id : ids) - { - const TorrentImpl *torrent = m_torrents.value(id); - if (!torrent) continue; - if (const int position = torrent->queuePosition(); position >= 0) - torrentQueue.emplace(position, torrent); - } + QList queuedTorrents = getQueuedTorrentsByID(ids); + std::ranges::sort(queuedTorrents, std::less<>(), &TorrentImpl::queuePosition); // Increase torrents queue position (starting with the one in the highest queue position) - while (!torrentQueue.empty()) - { - const TorrentImpl *torrent = torrentQueue.top().second; + for (TorrentImpl *torrent : asConst(queuedTorrents)) torrentQueuePositionUp(torrent->nativeHandle()); - torrentQueue.pop(); - } m_torrentsQueueChanged = true; } void SessionImpl::decreaseTorrentsQueuePos(const QList &ids) { - using ElementType = std::pair; - std::priority_queue torrentQueue; - - // Sort torrents by queue position - for (const TorrentID &id : ids) - { - const TorrentImpl *torrent = m_torrents.value(id); - if (!torrent) continue; - if (const int position = torrent->queuePosition(); position >= 0) - torrentQueue.emplace(position, torrent); - } + QList queuedTorrents = getQueuedTorrentsByID(ids); + std::ranges::sort(queuedTorrents, std::greater<>(), &TorrentImpl::queuePosition); // Decrease torrents queue position (starting with the one in the lowest queue position) - while (!torrentQueue.empty()) - { - const TorrentImpl *torrent = torrentQueue.top().second; + for (TorrentImpl *torrent : asConst(queuedTorrents)) torrentQueuePositionDown(torrent->nativeHandle()); - torrentQueue.pop(); - } for (const lt::torrent_handle &torrentHandle : asConst(m_downloadedMetadata)) torrentQueuePositionBottom(torrentHandle); @@ -2624,52 +2596,24 @@ void SessionImpl::decreaseTorrentsQueuePos(const QList &ids) void SessionImpl::topTorrentsQueuePos(const QList &ids) { - using ElementType = std::pair; - std::priority_queue torrentQueue; - - // Sort torrents by queue position - for (const TorrentID &id : ids) - { - const TorrentImpl *torrent = m_torrents.value(id); - if (!torrent) continue; - if (const int position = torrent->queuePosition(); position >= 0) - torrentQueue.emplace(position, torrent); - } + QList queuedTorrents = getQueuedTorrentsByID(ids); + std::ranges::sort(queuedTorrents, std::greater<>(), &TorrentImpl::queuePosition); // Top torrents queue position (starting with the one in the lowest queue position) - while (!torrentQueue.empty()) - { - const TorrentImpl *torrent = torrentQueue.top().second; + for (TorrentImpl *torrent : asConst(queuedTorrents)) torrentQueuePositionTop(torrent->nativeHandle()); - torrentQueue.pop(); - } m_torrentsQueueChanged = true; } void SessionImpl::bottomTorrentsQueuePos(const QList &ids) { - using ElementType = std::pair; - std::priority_queue - , std::greater> torrentQueue; - - // Sort torrents by queue position - for (const TorrentID &id : ids) - { - const TorrentImpl *torrent = m_torrents.value(id); - if (!torrent) continue; - if (const int position = torrent->queuePosition(); position >= 0) - torrentQueue.emplace(position, torrent); - } + QList queuedTorrents = getQueuedTorrentsByID(ids); + std::ranges::sort(queuedTorrents, std::less<>(), &TorrentImpl::queuePosition); // Bottom torrents queue position (starting with the one in the highest queue position) - while (!torrentQueue.empty()) - { - const TorrentImpl *torrent = torrentQueue.top().second; + for (TorrentImpl *torrent : asConst(queuedTorrents)) torrentQueuePositionBottom(torrent->nativeHandle()); - torrentQueue.pop(); - } for (const lt::torrent_handle &torrentHandle : asConst(m_downloadedMetadata)) torrentQueuePositionBottom(torrentHandle); @@ -5985,6 +5929,15 @@ TorrentImpl *SessionImpl::getTorrent(const lt::torrent_handle &nativeHandle) con return m_torrents.value(getInfoHash(nativeHandle).toTorrentID()); } +QList SessionImpl::getQueuedTorrentsByID(const QList &torrentIDs) const +{ + auto queuedTorrents = torrentIDs + | std::views::transform([this](const TorrentID &torrentID) { return m_torrents.value(torrentID); }) + | std::views::filter([](const TorrentImpl *torrent) { return torrent && (torrent->queuePosition() >= 0); }); + + return {queuedTorrents.begin(), queuedTorrents.end()}; +} + void SessionImpl::handleTorrentRemovedAlert(const lt::torrent_removed_alert */*alert*/) { // We cannot consider `torrent_removed_alert` as a starting point for removing content, diff --git a/src/base/bittorrent/sessionimpl.h b/src/base/bittorrent/sessionimpl.h index e923f8a13..40572895e 100644 --- a/src/base/bittorrent/sessionimpl.h +++ b/src/base/bittorrent/sessionimpl.h @@ -619,6 +619,7 @@ namespace BitTorrent TorrentImpl *createTorrent(const lt::torrent_handle &nativeHandle, LoadTorrentParams params); TorrentImpl *getTorrent(const lt::torrent_handle &nativeHandle) const; + QList getQueuedTorrentsByID(const QList &torrentIDs) const; void saveResumeData(); void saveTorrentsQueue();