]> Sergey Matveev's repositories - btrtrc.git/blob - pending-requests.go
Drop support for go 1.20
[btrtrc.git] / pending-requests.go
1 package torrent
2
3 import (
4         rbm "github.com/RoaringBitmap/roaring"
5         roaring "github.com/RoaringBitmap/roaring/BitSliceIndexing"
6 )
7
8 type pendingRequests struct {
9         m *roaring.BSI
10 }
11
12 func (p *pendingRequests) Dec(r RequestIndex) {
13         _r := uint64(r)
14         prev, _ := p.m.GetValue(_r)
15         if prev <= 0 {
16                 panic(prev)
17         }
18         p.m.SetValue(_r, prev-1)
19 }
20
21 func (p *pendingRequests) Inc(r RequestIndex) {
22         _r := uint64(r)
23         prev, _ := p.m.GetValue(_r)
24         p.m.SetValue(_r, prev+1)
25 }
26
27 func (p *pendingRequests) Init(maxIndex RequestIndex) {
28         p.m = roaring.NewDefaultBSI()
29 }
30
31 var allBits rbm.Bitmap
32
33 func init() {
34         allBits.AddRange(0, rbm.MaxRange)
35 }
36
37 func (p *pendingRequests) AssertEmpty() {
38         if p.m == nil {
39                 panic(p.m)
40         }
41         sum, _ := p.m.Sum(&allBits)
42         if sum != 0 {
43                 panic(sum)
44         }
45 }
46
47 func (p *pendingRequests) Get(r RequestIndex) int {
48         count, _ := p.m.GetValue(uint64(r))
49         return int(count)
50 }