2023-05-02 16:38:55 +02:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/telegram/MessageId.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
|
2023-05-02 17:40:22 +02:00
|
|
|
#include <functional>
|
|
|
|
|
2023-05-02 16:38:55 +02:00
|
|
|
namespace td {
|
|
|
|
|
2023-05-02 22:18:28 +02:00
|
|
|
class OrderedMessage {
|
|
|
|
public:
|
2023-05-02 22:49:59 +02:00
|
|
|
MessageId get_message_id() const {
|
|
|
|
return message_id_;
|
|
|
|
}
|
2023-05-02 16:38:55 +02:00
|
|
|
|
2023-05-03 11:59:10 +02:00
|
|
|
bool have_next() const {
|
|
|
|
return have_next_;
|
|
|
|
}
|
2023-05-02 16:38:55 +02:00
|
|
|
|
2023-05-02 22:18:28 +02:00
|
|
|
private:
|
2023-05-02 22:27:37 +02:00
|
|
|
int32 random_y_ = 0;
|
2023-05-02 16:38:55 +02:00
|
|
|
|
2023-05-03 11:59:10 +02:00
|
|
|
bool have_previous_ = false;
|
|
|
|
bool have_next_ = false;
|
|
|
|
|
2023-05-02 22:49:59 +02:00
|
|
|
MessageId message_id_;
|
|
|
|
|
2023-05-02 22:27:37 +02:00
|
|
|
unique_ptr<OrderedMessage> left_;
|
|
|
|
unique_ptr<OrderedMessage> right_;
|
2023-05-02 22:18:28 +02:00
|
|
|
|
|
|
|
friend class OrderedMessages;
|
2023-05-02 17:04:54 +02:00
|
|
|
};
|
|
|
|
|
2023-05-02 22:14:35 +02:00
|
|
|
class OrderedMessages {
|
|
|
|
public:
|
2023-05-02 18:07:02 +02:00
|
|
|
class IteratorBase {
|
|
|
|
vector<const OrderedMessage *> stack_;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
IteratorBase() = default;
|
|
|
|
|
|
|
|
// points iterator to message with greatest identifier which is less or equal than message_id
|
|
|
|
IteratorBase(const OrderedMessage *root, MessageId message_id) {
|
|
|
|
CHECK(!message_id.is_scheduled());
|
|
|
|
|
|
|
|
size_t last_right_pos = 0;
|
|
|
|
while (root != nullptr) {
|
|
|
|
stack_.push_back(root);
|
2023-05-02 22:27:37 +02:00
|
|
|
if (root->message_id_ <= message_id) {
|
2023-05-02 18:07:02 +02:00
|
|
|
last_right_pos = stack_.size();
|
2023-05-02 22:27:37 +02:00
|
|
|
root = root->right_.get();
|
2023-05-02 18:07:02 +02:00
|
|
|
} else {
|
2023-05-02 22:27:37 +02:00
|
|
|
root = root->left_.get();
|
2023-05-02 18:07:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
stack_.resize(last_right_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
const OrderedMessage *operator*() const {
|
|
|
|
return stack_.empty() ? nullptr : stack_.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
~IteratorBase() = default;
|
|
|
|
|
|
|
|
public:
|
|
|
|
IteratorBase(const IteratorBase &) = default;
|
|
|
|
IteratorBase &operator=(const IteratorBase &) = default;
|
|
|
|
IteratorBase(IteratorBase &&other) = default;
|
|
|
|
IteratorBase &operator=(IteratorBase &&other) = default;
|
|
|
|
|
|
|
|
void operator++() {
|
|
|
|
if (stack_.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const OrderedMessage *cur = stack_.back();
|
2023-05-02 22:27:37 +02:00
|
|
|
if (!cur->have_next_) {
|
2023-05-02 18:07:02 +02:00
|
|
|
stack_.clear();
|
|
|
|
return;
|
|
|
|
}
|
2023-05-02 22:27:37 +02:00
|
|
|
if (cur->right_ == nullptr) {
|
2023-05-02 18:07:02 +02:00
|
|
|
while (true) {
|
|
|
|
stack_.pop_back();
|
|
|
|
if (stack_.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const OrderedMessage *new_cur = stack_.back();
|
2023-05-02 22:27:37 +02:00
|
|
|
if (new_cur->left_.get() == cur) {
|
2023-05-02 18:07:02 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
cur = new_cur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-02 22:27:37 +02:00
|
|
|
cur = cur->right_.get();
|
2023-05-02 18:07:02 +02:00
|
|
|
while (cur != nullptr) {
|
|
|
|
stack_.push_back(cur);
|
2023-05-02 22:27:37 +02:00
|
|
|
cur = cur->left_.get();
|
2023-05-02 18:07:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator--() {
|
|
|
|
if (stack_.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const OrderedMessage *cur = stack_.back();
|
2023-05-02 22:27:37 +02:00
|
|
|
if (!cur->have_previous_) {
|
2023-05-02 18:07:02 +02:00
|
|
|
stack_.clear();
|
|
|
|
return;
|
|
|
|
}
|
2023-05-02 22:27:37 +02:00
|
|
|
if (cur->left_ == nullptr) {
|
2023-05-02 18:07:02 +02:00
|
|
|
while (true) {
|
|
|
|
stack_.pop_back();
|
|
|
|
if (stack_.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const OrderedMessage *new_cur = stack_.back();
|
2023-05-02 22:27:37 +02:00
|
|
|
if (new_cur->right_.get() == cur) {
|
2023-05-02 18:07:02 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
cur = new_cur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-02 22:27:37 +02:00
|
|
|
cur = cur->left_.get();
|
2023-05-02 18:07:02 +02:00
|
|
|
while (cur != nullptr) {
|
|
|
|
stack_.push_back(cur);
|
2023-05-02 22:27:37 +02:00
|
|
|
cur = cur->right_.get();
|
2023-05-02 18:07:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ConstIterator final : public IteratorBase {
|
|
|
|
public:
|
|
|
|
ConstIterator() = default;
|
|
|
|
|
|
|
|
ConstIterator(const OrderedMessage *root, MessageId message_id) : IteratorBase(root, message_id) {
|
|
|
|
}
|
|
|
|
|
|
|
|
const OrderedMessage *operator*() const {
|
|
|
|
return IteratorBase::operator*();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ConstIterator get_const_iterator(MessageId message_id) const {
|
|
|
|
return ConstIterator(messages_.get(), message_id);
|
|
|
|
}
|
|
|
|
|
2023-05-03 11:29:41 +02:00
|
|
|
void insert(MessageId message_id, bool auto_attach, bool have_previous, bool have_next, MessageId old_last_message_id,
|
|
|
|
const char *source);
|
2023-05-02 16:38:55 +02:00
|
|
|
|
2023-05-03 00:34:58 +02:00
|
|
|
void erase(MessageId message_id, bool only_from_memory);
|
2023-05-02 17:11:37 +02:00
|
|
|
|
2023-05-02 21:39:50 +02:00
|
|
|
void attach_message_to_previous(MessageId message_id, const char *source);
|
|
|
|
|
|
|
|
void attach_message_to_next(MessageId message_id, const char *source);
|
|
|
|
|
2023-05-02 17:11:37 +02:00
|
|
|
vector<MessageId> find_older_messages(MessageId max_message_id) const;
|
2023-05-02 17:17:06 +02:00
|
|
|
|
|
|
|
vector<MessageId> find_newer_messages(MessageId min_message_id) const;
|
2023-05-02 17:40:22 +02:00
|
|
|
|
2023-05-02 17:47:20 +02:00
|
|
|
MessageId find_message_by_date(int32 date, const std::function<int32(MessageId)> &get_message_date) const;
|
|
|
|
|
2023-05-02 17:40:22 +02:00
|
|
|
vector<MessageId> find_messages_by_date(int32 min_date, int32 max_date,
|
2023-05-02 17:47:20 +02:00
|
|
|
const std::function<int32(MessageId)> &get_message_date) const;
|
2023-05-02 20:27:16 +02:00
|
|
|
|
|
|
|
void traverse_messages(const std::function<bool(MessageId)> &need_scan_older,
|
|
|
|
const std::function<bool(MessageId)> &need_scan_newer) const;
|
2023-05-02 22:14:35 +02:00
|
|
|
|
|
|
|
private:
|
2023-05-03 11:29:41 +02:00
|
|
|
struct AttachInfo {
|
|
|
|
bool have_previous_ = false;
|
|
|
|
bool have_next_ = false;
|
|
|
|
|
|
|
|
AttachInfo(bool have_previous, bool have_next) : have_previous_(have_previous), have_next_(have_next) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-05-03 10:51:09 +02:00
|
|
|
class Iterator final : public IteratorBase {
|
|
|
|
public:
|
|
|
|
Iterator() = default;
|
|
|
|
|
|
|
|
Iterator(OrderedMessage *root, MessageId message_id) : IteratorBase(root, message_id) {
|
|
|
|
}
|
|
|
|
|
|
|
|
OrderedMessage *operator*() const {
|
|
|
|
return const_cast<OrderedMessage *>(IteratorBase::operator*());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-05-03 11:29:41 +02:00
|
|
|
AttachInfo auto_attach_message(MessageId message_id, MessageId last_message_id, const char *source);
|
|
|
|
|
2023-05-03 10:51:09 +02:00
|
|
|
Iterator get_iterator(MessageId message_id) {
|
|
|
|
return Iterator(messages_.get(), message_id);
|
|
|
|
}
|
|
|
|
|
2023-05-02 22:18:28 +02:00
|
|
|
static void do_find_older_messages(const OrderedMessage *ordered_message, MessageId max_message_id,
|
|
|
|
vector<MessageId> &message_ids);
|
|
|
|
|
|
|
|
static void do_find_newer_messages(const OrderedMessage *ordered_message, MessageId min_message_id,
|
|
|
|
vector<MessageId> &message_ids);
|
|
|
|
|
|
|
|
static MessageId do_find_message_by_date(const OrderedMessage *ordered_message, int32 date,
|
|
|
|
const std::function<int32(MessageId)> &get_message_date);
|
|
|
|
|
|
|
|
static void do_find_messages_by_date(const OrderedMessage *ordered_message, int32 min_date, int32 max_date,
|
|
|
|
const std::function<int32(MessageId)> &get_message_date,
|
|
|
|
vector<MessageId> &message_ids);
|
|
|
|
|
|
|
|
static void do_traverse_messages(const OrderedMessage *ordered_message,
|
|
|
|
const std::function<bool(MessageId)> &need_scan_older,
|
|
|
|
const std::function<bool(MessageId)> &need_scan_newer);
|
|
|
|
|
2023-05-02 22:14:35 +02:00
|
|
|
unique_ptr<OrderedMessage> messages_;
|
2023-05-02 16:38:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|