From 118fd187e31450c37613b14a6de842bce4f9e521 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Wed, 30 Jun 2021 22:10:53 +0300 Subject: [PATCH] Added abstract class for sparse ids slices. --- Telegram/CMakeLists.txt | 1 + .../data/data_abstract_sparse_ids.h | 78 +++++++++++++++++++ Telegram/SourceFiles/data/data_sparse_ids.cpp | 45 ----------- Telegram/SourceFiles/data/data_sparse_ids.h | 26 +------ .../SourceFiles/data/data_user_photos.cpp | 40 +++------- Telegram/SourceFiles/data/data_user_photos.h | 18 +---- 6 files changed, 97 insertions(+), 111 deletions(-) create mode 100644 Telegram/SourceFiles/data/data_abstract_sparse_ids.h diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index d3b6c8507..6b4b68744 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -393,6 +393,7 @@ PRIVATE data/stickers/data_stickers_set.h data/stickers/data_stickers.cpp data/stickers/data_stickers.h + data/data_abstract_sparse_ids.h data/data_abstract_structure.cpp data/data_abstract_structure.h data/data_auto_download.cpp diff --git a/Telegram/SourceFiles/data/data_abstract_sparse_ids.h b/Telegram/SourceFiles/data/data_abstract_sparse_ids.h new file mode 100644 index 000000000..f423bac35 --- /dev/null +++ b/Telegram/SourceFiles/data/data_abstract_sparse_ids.h @@ -0,0 +1,78 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#pragma once + +template +class AbstractSparseIds { +public: + using Id = typename IdsContainer::value_type; + + AbstractSparseIds() = default; + AbstractSparseIds( + const IdsContainer &ids, + std::optional fullCount, + std::optional skippedBefore, + std::optional skippedAfter) + : _ids(ids) + , _fullCount(fullCount) + , _skippedBefore(skippedBefore) + , _skippedAfter(skippedAfter) { + } + + std::optional fullCount() const { + return _fullCount; + } + std::optional skippedBefore() const { + return _skippedBefore; + } + std::optional skippedAfter() const { + return _skippedAfter; + } + std::optional indexOf(Id id) const { + const auto it = ranges::find(_ids, id); + if (it != _ids.end()) { + return (it - _ids.begin()); + } + return std::nullopt; + } + int size() const { + return _ids.size(); + } + Id operator[](int index) const { + Expects(index >= 0 && index < size()); + + return *(_ids.begin() + index); + } + std::optional distance(Id a, Id b) const { + if (const auto i = indexOf(a)) { + if (const auto j = indexOf(b)) { + return *j - *i; + } + } + return std::nullopt; + } + std::optional nearest(Id id) const { + if (const auto it = ranges::lower_bound(_ids, id); it != _ids.end()) { + return *it; + } else if (_ids.empty()) { + return std::nullopt; + } + return _ids.back(); + } + void reverse() { + ranges::reverse(_ids); + std::swap(_skippedBefore, _skippedAfter); + } + +private: + IdsContainer _ids; + std::optional _fullCount; + std::optional _skippedBefore; + std::optional _skippedAfter; + +}; diff --git a/Telegram/SourceFiles/data/data_sparse_ids.cpp b/Telegram/SourceFiles/data/data_sparse_ids.cpp index b58b79d64..69b669e91 100644 --- a/Telegram/SourceFiles/data/data_sparse_ids.cpp +++ b/Telegram/SourceFiles/data/data_sparse_ids.cpp @@ -10,51 +10,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include #include "storage/storage_sparse_ids_list.h" -SparseIdsSlice::SparseIdsSlice( - const base::flat_set &ids, - std::optional fullCount, - std::optional skippedBefore, - std::optional skippedAfter) -: _ids(ids) -, _fullCount(fullCount) -, _skippedBefore(skippedBefore) -, _skippedAfter(skippedAfter) { -} - -std::optional SparseIdsSlice::indexOf(MsgId msgId) const { - auto it = _ids.find(msgId); - if (it != _ids.end()) { - return (it - _ids.begin()); - } - return std::nullopt; -} - -MsgId SparseIdsSlice::operator[](int index) const { - Expects(index >= 0 && index < size()); - - return *(_ids.begin() + index); -} - -std::optional SparseIdsSlice::distance( - MsgId a, - MsgId b) const { - if (auto i = indexOf(a)) { - if (auto j = indexOf(b)) { - return *j - *i; - } - } - return std::nullopt; -} - -std::optional SparseIdsSlice::nearest(MsgId msgId) const { - if (auto it = ranges::lower_bound(_ids, msgId); it != _ids.end()) { - return *it; - } else if (_ids.empty()) { - return std::nullopt; - } - return _ids.back(); -} - SparseIdsMergedSlice::SparseIdsMergedSlice(Key key) : SparseIdsMergedSlice( key, diff --git a/Telegram/SourceFiles/data/data_sparse_ids.h b/Telegram/SourceFiles/data/data_sparse_ids.h index 7864573fc..e1801d24c 100644 --- a/Telegram/SourceFiles/data/data_sparse_ids.h +++ b/Telegram/SourceFiles/data/data_sparse_ids.h @@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once +#include "data/data_abstract_sparse_ids.h" #include "data/data_messages.h" namespace Storage { @@ -14,31 +15,10 @@ struct SparseIdsListResult; struct SparseIdsSliceUpdate; } // namespace Storage -class SparseIdsSlice { +class SparseIdsSlice final : public AbstractSparseIds> { public: using Key = MsgId; - - SparseIdsSlice() = default; - SparseIdsSlice( - const base::flat_set &ids, - std::optional fullCount, - std::optional skippedBefore, - std::optional skippedAfter); - - std::optional fullCount() const { return _fullCount; } - std::optional skippedBefore() const { return _skippedBefore; } - std::optional skippedAfter() const { return _skippedAfter; } - std::optional indexOf(MsgId msgId) const; - int size() const { return _ids.size(); } - MsgId operator[](int index) const; - std::optional distance(MsgId a, MsgId b) const; - std::optional nearest(MsgId msgId) const; - -private: - base::flat_set _ids; - std::optional _fullCount; - std::optional _skippedBefore; - std::optional _skippedAfter; + using AbstractSparseIds>::AbstractSparseIds; }; diff --git a/Telegram/SourceFiles/data/data_user_photos.cpp b/Telegram/SourceFiles/data/data_user_photos.cpp index fc968c07d..07ae87dd3 100644 --- a/Telegram/SourceFiles/data/data_user_photos.cpp +++ b/Telegram/SourceFiles/data/data_user_photos.cpp @@ -64,39 +64,23 @@ UserPhotosSlice::UserPhotosSlice( std::optional fullCount, std::optional skippedBefore, std::optional skippedAfter) -: _key(key) -, _ids(std::move(ids)) -, _fullCount(fullCount) -, _skippedBefore(skippedBefore) -, _skippedAfter(skippedAfter) { +: AbstractSparseIds>( + ids, + fullCount, + skippedBefore, + skippedAfter) +, _key(key) { } -void UserPhotosSlice::reverse() { - ranges::reverse(_ids); - std::swap(_skippedBefore, _skippedAfter); -} - -std::optional UserPhotosSlice::indexOf(PhotoId photoId) const { - auto it = ranges::find(_ids, photoId); - if (it != _ids.end()) { - return (it - _ids.begin()); - } - return std::nullopt; -} - -PhotoId UserPhotosSlice::operator[](int index) const { - Expects(index >= 0 && index < size()); - - return *(_ids.begin() + index); -} - -std::optional UserPhotosSlice::distance(const Key &a, const Key &b) const { +std::optional UserPhotosSlice::distance( + const Key &a, + const Key &b) const { if (a.userId != _key.userId || b.userId != _key.userId) { return std::nullopt; } - if (auto i = indexOf(a.photoId)) { - if (auto j = indexOf(b.photoId)) { + if (const auto i = indexOf(a.photoId)) { + if (const auto j = indexOf(b.photoId)) { return *j - *i; } } @@ -248,4 +232,4 @@ rpl::producer UserPhotosReversedViewer( slice.reverse(); return std::move(slice); }); -} \ No newline at end of file +} diff --git a/Telegram/SourceFiles/data/data_user_photos.h b/Telegram/SourceFiles/data/data_user_photos.h index f6ad791c9..42af886dd 100644 --- a/Telegram/SourceFiles/data/data_user_photos.h +++ b/Telegram/SourceFiles/data/data_user_photos.h @@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once +#include "data/data_abstract_sparse_ids.h" #include "storage/storage_user_photos.h" #include "base/weak_ptr.h" @@ -14,7 +15,7 @@ namespace Main { class Session; } // namespace Main -class UserPhotosSlice { +class UserPhotosSlice final : public AbstractSparseIds> { public: using Key = Storage::UserPhotosKey; @@ -26,24 +27,11 @@ public: std::optional skippedBefore, std::optional skippedAfter); - void reverse(); - - const Key &key() const { return _key; } - - std::optional fullCount() const { return _fullCount; } - std::optional skippedBefore() const { return _skippedBefore; } - std::optional skippedAfter() const { return _skippedAfter; } - std::optional indexOf(PhotoId msgId) const; - int size() const { return _ids.size(); } - PhotoId operator[](int index) const; std::optional distance(const Key &a, const Key &b) const; + const Key &key() const { return _key; } private: Key _key; - std::deque _ids; - std::optional _fullCount; - std::optional _skippedBefore; - std::optional _skippedAfter; friend class UserPhotosSliceBuilder;