BitfieldMan.h 6.0 KB

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