BitfieldMan.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #ifndef _D_BITFIELD_MAN_H_
  23. #define _D_BITFIELD_MAN_H_
  24. #include "common.h"
  25. #include <deque>
  26. typedef deque<int> BlockIndexes;
  27. class BitfieldMan {
  28. private:
  29. int blockLength;
  30. long long int totalLength;
  31. unsigned char* bitfield;
  32. unsigned char* useBitfield;
  33. unsigned char* filterBitfield;
  34. int bitfieldLength;
  35. int blocks;
  36. bool filterEnabled;
  37. int countSetBit(const unsigned char* bitfield, int len) const;
  38. int getMissingIndexRandomly(const unsigned char* bitfield, int len, int randMax) const;
  39. bool isBitSetInternal(const unsigned char* bitfield, int index) const;
  40. bool setBitInternal(unsigned char* bitfield, int index, bool on);
  41. bool setFilterBit(int index);
  42. public:
  43. BitfieldMan(int blockLength, long long int totalLength);
  44. BitfieldMan(const BitfieldMan& bitfieldMan);
  45. ~BitfieldMan();
  46. BitfieldMan& operator=(const BitfieldMan& bitfieldMan);
  47. int getBlockLength() const { return blockLength; }
  48. int getLastBlockLength() const {
  49. return totalLength-blockLength*(blocks-1);
  50. }
  51. int getBlockLength(int index) const {
  52. if(index == blocks-1) {
  53. return getLastBlockLength();
  54. } else if(0 <= index && index < blocks-1) {
  55. return getBlockLength();
  56. } else {
  57. return 0;
  58. }
  59. }
  60. long long int getTotalLength() const { return totalLength; }
  61. /**
  62. * affected by filter
  63. */
  64. int getMissingIndex(const unsigned char* bitfield, int len) const;
  65. /**
  66. * affected by filter
  67. */
  68. int getFirstMissingUnusedIndex(const unsigned char* bitfield, int len) const;
  69. /**
  70. * affected by filter
  71. */
  72. int getFirstMissingUnusedIndex() const;
  73. /**
  74. * affected by filter
  75. */
  76. int getMissingUnusedIndex(const unsigned char* bitfield, int len) const;
  77. /**
  78. * affected by filter
  79. */
  80. BlockIndexes getAllMissingIndexes() const;
  81. /**
  82. * affected by filter
  83. */
  84. int countMissingBlock() const;
  85. bool setUseBit(int index);
  86. bool unsetUseBit(int index);
  87. bool setBit(int index);
  88. bool unsetBit(int index);
  89. bool isBitSet(int index) const;
  90. bool isUseBitSet(int index) const;
  91. /**
  92. * affected by filter
  93. */
  94. bool isAllBitSet() const;
  95. const unsigned char* getBitfield() const { return bitfield; }
  96. int getBitfieldLength() const { return bitfieldLength; }
  97. /**
  98. * affected by filter
  99. */
  100. int countBlock() const;
  101. void setBitfield(const unsigned char* bitfield, int bitfieldLength);
  102. void clearAllBit();
  103. void setAllBit();
  104. void addFilter(long long int offset, long long int length);
  105. void clearFilter();
  106. void enableFilter();
  107. void disableFilter();
  108. bool isFilterEnabled() const;
  109. long long int getFilteredTotalLength() const;
  110. /**
  111. * affected by filter
  112. */
  113. long long int getCompletedLength() const;
  114. };
  115. #endif // _D_BITFIELD_MAN_H_