BitfieldMan.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 <vector>
  26. class BitfieldMan {
  27. private:
  28. int blockLength;
  29. long long int totalLength;
  30. unsigned char* bitfield;
  31. unsigned char* useBitfield;
  32. int bitfieldLength;
  33. int blocks;
  34. int countSetBit(const unsigned char* bitfield, int len) const;
  35. int getMissingIndexRandomly(const unsigned char* bitfield, int len, int randMax) const;
  36. bool isBitSetInternal(const unsigned char* bitfield, int index) const;
  37. public:
  38. BitfieldMan(int blockLength, long long int totalLength);
  39. BitfieldMan(const BitfieldMan& bitfieldMan);
  40. ~BitfieldMan();
  41. BitfieldMan& operator=(const BitfieldMan& bitfieldMan);
  42. int getBlockLength() const { return blockLength; }
  43. int getLastBlockLength() const {
  44. return totalLength-blockLength*(blocks-1);
  45. }
  46. int getBlockLength(int index) const {
  47. if(index == blocks-1) {
  48. return getLastBlockLength();
  49. } else if(0 <= index && index < blocks-1) {
  50. return getBlockLength();
  51. } else {
  52. return 0;
  53. }
  54. }
  55. long long int getTotalLength() const { return totalLength; }
  56. int getMissingIndex(const unsigned char* bitfield, int len) const;
  57. int getFirstMissingUnusedIndex(const unsigned char* bitfield, int len) const;
  58. int getFirstMissingUnusedIndex() const;
  59. int getMissingUnusedIndex(const unsigned char* bitfield, int len) const;
  60. vector<int> getAllMissingIndexes() const;
  61. int countMissingBlock() const;
  62. bool setUseBit(int index);
  63. bool unsetUseBit(int index);
  64. bool setBit(int index);
  65. bool unsetBit(int index);
  66. bool isBitSet(int index) const;
  67. bool isUseBitSet(int index) const;
  68. bool isAllBitSet() const;
  69. const unsigned char* getBitfield() const { return bitfield; }
  70. int getBitfieldLength() const { return bitfieldLength; }
  71. int countBlock() const { return blocks; }
  72. void setBitfield(const unsigned char* bitfield, int bitfieldLength);
  73. void clearAllBit();
  74. void setAllBit();
  75. };
  76. #endif // _D_BITFIELD_MAN_H_