2019-02-13 18:10:18 +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
|
|
|
|
*/
|
|
|
|
#include "media/streaming/media_streaming_loader.h"
|
|
|
|
|
|
|
|
namespace Media {
|
|
|
|
namespace Streaming {
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
bool LoadedPart::valid(int64 size) const {
|
2019-04-11 13:07:48 +00:00
|
|
|
return (offset != kFailedOffset)
|
|
|
|
&& ((bytes.size() == Loader::kPartSize)
|
|
|
|
|| (offset + bytes.size() == size));
|
|
|
|
}
|
|
|
|
|
2019-02-25 17:26:08 +00:00
|
|
|
bool operator<(
|
|
|
|
const PriorityQueue::Entry &a,
|
|
|
|
const PriorityQueue::Entry &b) {
|
|
|
|
if (a.priority > b.priority) {
|
|
|
|
return true;
|
|
|
|
} else if (a.priority < b.priority) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return a.value < b.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
bool PriorityQueue::add(int64 value) {
|
2019-02-25 17:26:08 +00:00
|
|
|
const auto i = ranges::find(_data, value, &Entry::value);
|
|
|
|
if (i == end(_data)) {
|
|
|
|
_data.insert({ value, _priority });
|
|
|
|
return true;
|
|
|
|
} else if (i->priority != _priority) {
|
|
|
|
_data.erase(i);
|
|
|
|
_data.insert({ value, _priority });
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
bool PriorityQueue::remove(int64 value) {
|
2019-02-25 17:26:08 +00:00
|
|
|
const auto i = ranges::find(_data, value, &Entry::value);
|
|
|
|
if (i == end(_data)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_data.erase(i);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-12-04 07:42:55 +00:00
|
|
|
bool PriorityQueue::empty() const {
|
|
|
|
return _data.empty();
|
|
|
|
}
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
std::optional<int64> PriorityQueue::front() const {
|
2019-02-25 17:26:08 +00:00
|
|
|
return _data.empty()
|
|
|
|
? std::nullopt
|
|
|
|
: std::make_optional(_data.front().value);
|
|
|
|
}
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
std::optional<int64> PriorityQueue::take() {
|
2019-02-25 17:26:08 +00:00
|
|
|
if (_data.empty()) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
const auto result = _data.front().value;
|
|
|
|
_data.erase(_data.begin());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-05-10 14:22:28 +00:00
|
|
|
base::flat_set<int64> PriorityQueue::takeInRange(int64 from, int64 till) {
|
|
|
|
auto result = base::flat_set<int64>();
|
2019-02-25 17:26:08 +00:00
|
|
|
for (auto i = _data.begin(); i != _data.end();) {
|
|
|
|
if (i->value >= from && i->value < till) {
|
|
|
|
result.emplace(i->value);
|
|
|
|
i = _data.erase(i);
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PriorityQueue::clear() {
|
|
|
|
_data.clear();
|
|
|
|
}
|
|
|
|
|
2019-12-23 10:27:20 +00:00
|
|
|
void PriorityQueue::resetPriorities() {
|
2019-02-25 17:26:08 +00:00
|
|
|
++_priority;
|
|
|
|
}
|
|
|
|
|
2019-02-13 18:10:18 +00:00
|
|
|
} // namespace Streaming
|
|
|
|
} // namespace Media
|