]> Sergey Matveev's repositories - btrtrc.git/blob - piece.go
Move piece into its own file
[btrtrc.git] / piece.go
1 package torrent
2
3 import (
4         "math/rand"
5         "sync"
6 )
7
8 type piecePriority byte
9
10 const (
11         piecePriorityNone piecePriority = iota
12         piecePriorityNormal
13         piecePriorityReadahead
14         piecePriorityNext
15         piecePriorityNow
16 )
17
18 type piece struct {
19         Hash              pieceSum
20         PendingChunkSpecs map[chunkSpec]struct{}
21         Hashing           bool
22         QueuedForHash     bool
23         EverHashed        bool
24         Event             sync.Cond
25         Priority          piecePriority
26 }
27
28 func (p *piece) shuffledPendingChunkSpecs() (css []chunkSpec) {
29         if len(p.PendingChunkSpecs) == 0 {
30                 return
31         }
32         css = make([]chunkSpec, 0, len(p.PendingChunkSpecs))
33         for cs := range p.PendingChunkSpecs {
34                 css = append(css, cs)
35         }
36         if len(css) <= 1 {
37                 return
38         }
39         for i := range css {
40                 j := rand.Intn(i + 1)
41                 css[i], css[j] = css[j], css[i]
42         }
43         return
44 }