diff options
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/priorityqueue/binary_heap.go | 10 | ||||
-rw-r--r-- | pkg/priorityqueue/binary_heap_test.go | 9 |
2 files changed, 8 insertions, 11 deletions
diff --git a/pkg/priorityqueue/binary_heap.go b/pkg/priorityqueue/binary_heap.go index fe6a06fd..f3d8f95b 100644 --- a/pkg/priorityqueue/binary_heap.go +++ b/pkg/priorityqueue/binary_heap.go @@ -25,7 +25,7 @@ func NewBinHeap() *BinHeap { } func (bh *BinHeap) fixUp() { - k := len(bh.items) - 1 + k := bh.len - 1 p := (k - 1) >> 1 // k-1 / 2 for k > 0 { @@ -41,7 +41,7 @@ func (bh *BinHeap) fixUp() { } } -func (bh *BinHeap) swap(i, j int) { +func (bh *BinHeap) swap(i, j uint64) { (bh.items)[i], (bh.items)[j] = (bh.items)[j], (bh.items)[i] } @@ -59,7 +59,7 @@ func (bh *BinHeap) fixDown(curr, end int) { idxToSwap = cTwoIdx } if *(bh.items)[idxToSwap].Priority() < *(bh.items)[curr].Priority() { - bh.swap(curr, idxToSwap) + bh.swap(uint64(curr), uint64(idxToSwap)) curr = idxToSwap cOneIdx = (curr << 1) + 1 } else { @@ -87,11 +87,11 @@ func (bh *BinHeap) GetMax() Item { bh.cond.L.Lock() defer bh.cond.L.Unlock() - for atomic.LoadUint64(&bh.len) == 0 { + if atomic.LoadUint64(&bh.len) == 0 { bh.cond.Wait() } - bh.swap(0, int(bh.len-1)) + bh.swap(0, bh.len-1) item := (bh.items)[int(bh.len)-1] bh.items = (bh).items[0 : int(bh.len)-1] diff --git a/pkg/priorityqueue/binary_heap_test.go b/pkg/priorityqueue/binary_heap_test.go index 149ec764..4c234dc5 100644 --- a/pkg/priorityqueue/binary_heap_test.go +++ b/pkg/priorityqueue/binary_heap_test.go @@ -68,10 +68,10 @@ func TestNewPriorityQueue(t *testing.T) { for { select { case <-tt.C: - fmt.Println(fmt.Sprintf("GetMax per second: %d", atomic.LoadUint64(&getPerSec))) fmt.Println(fmt.Sprintf("Insert per second: %d", atomic.LoadUint64(&insertsPerSec))) - atomic.StoreUint64(&getPerSec, 0) atomic.StoreUint64(&insertsPerSec, 0) + fmt.Println(fmt.Sprintf("GetMax per second: %d", atomic.LoadUint64(&getPerSec))) + atomic.StoreUint64(&getPerSec, 0) case <-stopCh: tt.Stop() return @@ -85,10 +85,7 @@ func TestNewPriorityQueue(t *testing.T) { case <-stopCh: return default: - it := pq.GetMax() - if it == nil { - continue - } + pq.GetMax() atomic.AddUint64(&getPerSec, 1) } } |