Piece.cc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #include "Piece.h"
  23. Piece Piece::nullPiece;
  24. Piece::Piece(const Piece& piece) {
  25. index = piece.index;
  26. length = piece.length;
  27. if(piece.bitfield == NULL) {
  28. bitfield = NULL;
  29. } else {
  30. bitfield = new BitfieldMan(*piece.bitfield);
  31. }
  32. }
  33. Piece& Piece::operator=(const Piece& piece) {
  34. if(this != &piece) {
  35. index = piece.index;
  36. length = piece.length;
  37. if(bitfield != NULL) {
  38. delete bitfield;
  39. }
  40. if(piece.bitfield == NULL) {
  41. bitfield = NULL;
  42. } else {
  43. bitfield = new BitfieldMan(*piece.bitfield);
  44. }
  45. }
  46. return *this;
  47. }
  48. void Piece::completeBlock(int blockIndex) {
  49. bitfield->setBit(blockIndex);
  50. bitfield->unsetUseBit(blockIndex);
  51. }
  52. void Piece::clearAllBlock() {
  53. bitfield->clearAllBit();
  54. }
  55. void Piece::setAllBlock() {
  56. bitfield->setAllBit();
  57. }
  58. bool Piece::pieceComplete() const {
  59. return bitfield->isAllBitSet();
  60. }
  61. void Piece::cancelBlock(int blockIndex) {
  62. bitfield->unsetUseBit(blockIndex);
  63. }
  64. int Piece::getMissingUnusedBlockIndex() const {
  65. int blockIndex = bitfield->getFirstMissingUnusedIndex();
  66. if(blockIndex == -1) {
  67. return blockIndex;
  68. }
  69. bitfield->setUseBit(blockIndex);
  70. return blockIndex;
  71. }
  72. BlockIndexes Piece::getAllMissingBlockIndexes() const {
  73. return bitfield->getAllMissingIndexes();
  74. }