]> Sergey Matveev's repositories - btrtrc.git/blobdiff - pending-requests.go
Extract pendingRequests
[btrtrc.git] / pending-requests.go
diff --git a/pending-requests.go b/pending-requests.go
new file mode 100644 (file)
index 0000000..4afefe6
--- /dev/null
@@ -0,0 +1,34 @@
+package torrent
+
+type pendingRequests struct {
+       m map[RequestIndex]int
+}
+
+func (p pendingRequests) Dec(r RequestIndex) {
+       p.m[r]--
+       n := p.m[r]
+       if n == 0 {
+               delete(p.m, r)
+       }
+       if n < 0 {
+               panic(n)
+       }
+}
+
+func (p pendingRequests) Inc(r RequestIndex) {
+       p.m[r]++
+}
+
+func (p *pendingRequests) Init() {
+       p.m = make(map[RequestIndex]int)
+}
+
+func (p *pendingRequests) AssertEmpty() {
+       if len(p.m) != 0 {
+               panic(p.m)
+       }
+}
+
+func (p pendingRequests) Get(r RequestIndex) int {
+       return p.m[r]
+}