BitfieldMan.h 5.8 KB

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