BitfieldMan.h 2.8 KB

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