Use add_to_top.

This commit is contained in:
levlam 2023-10-01 21:17:30 +03:00
parent 16cec9c2fb
commit c474942666
7 changed files with 19 additions and 80 deletions

View File

@ -756,16 +756,8 @@ void AnimationsManager::add_saved_animation_impl(FileId animation_id, bool add_o
return promise.set_error(Status::Error(400, "Can't save encrypted animations"));
}
auto it = std::find_if(saved_animation_ids_.begin(), saved_animation_ids_.end(), is_equal);
if (it == saved_animation_ids_.end()) {
if (static_cast<int32>(saved_animation_ids_.size()) == saved_animations_limit_) {
saved_animation_ids_.back() = animation_id;
} else {
saved_animation_ids_.push_back(animation_id);
}
it = saved_animation_ids_.end() - 1;
}
std::rotate(saved_animation_ids_.begin(), it, it + 1);
add_to_top_if(saved_animation_ids_, static_cast<size_t>(saved_animations_limit_), animation_id, is_equal);
CHECK(is_equal(saved_animation_ids_[0]));
if (saved_animation_ids_[0].get_remote() == 0 && animation_id.get_remote() != 0) {
saved_animation_ids_[0] = animation_id;

View File

@ -57,8 +57,6 @@
#include "td/utils/tl_helpers.h"
#include "td/utils/tl_parsers.h"
#include <algorithm>
namespace td {
class GetInlineBotResultsQuery final : public Td::ResultHandler {
@ -2183,17 +2181,8 @@ bool InlineQueriesManager::update_bot_usage(UserId bot_user_id) {
return false;
}
auto it = std::find(recently_used_bot_user_ids_.begin(), recently_used_bot_user_ids_.end(), bot_user_id);
if (it == recently_used_bot_user_ids_.end()) {
if (static_cast<int32>(recently_used_bot_user_ids_.size()) == MAX_RECENT_INLINE_BOTS) {
CHECK(!recently_used_bot_user_ids_.empty());
recently_used_bot_user_ids_.back() = bot_user_id;
} else {
recently_used_bot_user_ids_.push_back(bot_user_id);
}
it = recently_used_bot_user_ids_.end() - 1;
}
std::rotate(recently_used_bot_user_ids_.begin(), it, it + 1);
add_to_top(recently_used_bot_user_ids_, MAX_RECENT_INLINE_BOTS, bot_user_id);
return true;
}

View File

@ -87,8 +87,8 @@ class InlineQueriesManager final : public Actor {
tl_object_ptr<telegram_api::InputBotInlineMessageID> &&input_bot_inline_message_id);
private:
static constexpr int32 MAX_RECENT_INLINE_BOTS = 20; // some reasonable value
static constexpr int32 INLINE_QUERY_DELAY_MS = 400; // server side limit
static constexpr size_t MAX_RECENT_INLINE_BOTS = 20; // some reasonable value
static constexpr int32 INLINE_QUERY_DELAY_MS = 400; // server side limit
static constexpr int32 BOT_INLINE_MEDIA_RESULT_FLAG_HAS_PHOTO = 1 << 0;
static constexpr int32 BOT_INLINE_MEDIA_RESULT_FLAG_HAS_DOCUMENT = 1 << 1;

View File

@ -25,8 +25,6 @@
#include "td/utils/ScopeGuard.h"
#include "td/utils/Status.h"
#include <algorithm>
namespace td {
class GetAvailableReactionsQuery final : public Td::ResultHandler {
@ -340,16 +338,7 @@ void ReactionManager::add_recent_reaction(const ReactionType &reaction_type) {
return;
}
auto it = std::find(reactions.begin(), reactions.end(), reaction_type);
if (it == reactions.end()) {
if (static_cast<int32>(reactions.size()) == MAX_RECENT_REACTIONS) {
reactions.back() = reaction_type;
} else {
reactions.push_back(reaction_type);
}
it = reactions.end() - 1;
}
std::rotate(reactions.begin(), it, it + 1);
add_to_top(reactions, MAX_RECENT_REACTIONS, reaction_type);
recent_reactions_.hash_ = get_reaction_types_hash(reactions);
}

View File

@ -67,7 +67,7 @@ class ReactionManager final : public Actor {
void get_current_state(vector<td_api::object_ptr<td_api::Update>> &updates) const;
private:
static constexpr int32 MAX_RECENT_REACTIONS = 100; // some reasonable value
static constexpr size_t MAX_RECENT_REACTIONS = 100; // some reasonable value
struct Reaction {
ReactionType reaction_type_;

View File

@ -23,8 +23,6 @@
#include "td/utils/Slice.h"
#include "td/utils/SliceBuilder.h"
#include <algorithm>
namespace td {
RecentDialogList::RecentDialogList(Td *td, const char *name, size_t max_size)
@ -175,18 +173,8 @@ bool RecentDialogList::do_add_dialog(DialogId dialog_id) {
return false;
}
// TODO create function
auto it = std::find(dialog_ids_.begin(), dialog_ids_.end(), dialog_id);
if (it == dialog_ids_.end()) {
if (dialog_ids_.size() == max_size_) {
CHECK(!dialog_ids_.empty());
dialog_ids_.back() = dialog_id;
} else {
dialog_ids_.push_back(dialog_id);
}
it = dialog_ids_.end() - 1;
}
std::rotate(dialog_ids_.begin(), it, it + 1);
add_to_top(dialog_ids_, max_size_, dialog_id);
removed_dialog_ids_.erase(dialog_id);
return true;
}

View File

@ -7480,16 +7480,13 @@ int StickersManager::move_installed_sticker_set_to_top(StickerType sticker_type,
}
vector<StickerSetId> &current_sticker_set_ids = installed_sticker_set_ids_[type];
auto it = std::find(current_sticker_set_ids.begin(), current_sticker_set_ids.end(), sticker_set_id);
if (it == current_sticker_set_ids.end()) {
return -1;
}
if (sticker_set_id == current_sticker_set_ids[0]) {
CHECK(it == current_sticker_set_ids.begin());
if (!current_sticker_set_ids.empty() && sticker_set_id == current_sticker_set_ids[0]) {
return 0;
}
std::rotate(current_sticker_set_ids.begin(), it, it + 1);
if (!td::contains(current_sticker_set_ids, sticker_set_id)) {
return -1;
}
add_to_top(current_sticker_set_ids, current_sticker_set_ids.size(), sticker_set_id);
need_update_installed_sticker_sets_[type] = true;
return 1;
@ -8778,16 +8775,8 @@ void StickersManager::add_recent_sticker_impl(bool is_attached, FileId sticker_i
return promise.set_error(Status::Error(400, "Can't save encrypted stickers"));
}
auto it = std::find_if(sticker_ids.begin(), sticker_ids.end(), is_equal);
if (it == sticker_ids.end()) {
if (static_cast<int32>(sticker_ids.size()) == recent_stickers_limit_) {
sticker_ids.back() = sticker_id;
} else {
sticker_ids.push_back(sticker_id);
}
it = sticker_ids.end() - 1;
}
std::rotate(sticker_ids.begin(), it, it + 1);
add_to_top_if(sticker_ids, static_cast<size_t>(recent_stickers_limit_), sticker_id, is_equal);
if (sticker_ids[0].get_remote() == 0 && sticker_id.get_remote() != 0) {
sticker_ids[0] = sticker_id;
}
@ -9167,16 +9156,8 @@ void StickersManager::add_favorite_sticker_impl(FileId sticker_id, bool add_on_s
return promise.set_error(Status::Error(400, "Can't add to favorites encrypted stickers"));
}
auto it = std::find_if(favorite_sticker_ids_.begin(), favorite_sticker_ids_.end(), is_equal);
if (it == favorite_sticker_ids_.end()) {
if (static_cast<int32>(favorite_sticker_ids_.size()) == favorite_stickers_limit_) {
favorite_sticker_ids_.back() = sticker_id;
} else {
favorite_sticker_ids_.push_back(sticker_id);
}
it = favorite_sticker_ids_.end() - 1;
}
std::rotate(favorite_sticker_ids_.begin(), it, it + 1);
add_to_top_if(favorite_sticker_ids_, static_cast<size_t>(favorite_stickers_limit_), sticker_id, is_equal);
if (favorite_sticker_ids_[0].get_remote() == 0 && sticker_id.get_remote() != 0) {
favorite_sticker_ids_[0] = sticker_id;
}