BitfieldMan.h 6.4 KB

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