mirror of
https://github.com/transmission/transmission
synced 2025-01-30 10:52:00 +00:00
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:
parent
bdd813a4c3
commit
2fa693170a
5 changed files with 32 additions and 32 deletions
|
@ -18,16 +18,11 @@
|
|||
#include "Formatter.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
|
||||
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
|
||||
|
|
|
@ -6,16 +6,18 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QHash>
|
||||
#include <QSet>
|
||||
#include <QString>
|
||||
#include <QVariant>
|
||||
|
||||
#include <libtransmission/tr-macros.h>
|
||||
|
||||
#include "Utils.h" // for std::hash<QString>
|
||||
|
||||
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<QString, int> const& getMyChildRows();
|
||||
std::unordered_map<QString, int> const& getMyChildRows() const;
|
||||
|
||||
FileTreeItem* parent_ = {};
|
||||
QHash<QString, int> child_rows_;
|
||||
mutable std::unordered_map<QString, int> child_rows_;
|
||||
std::vector<FileTreeItem*> 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_ = {};
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
local_requests_.insert(tag, promise);
|
||||
local_requests_.try_emplace(tag, promise);
|
||||
tr_rpc_request_exec_json(session_, json.get(), localSessionCallback, this);
|
||||
}
|
||||
|
||||
|
@ -287,13 +287,15 @@ void RpcClient::networkRequestFinished(QNetworkReply* reply)
|
|||
|
||||
void RpcClient::localRequestFinished(TrVariantPtr response)
|
||||
{
|
||||
int64_t const tag = parseResponseTag(*response);
|
||||
RpcResponse const result = parseResponseData(*response);
|
||||
QFutureInterface<RpcResponse> 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
|
||||
|
|
|
@ -9,10 +9,10 @@
|
|||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <QFuture>
|
||||
#include <QFutureInterface>
|
||||
#include <QHash>
|
||||
#include <QNetworkRequest>
|
||||
#include <QNetworkReply>
|
||||
#include <QObject>
|
||||
|
@ -93,7 +93,7 @@ private:
|
|||
QString session_id_;
|
||||
QUrl url_;
|
||||
QNetworkAccessManager* nam_ = {};
|
||||
QHash<int64_t, QFutureInterface<RpcResponse>> local_requests_;
|
||||
std::unordered_map<int64_t, QFutureInterface<RpcResponse>> local_requests_;
|
||||
int64_t next_tag_ = {};
|
||||
bool const verbose_ = qEnvironmentVariableIsSet("TR_RPC_VERBOSE");
|
||||
};
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
#include <cstddef> // size_t
|
||||
#include <utility>
|
||||
|
||||
#include <QHash>
|
||||
#include <QPointer>
|
||||
#include <QRect>
|
||||
#include <QSpinBox>
|
||||
|
|
Loading…
Reference in a new issue