From 9841b28693e4bd4e30675cdd51b8cb3b8732ac88 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 22 Nov 2018 21:27:43 +0300 Subject: [PATCH] Add insert_message function. GitOrigin-RevId: c78957f5489e5efcc906c93b9ad9b9b5e2644240 --- td/telegram/MessagesManager.cpp | 68 ++++++++++++++++----------------- td/telegram/MessagesManager.h | 2 + 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index e12c97346..b9ee3c26e 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -19743,6 +19743,39 @@ const unique_ptr *MessagesManager::find_message(const return v; } +MessagesManager::Message *MessagesManager::insert_message(unique_ptr *v, unique_ptr message) { + auto message_id = message->message_id; + while (*v != nullptr && (*v)->random_y >= message->random_y) { + if ((*v)->message_id.get() < message_id.get()) { + v = &(*v)->right; + } else if ((*v)->message_id == message_id) { + UNREACHABLE(); + } else { + v = &(*v)->left; + } + } + + unique_ptr *left = &message->left; + unique_ptr *right = &message->right; + + unique_ptr cur = std::move(*v); + while (cur != nullptr) { + if (cur->message_id.get() < message_id.get()) { + *left = std::move(cur); + left = &((*left)->right); + cur = std::move(*left); + } else { + *right = std::move(cur); + right = &((*right)->left); + cur = std::move(*right); + } + } + CHECK(*left == nullptr); + CHECK(*right == nullptr); + *v = std::move(message); + return v->get(); +} + MessagesManager::Message *MessagesManager::get_message(Dialog *d, MessageId message_id) { return const_cast(get_message(static_cast(d), message_id)); } @@ -20408,40 +20441,7 @@ MessagesManager::Message *MessagesManager::add_message_to_dialog(Dialog *d, uniq } } - Message *result_message = nullptr; - { - // TODO function - auto v = &d->messages; - while (*v != nullptr && (*v)->random_y >= m->random_y) { - if ((*v)->message_id.get() < message_id.get()) { - v = &(*v)->right; - } else if ((*v)->message_id == message_id) { - UNREACHABLE(); - } else { - v = &(*v)->left; - } - } - - unique_ptr *left = &message->left; - unique_ptr *right = &message->right; - - unique_ptr cur = std::move(*v); - while (cur != nullptr) { - if (cur->message_id.get() < message_id.get()) { - *left = std::move(cur); - left = &((*left)->right); - cur = std::move(*left); - } else { - *right = std::move(cur); - right = &((*right)->left); - cur = std::move(*right); - } - } - CHECK(*left == nullptr); - CHECK(*right == nullptr); - *v = std::move(message); - result_message = v->get(); - } + Message *result_message = insert_message(&d->messages, std::move(message)); CHECK(result_message != nullptr); CHECK(d->messages != nullptr); diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index ed274ee71..988b2d063 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -1625,6 +1625,8 @@ class MessagesManager : public Actor { static unique_ptr *find_message(unique_ptr *v, MessageId message_id); static const unique_ptr *find_message(const unique_ptr *v, MessageId message_id); + static Message *insert_message(unique_ptr *v, unique_ptr message); + static Message *get_message(Dialog *d, MessageId message_id); static const Message *get_message(const Dialog *d, MessageId message_id);