2021-06-30 19:10:53 +00:00
|
|
|
/*
|
|
|
|
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) {
|
|
|
|
}
|
|
|
|
|
2022-02-24 15:04:24 +00:00
|
|
|
[[nodiscard]] std::optional<int> fullCount() const {
|
2021-06-30 19:10:53 +00:00
|
|
|
return _fullCount;
|
|
|
|
}
|
2022-02-24 15:04:24 +00:00
|
|
|
[[nodiscard]] std::optional<int> skippedBefore() const {
|
2021-06-30 19:10:53 +00:00
|
|
|
return _skippedBefore;
|
|
|
|
}
|
2022-02-24 15:04:24 +00:00
|
|
|
[[nodiscard]] std::optional<int> skippedAfter() const {
|
2021-06-30 19:10:53 +00:00
|
|
|
return _skippedAfter;
|
|
|
|
}
|
2022-02-24 15:04:24 +00:00
|
|
|
[[nodiscard]] std::optional<int> indexOf(Id id) const {
|
2021-06-30 19:10:53 +00:00
|
|
|
const auto it = ranges::find(_ids, id);
|
|
|
|
if (it != _ids.end()) {
|
|
|
|
return (it - _ids.begin());
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2022-02-24 15:04:24 +00:00
|
|
|
[[nodiscard]] int size() const {
|
2021-06-30 19:10:53 +00:00
|
|
|
return _ids.size();
|
|
|
|
}
|
2022-02-24 15:04:24 +00:00
|
|
|
[[nodiscard]] Id operator[](int index) const {
|
2021-06-30 19:10:53 +00:00
|
|
|
Expects(index >= 0 && index < size());
|
|
|
|
|
|
|
|
return *(_ids.begin() + index);
|
|
|
|
}
|
2022-02-24 15:04:24 +00:00
|
|
|
[[nodiscard]] std::optional<int> distance(Id a, Id b) const {
|
2021-06-30 19:10:53 +00:00
|
|
|
if (const auto i = indexOf(a)) {
|
|
|
|
if (const auto j = indexOf(b)) {
|
|
|
|
return *j - *i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2022-02-24 15:04:24 +00:00
|
|
|
[[nodiscard]] std::optional<Id> nearest(Id id) const {
|
|
|
|
static_assert(std::is_same_v<IdsContainer, base::flat_set<MsgId>>);
|
2021-06-30 19:10:53 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
};
|