Torrent example: replace a QMultiMap with a sorted vector
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>
parent
8fe6f14bf0
commit
eae4668afa
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue