PieceStatMan.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2009 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. #include "PieceStatMan.h"
  36. #include <algorithm>
  37. #include "SimpleRandomizer.h"
  38. #include "bitfield.h"
  39. namespace aria2 {
  40. PieceStat::PieceStat(size_t index):order_(0), index_(index), count_(0) {}
  41. void PieceStat::addCount()
  42. {
  43. if(count_ < SIZE_MAX) {
  44. ++count_;
  45. }
  46. }
  47. void PieceStat::subCount()
  48. {
  49. if(count_ > 0) {
  50. --count_;
  51. }
  52. }
  53. namespace {
  54. class GenPieceStat {
  55. private:
  56. size_t index_;
  57. public:
  58. GenPieceStat():index_(0) {}
  59. SharedHandle<PieceStat> operator()()
  60. {
  61. return SharedHandle<PieceStat>(new PieceStat(index_++));
  62. }
  63. };
  64. } // namespace
  65. PieceStatMan::PieceStatMan(size_t pieceNum, bool randomShuffle):
  66. pieceStats_(pieceNum),
  67. sortedPieceStatIndexes_(pieceNum)
  68. {
  69. std::generate(pieceStats_.begin(), pieceStats_.end(), GenPieceStat());
  70. std::vector<SharedHandle<PieceStat> > sortedPieceStats(pieceStats_);
  71. // we need some randomness in ordering.
  72. if(randomShuffle) {
  73. std::random_shuffle(sortedPieceStats.begin(), sortedPieceStats.end(),
  74. *(SimpleRandomizer::getInstance().get()));
  75. }
  76. {
  77. size_t order = 0;
  78. for(std::vector<SharedHandle<PieceStat> >::const_iterator i =
  79. sortedPieceStats.begin(), eoi = sortedPieceStats.end();
  80. i != eoi; ++i) {
  81. sortedPieceStatIndexes_[order] = (*i)->getIndex();
  82. (*i)->setOrder(order++);
  83. }
  84. }
  85. }
  86. PieceStatMan::~PieceStatMan() {}
  87. namespace {
  88. class PieceStatRarer {
  89. private:
  90. const std::vector<SharedHandle<PieceStat> >& pieceStats_;
  91. public:
  92. PieceStatRarer(const std::vector<SharedHandle<PieceStat> >& ps):
  93. pieceStats_(ps) {}
  94. bool operator()(size_t lhs, size_t rhs) const
  95. {
  96. return *pieceStats_[lhs] < *pieceStats_[rhs];
  97. }
  98. };
  99. } // namespace
  100. void PieceStatMan::addPieceStats(const unsigned char* bitfield,
  101. size_t bitfieldLength)
  102. {
  103. const size_t nbits = pieceStats_.size();
  104. assert(nbits <= bitfieldLength*8);
  105. for(size_t i = 0; i < nbits; ++i) {
  106. if(bitfield::test(bitfield, nbits, i)) {
  107. pieceStats_[i]->addCount();
  108. }
  109. }
  110. std::sort(sortedPieceStatIndexes_.begin(), sortedPieceStatIndexes_.end(),
  111. PieceStatRarer(pieceStats_));
  112. }
  113. void PieceStatMan::subtractPieceStats(const unsigned char* bitfield,
  114. size_t bitfieldLength)
  115. {
  116. const size_t nbits = pieceStats_.size();
  117. assert(nbits <= bitfieldLength*8);
  118. for(size_t i = 0; i < nbits; ++i) {
  119. if(bitfield::test(bitfield, nbits, i)) {
  120. pieceStats_[i]->subCount();
  121. }
  122. }
  123. std::sort(sortedPieceStatIndexes_.begin(), sortedPieceStatIndexes_.end(),
  124. PieceStatRarer(pieceStats_));
  125. }
  126. void PieceStatMan::updatePieceStats(const unsigned char* newBitfield,
  127. size_t newBitfieldLength,
  128. const unsigned char* oldBitfield)
  129. {
  130. const size_t nbits = pieceStats_.size();
  131. assert(nbits <= newBitfieldLength*8);
  132. for(size_t i = 0; i < nbits; ++i) {
  133. bool inNew = bitfield::test(newBitfield, nbits, i);
  134. bool inOld = bitfield::test(oldBitfield, nbits, i);
  135. if(inNew) {
  136. if(!inOld) {
  137. pieceStats_[i]->addCount();
  138. }
  139. } else if(inOld) {
  140. pieceStats_[i]->subCount();
  141. }
  142. }
  143. std::sort(sortedPieceStatIndexes_.begin(), sortedPieceStatIndexes_.end(),
  144. PieceStatRarer(pieceStats_));
  145. }
  146. void PieceStatMan::addPieceStats(size_t index)
  147. {
  148. std::vector<size_t>::iterator cur =
  149. std::lower_bound(sortedPieceStatIndexes_.begin(),
  150. sortedPieceStatIndexes_.end(),
  151. index, PieceStatRarer(pieceStats_));
  152. pieceStats_[index]->addCount();
  153. std::vector<size_t>::iterator to =
  154. std::upper_bound(cur+1, sortedPieceStatIndexes_.end(),
  155. index, PieceStatRarer(pieceStats_));
  156. std::rotate(cur, cur+1, to);
  157. }
  158. } // namespace aria2