]> Sergey Matveev's repositories - btrtrc.git/blob - pending-requests.go
Extract pendingRequests
[btrtrc.git] / pending-requests.go
1 package torrent
2
3 type pendingRequests struct {
4         m map[RequestIndex]int
5 }
6
7 func (p pendingRequests) Dec(r RequestIndex) {
8         p.m[r]--
9         n := p.m[r]
10         if n == 0 {
11                 delete(p.m, r)
12         }
13         if n < 0 {
14                 panic(n)
15         }
16 }
17
18 func (p pendingRequests) Inc(r RequestIndex) {
19         p.m[r]++
20 }
21
22 func (p *pendingRequests) Init() {
23         p.m = make(map[RequestIndex]int)
24 }
25
26 func (p *pendingRequests) AssertEmpty() {
27         if len(p.m) != 0 {
28                 panic(p.m)
29         }
30 }
31
32 func (p pendingRequests) Get(r RequestIndex) int {
33         return p.m[r]
34 }