BitfieldMan.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #ifndef _D_BITFIELD_MAN_H_
  36. #define _D_BITFIELD_MAN_H_
  37. #include "common.h"
  38. #include "Randomizer.h"
  39. #include <deque>
  40. typedef deque<int> BlockIndexes;
  41. class BitfieldMan {
  42. private:
  43. int32_t blockLength;
  44. int64_t totalLength;
  45. unsigned char* bitfield;
  46. unsigned char* useBitfield;
  47. unsigned char* filterBitfield;
  48. int32_t bitfieldLength;
  49. int32_t blocks;
  50. bool filterEnabled;
  51. RandomizerHandle randomizer;
  52. // for caching
  53. int32_t cachedNumMissingBlock;
  54. int32_t cachedNumFilteredBlock;
  55. int64_t cachedCompletedLength;
  56. int64_t cachedFilteredComletedLength;
  57. int64_t cachedFilteredTotalLength;
  58. int32_t countSetBit(const unsigned char* bitfield, int32_t len) const;
  59. int32_t getNthBitIndex(const unsigned char bit, int32_t nth) const;
  60. int32_t getMissingIndexRandomly(const unsigned char* bitfield, int32_t len) const;
  61. bool isBitSetInternal(const unsigned char* bitfield, int32_t index) const;
  62. bool setBitInternal(unsigned char* bitfield, int32_t index, bool on);
  63. bool setFilterBit(int32_t index);
  64. int32_t getStartIndex(int32_t index) const;
  65. int32_t getEndIndex(int32_t index) const;
  66. int64_t getCompletedLength(bool useFilter) const;
  67. public:
  68. BitfieldMan(int32_t blockLength, int64_t totalLength);
  69. BitfieldMan(const BitfieldMan& bitfieldMan);
  70. ~BitfieldMan();
  71. BitfieldMan& operator=(const BitfieldMan& bitfieldMan) {
  72. if(this != &bitfieldMan) {
  73. blockLength = bitfieldMan.blockLength;
  74. totalLength = bitfieldMan.totalLength;
  75. blocks = bitfieldMan.blocks;
  76. bitfieldLength = bitfieldMan.bitfieldLength;
  77. filterEnabled = bitfieldMan.filterEnabled;
  78. delete [] bitfield;
  79. bitfield = new unsigned char[bitfieldLength];
  80. memcpy(bitfield, bitfieldMan.bitfield, bitfieldLength);
  81. delete [] useBitfield;
  82. useBitfield = new unsigned char[bitfieldLength];
  83. memcpy(useBitfield, bitfieldMan.useBitfield, bitfieldLength);
  84. delete [] filterBitfield;
  85. if(filterEnabled) {
  86. filterBitfield = new unsigned char[bitfieldLength];
  87. memcpy(filterBitfield, bitfieldMan.filterBitfield, bitfieldLength);
  88. } else {
  89. filterBitfield = 0;
  90. }
  91. updateCache();
  92. }
  93. return *this;
  94. }
  95. int32_t getBlockLength() const { return blockLength; }
  96. int32_t getLastBlockLength() const {
  97. return totalLength-blockLength*(blocks-1);
  98. }
  99. int32_t getBlockLength(int32_t index) const {
  100. if(index == (int32_t)(blocks-1)) {
  101. return getLastBlockLength();
  102. } else if(0 <= index && index < (int32_t)(blocks-1)) {
  103. return getBlockLength();
  104. } else {
  105. return 0;
  106. }
  107. }
  108. int64_t getTotalLength() const { return totalLength; }
  109. /**
  110. * affected by filter
  111. */
  112. bool hasMissingPiece(const unsigned char* bitfield, int32_t len) const;
  113. /**
  114. * affected by filter
  115. */
  116. int32_t getMissingIndex(const unsigned char* bitfield, int32_t len) const;
  117. /**
  118. * affected by filter
  119. */
  120. int32_t getMissingIndex() const;
  121. /**
  122. * affected by filter
  123. */
  124. int32_t getFirstMissingUnusedIndex(const unsigned char* bitfield, int32_t len) const;
  125. /**
  126. * affected by filter
  127. */
  128. int32_t getFirstMissingUnusedIndex() const;
  129. /**
  130. * affected by filter
  131. */
  132. int32_t getMissingUnusedIndex(const unsigned char* bitfield, int32_t len) const;
  133. /**
  134. * affected by filter
  135. */
  136. int32_t getMissingUnusedIndex() const;
  137. /**
  138. * affected by filter
  139. */
  140. int32_t getSparseMissingUnusedIndex() const;
  141. /**
  142. * affected by filter
  143. */
  144. BlockIndexes getAllMissingIndexes() const;
  145. /**
  146. * affected by filter
  147. */
  148. BlockIndexes getAllMissingIndexes(const unsigned char* bitfield, int32_t len) const;
  149. /**
  150. * affected by filter
  151. */
  152. int32_t countMissingBlock() const;
  153. /**
  154. * affected by filter
  155. */
  156. int32_t countMissingBlockNow() const;
  157. bool setUseBit(int32_t index);
  158. bool unsetUseBit(int32_t index);
  159. bool setBit(int32_t index);
  160. bool unsetBit(int32_t index);
  161. bool isBitSet(int32_t index) const;
  162. bool isUseBitSet(int32_t index) const;
  163. /**
  164. * affected by filter
  165. */
  166. bool isFilteredAllBitSet() const;
  167. bool isAllBitSet() const;
  168. const unsigned char* getBitfield() const { return bitfield; }
  169. int32_t getBitfieldLength() const { return bitfieldLength; }
  170. /**
  171. * affected by filter
  172. */
  173. int32_t countBlock() const;
  174. /**
  175. * affected by filter
  176. */
  177. int32_t countFilteredBlockNow() const;
  178. int32_t getMaxIndex() const { return blocks-1; }
  179. void setBitfield(const unsigned char* bitfield, int32_t bitfieldLength);
  180. void clearAllBit();
  181. void setAllBit();
  182. void clearAllUseBit();
  183. void setAllUseBit();
  184. void addFilter(int64_t offset, int64_t length);
  185. /**
  186. * Clears filter and disables filter
  187. */
  188. void clearFilter();
  189. void enableFilter();
  190. void disableFilter();
  191. bool isFilterEnabled() const;
  192. /**
  193. * affected by filter
  194. */
  195. int64_t getFilteredTotalLength() const;
  196. /**
  197. * affected by filter
  198. */
  199. int64_t getFilteredTotalLengthNow() const;
  200. int64_t getCompletedLength() const;
  201. int64_t getCompletedLengthNow() const;
  202. /**
  203. * affected by filter
  204. */
  205. int64_t getFilteredCompletedLength() const;
  206. /**
  207. * affected by filter
  208. */
  209. int64_t getFilteredCompletedLengthNow() const;
  210. void setRandomizer(const RandomizerHandle& randomizer) {
  211. this->randomizer = randomizer;
  212. }
  213. RandomizerHandle getRandomizer() const {
  214. return randomizer;
  215. }
  216. void updateCache();
  217. bool isBitRangeSet(int32_t startIndex, int32_t endIndex) const;
  218. void unsetBitRange(int32_t startIndex, int32_t endIndex);
  219. };
  220. #endif // _D_BITFIELD_MAN_H_