media_streaming_loader.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "media/streaming/media_streaming_loader.h"
  8. namespace Media {
  9. namespace Streaming {
  10. bool LoadedPart::valid(int64 size) const {
  11. return (offset != kFailedOffset)
  12. && ((bytes.size() == Loader::kPartSize)
  13. || (offset + bytes.size() == size));
  14. }
  15. bool operator<(
  16. const PriorityQueue::Entry &a,
  17. const PriorityQueue::Entry &b) {
  18. if (a.priority > b.priority) {
  19. return true;
  20. } else if (a.priority < b.priority) {
  21. return false;
  22. } else {
  23. return a.value < b.value;
  24. }
  25. }
  26. bool PriorityQueue::add(int64 value) {
  27. const auto i = ranges::find(_data, value, &Entry::value);
  28. if (i == end(_data)) {
  29. _data.insert({ value, _priority });
  30. return true;
  31. } else if (i->priority != _priority) {
  32. _data.erase(i);
  33. _data.insert({ value, _priority });
  34. return true;
  35. }
  36. return false;
  37. }
  38. bool PriorityQueue::remove(int64 value) {
  39. const auto i = ranges::find(_data, value, &Entry::value);
  40. if (i == end(_data)) {
  41. return false;
  42. }
  43. _data.erase(i);
  44. return true;
  45. }
  46. bool PriorityQueue::empty() const {
  47. return _data.empty();
  48. }
  49. std::optional<int64> PriorityQueue::front() const {
  50. return _data.empty()
  51. ? std::nullopt
  52. : std::make_optional(_data.front().value);
  53. }
  54. std::optional<int64> PriorityQueue::take() {
  55. if (_data.empty()) {
  56. return std::nullopt;
  57. }
  58. const auto result = _data.front().value;
  59. _data.erase(_data.begin());
  60. return result;
  61. }
  62. base::flat_set<int64> PriorityQueue::takeInRange(int64 from, int64 till) {
  63. auto result = base::flat_set<int64>();
  64. for (auto i = _data.begin(); i != _data.end();) {
  65. if (i->value >= from && i->value < till) {
  66. result.emplace(i->value);
  67. i = _data.erase(i);
  68. } else {
  69. ++i;
  70. }
  71. }
  72. return result;
  73. }
  74. void PriorityQueue::clear() {
  75. _data.clear();
  76. }
  77. void PriorityQueue::resetPriorities() {
  78. ++_priority;
  79. }
  80. } // namespace Streaming
  81. } // namespace Media