SegList.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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>
  43. class SegList {
  44. public:
  45. SegList()
  46. : index_(0), val_(std::numeric_limits<T>::min())
  47. {}
  48. void clear()
  49. {
  50. seg_.clear();
  51. index_ = 0;
  52. val_ = std::numeric_limits<T>::min();
  53. }
  54. // Transforms list of segments so that they are sorted ascending
  55. // order of starting value and intersecting and touching segments
  56. // are all merged into one. This function resets current position.
  57. void normalize()
  58. {
  59. if(!seg_.empty()) {
  60. std::sort(seg_.begin(), seg_.end());
  61. std::vector<std::pair<T, T> > s;
  62. s.push_back(seg_.front());
  63. for(size_t i = 1, len = seg_.size(); i < len; ++i) {
  64. const std::pair<T, T>& x = seg_[i];
  65. if(x.first <= s.back().second) {
  66. if(s.back().second < x.second) {
  67. s.back().second = x.second;
  68. }
  69. } else {
  70. s.push_back(x);
  71. }
  72. }
  73. s.swap(seg_);
  74. index_ = 0;
  75. val_ = seg_.front().first;
  76. }
  77. }
  78. // Add segment [a, b). If a >= b, do nothing.
  79. void add(T a, T b)
  80. {
  81. if(a < b) {
  82. if(seg_.empty()) {
  83. val_ = std::max(val_, a);
  84. }
  85. seg_.push_back(std::make_pair(a, b));
  86. }
  87. }
  88. // Returns true if next value is available. Otherwise returns false.
  89. bool hasNext() const
  90. {
  91. return index_ < seg_.size() && val_ < seg_[index_].second;
  92. }
  93. // Returns next value. Advance current position to the next. If
  94. // this fuction is called when hasNext() returns false, returns 0.
  95. T next()
  96. {
  97. T res;
  98. if(index_ < seg_.size()) {
  99. res = val_++;
  100. if(val_ == seg_[index_].second) {
  101. ++index_;
  102. val_ = seg_[index_].first;
  103. }
  104. } else {
  105. res = 0;
  106. }
  107. return res;
  108. }
  109. // Returns next value. Current position is not advanced. If
  110. // this fuction is called when hasNext() returns false, returns 0.
  111. T peek() const
  112. {
  113. T res;
  114. if(index_ < seg_.size()) {
  115. res = val_;
  116. } else {
  117. res = 0;
  118. }
  119. return res;
  120. }
  121. private:
  122. std::vector<std::pair<T, T> > seg_;
  123. size_t index_;
  124. T val_;
  125. };
  126. } // namespace aria2
  127. #endif // D_SEG_LIST_H