-func init() {
- gob.Register(undirtiedChunksIter{})
-}
-
-type undirtiedChunksIter struct {
- TorrentDirtyChunks *roaring.Bitmap
- 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) undirtiedChunksIter() request_strategy.ChunksIter {
- // Use an iterator to jump between dirty bits.
- return undirtiedChunksIter{
- TorrentDirtyChunks: &p.t.dirtyChunks,
- StartRequestIndex: p.requestIndexOffset(),
- EndRequestIndex: p.requestIndexOffset() + p.numChunks(),
- }
-}
-
-func (p *Piece) iterUndirtiedChunks(f func(chunkIndexType)) {
- if true {
- p.undirtiedChunksIter().Iter(f)
- return
- }
- // The original implementation.
- for i := chunkIndexType(0); i < p.numChunks(); i++ {
- if p.chunkIndexDirty(i) {
- continue
- }
- f(i)
- }
-}
-