From bb11d11a724810db2a1dc4361557d2b27f4ac70a Mon Sep 17 00:00:00 2001 From: BotanEgg Date: Sat, 14 Jul 2012 06:28:23 +0800 Subject: [PATCH] Itr cache --- src/fs_utils.cpp | 6 +- src/properties/peerlistwidget.cpp | 6 +- src/properties/trackerlist.cpp | 6 +- src/properties/trackersadditiondlg.h | 4 +- src/qtlibtorrent/qbtsession.cpp | 108 +++++++++++++++-------- src/qtlibtorrent/qtorrenthandle.cpp | 11 ++- src/qtlibtorrent/torrentmodel.cpp | 18 ++-- src/qtlibtorrent/torrentspeedmonitor.cpp | 6 +- src/torrentpersistentdata.h | 11 ++- 9 files changed, 118 insertions(+), 58 deletions(-) diff --git a/src/fs_utils.cpp b/src/fs_utils.cpp index e9af3777f..cc71b148f 100644 --- a/src/fs_utils.cpp +++ b/src/fs_utils.cpp @@ -214,8 +214,10 @@ QString fsutils::fixFileNames(const QString& path) QList parts = raw_path.split('/'); if (parts.isEmpty()) return path; QByteArray last_part = parts.takeLast(); - QList::iterator it; - for (it = parts.begin(); it != parts.end(); ++it) { + + QList::iterator it = parts.begin(); + QList::iterator itend = parts.end(); + for ( ; it != itend; ++it) { // Make sure the filename is not too long if (it->size() > MAX_FILENAME_BYTES) { qWarning() << "Folder" << *it << "was cut because it was too long"; diff --git a/src/properties/peerlistwidget.cpp b/src/properties/peerlistwidget.cpp index 6084d16a2..ead9fed61 100644 --- a/src/properties/peerlistwidget.cpp +++ b/src/properties/peerlistwidget.cpp @@ -310,9 +310,11 @@ void PeerListWidget::loadPeers(const QTorrentHandle &h, bool force_hostname_reso boost::system::error_code ec; std::vector peers; h.get_peer_info(peers); - std::vector::const_iterator itr; QSet old_peers_set = m_peerItems.keys().toSet(); - for (itr = peers.begin(); itr != peers.end(); ++itr) { + + std::vector::const_iterator itr = peers.begin(); + std::vector::const_iterator itrend = peers.end(); + for ( ; itr != itrend; ++itr) { peer_info peer = *itr; QString peer_ip = misc::toQString(peer.ip.address().to_string(ec)); if (ec) continue; diff --git a/src/properties/trackerlist.cpp b/src/properties/trackerlist.cpp index c11d24b41..2f1a588c6 100644 --- a/src/properties/trackerlist.cpp +++ b/src/properties/trackerlist.cpp @@ -312,8 +312,10 @@ void TrackerList::deleteSelectedTrackers() { // Iterate of trackers and remove selected ones std::vector remaining_trackers; std::vector trackers = h.trackers(); - std::vector::iterator it; - for (it = trackers.begin(); it != trackers.end(); ++it) { + + std::vector::iterator it = trackers.begin(); + std::vector::iterator itend = trackers.end(); + for ( ; it != itend; ++it) { if (!urls_to_remove.contains(misc::toQString((*it).url))) { remaining_trackers.push_back(*it); } diff --git a/src/properties/trackersadditiondlg.h b/src/properties/trackersadditiondlg.h index caa145c55..033c6142e 100644 --- a/src/properties/trackersadditiondlg.h +++ b/src/properties/trackersadditiondlg.h @@ -87,8 +87,10 @@ public slots: QList existingTrackers; // Load from torrent handle std::vector tor_trackers = h.trackers(); + std::vector::iterator itr = tor_trackers.begin(); - while(itr != tor_trackers.end()) { + std::vector::iterator itrend = tor_trackers.end(); + while(itr != itrend) { existingTrackers << QUrl(misc::toQString(itr->url)); ++itr; } diff --git a/src/qtlibtorrent/qbtsession.cpp b/src/qtlibtorrent/qbtsession.cpp index 3b4d057d6..dc70c9dbd 100644 --- a/src/qtlibtorrent/qbtsession.cpp +++ b/src/qtlibtorrent/qbtsession.cpp @@ -212,8 +212,10 @@ void QBtSession::preAllocateAllFiles(bool b) { void QBtSession::processBigRatios() { qDebug("Process big ratios..."); std::vector torrents = s->get_torrents(); - std::vector::iterator torrentIT; - for (torrentIT = torrents.begin(); torrentIT != torrents.end(); ++torrentIT) { + + std::vector::iterator torrentIT = torrents.begin(); + std::vector::iterator torrentITend = torrents.end(); + for ( ; torrentIT != torrentITend; ++torrentIT) { const QTorrentHandle h(*torrentIT); if (!h.is_valid()) continue; if (h.is_seed()) { @@ -359,8 +361,10 @@ void QBtSession::configureSession() { } // Update torrent handles std::vector torrents = s->get_torrents(); - std::vector::iterator torrentIT; - for (torrentIT = torrents.begin(); torrentIT != torrents.end(); ++torrentIT) { + + std::vector::iterator torrentIT = torrents.begin(); + std::vector::iterator torrentITend = torrents.end(); + for ( ; torrentIT != torrentITend; ++torrentIT) { QTorrentHandle h = QTorrentHandle(*torrentIT); if (h.is_valid()) h.resolve_countries(resolve_countries); @@ -711,8 +715,10 @@ QTorrentHandle QBtSession::getTorrentHandle(const QString &hash) const { bool QBtSession::hasActiveTorrents() const { std::vector torrents = s->get_torrents(); - std::vector::iterator torrentIT; - for (torrentIT = torrents.begin(); torrentIT != torrents.end(); ++torrentIT) { + + std::vector::iterator torrentIT = torrents.begin(); + std::vector::iterator torrentITend = torrents.end(); + for ( ; torrentIT != torrentITend; ++torrentIT) { const QTorrentHandle h(*torrentIT); if (h.is_valid() && !h.is_paused() && !h.is_queued()) return true; @@ -722,8 +728,10 @@ bool QBtSession::hasActiveTorrents() const { bool QBtSession::hasDownloadingTorrents() const { std::vector torrents = s->get_torrents(); - std::vector::iterator torrentIT; - for (torrentIT = torrents.begin(); torrentIT != torrents.end(); ++torrentIT) { + + std::vector::iterator torrentIT = torrents.begin(); + std::vector::iterator torrentITend = torrents.end(); + for ( ; torrentIT != torrentITend; ++torrentIT) { if (torrentIT->is_valid()) { try { const torrent_status::state_t state = torrentIT->status().state; @@ -802,8 +810,10 @@ void QBtSession::deleteTorrent(const QString &hash, bool delete_local_files) { void QBtSession::pauseAllTorrents() { std::vector torrents = s->get_torrents(); - std::vector::iterator torrentIT; - for (torrentIT = torrents.begin(); torrentIT != torrents.end(); ++torrentIT) { + + std::vector::iterator torrentIT = torrents.begin(); + std::vector::iterator torrentITend = torrents.end(); + for ( ; torrentIT != torrentITend; ++torrentIT) { try { QTorrentHandle h = QTorrentHandle(*torrentIT); if (!h.is_paused()) { @@ -820,8 +830,10 @@ std::vector QBtSession::getTorrents() const { void QBtSession::resumeAllTorrents() { std::vector torrents = s->get_torrents(); - std::vector::iterator torrentIT; - for (torrentIT = torrents.begin(); torrentIT != torrents.end(); ++torrentIT) { + + std::vector::iterator torrentIT = torrents.begin(); + std::vector::iterator torrentITend = torrents.end(); + for ( ; torrentIT != torrentITend; ++torrentIT) { try { QTorrentHandle h = QTorrentHandle(*torrentIT); if (h.is_paused()) { @@ -1324,8 +1336,10 @@ void QBtSession::mergeTorrents(QTorrentHandle &h_ex, boost::intrusive_ptr 15 std::vector new_urlseeds = t->web_seeds(); - std::vector::iterator it; - for (it = new_urlseeds.begin(); it != new_urlseeds.end(); it++) { + + std::vector::iterator it = new_urlseeds.begin(); + std::vector::iterator itend = new_urlseeds.end(); + for ( ; it != itend; ++it) { const QString new_url = misc::toQString(it->url.c_str()); if (!old_urlseeds.contains(new_url)) { urlseeds_added = true; @@ -1334,8 +1348,10 @@ void QBtSession::mergeTorrents(QTorrentHandle &h_ex, boost::intrusive_ptr new_urlseeds = t->url_seeds(); - std::vector::iterator it; - for (it = new_urlseeds.begin(); it != new_urlseeds.end(); ++it) { + + std::vector::iterator it = new_urlseeds.begin(); + std::vector::iterator itend = new_urlseeds.end(); + for ( ; it != itend; ++it) { const QString new_url = misc::toQString(it->c_str()); if (!old_urlseeds.contains(new_url)) { urlseeds_added = true; @@ -1358,8 +1374,10 @@ void QBtSession::exportTorrentFiles(QString path) { } QDir torrentBackup(fsutils::BTBackupLocation()); std::vector handles = s->get_torrents(); - std::vector::iterator itr; - for (itr=handles.begin(); itr != handles.end(); ++itr) { + + std::vector::iterator itr=handles.begin(); + std::vector::iterator itrend=handles.end(); + for ( ; itr != itrend; ++itr) { const QTorrentHandle h(*itr); if (!h.is_valid()) { std::cerr << "Torrent Export: torrent is invalid, skipping..." << std::endl; @@ -1399,8 +1417,10 @@ void QBtSession::setMaxConnectionsPerTorrent(int max) { qDebug() << Q_FUNC_INFO << max; // Apply this to all session torrents std::vector handles = s->get_torrents(); - std::vector::const_iterator it; - for (it = handles.begin(); it != handles.end(); ++it) { + + std::vector::const_iterator it = handles.begin(); + std::vector::const_iterator itend = handles.end(); + for ( ; it != itend; ++it) { if (!it->is_valid()) continue; try { @@ -1413,8 +1433,10 @@ void QBtSession::setMaxUploadsPerTorrent(int max) { qDebug() << Q_FUNC_INFO << max; // Apply this to all session torrents std::vector handles = s->get_torrents(); - std::vector::const_iterator it; - for (it = handles.begin(); it != handles.end(); ++it) { + + std::vector::const_iterator it = handles.begin(); + std::vector::const_iterator itend = handles.end(); + for ( ; it != itend; ++it) { if (!it->is_valid()) continue; try { @@ -1564,8 +1586,10 @@ qreal QBtSession::getRealRatio(const QString &hash) const { // Called periodically void QBtSession::saveTempFastResumeData() { std::vector torrents = s->get_torrents(); - std::vector::iterator torrentIT; - for (torrentIT = torrents.begin(); torrentIT != torrents.end(); ++torrentIT) { + + std::vector::iterator torrentIT = torrents.begin(); + std::vector::iterator torrentITend = torrents.end(); + for ( ; torrentIT != torrentITend; ++torrentIT) { QTorrentHandle h = QTorrentHandle(*torrentIT); try { if (!h.is_valid() || !h.has_metadata() /*|| h.is_seed() || h.is_paused()*/) continue; @@ -1590,8 +1614,10 @@ void QBtSession::saveFastResumeData() { // Pause session s->pause(); std::vector torrents = s->get_torrents(); - std::vector::iterator torrentIT; - for (torrentIT = torrents.begin(); torrentIT != torrents.end(); ++torrentIT) { + + std::vector::iterator torrentIT = torrents.begin(); + std::vector::iterator torrentITend = torrents.end(); + for ( ; torrentIT != torrentITend; ++torrentIT) { QTorrentHandle h = QTorrentHandle(*torrentIT); if (!h.is_valid() || !h.has_metadata()) continue; try { @@ -1726,8 +1752,10 @@ void QBtSession::setDefaultTempPath(QString temppath) { // Disabling temp dir // Moving all torrents to their destination folder std::vector torrents = s->get_torrents(); - std::vector::iterator torrentIT; - for (torrentIT = torrents.begin(); torrentIT != torrents.end(); ++torrentIT) { + + std::vector::iterator torrentIT = torrents.begin(); + std::vector::iterator torrentITend = torrents.end(); + for ( ; torrentIT != torrentITend; ++torrentIT) { QTorrentHandle h = QTorrentHandle(*torrentIT); if (!h.is_valid()) continue; h.move_storage(getSavePath(h.hash())); @@ -1736,8 +1764,10 @@ void QBtSession::setDefaultTempPath(QString temppath) { qDebug("Enabling default temp path..."); // Moving all downloading torrents to temporary save path std::vector torrents = s->get_torrents(); - std::vector::iterator torrentIT; - for (torrentIT = torrents.begin(); torrentIT != torrents.end(); ++torrentIT) { + + std::vector::iterator torrentIT = torrents.begin(); + std::vector::iterator torrentITend = torrents.end(); + for ( ; torrentIT != torrentITend; ++torrentIT) { QTorrentHandle h = QTorrentHandle(*torrentIT); if (!h.is_valid()) continue; if (!h.is_seed()) { @@ -1811,8 +1841,10 @@ void QBtSession::setAppendLabelToSavePath(bool append) { if (appendLabelToSavePath) { // Move torrents storage to sub folder with label name std::vector torrents = s->get_torrents(); - std::vector::iterator torrentIT; - for (torrentIT = torrents.begin(); torrentIT != torrents.end(); ++torrentIT) { + + std::vector::iterator torrentIT = torrents.begin(); + std::vector::iterator torrentITend = torrents.end(); + for ( ; torrentIT != torrentITend; ++torrentIT) { QTorrentHandle h = QTorrentHandle(*torrentIT); appendLabelToTorrentSavePath(h); } @@ -1825,8 +1857,10 @@ void QBtSession::setAppendqBExtension(bool append) { appendqBExtension = !appendqBExtension; // append or remove .!qB extension for incomplete files std::vector torrents = s->get_torrents(); - std::vector::iterator torrentIT; - for (torrentIT = torrents.begin(); torrentIT != torrents.end(); ++torrentIT) { + + std::vector::iterator torrentIT = torrents.begin(); + std::vector::iterator torrentITend = torrents.end(); + for ( ; torrentIT != torrentITend; ++torrentIT) { QTorrentHandle h = QTorrentHandle(*torrentIT); appendqBextensionToTorrent(h, appendqBExtension); } @@ -2473,8 +2507,10 @@ void QBtSession::readAlerts() { qDebug() << "Sucessfully listening on" << p->endpoint.address().to_string(ec).c_str() << "/" << p->endpoint.port(); // Force reannounce on all torrents because some trackers blacklist some ports std::vector torrents = s->get_torrents(); - std::vector::iterator it; - for (it = torrents.begin(); it != torrents.end(); ++it) { + + std::vector::iterator it = torrents.begin(); + std::vector::iterator itend = torrents.end(); + for ( ; it != itend; ++it) { it->force_reannounce(); } emit listenSucceeded(); diff --git a/src/qtlibtorrent/qtorrenthandle.cpp b/src/qtlibtorrent/qtorrenthandle.cpp index 8d91cb352..f4ec4c575 100644 --- a/src/qtlibtorrent/qtorrenthandle.cpp +++ b/src/qtlibtorrent/qtorrenthandle.cpp @@ -281,8 +281,10 @@ QStringList QTorrentHandle::url_seeds() const { QStringList res; try { const std::set existing_seeds = torrent_handle::url_seeds(); - std::set::const_iterator it; - for (it = existing_seeds.begin(); it != existing_seeds.end(); ++it) { + + std::set::const_iterator it = existing_seeds.begin(); + std::set::const_iterator itend = existing_seeds.end(); + for ( ; it != itend; ++it) { qDebug("URL Seed: %s", it->c_str()); res << misc::toQString(*it); } @@ -582,7 +584,10 @@ QString QTorrentHandle::error() const { void QTorrentHandle::downloading_pieces(bitfield &bf) const { std::vector queue; torrent_handle::get_download_queue(queue); - for (std::vector::const_iterator it=queue.begin(); it!= queue.end(); ++it) { + + std::vector::const_iterator it = queue.begin(); + std::vector::const_iterator itend = queue.end(); + for ( ; it!= itend; ++it) { bf.set_bit(it->piece_index); } return; diff --git a/src/qtlibtorrent/torrentmodel.cpp b/src/qtlibtorrent/torrentmodel.cpp index aa0b8932b..0b007868e 100644 --- a/src/qtlibtorrent/torrentmodel.cpp +++ b/src/qtlibtorrent/torrentmodel.cpp @@ -205,8 +205,10 @@ TorrentModel::TorrentModel(QObject *parent) : void TorrentModel::populate() { // Load the torrents std::vector torrents = QBtSession::instance()->getSession()->get_torrents(); - std::vector::const_iterator it; - for (it = torrents.begin(); it != torrents.end(); ++it) { + + std::vector::const_iterator it = torrents.begin(); + std::vector::const_iterator itend = torrents.end(); + for ( ; it != itend; ++it) { addTorrent(QTorrentHandle(*it)); } // Refresh timer @@ -313,9 +315,11 @@ bool TorrentModel::setData(const QModelIndex &index, const QVariant &value, int int TorrentModel::torrentRow(const QString &hash) const { - QList::const_iterator it; int row = 0; - for (it = m_torrents.constBegin(); it != m_torrents.constEnd(); ++it) { + + QList::const_iterator it = m_torrents.constBegin(); + QList::const_iterator itend = m_torrents.constEnd(); + for ( ; it != itend; ++it) { if ((*it)->hash() == hash) return row; ++row; } @@ -395,8 +399,10 @@ void TorrentModel::forceModelRefresh() TorrentStatusReport TorrentModel::getTorrentStatusReport() const { TorrentStatusReport report; - QList::const_iterator it; - for (it = m_torrents.constBegin(); it != m_torrents.constEnd(); ++it) { + + QList::const_iterator it = m_torrents.constBegin(); + QList::const_iterator itend = m_torrents.constEnd(); + for ( ; it != itend; ++it) { switch((*it)->data(TorrentModelItem::TR_STATUS).toInt()) { case TorrentModelItem::STATE_DOWNLOADING: ++report.nb_active; diff --git a/src/qtlibtorrent/torrentspeedmonitor.cpp b/src/qtlibtorrent/torrentspeedmonitor.cpp index 969074d77..56bbfde3e 100644 --- a/src/qtlibtorrent/torrentspeedmonitor.cpp +++ b/src/qtlibtorrent/torrentspeedmonitor.cpp @@ -122,8 +122,10 @@ qlonglong TorrentSpeedMonitor::getETA(const QString &hash) const void TorrentSpeedMonitor::getSamples() { const std::vector torrents = m_session->getSession()->get_torrents(); - std::vector::const_iterator it; - for (it = torrents.begin(); it != torrents.end(); ++it) { + + std::vector::const_iterator it = torrents.begin(); + std::vector::const_iterator itend = torrents.end(); + for ( ; it != itend; ++it) { try { #if LIBTORRENT_VERSION_MINOR > 15 torrent_status st = it->status(0x0); diff --git a/src/torrentpersistentdata.h b/src/torrentpersistentdata.h index 274f730b1..23365edf2 100644 --- a/src/torrentpersistentdata.h +++ b/src/torrentpersistentdata.h @@ -63,9 +63,11 @@ public: QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); QHash all_data = settings.value("torrents-tmp").toHash(); QHash data = all_data.value(hash).toHash(); - std::vector::const_iterator pp_it = pp.begin(); QStringList pieces_priority; - while(pp_it != pp.end()) { + + std::vector::const_iterator pp_it = pp.begin(); + std::vector::const_iterator pp_itend = pp.end(); + while(pp_it != pp_itend) { pieces_priority << QString::number(*pp_it); ++pp_it; } @@ -206,8 +208,9 @@ public: static bool hasPerTorrentRatioLimit() { QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); const QHash all_data = settings.value("torrents").toHash(); - QHash::ConstIterator it; - for (it = all_data.constBegin(); it != all_data.constEnd(); it++) { + QHash::ConstIterator it = all_data.constBegin(); + QHash::ConstIterator itend = all_data.constEnd(); + for ( ; it != itend; ++it) { if (it.value().toHash().value("max_ratio", USE_GLOBAL_RATIO).toReal() >= 0) { return true; }