Avoid map/set double lookups
Fix some common anti-patterns with these data structures. - You can dereference the iterator returned by `find` to access the underlying value directly, without an extra `operator[]`/`at`. - Rather than checking for an element before insertion/deletion, you can just do the operation and if needed check the return value to determine if the insertion/deletion succeeded.
This commit is contained in:
1 parent
a5e85caf0a
commit
f2392e4048
15 files changed
+69
-74
No files matched your search
@@ -251,17 +251,18 @@ static std::unique_ptr<QDirIterator> GetIterator(const QString& dir)
|
||||
void GameTracker::RemoveDirectoryInternal(const QString& dir)
|
||||
{
|
||||
RemovePath(dir);
|
||||
auto it = GetIterator(dir);
|
||||
while (it->hasNext())
|
||||
const auto dir_it = GetIterator(dir);
|
||||
while (dir_it->hasNext())
|
||||
{
|
||||
QString path = QFileInfo(it->next()).canonicalFilePath();
|
||||
if (m_tracked_files.contains(path))
|
||||
QString path = QFileInfo(dir_it->next()).canonicalFilePath();
|
||||
if (const auto it = m_tracked_files.find(path); it != m_tracked_files.end())
|
||||
{
|
||||
m_tracked_files[path].remove(dir);
|
||||
if (m_tracked_files[path].empty())
|
||||
auto& set = *it;
|
||||
set.remove(dir);
|
||||
if (set.isEmpty())
|
||||
{
|
||||
RemovePath(path);
|
||||
m_tracked_files.remove(path);
|
||||
m_tracked_files.erase(it);
|
||||
if (m_started)
|
||||
emit GameRemoved(path.toStdString());
|
||||
}
|
||||
@@ -271,16 +272,14 @@ void GameTracker::RemoveDirectoryInternal(const QString& dir)
|
||||
|
||||
void GameTracker::UpdateDirectoryInternal(const QString& dir)
|
||||
{
|
||||
auto it = GetIterator(dir);
|
||||
while (it->hasNext() && !m_processing_halted)
|
||||
const auto dir_it = GetIterator(dir);
|
||||
while (dir_it->hasNext() && !m_processing_halted)
|
||||
{
|
||||
QString path = QFileInfo(it->next()).canonicalFilePath();
|
||||
QString path = QFileInfo(dir_it->next()).canonicalFilePath();
|
||||
|
||||
if (m_tracked_files.contains(path))
|
||||
if (const auto it = m_tracked_files.find(path); it != m_tracked_files.end())
|
||||
{
|
||||
auto& tracked_file = m_tracked_files[path];
|
||||
if (!tracked_file.contains(dir))
|
||||
tracked_file.insert(dir);
|
||||
it->insert(dir);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -339,8 +338,7 @@ QSet<QString> GameTracker::FindMissingFiles(const QString& dir)
|
||||
while (it->hasNext())
|
||||
{
|
||||
QString path = QFileInfo(it->next()).canonicalFilePath();
|
||||
if (m_tracked_files.contains(path))
|
||||
missing_files.remove(path);
|
||||
m_tracked_files.remove(path);
|
||||
}
|
||||
|
||||
return missing_files;
|
||||
|
||||
Reference in new issue
Block a user