X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=pending-requests.go;h=dcb1faf19fb95b206aff8862b276a08b87213a7a;hb=45c91b322ad137206ea38aa6a90a5ea59b1f85d8;hp=4afefe65027c1d087e1c8f3d26de13b4da73a92e;hpb=9aff9f35929ff67872cc5f76cc386de45c3758e8;p=btrtrc.git diff --git a/pending-requests.go b/pending-requests.go index 4afefe65..dcb1faf1 100644 --- a/pending-requests.go +++ b/pending-requests.go @@ -1,34 +1,50 @@ package torrent +import ( + rbm "github.com/RoaringBitmap/roaring" + roaring "github.com/RoaringBitmap/roaring/BitSliceIndexing" +) + type pendingRequests struct { - m map[RequestIndex]int + m *roaring.BSI } -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) 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) Inc(r RequestIndex) { - p.m[r]++ +func (p *pendingRequests) Init(maxIndex RequestIndex) { + p.m = roaring.NewDefaultBSI() } -func (p *pendingRequests) Init() { - p.m = make(map[RequestIndex]int) +var allBits rbm.Bitmap + +func init() { + allBits.AddRange(0, rbm.MaxRange) } func (p *pendingRequests) AssertEmpty() { - if len(p.m) != 0 { + if p.m == nil { panic(p.m) } + sum, _ := p.m.Sum(&allBits) + if sum != 0 { + panic(sum) + } } -func (p pendingRequests) Get(r RequestIndex) int { - return p.m[r] +func (p *pendingRequests) Get(r RequestIndex) int { + count, _ := p.m.GetValue(uint64(r)) + return int(count) }