RarestPieceSelector.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 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 "RarestPieceSelector.h"
  36. #include <algorithm>
  37. #include "SimpleRandomizer.h"
  38. namespace aria2 {
  39. PieceStat::PieceStat(size_t index):_order(0), _index(index), _count(0) {}
  40. bool PieceStat::operator<(const PieceStat& pieceStat) const
  41. {
  42. if(_count == pieceStat._count) {
  43. return _order < pieceStat._order;
  44. } else {
  45. return _count < pieceStat._count;
  46. }
  47. }
  48. void PieceStat::addCount()
  49. {
  50. if(_count < SIZE_MAX) {
  51. ++_count;
  52. }
  53. }
  54. void PieceStat::subCount()
  55. {
  56. if(_count > 0) {
  57. --_count;
  58. }
  59. }
  60. size_t PieceStat::getIndex() const
  61. {
  62. return _index;
  63. }
  64. size_t PieceStat::getCount() const
  65. {
  66. return _count;
  67. }
  68. void PieceStat::setOrder(size_t order)
  69. {
  70. _order = order;
  71. }
  72. size_t PieceStat::getOrder() const
  73. {
  74. return _order;
  75. }
  76. class GenPieceStat {
  77. private:
  78. size_t _index;
  79. public:
  80. GenPieceStat():_index(0) {}
  81. SharedHandle<PieceStat> operator()()
  82. {
  83. return SharedHandle<PieceStat>(new PieceStat(_index++));
  84. }
  85. };
  86. RarestPieceSelector::RarestPieceSelector(size_t pieceNum, bool randomShuffle):
  87. _pieceStats(pieceNum)
  88. {
  89. std::generate(_pieceStats.begin(), _pieceStats.end(), GenPieceStat());
  90. _sortedPieceStats = _pieceStats;
  91. // we need some randomness in ordering.
  92. if(randomShuffle) {
  93. std::random_shuffle(_sortedPieceStats.begin(), _sortedPieceStats.end(),
  94. *(SimpleRandomizer::getInstance().get()));
  95. }
  96. {
  97. size_t order = 0;
  98. for(std::deque<SharedHandle<PieceStat> >::iterator i = _sortedPieceStats.begin();
  99. i != _sortedPieceStats.end(); ++i) {
  100. (*i)->setOrder(order++);
  101. }
  102. }
  103. }
  104. class FindRarestPiece
  105. {
  106. private:
  107. const std::deque<size_t>& _indexes;
  108. public:
  109. FindRarestPiece(const std::deque<size_t>& indexes):_indexes(indexes) {}
  110. bool operator()(const SharedHandle<PieceStat>& pieceStat)
  111. {
  112. return std::binary_search(_indexes.begin(), _indexes.end(), pieceStat->getIndex());
  113. }
  114. };
  115. bool RarestPieceSelector::select
  116. (size_t& index,
  117. const std::deque<size_t>& candidateIndexes) const
  118. {
  119. std::deque<SharedHandle<PieceStat> >::const_iterator i =
  120. std::find_if(_sortedPieceStats.begin(), _sortedPieceStats.end(),
  121. FindRarestPiece(candidateIndexes));
  122. if(i == _sortedPieceStats.end()) {
  123. return false;
  124. } else {
  125. index = (*i)->getIndex();
  126. return true;
  127. }
  128. }
  129. void RarestPieceSelector::addPieceStats(const unsigned char* bitfield,
  130. size_t bitfieldLength)
  131. {
  132. size_t index = 0;
  133. for(size_t bi = 0; bi < bitfieldLength; ++bi) {
  134. for(size_t i = 0; i < 8; ++i, ++index) {
  135. unsigned char mask = 128 >> i;
  136. if(bitfield[bi]&mask) {
  137. _pieceStats[index]->addCount();
  138. }
  139. }
  140. }
  141. std::sort(_sortedPieceStats.begin(), _sortedPieceStats.end());
  142. }
  143. void RarestPieceSelector::subtractPieceStats(const unsigned char* bitfield,
  144. size_t bitfieldLength)
  145. {
  146. size_t index = 0;
  147. for(size_t bi = 0; bi < bitfieldLength; ++bi) {
  148. for(size_t i = 0; i < 8; ++i, ++index) {
  149. unsigned char mask = 128 >> i;
  150. if(bitfield[bi]&mask) {
  151. _pieceStats[index]->subCount();
  152. }
  153. }
  154. }
  155. std::sort(_sortedPieceStats.begin(), _sortedPieceStats.end());
  156. }
  157. void RarestPieceSelector::updatePieceStats(const unsigned char* newBitfield,
  158. size_t newBitfieldLength,
  159. const unsigned char* oldBitfield)
  160. {
  161. size_t index = 0;
  162. for(size_t bi = 0; bi < newBitfieldLength; ++bi) {
  163. for(size_t i = 0; i < 8; ++i, ++index) {
  164. unsigned char mask = 128 >> i;
  165. if((newBitfield[bi]&mask) && !(oldBitfield[bi]&mask)) {
  166. _pieceStats[index]->addCount();
  167. } else if(!(newBitfield[bi]&mask) && (oldBitfield[bi]&mask)) {
  168. _pieceStats[index]->subCount();
  169. }
  170. }
  171. }
  172. std::sort(_sortedPieceStats.begin(), _sortedPieceStats.end());
  173. }
  174. void RarestPieceSelector::addPieceStats(size_t index)
  175. {
  176. SharedHandle<PieceStat> pieceStat(_pieceStats[index]);
  177. {
  178. std::deque<SharedHandle<PieceStat> >::iterator cur =
  179. std::lower_bound(_sortedPieceStats.begin(), _sortedPieceStats.end(),
  180. pieceStat);
  181. _sortedPieceStats.erase(cur);
  182. }
  183. pieceStat->addCount();
  184. std::deque<SharedHandle<PieceStat> >::iterator to =
  185. std::lower_bound(_sortedPieceStats.begin(), _sortedPieceStats.end(),
  186. pieceStat);
  187. _sortedPieceStats.insert(to, pieceStat);
  188. }
  189. const std::deque<SharedHandle<PieceStat> >&
  190. RarestPieceSelector::getSortedPieceStats() const
  191. {
  192. return _sortedPieceStats;
  193. }
  194. } // namespace aria2