]> Sergey Matveev's repositories - btrtrc.git/blobdiff - file.go
Use roaring.Bitmap directly for completed pieces
[btrtrc.git] / file.go
diff --git a/file.go b/file.go
index 60317a23ce2adf9931a1608fefb7380578c982d2..45defe6e87cbdc3714990da10f6d8383a95b6a6f 100644 (file)
--- a/file.go
+++ b/file.go
@@ -1,6 +1,7 @@
 package torrent
 
 import (
+       "github.com/RoaringBitmap/roaring"
        "github.com/anacrolix/missinggo/v2/bitmap"
 
        "github.com/anacrolix/torrent/metainfo"
@@ -59,32 +60,32 @@ func fileBytesLeft(
        fileEndPieceIndex int,
        fileTorrentOffset int64,
        fileLength int64,
-       torrentCompletedPieces bitmap.Bitmap,
+       torrentCompletedPieces *roaring.Bitmap,
 ) (left int64) {
        numPiecesSpanned := fileEndPieceIndex - fileFirstPieceIndex
        switch numPiecesSpanned {
        case 0:
        case 1:
-               if !torrentCompletedPieces.Get(bitmap.BitIndex(fileFirstPieceIndex)) {
+               if !torrentCompletedPieces.Contains(bitmap.BitIndex(fileFirstPieceIndex)) {
                        left += fileLength
                }
        default:
-               if !torrentCompletedPieces.Get(bitmap.BitIndex(fileFirstPieceIndex)) {
+               if !torrentCompletedPieces.Contains(bitmap.BitIndex(fileFirstPieceIndex)) {
                        left += torrentUsualPieceSize - (fileTorrentOffset % torrentUsualPieceSize)
                }
-               if !torrentCompletedPieces.Get(bitmap.BitIndex(fileEndPieceIndex - 1)) {
+               if !torrentCompletedPieces.Contains(bitmap.BitIndex(fileEndPieceIndex - 1)) {
                        left += fileTorrentOffset + fileLength - int64(fileEndPieceIndex-1)*torrentUsualPieceSize
                }
-               completedMiddlePieces := torrentCompletedPieces.Copy()
+               completedMiddlePieces := torrentCompletedPieces.Clone()
                completedMiddlePieces.RemoveRange(0, bitmap.BitRange(fileFirstPieceIndex+1))
                completedMiddlePieces.RemoveRange(bitmap.BitRange(fileEndPieceIndex-1), bitmap.ToEnd)
-               left += int64(numPiecesSpanned-2-pieceIndex(completedMiddlePieces.Len())) * torrentUsualPieceSize
+               left += int64(numPiecesSpanned-2-pieceIndex(completedMiddlePieces.GetCardinality())) * torrentUsualPieceSize
        }
        return
 }
 
 func (f *File) bytesLeft() (left int64) {
-       return fileBytesLeft(int64(f.t.usualPieceSize()), f.firstPieceIndex(), f.endPieceIndex(), f.offset, f.length, f.t._completedPieces)
+       return fileBytesLeft(int64(f.t.usualPieceSize()), f.firstPieceIndex(), f.endPieceIndex(), f.offset, f.length, &f.t._completedPieces)
 }
 
 // The relative file path for a multi-file torrent, and the torrent name for a