From 2fa693170adda89b4f9dad10abc855e6b8ca8642 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 30 Jun 2023 14:36:08 -0500 Subject: [PATCH] refactor: prefer std::unordred_map over QHash (#5701) * refactor: use std::unordered_map instead of QHash in RpcClient.cc * chore remove unused #include * refactor: use std::unordered_map instead of QHash in FileTreeItem --- qt/FileTreeItem.cc | 33 +++++++++++++++------------------ qt/FileTreeItem.h | 10 ++++++---- qt/RpcClient.cc | 16 +++++++++------- qt/RpcClient.h | 4 ++-- qt/Utils.h | 1 - 5 files changed, 32 insertions(+), 32 deletions(-) diff --git a/qt/FileTreeItem.cc b/qt/FileTreeItem.cc index eab839b37..65f4e196c 100644 --- a/qt/FileTreeItem.cc +++ b/qt/FileTreeItem.cc @@ -18,16 +18,11 @@ #include "Formatter.h" #include "IconCache.h" -QHash const& FileTreeItem::getMyChildRows() +std::unordered_map const& FileTreeItem::getMyChildRows() const { - int const n = childCount(); - // ensure that all the rows are hashed - while (first_unhashed_row_ < n) - { - child_rows_.insert(children_[first_unhashed_row_]->name(), first_unhashed_row_); - ++first_unhashed_row_; - } + for (int const n = childCount(); first_unhashed_row_ < n; ++first_unhashed_row_) + child_rows_.emplace(children_[first_unhashed_row_]->name(), first_unhashed_row_); return child_rows_; } @@ -50,7 +45,7 @@ FileTreeItem::~FileTreeItem() } // remove this child from the parent - parent_->child_rows_.remove(name()); + parent_->child_rows_.erase(name()); it = siblings.erase(it); // 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* 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()); + return item; } - return item; + return {}; } 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); - assert(i == -1 || this == parent_->children_[i]); + auto const idx = iter->second; + assert(this == parent_->children_[idx]); + return idx; } - return i; + return -1; } QVariant FileTreeItem::data(int column, int role) const diff --git a/qt/FileTreeItem.h b/qt/FileTreeItem.h index 4de9230e5..467e7b1c7 100644 --- a/qt/FileTreeItem.h +++ b/qt/FileTreeItem.h @@ -6,16 +6,18 @@ #pragma once #include +#include #include #include -#include #include #include #include #include +#include "Utils.h" // for std::hash + class FileTreeItem { Q_DECLARE_TR_FUNCTIONS(FileTreeItem) @@ -91,15 +93,15 @@ private: void getSubtreeWantedSize(uint64_t& have, uint64_t& total) const; double progress() const; uint64_t size() const; - QHash const& getMyChildRows(); + std::unordered_map const& getMyChildRows() const; FileTreeItem* parent_ = {}; - QHash child_rows_; + mutable std::unordered_map child_rows_; std::vector children_; QString name_; uint64_t const total_size_ = {}; uint64_t have_size_ = {}; - int first_unhashed_row_ = {}; + mutable int first_unhashed_row_ = {}; int const file_index_ = {}; int priority_ = {}; bool is_wanted_ = {}; diff --git a/qt/RpcClient.cc b/qt/RpcClient.cc index 8e7dd8f5b..3c1ca3081 100644 --- a/qt/RpcClient.cc +++ b/qt/RpcClient.cc @@ -167,7 +167,7 @@ void RpcClient::sendLocalRequest(TrVariantPtr json, QFutureInterface promise = local_requests_.take(tag); + if (auto node = local_requests_.extract(parseResponseTag(*response)); node) + { + auto const result = parseResponseData(*response); - promise.setProgressRange(0, 1); - promise.setProgressValue(1); - promise.reportFinished(&result); + auto& promise = node.mapped(); + promise.setProgressRange(0, 1); + promise.setProgressValue(1); + promise.reportFinished(&result); + } } int64_t RpcClient::parseResponseTag(tr_variant& response) const diff --git a/qt/RpcClient.h b/qt/RpcClient.h index 4777620dc..8774c2f49 100644 --- a/qt/RpcClient.h +++ b/qt/RpcClient.h @@ -9,10 +9,10 @@ #include #include #include +#include #include #include -#include #include #include #include @@ -93,7 +93,7 @@ private: QString session_id_; QUrl url_; QNetworkAccessManager* nam_ = {}; - QHash> local_requests_; + std::unordered_map> local_requests_; int64_t next_tag_ = {}; bool const verbose_ = qEnvironmentVariableIsSet("TR_RPC_VERBOSE"); }; diff --git a/qt/Utils.h b/qt/Utils.h index fe585c756..12521062f 100644 --- a/qt/Utils.h +++ b/qt/Utils.h @@ -8,7 +8,6 @@ #include // size_t #include -#include #include #include #include