BitfieldMan.h 6.5 KB

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