]> Sergey Matveev's repositories - btrtrc.git/blob - undirtied-chunks-iter.go
Move undirtiedChunksIter into its own file
[btrtrc.git] / undirtied-chunks-iter.go
1 package torrent
2
3 import (
4         "github.com/anacrolix/torrent/typed-roaring"
5 )
6
7 // Use an iterator to jump between dirty bits.
8 type undirtiedChunksIter struct {
9         TorrentDirtyChunks *typedRoaring.Bitmap[RequestIndex]
10         StartRequestIndex  RequestIndex
11         EndRequestIndex    RequestIndex
12 }
13
14 func (me *undirtiedChunksIter) Iter(f func(chunkIndexType)) {
15         it := me.TorrentDirtyChunks.Iterator()
16         startIndex := me.StartRequestIndex
17         endIndex := me.EndRequestIndex
18         it.AdvanceIfNeeded(startIndex)
19         lastDirty := startIndex - 1
20         for it.HasNext() {
21                 next := it.Next()
22                 if next >= endIndex {
23                         break
24                 }
25                 for index := lastDirty + 1; index < next; index++ {
26                         f(index - startIndex)
27                 }
28                 lastDirty = next
29         }
30         for index := lastDirty + 1; index < endIndex; index++ {
31                 f(index - startIndex)
32         }
33         return
34 }