Added abstract class for sparse ids slices.

This commit is contained in:
23rd 2021-06-30 22:10:53 +03:00
parent baa47bde7f
commit 118fd187e3
6 changed files with 97 additions and 111 deletions

View File

@ -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

View File

@ -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 <typename IdsContainer>
class AbstractSparseIds {
public:
using Id = typename IdsContainer::value_type;
AbstractSparseIds() = default;
AbstractSparseIds(
const IdsContainer &ids,
std::optional<int> fullCount,
std::optional<int> skippedBefore,
std::optional<int> skippedAfter)
: _ids(ids)
, _fullCount(fullCount)
, _skippedBefore(skippedBefore)
, _skippedAfter(skippedAfter) {
}
std::optional<int> fullCount() const {
return _fullCount;
}
std::optional<int> skippedBefore() const {
return _skippedBefore;
}
std::optional<int> skippedAfter() const {
return _skippedAfter;
}
std::optional<int> 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<int> 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<Id> 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<int> _fullCount;
std::optional<int> _skippedBefore;
std::optional<int> _skippedAfter;
};

View File

@ -10,51 +10,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include <rpl/combine.h>
#include "storage/storage_sparse_ids_list.h"
SparseIdsSlice::SparseIdsSlice(
const base::flat_set<MsgId> &ids,
std::optional<int> fullCount,
std::optional<int> skippedBefore,
std::optional<int> skippedAfter)
: _ids(ids)
, _fullCount(fullCount)
, _skippedBefore(skippedBefore)
, _skippedAfter(skippedAfter) {
}
std::optional<int> 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<int> 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<MsgId> 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,

View File

@ -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<base::flat_set<MsgId>> {
public:
using Key = MsgId;
SparseIdsSlice() = default;
SparseIdsSlice(
const base::flat_set<MsgId> &ids,
std::optional<int> fullCount,
std::optional<int> skippedBefore,
std::optional<int> skippedAfter);
std::optional<int> fullCount() const { return _fullCount; }
std::optional<int> skippedBefore() const { return _skippedBefore; }
std::optional<int> skippedAfter() const { return _skippedAfter; }
std::optional<int> indexOf(MsgId msgId) const;
int size() const { return _ids.size(); }
MsgId operator[](int index) const;
std::optional<int> distance(MsgId a, MsgId b) const;
std::optional<MsgId> nearest(MsgId msgId) const;
private:
base::flat_set<MsgId> _ids;
std::optional<int> _fullCount;
std::optional<int> _skippedBefore;
std::optional<int> _skippedAfter;
using AbstractSparseIds<base::flat_set<MsgId>>::AbstractSparseIds;
};

View File

@ -64,39 +64,23 @@ UserPhotosSlice::UserPhotosSlice(
std::optional<int> fullCount,
std::optional<int> skippedBefore,
std::optional<int> skippedAfter)
: _key(key)
, _ids(std::move(ids))
, _fullCount(fullCount)
, _skippedBefore(skippedBefore)
, _skippedAfter(skippedAfter) {
: AbstractSparseIds<std::deque<PhotoId>>(
ids,
fullCount,
skippedBefore,
skippedAfter)
, _key(key) {
}
void UserPhotosSlice::reverse() {
ranges::reverse(_ids);
std::swap(_skippedBefore, _skippedAfter);
}
std::optional<int> 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<int> UserPhotosSlice::distance(const Key &a, const Key &b) const {
std::optional<int> 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<UserPhotosSlice> UserPhotosReversedViewer(
slice.reverse();
return std::move(slice);
});
}
}

View File

@ -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<std::deque<PhotoId>> {
public:
using Key = Storage::UserPhotosKey;
@ -26,24 +27,11 @@ public:
std::optional<int> skippedBefore,
std::optional<int> skippedAfter);
void reverse();
const Key &key() const { return _key; }
std::optional<int> fullCount() const { return _fullCount; }
std::optional<int> skippedBefore() const { return _skippedBefore; }
std::optional<int> skippedAfter() const { return _skippedAfter; }
std::optional<int> indexOf(PhotoId msgId) const;
int size() const { return _ids.size(); }
PhotoId operator[](int index) const;
std::optional<int> distance(const Key &a, const Key &b) const;
const Key &key() const { return _key; }
private:
Key _key;
std::deque<PhotoId> _ids;
std::optional<int> _fullCount;
std::optional<int> _skippedBefore;
std::optional<int> _skippedAfter;
friend class UserPhotosSliceBuilder;