From 56c3a2fbed0427255f54c69dab01cac8cff2db66 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 21 Oct 2019 16:25:56 +0300 Subject: [PATCH] Use td::remove_if. GitOrigin-RevId: 5cf21381966dc58689a06cc94cb8843fd8087bb9 --- td/telegram/ConfigManager.cpp | 4 +- td/telegram/MessageEntity.cpp | 29 ++++------ td/telegram/MessagesManager.cpp | 4 +- td/telegram/StickersManager.cpp | 16 +++--- td/telegram/cli.cpp | 2 +- td/telegram/files/FileGcWorker.cpp | 76 ++++++++++++-------------- td/telegram/files/FileManager.cpp | 3 +- td/telegram/net/AuthDataShared.cpp | 7 +-- td/telegram/net/ConnectionCreator.cpp | 14 ++--- td/telegram/net/DcOptionsSet.cpp | 10 ++-- td/telegram/net/PublicRsaKeyShared.cpp | 4 +- td/telegram/net/Session.cpp | 4 +- tdutils/td/utils/misc.h | 2 +- 13 files changed, 74 insertions(+), 101 deletions(-) diff --git a/td/telegram/ConfigManager.cpp b/td/telegram/ConfigManager.cpp index 5c7ac821d..d8ae7ab4a 100644 --- a/td/telegram/ConfigManager.cpp +++ b/td/telegram/ConfigManager.cpp @@ -468,9 +468,7 @@ ActorOwn<> get_full_config(DcOption option, Promise promise, ActorSh std::vector> auth_key_listeners_; void notify() { - auto it = std::remove_if(auth_key_listeners_.begin(), auth_key_listeners_.end(), - [&](auto &listener) { return !listener->notify(); }); - auth_key_listeners_.erase(it, auth_key_listeners_.end()); + td::remove_if(auth_key_listeners_, [&](auto &listener) { return !listener->notify(); }); } string auth_key_key() const { diff --git a/td/telegram/MessageEntity.cpp b/td/telegram/MessageEntity.cpp index 0a69a8427..6c580daf9 100644 --- a/td/telegram/MessageEntity.cpp +++ b/td/telegram/MessageEntity.cpp @@ -985,15 +985,13 @@ const std::unordered_set &get_valid_short_usernames() { vector find_mentions(Slice str) { auto mentions = match_mentions(str); - mentions.erase(std::remove_if(mentions.begin(), mentions.end(), - [](Slice mention) { - mention.remove_prefix(1); - if (mention.size() >= 5) { - return false; - } - return get_valid_short_usernames().count(mention) == 0; - }), - mentions.end()); + td::remove_if(mentions, [](Slice mention) { + mention.remove_prefix(1); + if (mention.size() >= 5) { + return false; + } + return get_valid_short_usernames().count(mention) == 0; + }); return mentions; } @@ -2572,9 +2570,7 @@ static std::pair remove_invalid_entities(const string &text, vect CHECK(nested_entities_stack.empty()); CHECK(current_entity == entities.size()); - entities.erase( - std::remove_if(entities.begin(), entities.end(), [](const auto &entity) { return entity.length == 0; }), - entities.end()); + td::remove_if(entities, [](const auto &entity) { return entity.length == 0; }); return {last_non_whitespace_pos, last_non_whitespace_utf16_offset}; } @@ -2651,12 +2647,9 @@ Status fix_formatted_text(string &text, vector &entities, bool al } text.resize(new_size); - entities.erase( - std::remove_if(entities.begin(), entities.end(), - [text_utf16_length = narrow_cast(utf8_utf16_length(text))](const auto &entity) { - return entity.offset + entity.length > text_utf16_length; - }), - entities.end()); + td::remove_if(entities, [text_utf16_length = narrow_cast(utf8_utf16_length(text))](const auto &entity) { + return entity.offset + entity.length > text_utf16_length; + }); } if (!skip_new_entities) { diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 825928e13..372854df6 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -13476,9 +13476,7 @@ int32 MessagesManager::get_pinned_dialogs_limit(FolderId folder_id) { } vector MessagesManager::remove_secret_chat_dialog_ids(vector dialog_ids) { - dialog_ids.erase(std::remove_if(dialog_ids.begin(), dialog_ids.end(), - [](DialogId dialog_id) { return dialog_id.get_type() == DialogType::SecretChat; }), - dialog_ids.end()); + td::remove_if(dialog_ids, [](DialogId dialog_id) { return dialog_id.get_type() == DialogType::SecretChat; }); return dialog_ids; } diff --git a/td/telegram/StickersManager.cpp b/td/telegram/StickersManager.cpp index 3df42fbcb..8122e7230 100644 --- a/td/telegram/StickersManager.cpp +++ b/td/telegram/StickersManager.cpp @@ -4873,15 +4873,13 @@ void StickersManager::on_get_language_codes(const string &key, Result now - max_time_from_last_access double now = Clocks::system(); - files.erase( - std::remove_if( - files.begin(), files.end(), - [&](const FullFileInfo &info) { - if (token_) { - return false; - } - if (immune_types[narrow_cast(info.file_type)]) { - type_immunity_ignored_cnt++; - new_stats.add(FullFileInfo(info)); - return true; - } - if (std::find(parameters.exclude_owner_dialog_ids.begin(), parameters.exclude_owner_dialog_ids.end(), - info.owner_dialog_id) != parameters.exclude_owner_dialog_ids.end()) { - exclude_owner_dialog_id_ignored_cnt++; - new_stats.add(FullFileInfo(info)); - return true; - } - if (!parameters.owner_dialog_ids.empty() && - std::find(parameters.owner_dialog_ids.begin(), parameters.owner_dialog_ids.end(), - info.owner_dialog_id) == parameters.owner_dialog_ids.end()) { - owner_dialog_id_ignored_cnt++; - new_stats.add(FullFileInfo(info)); - return true; - } - if (static_cast(info.mtime_nsec / 1000000000) > now - parameters.immunity_delay) { - // new files are immune to gc - time_immunity_ignored_cnt++; - new_stats.add(FullFileInfo(info)); - return true; - } + td::remove_if(files, [&](const FullFileInfo &info) { + if (token_) { + return false; + } + if (immune_types[narrow_cast(info.file_type)]) { + type_immunity_ignored_cnt++; + new_stats.add(FullFileInfo(info)); + return true; + } + if (std::find(parameters.exclude_owner_dialog_ids.begin(), parameters.exclude_owner_dialog_ids.end(), + info.owner_dialog_id) != parameters.exclude_owner_dialog_ids.end()) { + exclude_owner_dialog_id_ignored_cnt++; + new_stats.add(FullFileInfo(info)); + return true; + } + if (!parameters.owner_dialog_ids.empty() && + std::find(parameters.owner_dialog_ids.begin(), parameters.owner_dialog_ids.end(), info.owner_dialog_id) == + parameters.owner_dialog_ids.end()) { + owner_dialog_id_ignored_cnt++; + new_stats.add(FullFileInfo(info)); + return true; + } + if (static_cast(info.mtime_nsec / 1000000000) > now - parameters.immunity_delay) { + // new files are immune to gc + time_immunity_ignored_cnt++; + new_stats.add(FullFileInfo(info)); + return true; + } - if (static_cast(info.atime_nsec / 1000000000) < now - parameters.max_time_from_last_access) { - do_remove_file(info); - total_removed_size += info.size; - remove_by_atime_cnt++; - return true; - } - return false; - }), - files.end()); + if (static_cast(info.atime_nsec / 1000000000) < now - parameters.max_time_from_last_access) { + do_remove_file(info); + total_removed_size += info.size; + remove_by_atime_cnt++; + return true; + } + return false; + }); if (token_) { return promise.set_error(Status::Error(500, "Request aborted")); } diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index 7f1753147..b6f6d7324 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -2638,8 +2638,7 @@ void FileManager::run_upload(FileNodePtr node, std::vector bad_parts) { } auto new_priority = narrow_cast(bad_parts.empty() ? -priority : priority); - bad_parts.erase(std::remove_if(bad_parts.begin(), bad_parts.end(), [](auto part_id) { return part_id < 0; }), - bad_parts.end()); + td::remove_if(bad_parts, [](auto part_id) { return part_id < 0; }); QueryId id = queries_container_.create(Query{file_id, Query::Upload}); node->upload_id_ = id; diff --git a/td/telegram/net/AuthDataShared.cpp b/td/telegram/net/AuthDataShared.cpp index 4e2eac560..3a64836e5 100644 --- a/td/telegram/net/AuthDataShared.cpp +++ b/td/telegram/net/AuthDataShared.cpp @@ -11,11 +11,10 @@ #include "td/utils/format.h" #include "td/utils/logging.h" +#include "td/utils/misc.h" #include "td/utils/port/RwMutex.h" #include "td/utils/tl_helpers.h" -#include - namespace td { class AuthDataSharedImpl : public AuthDataShared { @@ -103,9 +102,7 @@ class AuthDataSharedImpl : public AuthDataShared { void notify() { auto lock = rw_mutex_.lock_read(); - auto it = std::remove_if(auth_key_listeners_.begin(), auth_key_listeners_.end(), - [&](auto &listener) { return !listener->notify(); }); - auth_key_listeners_.erase(it, auth_key_listeners_.end()); + td::remove_if(auth_key_listeners_, [&](auto &listener) { return !listener->notify(); }); } void log_auth_key(const mtproto::AuthKey &auth_key) { diff --git a/td/telegram/net/ConnectionCreator.cpp b/td/telegram/net/ConnectionCreator.cpp index a213f4366..da88b3fdd 100644 --- a/td/telegram/net/ConnectionCreator.cpp +++ b/td/telegram/net/ConnectionCreator.cpp @@ -812,14 +812,12 @@ void ConnectionCreator::client_loop(ClientInfo &client) { VLOG(connections) << "In client_loop: " << tag("client", format::as_hex(client.hash)); // Remove expired ready connections - client.ready_connections.erase( - std::remove_if(client.ready_connections.begin(), client.ready_connections.end(), - [&, expires_at = Time::now_cached() - ClientInfo::READY_CONNECTIONS_TIMEOUT](auto &v) { - bool drop = v.second < expires_at; - VLOG_IF(connections, drop) << "Drop expired " << tag("connection", v.first.get()); - return drop; - }), - client.ready_connections.end()); + td::remove_if(client.ready_connections, + [&, expires_at = Time::now_cached() - ClientInfo::READY_CONNECTIONS_TIMEOUT](auto &v) { + bool drop = v.second < expires_at; + VLOG_IF(connections, drop) << "Drop expired " << tag("connection", v.first.get()); + return drop; + }); // Send ready connections into promises { diff --git a/td/telegram/net/DcOptionsSet.cpp b/td/telegram/net/DcOptionsSet.cpp index c031ac933..5731d2d60 100644 --- a/td/telegram/net/DcOptionsSet.cpp +++ b/td/telegram/net/DcOptionsSet.cpp @@ -8,6 +8,7 @@ #include "td/utils/format.h" #include "td/utils/logging.h" +#include "td/utils/misc.h" #include #include @@ -98,8 +99,7 @@ vector DcOptionsSet::find_all_connections(DcId dc_ } else { bool have_ipv4 = std::any_of(options.begin(), options.end(), [](auto &v) { return !v.option->is_ipv6(); }); if (have_ipv4) { - options.erase(std::remove_if(options.begin(), options.end(), [](auto &v) { return v.option->is_ipv6(); }), - options.end()); + td::remove_if(options, [](auto &v) { return v.option->is_ipv6(); }); } } } else { @@ -111,15 +111,13 @@ vector DcOptionsSet::find_all_connections(DcId dc_ if (prefer_ipv6) { bool have_ipv6 = std::any_of(options.begin(), options.end(), [](auto &v) { return v.option->is_ipv6(); }); if (have_ipv6) { - options.erase(std::remove_if(options.begin(), options.end(), [](auto &v) { return !v.option->is_ipv6(); }), - options.end()); + td::remove_if(options, [](auto &v) { return !v.option->is_ipv6(); }); } } bool have_media_only = std::any_of(options.begin(), options.end(), [](auto &v) { return v.option->is_media_only(); }); if (have_media_only) { - options.erase(std::remove_if(options.begin(), options.end(), [](auto &v) { return !v.option->is_media_only(); }), - options.end()); + td::remove_if(options, [](auto &v) { return !v.option->is_media_only(); }); } return options; diff --git a/td/telegram/net/PublicRsaKeyShared.cpp b/td/telegram/net/PublicRsaKeyShared.cpp index 56a975053..0c99cb6ac 100644 --- a/td/telegram/net/PublicRsaKeyShared.cpp +++ b/td/telegram/net/PublicRsaKeyShared.cpp @@ -8,6 +8,7 @@ #include "td/utils/format.h" #include "td/utils/logging.h" +#include "td/utils/misc.h" #include "td/utils/Slice.h" #include "td/utils/Status.h" @@ -153,8 +154,7 @@ RSA *PublicRsaKeyShared::get_rsa_locked(int64 fingerprint) { void PublicRsaKeyShared::notify() { auto lock = rw_mutex_.lock_read(); - auto it = std::remove_if(listeners_.begin(), listeners_.end(), [&](auto &listener) { return !listener->notify(); }); - listeners_.erase(it, listeners_.end()); + td::remove_if(listeners_, [&](auto &listener) { return !listener->notify(); }); } } // namespace td diff --git a/td/telegram/net/Session.cpp b/td/telegram/net/Session.cpp index 6c9fde103..e7f6a1b7f 100644 --- a/td/telegram/net/Session.cpp +++ b/td/telegram/net/Session.cpp @@ -34,7 +34,6 @@ #include "td/utils/Timer.h" #include "td/utils/tl_parsers.h" -#include #include #include @@ -510,7 +509,7 @@ void Session::on_session_failed(Status status) { } void Session::on_container_sent(uint64 container_id, vector msg_ids) { - auto erase_from = std::remove_if(msg_ids.begin(), msg_ids.end(), [&](uint64 msg_id) { + td::remove_if(msg_ids, [&](uint64 msg_id) { auto it = sent_queries_.find(msg_id); if (it == sent_queries_.end()) { return true; // remove @@ -518,7 +517,6 @@ void Session::on_container_sent(uint64 container_id, vector msg_ids) { it->second.container_id = container_id; return false; }); - msg_ids.erase(erase_from, msg_ids.end()); if (msg_ids.empty()) { return; } diff --git a/tdutils/td/utils/misc.h b/tdutils/td/utils/misc.h index fb1d89ce5..b7a1584da 100644 --- a/tdutils/td/utils/misc.h +++ b/tdutils/td/utils/misc.h @@ -84,7 +84,7 @@ auto transform(T &&v, const Func &f) { } template -void remove_if(vector &v, const Func &f) { +void remove_if(T &v, const Func &f) { size_t i = 0; while (i != v.size() && !f(v[i])) { i++;