refactor: prefer std::unordred_map over QHash (#5701)

* refactor: use std::unordered_map instead of QHash in RpcClient.cc

* chore remove unused #include <QHash>

* refactor: use std::unordered_map instead of QHash in FileTreeItem
This commit is contained in:
Charles Kerr 2023-06-30 14:36:08 -05:00 committed by GitHub
parent bdd813a4c3
commit 2fa693170a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 32 deletions

View File

@ -18,16 +18,11 @@
#include "Formatter.h" #include "Formatter.h"
#include "IconCache.h" #include "IconCache.h"
QHash<QString, int> const& FileTreeItem::getMyChildRows() std::unordered_map<QString, int> const& FileTreeItem::getMyChildRows() const
{ {
int const n = childCount();
// ensure that all the rows are hashed // ensure that all the rows are hashed
while (first_unhashed_row_ < n) for (int const n = childCount(); first_unhashed_row_ < n; ++first_unhashed_row_)
{ child_rows_.emplace(children_[first_unhashed_row_]->name(), first_unhashed_row_);
child_rows_.insert(children_[first_unhashed_row_]->name(), first_unhashed_row_);
++first_unhashed_row_;
}
return child_rows_; return child_rows_;
} }
@ -50,7 +45,7 @@ FileTreeItem::~FileTreeItem()
} }
// remove this child from the parent // remove this child from the parent
parent_->child_rows_.remove(name()); parent_->child_rows_.erase(name());
it = siblings.erase(it); it = siblings.erase(it);
// invalidate the row numbers of the siblings that came after this child // invalidate the row numbers of the siblings that came after this child
@ -67,28 +62,30 @@ void FileTreeItem::appendChild(FileTreeItem* child)
FileTreeItem* FileTreeItem::child(QString const& filename) FileTreeItem* FileTreeItem::child(QString const& filename)
{ {
FileTreeItem* item(nullptr); auto const& child_rows = getMyChildRows();
if (int const row = getMyChildRows().value(filename, -1); row != -1) if (auto const iter = child_rows.find(filename); iter != std::end(child_rows))
{ {
item = child(row); auto* const item = child(iter->second);
assert(filename == item->name()); assert(filename == item->name());
return item;
} }
return item; return {};
} }
int FileTreeItem::row() const int FileTreeItem::row() const
{ {
int i(-1); auto const& child_rows = parent_->getMyChildRows();
if (parent_ != nullptr) if (auto const iter = child_rows.find(name()); iter != std::end(child_rows))
{ {
i = parent_->getMyChildRows().value(name(), -1); auto const idx = iter->second;
assert(i == -1 || this == parent_->children_[i]); assert(this == parent_->children_[idx]);
return idx;
} }
return i; return -1;
} }
QVariant FileTreeItem::data(int column, int role) const QVariant FileTreeItem::data(int column, int role) const

View File

@ -6,16 +6,18 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include <unordered_map>
#include <vector> #include <vector>
#include <QCoreApplication> #include <QCoreApplication>
#include <QHash>
#include <QSet> #include <QSet>
#include <QString> #include <QString>
#include <QVariant> #include <QVariant>
#include <libtransmission/tr-macros.h> #include <libtransmission/tr-macros.h>
#include "Utils.h" // for std::hash<QString>
class FileTreeItem class FileTreeItem
{ {
Q_DECLARE_TR_FUNCTIONS(FileTreeItem) Q_DECLARE_TR_FUNCTIONS(FileTreeItem)
@ -91,15 +93,15 @@ private:
void getSubtreeWantedSize(uint64_t& have, uint64_t& total) const; void getSubtreeWantedSize(uint64_t& have, uint64_t& total) const;
double progress() const; double progress() const;
uint64_t size() const; uint64_t size() const;
QHash<QString, int> const& getMyChildRows(); std::unordered_map<QString, int> const& getMyChildRows() const;
FileTreeItem* parent_ = {}; FileTreeItem* parent_ = {};
QHash<QString, int> child_rows_; mutable std::unordered_map<QString, int> child_rows_;
std::vector<FileTreeItem*> children_; std::vector<FileTreeItem*> children_;
QString name_; QString name_;
uint64_t const total_size_ = {}; uint64_t const total_size_ = {};
uint64_t have_size_ = {}; uint64_t have_size_ = {};
int first_unhashed_row_ = {}; mutable int first_unhashed_row_ = {};
int const file_index_ = {}; int const file_index_ = {};
int priority_ = {}; int priority_ = {};
bool is_wanted_ = {}; bool is_wanted_ = {};

View File

@ -167,7 +167,7 @@ void RpcClient::sendLocalRequest(TrVariantPtr json, QFutureInterface<RpcResponse
fmt::print("{:s}:{:d} sending req:\n{:s}\n", __FILE__, __LINE__, tr_variantToStr(json.get(), TR_VARIANT_FMT_JSON)); fmt::print("{:s}:{:d} sending req:\n{:s}\n", __FILE__, __LINE__, tr_variantToStr(json.get(), TR_VARIANT_FMT_JSON));
} }
local_requests_.insert(tag, promise); local_requests_.try_emplace(tag, promise);
tr_rpc_request_exec_json(session_, json.get(), localSessionCallback, this); tr_rpc_request_exec_json(session_, json.get(), localSessionCallback, this);
} }
@ -287,13 +287,15 @@ void RpcClient::networkRequestFinished(QNetworkReply* reply)
void RpcClient::localRequestFinished(TrVariantPtr response) void RpcClient::localRequestFinished(TrVariantPtr response)
{ {
int64_t const tag = parseResponseTag(*response); if (auto node = local_requests_.extract(parseResponseTag(*response)); node)
RpcResponse const result = parseResponseData(*response); {
QFutureInterface<RpcResponse> promise = local_requests_.take(tag); auto const result = parseResponseData(*response);
promise.setProgressRange(0, 1); auto& promise = node.mapped();
promise.setProgressValue(1); promise.setProgressRange(0, 1);
promise.reportFinished(&result); promise.setProgressValue(1);
promise.reportFinished(&result);
}
} }
int64_t RpcClient::parseResponseTag(tr_variant& response) const int64_t RpcClient::parseResponseTag(tr_variant& response) const

View File

@ -9,10 +9,10 @@
#include <memory> #include <memory>
#include <optional> #include <optional>
#include <string_view> #include <string_view>
#include <unordered_map>
#include <QFuture> #include <QFuture>
#include <QFutureInterface> #include <QFutureInterface>
#include <QHash>
#include <QNetworkRequest> #include <QNetworkRequest>
#include <QNetworkReply> #include <QNetworkReply>
#include <QObject> #include <QObject>
@ -93,7 +93,7 @@ private:
QString session_id_; QString session_id_;
QUrl url_; QUrl url_;
QNetworkAccessManager* nam_ = {}; QNetworkAccessManager* nam_ = {};
QHash<int64_t, QFutureInterface<RpcResponse>> local_requests_; std::unordered_map<int64_t, QFutureInterface<RpcResponse>> local_requests_;
int64_t next_tag_ = {}; int64_t next_tag_ = {};
bool const verbose_ = qEnvironmentVariableIsSet("TR_RPC_VERBOSE"); bool const verbose_ = qEnvironmentVariableIsSet("TR_RPC_VERBOSE");
}; };

View File

@ -8,7 +8,6 @@
#include <cstddef> // size_t #include <cstddef> // size_t
#include <utility> #include <utility>
#include <QHash>
#include <QPointer> #include <QPointer>
#include <QRect> #include <QRect>
#include <QSpinBox> #include <QSpinBox>