From daff06cf20fcfdeeb0671a2ed42b7cb95ab7070f Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 9 May 2022 12:51:01 +1000 Subject: [PATCH] Move undirtiedChunksIter into its own file --- piece.go | 30 ------------------------------ undirtied-chunks-iter.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 30 deletions(-) create mode 100644 undirtied-chunks-iter.go diff --git a/piece.go b/piece.go index 44f2dd9d..d892a406 100644 --- a/piece.go +++ b/piece.go @@ -10,7 +10,6 @@ import ( "github.com/anacrolix/torrent/metainfo" pp "github.com/anacrolix/torrent/peer_protocol" "github.com/anacrolix/torrent/storage" - "github.com/anacrolix/torrent/typed-roaring" ) type Piece struct { @@ -249,35 +248,6 @@ func init() { gob.Register(undirtiedChunksIter{}) } -// Use an iterator to jump between dirty bits. -type undirtiedChunksIter struct { - TorrentDirtyChunks *typedRoaring.Bitmap[RequestIndex] - StartRequestIndex RequestIndex - EndRequestIndex RequestIndex -} - -func (me *undirtiedChunksIter) Iter(f func(chunkIndexType)) { - it := me.TorrentDirtyChunks.Iterator() - startIndex := me.StartRequestIndex - endIndex := me.EndRequestIndex - it.AdvanceIfNeeded(startIndex) - lastDirty := startIndex - 1 - for it.HasNext() { - next := it.Next() - if next >= endIndex { - break - } - for index := lastDirty + 1; index < next; index++ { - f(index - startIndex) - } - lastDirty = next - } - for index := lastDirty + 1; index < endIndex; index++ { - f(index - startIndex) - } - return -} - func (p *Piece) requestIndexOffset() RequestIndex { return p.t.pieceRequestIndexOffset(p.index) } diff --git a/undirtied-chunks-iter.go b/undirtied-chunks-iter.go new file mode 100644 index 00000000..14f88a24 --- /dev/null +++ b/undirtied-chunks-iter.go @@ -0,0 +1,34 @@ +package torrent + +import ( + "github.com/anacrolix/torrent/typed-roaring" +) + +// Use an iterator to jump between dirty bits. +type undirtiedChunksIter struct { + TorrentDirtyChunks *typedRoaring.Bitmap[RequestIndex] + StartRequestIndex RequestIndex + EndRequestIndex RequestIndex +} + +func (me *undirtiedChunksIter) Iter(f func(chunkIndexType)) { + it := me.TorrentDirtyChunks.Iterator() + startIndex := me.StartRequestIndex + endIndex := me.EndRequestIndex + it.AdvanceIfNeeded(startIndex) + lastDirty := startIndex - 1 + for it.HasNext() { + next := it.Next() + if next >= endIndex { + break + } + for index := lastDirty + 1; index < next; index++ { + f(index - startIndex) + } + lastDirty = next + } + for index := lastDirty + 1; index < endIndex; index++ { + f(index - startIndex) + } + return +}