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