SegList.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2011 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef D_SEG_LIST_H
  36. #define D_SEG_LIST_H
  37. #include <cstdlib>
  38. #include <vector>
  39. #include <limits>
  40. #include <algorithm>
  41. namespace aria2 {
  42. template <typename T> class SegList {
  43. public:
  44. SegList() : index_(0), val_(std::numeric_limits<T>::min()) {}
  45. // Don't allow copying
  46. SegList(const SegList&) = delete;
  47. SegList& operator=(const SegList&) = delete;
  48. SegList(SegList&&) = default;
  49. void clear()
  50. {
  51. segs_.clear();
  52. index_ = 0;
  53. val_ = std::numeric_limits<T>::min();
  54. }
  55. // Transforms list of segments so that they are sorted ascending
  56. // order of starting value and intersecting and touching segments
  57. // are all merged into one. This function resets current position.
  58. void normalize()
  59. {
  60. if (!segs_.empty()) {
  61. std::sort(std::begin(segs_), std::end(segs_));
  62. std::vector<std::pair<T, T>> s;
  63. s.push_back(segs_.front());
  64. for (size_t i = 1, len = segs_.size(); i < len; ++i) {
  65. auto& x = segs_[i];
  66. if (x.first <= s.back().second) {
  67. if (s.back().second < x.second) {
  68. s.back().second = x.second;
  69. }
  70. }
  71. else {
  72. s.push_back(x);
  73. }
  74. }
  75. s.swap(segs_);
  76. index_ = 0;
  77. val_ = segs_.front().first;
  78. }
  79. }
  80. // Add segment [a, b). If a >= b, do nothing.
  81. void add(T a, T b)
  82. {
  83. if (a < b) {
  84. if (segs_.empty()) {
  85. val_ = std::max(val_, a);
  86. }
  87. segs_.emplace_back(a, b);
  88. }
  89. }
  90. // Returns true if next value is available. Otherwise returns false.
  91. bool hasNext() const
  92. {
  93. return index_ < segs_.size() && val_ < segs_[index_].second;
  94. }
  95. // Returns next value. Advance current position to the next. If
  96. // this function is called when hasNext() returns false, returns 0.
  97. T next()
  98. {
  99. T res;
  100. auto len = segs_.size();
  101. if (index_ < len) {
  102. res = val_++;
  103. if (val_ == segs_[index_].second) {
  104. ++index_;
  105. if (index_ < len) {
  106. val_ = segs_[index_].first;
  107. }
  108. }
  109. }
  110. else {
  111. res = 0;
  112. }
  113. return res;
  114. }
  115. // Returns next value. Current position is not advanced. If
  116. // this function is called when hasNext() returns false, returns 0.
  117. T peek() const
  118. {
  119. T res;
  120. if (index_ < segs_.size()) {
  121. res = val_;
  122. }
  123. else {
  124. res = 0;
  125. }
  126. return res;
  127. }
  128. private:
  129. std::vector<std::pair<T, T>> segs_;
  130. size_t index_;
  131. T val_;
  132. };
  133. } // namespace aria2
  134. #endif // D_SEG_LIST_H