From f5d8e4de83c8a9c04cd0c542507f5e2c9bf72996 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 10 Feb 2022 12:23:52 +0300 Subject: [PATCH] Use table_remove_if. --- td/telegram/ContactsManager.cpp | 11 ++--------- td/telegram/files/FileStats.cpp | 18 +++++++----------- tddb/td/db/BinlogKeyValue.h | 14 +++++++------- 3 files changed, 16 insertions(+), 27 deletions(-) diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 26a9bdb13..536c62d49 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -3442,15 +3442,8 @@ void ContactsManager::on_channel_participant_cache_timeout(ChannelId channel_id) auto &participants = channel_participants_it->second.participants_; auto min_access_date = G()->unix_time() - CHANNEL_PARTICIPANT_CACHE_TIME; - vector to_delete_dialog_ids; - for (auto it = participants.begin(); it != participants.end(); ++it) { - if (it->second.last_access_date_ < min_access_date) { - to_delete_dialog_ids.push_back(it->first); - } - } - for (auto dialog_id : to_delete_dialog_ids) { - participants.erase(dialog_id); - } + table_remove_if(participants, + [min_access_date](const auto &it) { return it.second.last_access_date_ < min_access_date; }); if (participants.empty()) { channel_participants_.erase(channel_participants_it); diff --git a/td/telegram/files/FileStats.cpp b/td/telegram/files/FileStats.cpp index 5a8e2d73f..5b4138d16 100644 --- a/td/telegram/files/FileStats.cpp +++ b/td/telegram/files/FileStats.cpp @@ -112,21 +112,17 @@ void FileStats::apply_dialog_ids(const vector &dialog_ids) { std::unordered_set all_dialogs(dialog_ids.begin(), dialog_ids.end()); StatByType other_stats; bool other_flag = false; - std::vector to_remove; - for (auto it = stat_by_owner_dialog_id_.begin(); it != stat_by_owner_dialog_id_.end(); ++it) { - if (!all_dialogs.count(it->first)) { + table_remove_if(stat_by_owner_dialog_id_, [&](const auto &it) { + if (!all_dialogs.count(it.first)) { for (int32 i = 0; i < MAX_FILE_TYPE; i++) { - other_stats[i].size += it->second[i].size; - other_stats[i].cnt += it->second[i].cnt; + other_stats[i].size += it.second[i].size; + other_stats[i].cnt += it.second[i].cnt; } other_flag = true; - to_remove.push_back(it->first); + return true; } - } - - for (auto id : to_remove) { - stat_by_owner_dialog_id_.erase(id); - } + return false; + }); if (other_flag) { DialogId other_dialog_id; // prevents MSVC warning C4709: comma operator within array index expression diff --git a/tddb/td/db/BinlogKeyValue.h b/tddb/td/db/BinlogKeyValue.h index 8ab2cc463..1759308cf 100644 --- a/tddb/td/db/BinlogKeyValue.h +++ b/tddb/td/db/BinlogKeyValue.h @@ -13,6 +13,7 @@ #include "td/actor/PromiseFuture.h" +#include "td/utils/algorithm.h" #include "td/utils/buffer.h" #include "td/utils/common.h" #include "td/utils/logging.h" @@ -215,14 +216,13 @@ class BinlogKeyValue final : public KeyValueSyncInterface { void erase_by_prefix(Slice prefix) final { auto lock = rw_mutex_.lock_write().move_as_ok(); vector ids; - for (auto it = map_.begin(); it != map_.end();) { - if (begins_with(it->first, prefix)) { - ids.push_back(it->second.second); - it = map_.erase(it); - } else { - ++it; + table_remove_if(map_, [&](const auto &it) { + if (begins_with(it.first, prefix)) { + ids.push_back(it.second.second); + return true; } - } + return false; + }); auto seq_no = binlog_->next_id(narrow_cast(ids.size())); lock.reset(); for (auto id : ids) {