summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2019-05-23 12:40:12 +0200
committerMarc Mutz <marc.mutz@kdab.com>2019-05-25 09:30:51 +0000
commiteae4668afaaed2723383d8c19858d3e212f3e8ec (patch)
treecb7c4aefd8e348bf874e08c274ef2389da9aefc6
parent8fe6f14bf025a4f0b4f8c6c6133d54652e1b3b4e (diff)
Torrent example: replace a QMultiMap with a sorted vectordev
This came about trying to remove the Java-style iterator. It was used to iterate in reverse order, something QMap can't do, easily, due to lack of rbegin()/rend(). Instead of writing ugly loops, use a vector of pairs, fill it, sort it, then iterate over that one in reverse. Change-Id: I09c8a2732a0699fff4c497778745523e20d348a1 Reviewed-by: Paul Wicking <paul.wicking@qt.io>
-rw-r--r--examples/network/torrent/torrentclient.cpp14
1 files changed, 7 insertions, 7 deletions
diff --git a/examples/network/torrent/torrentclient.cpp b/examples/network/torrent/torrentclient.cpp
index bd050153a4..b4cbbb7a45 100644
--- a/examples/network/torrent/torrentclient.cpp
+++ b/examples/network/torrent/torrentclient.cpp
@@ -1084,25 +1084,25 @@ void TorrentClient::scheduleUploads()
// seeding, we sort by upload speed. Seeds are left out; there's
// no use in unchoking them.
QList<PeerWireClient *> allClients = d->connections;
- QMultiMap<int, PeerWireClient *> transferSpeeds;
+ QVector<QPair<qint64, PeerWireClient *>> transferSpeeds;
for (PeerWireClient *client : qAsConst(allClients)) {
if (client->state() == QAbstractSocket::ConnectedState
&& client->availablePieces().count(true) != d->pieceCount) {
if (d->state == Seeding) {
- transferSpeeds.insert(client->uploadSpeed(), client);
+ transferSpeeds.push_back({client->uploadSpeed(), client});
} else {
- transferSpeeds.insert(client->downloadSpeed(), client);
+ transferSpeeds.push_back({client->downloadSpeed(), client});
}
}
}
+ std::sort(transferSpeeds.begin(), transferSpeeds.end());
+
// Unchoke the top 'MaxUploads' downloaders (peers that we are
// uploading to) and choke all others.
int maxUploaders = MaxUploads;
- QMapIterator<int, PeerWireClient *> it(transferSpeeds);
- it.toBack();
- while (it.hasPrevious()) {
- PeerWireClient *client = it.previous().value();
+ for (auto rit = transferSpeeds.crbegin(), rend = transferSpeeds.crend(); rit != rend; ++rit) {
+ PeerWireClient *client = rit->second;
bool interested = (client->peerWireState() & PeerWireClient::PeerIsInterested);
if (maxUploaders) {