Speed up lookup operation in TransferListModel

Previously lookup is O(n), add operation is O(n), remove operation is
O(n).
Now lookup is O(1), add operation is O(1), remove operation is O(n).
n is the number of torrents already recorded.
This commit is contained in:
Chocobo1
2019-08-17 17:09:31 +08:00
parent 9c964cdd97
commit 863c9f9876
2 changed files with 36 additions and 29 deletions

View File

@@ -84,8 +84,8 @@ public:
explicit TransferListModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &index = {}) const override;
int columnCount(const QModelIndex &parent=QModelIndex()) const override;
int rowCount(const QModelIndex &parent = {}) const override;
int columnCount(const QModelIndex &parent = {}) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
@@ -100,7 +100,8 @@ private slots:
void handleTorrentsUpdated(const QVector<BitTorrent::TorrentHandle *> &torrents);
private:
QList<BitTorrent::TorrentHandle *> m_torrents;
QList<BitTorrent::TorrentHandle *> m_torrentList; // maps row number to torrent handle
QHash<BitTorrent::TorrentHandle *, int> m_torrentMap; // maps torrent handle to row number
};
#endif // TRANSFERLISTMODEL_H