From cdefe9b1ee5b8cc70c3885a8e114773e5d644225 Mon Sep 17 00:00:00 2001 From: levlam Date: Sat, 19 Jan 2019 20:19:29 +0300 Subject: [PATCH] SetWithPosition CE and other fixes. GitOrigin-RevId: 69278e7906c738924e1a4771b5b755ff349ea26f --- td/telegram/FileReferenceManager.cpp | 6 +- td/telegram/FileReferenceManager.h | 5 +- td/telegram/MessageContent.cpp | 9 +- td/telegram/SetWithPosition.h | 124 ++++++++++++--------------- td/telegram/TdDb.h | 2 +- td/telegram/WallpaperManager.cpp | 3 + td/telegram/WallpaperManager.h | 1 + td/telegram/files/FileDbId.h | 2 +- td/telegram/files/FileId.hpp | 1 + td/telegram/files/FileSourceId.h | 4 + test/db.cpp | 2 +- test/set_with_position.cpp | 87 ++++++++++++++++--- 12 files changed, 150 insertions(+), 96 deletions(-) diff --git a/td/telegram/FileReferenceManager.cpp b/td/telegram/FileReferenceManager.cpp index 8e26e674..c491f004 100644 --- a/td/telegram/FileReferenceManager.cpp +++ b/td/telegram/FileReferenceManager.cpp @@ -11,14 +11,12 @@ #include "td/telegram/files/FileManager.h" #include "td/telegram/Global.h" #include "td/telegram/MessagesManager.h" +#include "td/telegram/WallpaperManager.h" #include "td/telegram/WebPagesManager.h" -#include "td/actor/MultiPromise.h" - -#include "td/utils/format.h" +#include "td/utils/logging.h" #include "td/utils/misc.h" #include "td/utils/overloaded.h" -#include "td/utils/Variant.h" namespace td { diff --git a/td/telegram/FileReferenceManager.h b/td/telegram/FileReferenceManager.h index 88d44f53..5914653c 100644 --- a/td/telegram/FileReferenceManager.h +++ b/td/telegram/FileReferenceManager.h @@ -9,15 +9,16 @@ #include "td/actor/actor.h" #include "td/actor/PromiseFuture.h" -#include "td/telegram/ChatId.h" #include "td/telegram/ChannelId.h" +#include "td/telegram/ChatId.h" #include "td/telegram/files/FileId.h" -#include "td/telegram/files/FileManager.h" #include "td/telegram/files/FileSourceId.h" #include "td/telegram/MessageId.h" #include "td/telegram/SetWithPosition.h" #include "td/telegram/UserId.h" +#include "td/utils/logging.h" +#include "td/utils/Status.h" #include "td/utils/Variant.h" #include diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index ffc31912..ff40dbec 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -3158,8 +3158,7 @@ static auto secret_to_telegram(secret_api::fileLocationUnavailable &file_locatio // fileLocation#53d69076 dc_id:int volume_id:long local_id:int secret:long = FileLocation; static auto secret_to_telegram(secret_api::fileLocation &file_location) { return make_tl_object(file_location.dc_id_, file_location.volume_id_, - file_location.local_id_, file_location.secret_, - BufferSlice()); + file_location.local_id_, file_location.secret_, BufferSlice()); } // photoSizeEmpty#e17e23c type:string = PhotoSize; @@ -3307,9 +3306,9 @@ static auto secret_to_telegram_document(secret_api::decryptedMessageMediaExterna if (!clean_input_string(from.mime_type_)) { from.mime_type_.clear(); } - return make_tl_object( - from.id_, from.access_hash_, BufferSlice(), from.date_, from.mime_type_, from.size_, - secret_to_telegram(*from.thumb_), from.dc_id_, secret_to_telegram(from.attributes_)); + return make_tl_object(from.id_, from.access_hash_, BufferSlice(), from.date_, from.mime_type_, + from.size_, secret_to_telegram(*from.thumb_), + from.dc_id_, secret_to_telegram(from.attributes_)); } template diff --git a/td/telegram/SetWithPosition.h b/td/telegram/SetWithPosition.h index cff7449e..915d3ed2 100644 --- a/td/telegram/SetWithPosition.h +++ b/td/telegram/SetWithPosition.h @@ -5,41 +5,58 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #pragma once + #include "td/utils/common.h" +#include "td/utils/logging.h" +#include "td/utils/misc.h" #include +#include namespace td { + template class FastSetWithPosition { public: - void add(int x) { + void add(T x) { if (checked_.count(x) != 0) { return; } not_checked_.insert(x); } - void remove(int x) { + + void remove(T x) { checked_.erase(x); not_checked_.erase(x); } - bool has_next() { + + bool has_next() const { return !not_checked_.empty(); } + void reset_position() { - not_checked_.insert(checked_.begin(), checked_.end()); - checked_ = {}; + if (not_checked_.empty()) { + not_checked_ = std::move(checked_); + } else { + not_checked_.insert(checked_.begin(), checked_.end()); + } + reset_to_empty(checked_); } T next() { CHECK(has_next()); - auto res = *not_checked_.begin(); - not_checked_.erase(not_checked_.begin()); + auto it = not_checked_.begin(); + auto res = *it; + not_checked_.erase(it); checked_.insert(res); return res; } void merge(FastSetWithPosition &&other) { + if (this == &other) { + return; + } + if (size() < other.size()) { std::swap(*this, other); } @@ -47,6 +64,7 @@ class FastSetWithPosition { not_checked_.erase(x); checked_.insert(x); } + for (auto x : other.not_checked_) { if (checked_.count(x) != 0) { continue; @@ -54,6 +72,7 @@ class FastSetWithPosition { not_checked_.insert(x); } } + size_t size() const { return checked_.size() + not_checked_.size(); } @@ -66,7 +85,7 @@ class FastSetWithPosition { template class SetWithPosition { public: - void add(int x) { + void add(T x) { if (fast_) { fast_->add(x); return; @@ -74,7 +93,7 @@ class SetWithPosition { if (!has_value_) { value_ = x; has_value_ = true; - is_cheched_ = false; + is_checked_ = false; return; } if (value_ == x) { @@ -83,28 +102,31 @@ class SetWithPosition { make_fast(); fast_->add(x); } - void remove(int x) { + + void remove(T x) { if (fast_) { fast_->remove(x); return; } if (has_value_ && value_ == x) { has_value_ = false; - is_cheched_ = false; + is_checked_ = false; } } - bool has_next() { + + bool has_next() const { if (fast_) { return fast_->has_next(); } - return has_value_ && !is_cheched_; + return has_value_ && !is_checked_; } + void reset_position() { if (fast_) { fast_->reset_position(); return; } - is_cheched_ = false; + is_checked_ = false; } T next() { @@ -112,33 +134,46 @@ class SetWithPosition { if (fast_) { return fast_->next(); } - is_cheched_ = true; + is_checked_ = true; return value_; } void merge(SetWithPosition &&other) { + if (this == &other) { + return; + } if (size() < other.size()) { std::swap(*this, other); } if (other.size() == 0) { return; } + if (other.fast_ == nullptr && fast_ == nullptr && value_ == other.value_) { + is_checked_ |= other.is_checked_; + other.value_ = T(); + other.has_value_ = false; + other.is_checked_ = false; + return; + } make_fast(); other.make_fast(); fast_->merge(std::move(*other.fast_)); + reset_to_empty(other); } + size_t size() const { if (fast_) { return fast_->size(); } - return has_value_; + return static_cast(has_value_); } private: - T value_; + T value_{}; bool has_value_{false}; - bool is_cheched_{false}; + bool is_checked_{false}; unique_ptr> fast_; + void make_fast() { if (fast_) { return; @@ -146,61 +181,10 @@ class SetWithPosition { fast_ = make_unique>(); CHECK(has_value_); fast_->add(value_); - if (is_cheched_) { + if (is_checked_) { fast_->next(); } } }; -template -class OldSetWithPosition { - public: - void add(T value) { - auto it = std::find(values_.begin(), values_.end(), value); - if (it != end(values_)) { - return; - } - values_.push_back(value); - } - void remove(T value) { - auto it = std::find(values_.begin(), values_.end(), value); - if (it == end(values_)) { - return; - } - size_t i = it - values_.begin(); - values_.erase(it); - if (pos_ > i) { - pos_--; - } - } - void reset_position() { - pos_ = 0; - } - T next() { - return values_[pos_++]; - } - bool has_next() { - return pos_ < values_.size(); - } - void merge(OldSetWithPosition &&other) { - OldSetWithPosition res; - for (size_t i = 0; i < pos_; i++) { - res.add(values_[i]); - } - for (size_t i = 0; i < other.pos_; i++) { - res.add(other.values_[i]); - } - res.pos_ = res.values_.size(); - for (size_t i = pos_; i < values_.size(); i++) { - res.add(values_[i]); - } - for (size_t i = other.pos_; i < other.values_.size(); i++) { - res.add(other.values_[i]); - } - *this = std::move(res); - } - private: - std::vector values_; - size_t pos_{0}; -}; } // namespace td diff --git a/td/telegram/TdDb.h b/td/telegram/TdDb.h index 0faa6a77..3adf36df 100644 --- a/td/telegram/TdDb.h +++ b/td/telegram/TdDb.h @@ -8,8 +8,8 @@ #include "td/telegram/TdParameters.h" -#include "td/db/binlog/BinlogInterface.h" #include "td/db/binlog/BinlogEvent.h" +#include "td/db/binlog/BinlogInterface.h" #include "td/db/DbKey.h" #include "td/db/KeyValueSyncInterface.h" diff --git a/td/telegram/WallpaperManager.cpp b/td/telegram/WallpaperManager.cpp index 6740c7d2..697b43b6 100644 --- a/td/telegram/WallpaperManager.cpp +++ b/td/telegram/WallpaperManager.cpp @@ -9,10 +9,13 @@ #include "td/telegram/td_api.h" #include "td/telegram/telegram_api.h" +#include "td/telegram/DialogId.h" #include "td/telegram/Global.h" #include "td/telegram/Photo.h" #include "td/telegram/Td.h" +#include "td/utils/buffer.h" +#include "td/utils/logging.h" #include "td/utils/misc.h" namespace td { diff --git a/td/telegram/WallpaperManager.h b/td/telegram/WallpaperManager.h index 2fd0d738..ba25e180 100644 --- a/td/telegram/WallpaperManager.h +++ b/td/telegram/WallpaperManager.h @@ -8,6 +8,7 @@ #include "td/telegram/Photo.h" #include "td/telegram/td_api.h" +#include "td/telegram/telegram_api.h" #include "td/actor/actor.h" #include "td/actor/PromiseFuture.h" diff --git a/td/telegram/files/FileDbId.h b/td/telegram/files/FileDbId.h index f423a1bc..26b37fb7 100644 --- a/td/telegram/files/FileDbId.h +++ b/td/telegram/files/FileDbId.h @@ -19,7 +19,7 @@ class FileDbId { public: FileDbId() = default; - FileDbId(uint64 file_db_id) : id(file_db_id) { + explicit FileDbId(uint64 file_db_id) : id(file_db_id) { } template ::value>> FileDbId(T1 file_db_id) = delete; diff --git a/td/telegram/files/FileId.hpp b/td/telegram/files/FileId.hpp index 812ca5b0..1ff2abbd 100644 --- a/td/telegram/files/FileId.hpp +++ b/td/telegram/files/FileId.hpp @@ -8,6 +8,7 @@ #include "td/telegram/files/FileId.h" +#include "td/telegram/files/FileManager.h" #include "td/telegram/files/FileManager.hpp" #include "td/telegram/Td.h" diff --git a/td/telegram/files/FileSourceId.h b/td/telegram/files/FileSourceId.h index 430c786d..3dc1512a 100644 --- a/td/telegram/files/FileSourceId.h +++ b/td/telegram/files/FileSourceId.h @@ -33,6 +33,10 @@ class FileSourceId { return id; } + bool operator<(const FileSourceId &other) const { + return id < other.id; + } + bool operator==(const FileSourceId &other) const { return id == other.id; } diff --git a/test/db.cpp b/test/db.cpp index 37d4acf2..ac984227 100644 --- a/test/db.cpp +++ b/test/db.cpp @@ -6,9 +6,9 @@ // #include "td/db/binlog/BinlogHelper.h" #include "td/db/binlog/ConcurrentBinlog.h" -#include "td/db/SqliteConnectionSafe.h" #include "td/db/BinlogKeyValue.h" #include "td/db/SeqKeyValue.h" +#include "td/db/SqliteConnectionSafe.h" #include "td/db/SqliteKeyValue.h" #include "td/db/SqliteKeyValueSafe.h" #include "td/db/TsSeqKeyValue.h" diff --git a/test/set_with_position.cpp b/test/set_with_position.cpp index d56ad3c1..aeca1752 100644 --- a/test/set_with_position.cpp +++ b/test/set_with_position.cpp @@ -4,16 +4,75 @@ // 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) // -#include "td/utils/tests.h" -#include "td/utils/Random.h" - #include "td/telegram/SetWithPosition.h" +#include "td/utils/common.h" +#include "td/utils/logging.h" +#include "td/utils/Random.h" +#include "td/utils/tests.h" + +#include +#include #include +#include using namespace td; -template class Set = SetWithPosition> +template +class OldSetWithPosition { + public: + void add(T value) { + auto it = std::find(values_.begin(), values_.end(), value); + if (it != values_.end()) { + return; + } + values_.push_back(value); + } + void remove(T value) { + auto it = std::find(values_.begin(), values_.end(), value); + if (it == values_.end()) { + return; + } + size_t i = it - values_.begin(); + values_.erase(it); + if (pos_ > i) { + pos_--; + } + } + void reset_position() { + pos_ = 0; + } + T next() { + CHECK(has_next()); + return values_[pos_++]; + } + bool has_next() const { + return pos_ < values_.size(); + } + void merge(OldSetWithPosition &&other) { + OldSetWithPosition res; + for (size_t i = 0; i < pos_; i++) { + res.add(values_[i]); + } + for (size_t i = 0; i < other.pos_; i++) { + res.add(other.values_[i]); + } + res.pos_ = res.values_.size(); + for (size_t i = pos_; i < values_.size(); i++) { + res.add(values_[i]); + } + for (size_t i = other.pos_; i < other.values_.size(); i++) { + res.add(other.values_[i]); + } + *this = std::move(res); + } + + private: + std::vector values_; + size_t pos_{0}; +}; + +template class SetWithPosition> class CheckedSetWithPosition { public: void add(int x) { @@ -28,7 +87,7 @@ class CheckedSetWithPosition { checked_.erase(x); not_checked_.erase(x); } - bool has_next() { + bool has_next() const { auto res = !not_checked_.empty(); //LOG(ERROR) << res; ASSERT_EQ(res, s_.has_next()); @@ -74,11 +133,11 @@ class CheckedSetWithPosition { private: std::set checked_; std::set not_checked_; - Set s_; + SetWithPosition s_; }; template