forked from anacrolix/torrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pending-requests.go
50 lines (41 loc) · 872 Bytes
/
pending-requests.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package torrent
import (
rbm "github.com/RoaringBitmap/roaring"
roaring "github.com/RoaringBitmap/roaring/BitSliceIndexing"
)
type pendingRequests struct {
m *roaring.BSI
}
func (p *pendingRequests) Dec(r RequestIndex) {
_r := uint64(r)
prev, _ := p.m.GetValue(_r)
if prev <= 0 {
panic(prev)
}
p.m.SetValue(_r, prev-1)
}
func (p *pendingRequests) Inc(r RequestIndex) {
_r := uint64(r)
prev, _ := p.m.GetValue(_r)
p.m.SetValue(_r, prev+1)
}
func (p *pendingRequests) Init(maxIndex RequestIndex) {
p.m = roaring.NewDefaultBSI()
}
var allBits rbm.Bitmap
func init() {
allBits.AddRange(0, rbm.MaxRange)
}
func (p *pendingRequests) AssertEmpty() {
if p.m == nil {
panic(p.m)
}
sum, _ := p.m.Sum(&allBits)
if sum != 0 {
panic(sum)
}
}
func (p *pendingRequests) Get(r RequestIndex) int {
count, _ := p.m.GetValue(uint64(r))
return int(count)
}